Example usage for org.objectweb.asm.tree AbstractInsnNode getNext

List of usage examples for org.objectweb.asm.tree AbstractInsnNode getNext

Introduction

In this page you can find the example usage for org.objectweb.asm.tree AbstractInsnNode getNext.

Prototype

public AbstractInsnNode getNext() 

Source Link

Document

Returns the next instruction in the list to which this instruction belongs, if any.

Usage

From source file:com.navercorp.pinpoint.profiler.instrument.ASMMethodNodeAdapter.java

License:Apache License

public int getLineNumber() {
    AbstractInsnNode node = this.methodNode.instructions.getFirst();
    while (node != null) {
        if (node.getType() == AbstractInsnNode.LINE) {
            return ((LineNumberNode) node).line;
        }/*from  w w w. ja  v  a2  s. com*/
        node = node.getNext();
    }

    return 0;
}

From source file:com.navercorp.pinpoint.profiler.instrument.ASMMethodNodeAdapter.java

License:Apache License

public void remapMethodInsnNode(final ASMMethodInsnNodeRemapper remapper) {
    AbstractInsnNode insnNode = this.methodNode.instructions.getFirst();
    while (insnNode != null) {
        if (insnNode instanceof MethodInsnNode) {
            final MethodInsnNode methodInsnNode = (MethodInsnNode) insnNode;
            remapper.mapping(methodInsnNode);
        }/*from w w  w .j a  v  a2  s .  c om*/
        insnNode = insnNode.getNext();
    }
}

From source file:com.navercorp.pinpoint.profiler.instrument.ASMMethodNodeAdapter.java

License:Apache License

public void addAfterInterceptor(final int interceptorId, final InterceptorDefinition interceptorDefinition,
        final int apiId) {
    initInterceptorLocalVariables(interceptorId, interceptorDefinition, apiId);

    // add try catch block.
    final ASMTryCatch tryCatch = new ASMTryCatch(this.methodNode);
    this.methodNode.instructions.insertBefore(this.methodVariables.getEnterInsnNode(),
            tryCatch.getStartLabelNode());
    this.methodNode.instructions.insert(this.methodVariables.getExitInsnNode(), tryCatch.getEndLabelNode());

    // find return.
    AbstractInsnNode insnNode = this.methodNode.instructions.getFirst();
    while (insnNode != null) {
        final int opcode = insnNode.getOpcode();
        if (this.methodVariables.isReturnCode(opcode)) {
            final InsnList instructions = new InsnList();
            this.methodVariables.storeResultVar(instructions, opcode);
            invokeAfterInterceptor(instructions, interceptorDefinition, false);
            this.methodNode.instructions.insertBefore(insnNode, instructions);
        }//from  www  .  j a v a2 s  . c o m
        insnNode = insnNode.getNext();
    }

    // try catch handler.
    InsnList instructions = new InsnList();
    this.methodVariables.storeThrowableVar(instructions);
    invokeAfterInterceptor(instructions, interceptorDefinition, true);
    // throw exception.
    this.methodVariables.loadInterceptorThrowVar(instructions);
    this.methodNode.instructions.insert(tryCatch.getEndLabelNode(), instructions);
    tryCatch.sort();
}

From source file:cuchaz.enigma.CompiledSourceTypeLoader.java

License:Open Source License

