### Execution Task with Stack Analysis (Java) Source: https://github.com/graxcode/threadtear/blob/master/README.md Illustrates creating an execution task that uses me.nov.threadtear.analysis.stack.ConstantTracker for detailed stack analysis. The analyzeAndRewrite method shows how to use the Analyzer to get stack frames and potentially rewrite instructions based on known constant values on the stack. It also includes implementations for IConstantReferenceHandler methods to assist the tracker. ```Java public class MyExecution extends Execution implements IConstantReferenceHandler { public MyExecution() { super(ExecutionCategory.GENERIC, "My execution", "Performs stack analysis and replaces code."); } @Override public boolean execute(Map classes, boolean verbose) { classes.values().stream().map(c -> c.node).forEach(this::analyzeAndRewrite); return true; } public void analyzeAndRewrite(ClassNode cn) { cn.methods.forEach(m -> { // this analyzer keeps known stack values, e.g. can be useful for jump prediction Analyzer a = new Analyzer(new ConstantTracker(this, Access.isStatic(m.access), m.maxLocals, m.desc, new Object[0])); try { a.analyze(cn.name, m); } catch (AnalyzerException e) { logger.severe("Failed stack analysis in " + cn.name + "." + m.name + ":" + e.getMessage()); return; } Frame[] frames = a.getFrames(); InsnList rewrittenCode = new InsnList(); Map labels = Instructions.cloneLabels(m.instructions); // rewrite method instructions for (int i = 0; i < m.instructions.size(); i++) { AbstractInsnNode ain = m.instructions.get(i); Frame frame = frames[i]; // replace / modify instructions, etc... if (frame.getStackSize() > 0) { ConstantValue top = frame.getStack(frame.getStackSize() - 1); if (top.isKnown() && top.isInteger()) { int knownTopStackValue = top.getInteger(); // use the known stack to remove jumps, simplify code, etc... // if(...) { rewrittenCode.add(...); } continue; } } rewrittenCode.add(ain.clone(labels)); } // update instructions and fix try catch blocks, local variables, etc... Instructions.updateInstructions(m, labels, rewrittenCode); }); } /** * Use this method to predict stack values if fields are loaded */ @Override public Object getFieldValueOrNull(BasicValue v, String owner, String name, String desc) { return null; } /** * Use this method to predict stack values if methods are invoked on known objects */ @Override public Object getMethodReturnOrNull(BasicValue v, String owner, String name, String desc, List values) { if (name.equals("toCharArray") && owner.equals("java/lang/String")) { if (!values.get(0).isKnown()) { // invocation target is not known, we can't compute the return return null; } return ((String) values.get(0).getValue()).toCharArray(); } return null; } } ``` -------------------------------- ### Custom Execution with Runtime Class Loading (Java) Source: https://github.com/graxcode/threadtear/blob/master/README.md Shows how to create an execution task that loads classes at runtime using me.nov.threadtear.asm.vm.VM and implementing IVMReferenceHandler. The execute method demonstrates loading a class node into a java.lang.Class object, and tryClassLoad handles resolving class references for the VM. ```Java public class MyExecution extends Execution implements IVMReferenceHandler { public MyExecution() { super(ExecutionCategory.GENERIC, "My execution", "Loads ClassNodes at runtime"); } @Override public boolean execute(Map classes, boolean verbose) { classes.values().stream().map(c -> c.node).forEach(c -> { VM vm = VM.constructVM(this); //transform bytecode to java.lang.Class Class loadedClass = vm.loadClass(c.name.replace('/', '.'), true); //do stuff with your class here loadedClass.getMethods()[0].invoke(...); return true; }); } /** * Will get invoked by VM, when VM.loadClass is called */ @Override public ClassNode tryClassLoad(String name) { //try to find the class to be loaded in open jar archive return classes.containsKey(name) ? classes.get(name).node : null; } } ``` -------------------------------- ### Creating Basic Custom Execution Task (Java) Source: https://github.com/graxcode/threadtear/blob/master/README.md Demonstrates how to create a simple custom execution task by extending me.nov.threadtear.execution.Execution. It shows the constructor for setting category, name, and description, and the execute method where class transformations using ASM's tree API can be implemented. ```Java public class MyExecution extends Execution { public MyExecution() { super(ExecutionCategory.CLEANING /* category */, "My execution" /* name */, "Executes something" /* description, can use html */); } /** * This method is invoked when the user clicks on the Run button * @return true if success, false if failure */ @Override public boolean execute(Map classes, boolean verbose) { classes.values().stream().map(c -> c.node).forEach(c -> { //transform the classes here using the tree-API of ASM }); return false; } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.