Example usage for org.objectweb.asm.tree InsnList InsnList

List of usage examples for org.objectweb.asm.tree InsnList InsnList

Introduction

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

Prototype

InsnList

Source Link

Usage

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

License:Apache License

private void initInterceptorLocalVariables(final int interceptorId,
        final InterceptorDefinition interceptorDefinition, final int apiId) {
    final InsnList instructions = new InsnList();
    if (this.methodVariables.initInterceptorLocalVariables(instructions, interceptorId, interceptorDefinition,
            apiId)) {//from   ww  w. j  ava  2 s . co m
        // if first time.
        this.methodNode.instructions.insertBefore(this.methodVariables.getEnterInsnNode(), instructions);
    }
}

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

License:Apache License

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

    final InsnList instructions = new InsnList();
    this.methodVariables.loadInterceptorLocalVariables(instructions, interceptorDefinition, false);

    final String description = Type.getMethodDescriptor(interceptorDefinition.getBeforeMethod());
    instructions.add(new MethodInsnNode(Opcodes.INVOKEINTERFACE,
            Type.getInternalName(interceptorDefinition.getInterceptorBaseClass()), "before", description,
            true));//from   w  w  w  . j a v  a2  s  .  c o m
    this.methodNode.instructions.insertBefore(this.methodVariables.getEnterInsnNode(), instructions);
}

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 ww w.ja  va2 s. c  om*/
        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:com.navercorp.pinpoint.profiler.instrument.ASMMethodVariablesTest.java

License:Apache License

@Test
public void initInterceptorLocalVariables() throws Exception {
    MethodNode methodNode = ASMClassNodeLoader
            .get("com.navercorp.pinpoint.profiler.instrument.mock.ConstructorChildClass", "<init>");
    ASMMethodVariables variables = new ASMMethodVariables(
            "com.navercorp.pinpoint.profiler.instrument.mock.ConstructorChildClass", methodNode);

    assertNull(variables.getEnterInsnNode());
    assertNull(variables.getEnterInsnNode());

    InterceptorRegistryBinder interceptorRegistryBinder = new DefaultInterceptorRegistryBinder();
    int interceptorId = interceptorRegistryBinder.getInterceptorRegistryAdaptor()
            .addInterceptor(new ArgsArrayInterceptor());
    final InterceptorDefinition interceptorDefinition = new InterceptorDefinitionFactory()
            .createInterceptorDefinition(ArgsArrayInterceptor.class);

    InsnList instructions = new InsnList();
    boolean first = variables.initInterceptorLocalVariables(instructions, interceptorId, interceptorDefinition,
            -1);/*from w w w  . j  a  va  2s. c om*/
    assertEquals(true, first);
    assertNotNull(variables.getEnterInsnNode());
    assertNotNull(variables.getEnterInsnNode());
}

From source file:com.offbynull.coroutines.instrumenter.asm.InstructionUtils.java

License:Open Source License

/**
 * Returns an empty instruction list.
 * @return empty instruction list
 */
public static InsnList empty() {
    return new InsnList();
}

From source file:com.offbynull.coroutines.instrumenter.asm.InstructionUtils.java

License:Open Source License

/**
 * Clones an invokevirtual/invokespecial/invokeinterface/invokedynamic node and returns it as an instruction list.
 * @param insnNode instruction to clone// w  w w .  j  a v  a2 s  .co m
 * @throws NullPointerException if any argument is {@code null}
 * @throws IllegalArgumentException if node isn't of invoke type
 * @return instruction list with cloned instruction
 */
public static InsnList cloneInvokeNode(AbstractInsnNode insnNode) {
    Validate.notNull(insnNode);
    Validate.isTrue(insnNode instanceof MethodInsnNode || insnNode instanceof InvokeDynamicInsnNode);

    InsnList ret = new InsnList();
    ret.add(insnNode.clone(new HashMap<>()));

    return ret;
}

From source file:com.offbynull.coroutines.instrumenter.asm.InstructionUtils.java

License:Open Source License

/**
 * Clones a monitorenter/monitorexit node and returns it as an instruction list.
 * @param insnNode instruction to clone/*w  w  w.j a  v  a2 s. c  om*/
 * @throws NullPointerException if any argument is {@code null}
 * @throws IllegalArgumentException if node isn't of invoke type
 * @return instruction list with cloned instruction
 */
public static InsnList cloneMonitorNode(AbstractInsnNode insnNode) {
    Validate.notNull(insnNode);
    Validate.isTrue(insnNode instanceof InsnNode);
    Validate.isTrue(
            insnNode.getOpcode() == Opcodes.MONITORENTER || insnNode.getOpcode() == Opcodes.MONITOREXIT);

    InsnList ret = new InsnList();
    ret.add(insnNode.clone(new HashMap<>()));

    return ret;
}

From source file:com.offbynull.coroutines.instrumenter.asm.InstructionUtils.java

License:Open Source License

/**
 * Clones an instruction list. All labels are remapped unless otherwise specified in {@code globalLabels}.
 * @param insnList instruction list to clone
 * @param globalLabels set of labels that should not be remapped
 * @throws NullPointerException if any argument is {@code null}
 * @return instruction list with cloned instructions
 *//*from w  w w  .ja v  a2 s . c o  m*/
public static InsnList cloneInsnList(InsnList insnList, Set<LabelNode> globalLabels) {
    Validate.notNull(insnList);

    // remap all labelnodes
    Map<LabelNode, LabelNode> labelNodeMapping = new HashMap<>();
    ListIterator<AbstractInsnNode> it = insnList.iterator();
    while (it.hasNext()) {
        AbstractInsnNode abstractInsnNode = it.next();
        if (abstractInsnNode instanceof LabelNode) {
            LabelNode existingLabelNode = (LabelNode) abstractInsnNode;
            labelNodeMapping.put(existingLabelNode, new LabelNode());
        }
    }

    // override remapping such that global labels stay the same
    for (LabelNode globalLabel : globalLabels) {
        labelNodeMapping.put(globalLabel, globalLabel);
    }

    // clone
    InsnList ret = new InsnList();
    it = insnList.iterator();
    while (it.hasNext()) {
        AbstractInsnNode abstractInsnNode = it.next();
        ret.add(abstractInsnNode.clone(labelNodeMapping));
    }

    return ret;
}

From source file:com.offbynull.coroutines.instrumenter.asm.InstructionUtils.java

License:Open Source License

/**
 * Combines multiple instruction lists in to a single instruction list.
 * @param insnLists instruction lists to merge
 * @throws NullPointerException if any argument is {@code null} or contains {@code null}
 * @return merged instructions//from w ww  . j av  a  2  s . c  o m
 */
public static InsnList merge(InsnList... insnLists) {
    Validate.notNull(insnLists);
    Validate.noNullElements(insnLists);

    InsnList ret = new InsnList();
    for (InsnList insnList : insnLists) {
        ret.add(insnList);
    }

    return ret;
}

From source file:com.offbynull.coroutines.instrumenter.asm.InstructionUtils.java

License:Open Source License

/**
 * Generates instructions for an unconditional jump to a label.
 * @param labelNode label to jump to// ww  w  . j av a 2s.  c o m
 * @throws NullPointerException if any argument is {@code null}
 * @return instructions for an unconditional jump to {@code labelNode}
 */
public static InsnList jumpTo(LabelNode labelNode) {
    Validate.notNull(labelNode);

    InsnList ret = new InsnList();
    ret.add(new JumpInsnNode(Opcodes.GOTO, labelNode));

    return ret;
}