Skip to content

Commit

Permalink
Add textifier functions to ASMAPI, helpful in debugging.
Browse files Browse the repository at this point in the history
  • Loading branch information
LexManos committed Jan 14, 2021
1 parent 997eab2 commit 5579913
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ dependencies {
implementation('org.ow2.asm:asm:7.2')
implementation('org.ow2.asm:asm-commons:7.2')
implementation('org.ow2.asm:asm-tree:7.2')
implementation('org.ow2.asm:asm-util:7.2')
implementation('net.minecraftforge:forgespi:3.1.+')
implementation('com.google.code.findbugs:jsr305:3.0.2')

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/net/minecraftforge/coremod/CoreModEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public class CoreModEngine {
private static final Marker COREMOD = MarkerManager.getMarker("COREMOD");
private List<CoreMod> coreMods = new ArrayList<>();
static final Set<String> ALLOWED_PACKAGES = new HashSet<>(Arrays.asList(
"java.util.function"
"java.util.function",
"org.objectweb.asm.util" // ASM util has nice debugging things like Trace visitors
));
static final Set<String> ALLOWED_CLASSES = new HashSet<>(Arrays.asList(
"net.minecraftforge.coremod.api.ASMAPI","org.objectweb.asm.Opcodes",
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/net/minecraftforge/coremod/api/ASMAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@
import net.minecraftforge.coremod.CoreModTracker;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.*;
import org.objectweb.asm.util.Textifier;
import org.objectweb.asm.util.TraceClassVisitor;
import org.objectweb.asm.util.TraceMethodVisitor;

import javax.annotation.Nullable;
import javax.script.ScriptException;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Modifier;
import java.util.Iterator;
import java.util.ListIterator;
Expand Down Expand Up @@ -305,4 +310,30 @@ public static Object loadData(String file) throws ScriptException, IOException {
public static void log(String level, String message, Object... args) {
CoreModTracker.log(level, message, args);
}

public static String classNodeToString(ClassNode node) {
Textifier text = new Textifier();
node.accept(new TraceClassVisitor(null, text, null));
return toString(text);
}

public static String fieldNodeToString(FieldNode node) {
Textifier text = new Textifier();
node.accept(new TraceClassVisitor(null, text, null));
return toString(text);
}

public static String methodNodeToString(MethodNode node) {
Textifier text = new Textifier();
node.accept(new TraceMethodVisitor(text));
return toString(text);
}

private static String toString(Textifier text) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
text.print(pw);
pw.flush();
return sw.toString();
}
}

0 comments on commit 5579913

Please sign in to comment.