Skip to content

Commit

Permalink
util.java_class_context
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Oct 10, 2022
1 parent 207f385 commit 18deda3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
Expand Up @@ -26,6 +26,7 @@ public class Comparable {
// "Is less than or equal to" is written as "<=" or "or_less".
// "does this list or map contain" is written as "contains". For example, "- if a|b|c contains b:" or "- if [a=1;b=2] contains b:"
// "is this in the list or map" is written as "in". For example, "- if b in a|b|c:", or "- if [a=1;b=2] contains b:"
// "does this object or text match an advanced matcher" is written as "matches". For example, "- if <player.location.below> matches stone:"
//
// Note: When using an operator in a tag,
// keep in mind that < and >, and even >= and <= must be either escaped, or referred to by name.
Expand Down
Expand Up @@ -948,10 +948,29 @@ else if (attribute.startsWith("format", 2) && attribute.hasParam()) {
// This tag is strictly for internal debugging reasons.
// WARNING: Different Java versions generate different stack trace formats and details.
// WARNING: Java internally limits stack trace generation in a variety of ways. This tag cannot be relied on to output anything.
// For gathering stable context, prefer <@link tag util.java_class_context>
// -->
tagProcessor.registerTag(ElementTag.class, "stack_trace", (attribute, object) -> {
return new ElementTag(DebugInternals.getFullExceptionMessage(new RuntimeException("TRACE"), false));
});

// <--[tag]
// @attribute <util.java_class_context>
// @returns ListTag
// @description
// Returns a list of class names in the current stack history.
// More stable than <@link tag util.stack_trace>
// This contains a lot of stray content, the first 4 to 20 or so classes in this list are likely irrelevant to what you're searching for.
// Each entry in the list is a raw class name, like "com.denizenscript.denizencore.tags.core.UtilTagBase".
// Class names may appear multiple times in a row if that class contains methods that call each other.
// -->
tagProcessor.registerTag(ListTag.class, "java_class_context", (attribute, object) -> {
ListTag list = new ListTag();
for (Class<?> clazz : DebugInternals.getClassContext()) {
list.addObject(new ElementTag(clazz.getName(), true));
}
return list;
});
}

public static final long serverStartTimeMillis = CoreUtilities.monotonicMillis();
Expand Down
Expand Up @@ -411,7 +411,8 @@ public static void internalFinalOutputPath(String message, Debuggable caller, bo
/** Used for "log" to get class names. */
public static final Map<Class<?>, String> classNameCache = new WeakHashMap<>();

/** Used for "log" to get class names. This class janks access to SecurityManager to be open to the DebugInternals class. */
/** Used for "log" to get class names. This class janks access to SecurityManager to be open to the DebugInternals class.
* Note: the format of this code is extremely unstable and jank as it relates to Java security internals stuff, do not touch unless you 100% know what you're doing. */
public static class SecurityManagerTrick extends SecurityManager {
@Override
@SuppressWarnings("rawtypes")
Expand All @@ -420,6 +421,11 @@ protected Class[] getClassContext() {
}
}

/** Helper to get the current class context. */
public static Class[] getClassContext() {
return new SecurityManagerTrick().getClassContext();
}

/** Used for "log" to get class names. */
public static boolean canGetClass = true;

Expand Down

0 comments on commit 18deda3

Please sign in to comment.