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

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

Introduction

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

Prototype

public MethodNode() 

Source Link

Document

Constructs an uninitialized MethodNode .

Usage

From source file:org.diorite.inject.controller.TransformerMethodInjector.java

License:Open Source License

private void injectMethods() {
    MethodNode methodNode = new MethodNode();
    for (ControllerMethodData methodData : this.injectTransformer.classData.getMethods()) {
        TransformerInvokerGenerator.generateMethodInjection(this.injectTransformer.classData, methodData,
                methodNode, true, -1);/* ww w.  ja va 2  s.co  m*/
    }
    for (TransformerInitMethodData methodData : this.injectTransformer.inits.values()) {
        InsnList instructions = methodData.node.instructions;
        for (InsnNode node : methodData.returns) {
            instructions.insertBefore(node, methodNode.instructions);
        }
    }
}

From source file:org.diorite.inject.impl.controller.TransformerFieldInjector.java

License:Open Source License

private void injectField(TransformerFieldPair fieldPair, TransformerInjectTracker result) {
    ControllerFieldData<?> fieldData = fieldPair.data;
    assert fieldData != null;

    MethodInsnNode injectionInvokeNode = result.getResult();
    FieldInsnNode putfieldNode = result.getFieldInsnNode();

    // insert invoke methods:
    MethodNode codeBefore = new MethodNode();
    MethodNode codeAfter = new MethodNode();
    this.injectTransformer.fillMethodInvokes(codeBefore, codeAfter, fieldData);

    // node list for PUTFIELD
    InsnList initNodeList = result.getInitNodeList();
    // node list for invoke execution
    InsnList resultNodeList = result.getResultNodeList();

    if (codeBefore.instructions.size() != 0) {
        // invoke before should be added before PUTFIELD and before INVOKE
        resultNodeList.insertBefore(injectionInvokeNode, codeBefore.instructions);
    }/* www. j  a v a  2s . c  o m*/
    if (codeAfter.instructions.size() != 0) {
        // invoke after should be added after PUTFIELD
        initNodeList.insert(putfieldNode, codeAfter.instructions);
    }

    // and replace placeholder node with real injections:
    {
        MethodNode tempNode = new MethodNode();
        TransformerInvokerGenerator.generateFieldInjection(this.injectTransformer.classData, fieldData,
                tempNode, -1, fieldPair.placeholderType);
        resultNodeList.insertBefore(injectionInvokeNode, tempNode.instructions);
        resultNodeList.remove(injectionInvokeNode);
    }
}

From source file:org.jacoco.core.internal.analysis.AC_MethodAnalyzerTest.java

License:Open Source License

@Before
public void setup() {
    nextProbeId = 0;/*from   w w w  . j a  va  2s .c om*/
    method = new MethodNode();
    method.tryCatchBlocks = new ArrayList<TryCatchBlockNode>();
    probes = new boolean[32];
}

From source file:org.jacoco.core.internal.instr.CondyProbeArrayStrategyTest.java

License:Open Source License

@Test
public void should_store_instance_using_condy_and_checkcast() {
    final MethodNode m = new MethodNode();
    final int maxStack = strategy.storeInstance(m, false, 1);

    assertEquals(1, maxStack);/*from  w  ww .j  a  va  2  s.  co m*/

    final ConstantDynamic constantDynamic = (ConstantDynamic) ((LdcInsnNode) m.instructions.get(0)).cst;
    assertEquals("$jacocoData", constantDynamic.getName());
    assertEquals("Ljava/lang/Object;", constantDynamic.getDescriptor());

    final Handle bootstrapMethod = constantDynamic.getBootstrapMethod();
    assertEquals(Opcodes.H_INVOKESTATIC, bootstrapMethod.getTag());
    assertEquals("ClassName", bootstrapMethod.getOwner());
    assertEquals("$jacocoInit", bootstrapMethod.getName());
    assertEquals("(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/Class;)[Z",
            bootstrapMethod.getDesc());
    assertTrue(bootstrapMethod.isInterface());

    final TypeInsnNode castInstruction = (TypeInsnNode) m.instructions.get(1);
    assertEquals(Opcodes.CHECKCAST, castInstruction.getOpcode());
    assertEquals("[Z", castInstruction.desc);

    final VarInsnNode storeInstruction = (VarInsnNode) m.instructions.get(2);
    assertEquals(Opcodes.ASTORE, storeInstruction.getOpcode());
    assertEquals(1, storeInstruction.var);

    assertEquals(3, m.instructions.size());
}

From source file:org.lenskit.data.entities.AbstractBeanEntity.java

License:Open Source License

private static MethodNode generateGetterConstructor() {
    MethodNode cn = new MethodNode();
    cn.name = "<init>";
    cn.desc = "()V";
    cn.access = ACC_PUBLIC;/*from   ww  w .  j  a  v a2 s  . c  o m*/
    cn.exceptions = Collections.emptyList();
    cn.maxStack = 1;
    cn.maxLocals = 1;
    // load the instance
    cn.visitVarInsn(ALOAD, 0);
    // call superclass constructor
    cn.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(BeanAttributeGetter.class), "<init>", "()V", false);
    cn.visitInsn(RETURN);
    return cn;
}

From source file:org.lenskit.data.entities.AbstractBeanEntity.java

License:Open Source License

