org.objectweb.asm.tree.analysis.BasicInterpreter Java Examples

The following examples show how to use org.objectweb.asm.tree.analysis.BasicInterpreter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MethodUtils.java    From JByteMod-Beta with GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void removeDeadCode(ClassNode cn, MethodNode mn) {
  Analyzer analyzer = new Analyzer(new BasicInterpreter());
  try {
    analyzer.analyze(cn.name, mn);
  } catch (AnalyzerException e) {
    ErrorDisplay.error("Could not analyze the code: " + e.getMessage());
    return;
  }
  Frame[] frames = analyzer.getFrames();
  AbstractInsnNode[] insns = mn.instructions.toArray();
  for (int i = 0; i < frames.length; i++) {
    AbstractInsnNode insn = insns[i];
    if (frames[i] == null && insn.getType() != AbstractInsnNode.LABEL) {
      mn.instructions.remove(insn);
      insns[i] = null;
    }
  }
}
 
Example #2
Source File: ConstructorThisInterpreter.java    From AVM with MIT License 4 votes vote down vote up
public ConstructorThisInterpreter() {
    super(Opcodes.ASM6);
    this.underlying = new BasicInterpreter();
    // NOTE:  This is based on the assumption that a non-static method's first newValue() call is for "this".
    this.isNextThis = true;
}
 
Example #3
Source File: InsnFinder.java    From Mixin with MIT License 4 votes vote down vote up
public PopAnalyzer(AbstractInsnNode node) {
    super(new BasicInterpreter());
    this.node = node;
}
 
Example #4
Source File: ClassVisitorExample.java    From grappa with Apache License 2.0 4 votes vote down vote up
@Override
public MethodVisitor visitMethod(final int access, final String name,
    final String desc, final String signature, final String[] exceptions)
{
    // Unused?
    /*
    final MethodVisitor mv = super.visitMethod(access, name, desc,
        signature, exceptions);
    */

    return new MethodNode(Opcodes.ASM5, access, name, desc, signature,
        exceptions)
    {
        @Override
        public void visitEnd()
        {
            super.visitEnd();

            try {
                final BasicInterpreter basicInterpreter
                    = new BasicInterpreter();
                final Analyzer<BasicValue> analyzer
                    = new Analyzer<>(basicInterpreter);
                final AbstractInsnNode[] nodes = instructions.toArray();
                final Frame<BasicValue>[] frames
                    = analyzer.analyze(className, this);
                int areturn = -1;
                for (int i = nodes.length -1; i >= 0; i--)
                {
                    if (nodes[i].getOpcode() == Opcodes.ARETURN) {
                        areturn = i;
                        System.out.println(className + "." + name + desc);
                        System.out.println("Found areturn at: " + i);
                    } else if (areturn != -1
                        && nodes[i].getOpcode() != -1
                        && frames[i].getStackSize() == 0) {
                        System.out.println("Found start of block at: " + i);

                        final InsnList list = new InsnList();
                        for (int j = i; j <= areturn; j++)
                            list.add(nodes[j]);
                        final Textifier textifier = new Textifier();
                        final PrintWriter pw = new PrintWriter(System.out);
                        list.accept(new TraceMethodVisitor(textifier));
                        textifier.print(pw);
                        pw.flush();
                        System.out.println("\n\n");
                        areturn = -1;
                    }
                }
            }
            catch (AnalyzerException e) {
                e.printStackTrace();
            }

            if (mv != null)
                accept(mv);
        }
    };
}