private void removeRedundantClassCalls(ClassNode node) {
    // remove <obj>.getClass() calls that are seemingly injected
    //   DUP//  w  ww .j a  v  a2s .  c o m
    //   INVOKEVIRTUAL java/lang/Object.getClass ()Ljava/lang/Class;
    //   POP
    for (MethodNode methodNode : node.methods) {
        AbstractInsnNode insnNode = methodNode.instructions.getFirst();
        while (insnNode != null) {
            if (insnNode instanceof MethodInsnNode && insnNode.getOpcode() == Opcodes.INVOKEVIRTUAL) {
                MethodInsnNode methodInsnNode = (MethodInsnNode) insnNode;
                if (methodInsnNode.name.equals("getClass") && methodInsnNode.owner.equals("java/lang/Object")
                        && methodInsnNode.desc.equals("()Ljava/lang/Class;")) {
                    AbstractInsnNode previous = methodInsnNode.getPrevious();
                    AbstractInsnNode next = methodInsnNode.getNext();
                    if (previous.getOpcode() == Opcodes.DUP && next.getOpcode() == Opcodes.POP) {
                        insnNode = previous.getPrevious();//reset the iterator so it gets the new next instruction
                        methodNode.instructions.remove(previous);
                        methodNode.instructions.remove(methodInsnNode);
                        methodNode.instructions.remove(next);
                    }
                }
            }
            insnNode = insnNode.getNext();
        }
    }
}

From source file:cuchaz.enigma.TranslatingTypeLoader.java

License:Open Source License

private byte[] loadType(String className) {

    // NOTE: don't know if class name is obf or deobf
    ClassEntry classEntry = new ClassEntry(className);
    ClassEntry obfClassEntry = this.obfuscatingTranslator.getTranslatedClass(classEntry);

    // is this an inner class referenced directly? (ie trying to load b instead of a$b)
    if (!obfClassEntry.isInnerClass()) {
        List<ClassEntry> classChain = this.jarIndex.getObfClassChain(obfClassEntry);
        if (classChain.size() > 1) {
            System.err.println(String.format("WARNING: no class %s after inner class reconstruction. Try %s",
                    className, obfClassEntry.buildClassEntry(classChain)));
            return null;
        }//from  www  . j  a va2 s .co  m
    }

    // is this a class we should even know about?
    if (!this.jarIndex.containsObfClass(obfClassEntry)) {
        return null;
    }

    // DEBUG
    //System.out.println(String.format("Looking for %s (obf: %s)", classEntry.getName(), obfClassEntry.getName()));

    // find the class in the jar
    ClassNode node = findClassInJar(obfClassEntry);
    if (node == null) {
        // couldn't find it
        return null;
    }

    // remove <obj>.getClass() calls that are seemingly injected
    //   DUP
    //   INVOKEVIRTUAL java/lang/Object.getClass ()Ljava/lang/Class;
    //   POP
    for (MethodNode methodNode : node.methods) {
        AbstractInsnNode insnNode = methodNode.instructions.getFirst();
        while (insnNode != null) {
            if (insnNode instanceof MethodInsnNode && insnNode.getOpcode() == Opcodes.INVOKEVIRTUAL) {
                MethodInsnNode methodInsnNode = (MethodInsnNode) insnNode;
                if (methodInsnNode.name.equals("getClass") && methodInsnNode.owner.equals("java/lang/Object")
                        && methodInsnNode.desc.equals("()Ljava/lang/Class;")) {
                    AbstractInsnNode previous = methodInsnNode.getPrevious();
                    AbstractInsnNode next = methodInsnNode.getNext();
                    if (previous.getOpcode() == Opcodes.DUP && next.getOpcode() == Opcodes.POP) {
                        insnNode = previous.getPrevious();//reset the iterator so it gets the new next instruction
                        methodNode.instructions.remove(previous);
                        methodNode.instructions.remove(methodInsnNode);
                        methodNode.instructions.remove(next);
                    }
                }
            }
            insnNode = insnNode.getNext();
        }
    }

    ClassWriter writer = new ClassWriter(0);
    transformInto(node, writer);

    // we have a transformed class!
    return writer.toByteArray();
}

From source file:de.scoopgmbh.copper.instrument.TryCatchBlockHandler.java

License:Apache License