private static MethodNode generateGetterMethod(Class<? extends AbstractBeanEntity> type, Method getter) {
    MethodNode gn = new MethodNode();
    gn.name = "get";
    gn.desc = Type.getMethodDescriptor(Type.getType(Object.class), Type.getType(AbstractBeanEntity.class));
    gn.access = ACC_PUBLIC;//from  w  w w . j  a  v  a  2s . c o  m
    gn.exceptions = Collections.emptyList();
    Type rt = Type.getReturnType(getter);
    gn.maxLocals = 2;
    gn.maxStack = 1 + rt.getSize();
    gn.visitCode();
    // load the target object from parameter
    gn.visitVarInsn(ALOAD, 1);
    // cast to target object type
    gn.visitTypeInsn(CHECKCAST, Type.getInternalName(type));
    // call target object method
    gn.visitMethodInsn(INVOKEVIRTUAL, Type.getInternalName(type), getter.getName(),
            Type.getMethodDescriptor(getter), false);
    // convert from primitive to object if necessary
    CGUtils.adaptFromType(gn, getter.getReturnType());
    gn.visitInsn(ARETURN);
    return gn;
}

From source file:org.lenskit.data.entities.AbstractBeanEntity.java

License:Open Source License

private static MethodNode generateLongGetterMethod(Class<? extends AbstractBeanEntity> type, Method getter) {
    MethodNode gn = new MethodNode();
    gn.name = "getLong";
    gn.desc = Type.getMethodDescriptor(Type.LONG_TYPE, Type.getType(AbstractBeanEntity.class));
    gn.access = ACC_PUBLIC;//from  w w  w .  j  a v a  2s .  c  o m
    gn.exceptions = Collections.emptyList();
    gn.maxLocals = 2;
    gn.maxStack = 2;
    gn.visitCode();
    gn.visitVarInsn(ALOAD, 1);
    gn.visitTypeInsn(CHECKCAST, Type.getInternalName(type));
    gn.visitMethodInsn(INVOKEVIRTUAL, Type.getInternalName(type), getter.getName(),
            Type.getMethodDescriptor(getter), false);
    gn.visitInsn(LRETURN);
    return gn;
}

From source file:org.lenskit.data.entities.AbstractBeanEntity.java

License:Open Source License

private static MethodNode generateDoubleGetterMethod(Class<? extends AbstractBeanEntity> type, Method getter) {
    MethodNode gn = new MethodNode();
    gn.name = "getDouble";
    gn.desc = Type.getMethodDescriptor(Type.DOUBLE_TYPE, Type.getType(AbstractBeanEntity.class));
    gn.access = ACC_PUBLIC;/*w w  w .java2  s  . co  m*/
    gn.exceptions = Collections.emptyList();
    gn.maxLocals = 2;
    gn.maxStack = 2;
    gn.visitCode();
    gn.visitVarInsn(ALOAD, 1);
    gn.visitTypeInsn(CHECKCAST, Type.getInternalName(type));
    gn.visitMethodInsn(INVOKEVIRTUAL, Type.getInternalName(type), getter.getName(),
            Type.getMethodDescriptor(getter), false);
    gn.visitInsn(DRETURN);
    return gn;
}

From source file:org.lenskit.data.entities.AbstractBeanEntityBuilder.java

License:Open Source License

private static MethodNode generateBeanConstructor(Class<? extends AttrMethod> superclass) {
    MethodNode ctor = new MethodNode();
    ctor.access = ACC_PUBLIC;//ww w.jav a2 s  .co  m
    ctor.desc = getMethodDescriptor(VOID_TYPE, getType(TypedName.class));
    ctor.name = "<init>";
    ctor.exceptions = Collections.emptyList();
    ctor.maxLocals = 2;
    ctor.maxStack = 2;
    // load instance ('this')
    ctor.visitVarInsn(ALOAD, 0);
    // load the attribute
    ctor.visitVarInsn(ALOAD, 1);
    // invoke superclass constructor with attribute
    ctor.visitMethodInsn(INVOKESPECIAL, getInternalName(superclass), "<init>", ctor.desc, false);
    ctor.visitInsn(RETURN);
    return ctor;
}

From source file:org.lenskit.data.entities.AbstractBeanEntityBuilder.java

License:Open Source License

private static MethodNode generateSetter(Class<? extends AbstractBeanEntityBuilder> type, Method smethod) {
    MethodNode setter = new MethodNode();
    setter.access = ACC_PUBLIC;/*from  w ww . ja v a 2  s  . co m*/
    setter.desc = getMethodDescriptor(VOID_TYPE, getType(AbstractBeanEntityBuilder.class),
            getType(Object.class));
    setter.name = "set";
    setter.exceptions = Collections.emptyList();
    setter.maxLocals = 3;
    setter.maxStack = 2;
    // load target object
    setter.visitVarInsn(ALOAD, 1);
    // cast target object
    setter.visitTypeInsn(CHECKCAST, getInternalName(type));
    // load attribute value
    setter.visitVarInsn(ALOAD, 2);
    // convert attribute value if necessary
    setter.maxStack += CGUtils.adaptToType(setter, smethod.getParameterTypes()[0]);
    // call real setter
    setter.visitMethodInsn(INVOKEVIRTUAL, getInternalName(type), smethod.getName(),
            getMethodDescriptor(smethod), false);
    setter.visitInsn(RETURN);
    return setter;
}