@SuppressWarnings("unchecked")
public void instrument(ClassNode cn) {
    //if (1 == 1) return;

    for (MethodNode m : (List<MethodNode>) cn.methods) {
        if (!m.exceptions.contains(INTERRUPT_EXCEPTION_NAME) || m.tryCatchBlocks.isEmpty()) {
            continue;
        }//from   ww  w  . j a v a 2s .  c  o m
        logger.info("Instrument " + cn.name + "." + m.name);
        HashSet<Label> labels = new HashSet<Label>();
        for (TryCatchBlockNode catchNode : (List<TryCatchBlockNode>) m.tryCatchBlocks) {
            if (labels.contains(catchNode.handler.getLabel())) {
                // some handlers share their handling code - check it out to prevent double instrumentation
                logger.info("skipping node");
                continue;
            }
            labels.add(catchNode.handler.getLabel());

            LabelNode labelNode = catchNode.handler;
            AbstractInsnNode lineNumberNode = labelNode.getNext() instanceof LineNumberNode
                    ? labelNode.getNext()
                    : labelNode;
            FrameNode frameNode = (FrameNode) lineNumberNode.getNext();
            VarInsnNode varInsnNode = (VarInsnNode) frameNode.getNext();
            AbstractInsnNode insertPoint = varInsnNode;

            if (catchNode.type == null) {
                // this is probably a finally block;
                if (insertPoint.getNext() != null && insertPoint.getNext() instanceof LabelNode) {
                    insertPoint = insertPoint.getNext();
                }
            }

            LabelNode labelNode4ifeg = new LabelNode();
            InsnList newCode = new InsnList();
            newCode.add(new VarInsnNode(Opcodes.ALOAD, varInsnNode.var));
            newCode.add(new TypeInsnNode(Opcodes.INSTANCEOF, INTERRUPT_EXCEPTION_NAME));
            newCode.add(new JumpInsnNode(Opcodes.IFEQ, labelNode4ifeg));
            newCode.add(new VarInsnNode(Opcodes.ALOAD, varInsnNode.var));
            newCode.add(new TypeInsnNode(Opcodes.CHECKCAST, INTERRUPT_EXCEPTION_NAME));
            newCode.add(new InsnNode(Opcodes.ATHROW));
            newCode.add(labelNode4ifeg);
            m.instructions.insert(insertPoint, newCode);
        }
    }
}

From source file:de.tuberlin.uebb.jbop.optimizer.array.FieldArrayValueInlinerTest.java

License:Open Source License

/**
 * Tests that FieldArrayValueInliner is working correctly.
 * /*  w w  w . j av a2s  .c  om*/
 * @throws Exception
 *           the exception
 */
@Test
public void testFieldArrayValueInliner() throws Exception {
    // INIT
    final String owner = "de.tuberlin.uebb.jbop.optimizer.array.FieldArrayValueTestClass";
    final ClassNodeBuilder builder = ClassNodeBuilder.createClass(owner).//
            addField("doubleArray1", "[D").withAnnotation(ImmutableArray.class).initArrayWith(1.0, 2.0, 3.0).//
            addField("doubleArray2", "[D").withAnnotation(ImmutableArray.class).initArrayWith(4.0, 5.0, 6.0).//
            addMethod("sumArrayValues", "()D").withAnnotation(Optimizable.class).//
            loadFieldArrayValue("doubleArray1", 0).// 4 -> 1
            loadFieldArrayValue("doubleArray2", 0).// 4 -> 1
            addInsn(new InsnNode(Opcodes.DADD)).// 1
            loadFieldArrayValue("doubleArray1", 1).// 4 -> 1
            loadFieldArrayValue("doubleArray2", 1).// 4 -> 1
            addInsn(new InsnNode(Opcodes.DADD)).// 1
            addInsn(new InsnNode(Opcodes.DADD)).// 1
            loadFieldArrayValue("doubleArray1", 2).// 4 -> 1
            loadFieldArrayValue("doubleArray2", 2).// 4 -> 1
            addInsn(new InsnNode(Opcodes.DADD)).// 1
            addInsn(new InsnNode(Opcodes.DADD)).// 1
            addInsn(new InsnNode(Opcodes.DRETURN));// 1
    //

    final FieldArrayValueInliner inliner = new FieldArrayValueInliner();
    inliner.setClassNode(builder.getClassNode());
    inliner.setInputObject(builder.instance());

    final MethodNode method = builder.getMethod("sumArrayValues");
    assertEquals(30, method.instructions.size());

    // RUN STEP 1
    final InsnList optimized = inliner.optimize(method.instructions, method);

    // ASSERT STEP 1

    assertEquals(12, optimized.size());

    // first value pair
    AbstractInsnNode currentNode = optimized.getFirst();
    assertEquals(1.0, NodeHelper.getNumberValue(currentNode).doubleValue(), .0001);
    currentNode = currentNode.getNext();
    assertEquals(4.0, NodeHelper.getNumberValue(currentNode).doubleValue(), .0001);
    currentNode = currentNode.getNext();

    // second value pair
    currentNode = currentNode.getNext();
    assertEquals(2.0, NodeHelper.getNumberValue(currentNode).doubleValue(), .0001);
    currentNode = currentNode.getNext();
    assertEquals(5.0, NodeHelper.getNumberValue(currentNode).doubleValue(), .0001);
    currentNode = currentNode.getNext();
    currentNode = currentNode.getNext();

    // third value pair
    currentNode = currentNode.getNext();
    assertEquals(3.0, NodeHelper.getNumberValue(currentNode).doubleValue(), .0001);
    currentNode = currentNode.getNext();
    assertEquals(6.0, NodeHelper.getNumberValue(currentNode).doubleValue(), .0001);

    // RUN STEP 3
    final InsnList optimized3 = inliner.optimize(method.instructions, method);

    // ASSERT STEP 3
    assertEquals(12, optimized3.size());
}

From source file:de.tuberlin.uebb.jbop.optimizer.utils.Loop.java

License:Open Source License

/**
 * Gets the body./*from  w w w  . j  a  v  a 2s .  com*/
 * 
 * @return the body
 */
protected List<AbstractInsnNode> getBody() {
    final List<AbstractInsnNode> body = new ArrayList<>();
    AbstractInsnNode currentNode = firstOfBody;
    final AbstractInsnNode endNode = getIInc();
    while (currentNode != endNode) {
        body.add(currentNode);
        currentNode = currentNode.getNext();
    }
    return body;
}

From source file:de.tuberlin.uebb.jbop.optimizer.utils.LoopMatcher.java

License:Open Source License

/**
 * Gets the loop./*from w ww. j  a v  a  2s . c om*/
 * 
 * @param node
 *          the node
 * @return the loop
 */
public static Loop getLoop(final AbstractInsnNode node) {
    if (node == null) {
        return null;
    }

    final int opcode = node.getOpcode();
    if (opcode != ISTORE) {
        return null;
    }

    final AbstractInsnNode next = node.getNext();

    if (NodeHelper.isGoto(next)) {
        return getTypeOneLoop(node, next);
    }
    return getTypeTwoLoop(node, next);
}

From source file:de.tuberlin.uebb.jbop.optimizer.utils.LoopMatcher.java

License:Open Source License

private static AbstractInsnNode getStoreOfLoopForIInc(final AbstractInsnNode node) {
    if (node == null) {
        return null;
    }//from  ww  w .j ava  2s.  co  m

    if (!(node instanceof IincInsnNode)) {
        return null;
    }
    final AbstractInsnNode next = node.getNext();
    if (NodeHelper.isGoto(next)) {
        return ((JumpInsnNode) next).label.getPrevious();
    }
    if (next instanceof LabelNode) {
        final AbstractInsnNode gotoNode = findGotoForLabel(next, true);
        if (gotoNode == null) {
            return null;
        }
        return gotoNode.getPrevious();
    }

    return null;
}