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

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

Introduction

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

Prototype

@Override
    public void visitMethodInsn(final int opcodeAndSource, final String owner, final String name,
            final String descriptor, final boolean isInterface) 

Source Link

Usage

From source file:clientapi.load.transform.impl.ValueAccessorTransformer.java

License:Apache License

/**
 * Creates the field-getter getter method in the specified {@code ClassNode}
 *
 * @param cn The ClassNode//from  w  w  w  .  j  a v  a 2  s .c  o m
 */
private void createFieldGetter(ClassNode cn) {
    MethodNode mn = new MethodNode(ACC_PUBLIC | ACC_FINAL, "getFieldGetter",
            "(Ljava/lang/String;)Ljava/util/function/Supplier;", null, null);

    // Create a check for all labeled fields in the cache
    fieldCache.forEach((id, fn) -> {
        MethodNode handle;
        {
            // Create lambda handle method
            handle = new MethodNode(ACC_PRIVATE | ACC_SYNTHETIC, "lambda$getFieldGetter$" + current++,
                    "()Ljava/lang/Object;", null, null);

            // Get the field value
            handle.visitVarInsn(ALOAD, 0);
            handle.visitFieldInsn(GETFIELD, cn.name, fn.name, fn.desc);

            // If the field is a primitive type, get the object representation
            String object = getObject(fn.desc);
            if (!object.equals(fn.desc))
                handle.visitMethodInsn(INVOKESTATIC, object, "valueOf", "(" + fn.desc + ")L" + object + ";",
                        false);

            // Return the value
            handle.visitInsn(ARETURN);

            // Add the handle method to the class
            cn.methods.add(handle);
        }

        // Create label for IF statement jump
        Label skip = new Label();

        // Compare the target value with the expected value
        mn.visitVarInsn(ALOAD, 1);
        mn.visitLdcInsn(id);
        mn.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z", false);

        // Jump if the input doesn't match the expected value
        mn.visitJumpInsn(IFEQ, skip);

        // Return the getter
        mn.visitVarInsn(ALOAD, 0);
        mn.visitInvokeDynamicInsn("get", "(L" + cn.name + ";)Ljava/util/function/Supplier;",
                // Define the bootstrap method
                METAFACTORY,
                // Fill the remaining 3 args for the method
                Type.getMethodType("()Ljava/lang/Object;"), createMethodHandle(H_INVOKESPECIAL, cn, handle),
                Type.getMethodType("()Ljava/lang/Object;"));
        mn.visitInsn(ARETURN);

        // Indicate where the IF statement should jump to if it fails
        mn.visitLabel(skip);
    });
    mn.visitInsn(ACONST_NULL);
    mn.visitInsn(ARETURN);

    cn.methods.add(mn);
}

From source file:clientapi.load.transform.impl.ValueAccessorTransformer.java

License:Apache License

/**
 * Creates the field-setter getter method in the specified {@code ClassNode}
 *
 * @see ValueAccessor//  w w  w  .ja va 2  s.  c om
 *
 * @param cn The ClassNode
 */
private void createFieldSetter(ClassNode cn) {
    MethodNode mn = new MethodNode(ACC_PUBLIC | ACC_FINAL, "getFieldSetter",
            "(Ljava/lang/String;)Ljava/util/function/Consumer;", null, null);

    // Create a check for all labeled fields in the cache
    fieldCache.forEach((id, fn) -> {
        MethodNode handle;
        {
            // Create lambda handle method
            handle = new MethodNode(ACC_PRIVATE | ACC_SYNTHETIC, "lambda$getFieldSetter$" + current++,
                    "(Ljava/lang/Object;)V", null, null);

            handle.visitVarInsn(ALOAD, 0);
            handle.visitVarInsn(ALOAD, 1);
            handle.visitTypeInsn(CHECKCAST, getStrippedDesc(getObject(fn.desc)));

            // If the field is a primitive type, get the primitive value
            String object = getObject(fn.desc);
            if (!object.equals(fn.desc))
                handle.visitMethodInsn(INVOKEVIRTUAL, object, getClassName(fn.desc) + "Value", "()" + fn.desc,
                        false);

            // Set the field value
            handle.visitFieldInsn(PUTFIELD, cn.name, fn.name, fn.desc);
            handle.visitInsn(RETURN);

            // Add the handle method to the class
            cn.methods.add(handle);
        }

        // Create label for IF statement jump
        Label skip = new Label();

        // Compare the target value with the expected value
        mn.visitVarInsn(ALOAD, 1);
        mn.visitLdcInsn(id);
        mn.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z", false);

        // Jump if the input doesn't match the expected value
        mn.visitJumpInsn(IFEQ, skip);

        // Return the setter
        mn.visitVarInsn(ALOAD, 0);
        mn.visitInvokeDynamicInsn("accept", "(L" + cn.name + ";)Ljava/util/function/Consumer;",
                // Define the bootstrap method
                METAFACTORY,
                // Fill the remaining 3 args for the method
                Type.getMethodType("(Ljava/lang/Object;)V"), createMethodHandle(H_INVOKESPECIAL, cn, handle),
                Type.getMethodType("(Ljava/lang/Object;)V"));
        mn.visitInsn(ARETURN);

        // Indicate where the IF statement should jump to if it fails
        mn.visitLabel(skip);
    });
    mn.visitInsn(ACONST_NULL);
    mn.visitInsn(ARETURN);

    cn.methods.add(mn);
}

From source file:de.sanandrew.core.manpack.transformer.TransformELBAttackingPlayer.java

License:Creative Commons License

private static byte[] transformAccessors(byte[] bytes) {
    ClassNode clazz = ASMHelper.createClassNode(bytes);

    int complete = 0;
    for (MethodNode method : clazz.methods) {
        switch (method.name) {
        case "getELAttackingPlayer": {
            method.instructions.clear();
            method.visitCode();/*from   w w w .  j a  va2s  .  com*/
            Label l0 = new Label();
            method.visitLabel(l0);
            method.visitVarInsn(Opcodes.ALOAD, 0);
            method.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "net/minecraft/entity/EntityLivingBase",
                    "_SAP_getAttackingPlayer", "()Lnet/minecraft/entity/player/EntityPlayer;", false);
            method.visitInsn(Opcodes.ARETURN);
            Label l1 = new Label();
            method.visitLabel(l1);
            method.visitLocalVariable("entity", "Lnet/minecraft/entity/EntityLivingBase;", null, l0, l1, 0);
            method.visitMaxs(0, 0);
            method.visitEnd();
            complete++;
            continue;
        }
        case "setELAttackingPlayer": {
            method.instructions.clear();
            method.visitCode();
            Label l0 = new Label();
            method.visitLabel(l0);
            method.visitVarInsn(Opcodes.ALOAD, 1);
            method.visitVarInsn(Opcodes.ALOAD, 0);
            method.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "net/minecraft/entity/EntityLivingBase",
                    "_SAP_setAttackingPlayer", "(Lnet/minecraft/entity/player/EntityPlayer;)V", false);
            Label l1 = new Label();
            method.visitLabel(l1);
            method.visitInsn(Opcodes.RETURN);
            Label l2 = new Label();
            method.visitLabel(l2);
            method.visitLocalVariable("player", "Lnet/minecraft/entity/player/EntityPlayer;", null, l0, l2, 0);
            method.visitLocalVariable("entity", "Lnet/minecraft/entity/EntityLivingBase;", null, l0, l2, 1);
            method.visitMaxs(0, 0);
            method.visitEnd();
            complete++;
            continue;
        }
        case "getELRecentlyHit": {
            method.instructions.clear();
            method.visitCode();
            Label l0 = new Label();
            method.visitLabel(l0);
            method.visitVarInsn(Opcodes.ALOAD, 0);
            method.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "net/minecraft/entity/EntityLivingBase",
                    "_SAP_getRecentlyHit", "()I", false);
            method.visitInsn(Opcodes.IRETURN);
            Label l1 = new Label();
            method.visitLabel(l1);
            method.visitLocalVariable("entity", "Lnet/minecraft/entity/EntityLivingBase;", null, l0, l1, 0);
            method.visitMaxs(0, 0);
            method.visitEnd();
            complete++;
            continue;
        }
        case "setELRecentlyHit": {
            method.instructions.clear();
            method.visitCode();
            Label l0 = new Label();
            method.visitLabel(l0);
            method.visitVarInsn(Opcodes.ALOAD, 1);
            method.visitVarInsn(Opcodes.ILOAD, 0);
            method.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "net/minecraft/entity/EntityLivingBase",
                    "_SAP_setRecentlyHit", "(I)V", false);
            Label l1 = new Label();
            method.visitLabel(l1);
            method.visitInsn(Opcodes.RETURN);
            Label l2 = new Label();
            method.visitLabel(l2);
            method.visitLocalVariable("hit", "I", null, l0, l2, 0);
            method.visitLocalVariable("entity", "Lnet/minecraft/entity/EntityLivingBase;", null, l0, l2, 1);
            method.visitMaxs(0, 0);
            method.visitEnd();
            complete++;
            continue;
        }
        }

        if (complete >= 4) {
            break;
        }
    }

    bytes = ASMHelper.createBytes(clazz, ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);

    return bytes;
}

From source file:net.minecrell.quartz.launch.util.Methods.java

License:MIT License

private static void forward(MethodNode methodNode, String owner, String name) {
    Type[] parameters = Type.getArgumentTypes(methodNode.desc);
    for (int i = 0; i < parameters.length; i++) {
        methodNode.visitVarInsn(parameters[i].getOpcode(ILOAD), i);
    }//from  w w  w  .  j  ava 2  s .  c  o  m

    methodNode.visitMethodInsn(INVOKESPECIAL, owner, name, Type.getMethodDescriptor(Type.VOID_TYPE, parameters),
            false);
    methodNode.visitInsn(Type.getReturnType(methodNode.desc).getOpcode(IRETURN));
}

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

License:Open Source License

public static int generateFieldInjection(ControllerClassData classData, ControllerFieldData<?> fieldData,
        MethodNode mv, int lineNumber) {
    AbstractInsnNode[] result = new AbstractInsnNode[2];
    FieldDescription.InDefinedShape member = fieldData.getMember();
    TypeDescription fieldType = member.getType().asErasure();
    boolean isStatic = member.isStatic();

    lineNumber = AsmUtils.printLineNumber(mv, lineNumber);

    if (isStatic) {
        mv.visitInsn(ACONST_NULL);/*from ww w  .  ja v a2  s . c om*/
    } else {
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 0);
    }

    AsmUtils.storeInt(mv, classData.getIndex());
    AsmUtils.storeInt(mv, fieldData.getIndex());
    mv.visitMethodInsn(INVOKESTATIC, INJECTOR_CLASS, INJECTOR_FIELD, INJECTOR_FIELD_DESC, false);
    return lineNumber;
}

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

License:Open Source License

public static int printMethod(MethodNode mv, String clazz, String method, boolean isStatic, int lineNumber) {
    lineNumber = AsmUtils.printLineNumber(mv, lineNumber);
    if (isStatic) {
        mv.visitMethodInsn(INVOKESTATIC, clazz, method, "()V", false);
    } else {/*  w  w  w .j a  va  2s.c om*/
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, clazz, method, "()V", false);
    }
    return lineNumber;
}

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

License:Open Source License

public static void generateMethodInjection(ControllerClassData classData, ControllerMethodData methodData,
        MethodNode mv, boolean printMethods, int lineNumber) {
    MethodDescription.InDefinedShape member = methodData.getMember();
    boolean isStatic = member.isStatic();

    if (printMethods) {
        lineNumber = printMethods(mv, classData.getType().getInternalName(), methodData.getBefore(), isStatic,
                lineNumber);/*from www  . ja v  a 2  s .c o m*/
    }
    lineNumber = AsmUtils.printLineNumber(mv, lineNumber);

    if (!isStatic) {
        mv.visitVarInsn(ALOAD, 0);
    }

    for (InjectValueData<?, Generic> valueData : methodData.getInjectValues()) {
        if (isStatic) {
            mv.visitInsn(ACONST_NULL);
        } else {
            mv.visitVarInsn(ALOAD, 0);
        }
        AsmUtils.storeInt(mv, classData.getIndex());
        AsmUtils.storeInt(mv, methodData.getIndex());
        AsmUtils.storeInt(mv, valueData.getIndex());
        lineNumber = AsmUtils.printLineNumber(mv, lineNumber);
        mv.visitMethodInsn(INVOKESTATIC, INJECTOR_CLASS, INJECTOR_METHOD, INJECTOR_METHOD_DESC, false);
        TypeDescription paramType = valueData.getType().asErasure();
        mv.visitTypeInsn(CHECKCAST, paramType.getInternalName()); // skip cast check?
    }

    lineNumber = AsmUtils.printLineNumber(mv, lineNumber);

    if (isStatic) {
        mv.visitMethodInsn(INVOKESTATIC, classData.getType().getInternalName(), member.getName(),
                member.getDescriptor(), false);
    } else {
        mv.visitMethodInsn(INVOKESPECIAL, classData.getType().getInternalName(), member.getName(),
                member.getDescriptor(), false);
    }

    if (printMethods) {
        printMethods(mv, classData.getType().getInternalName(), methodData.getAfter(), isStatic, lineNumber);
    }
}

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

License:Open Source License

public static int generateFieldInjection(ControllerClassData classData, ControllerFieldData<?> fieldData,
        MethodNode mv, int lineNumber, PlaceholderType placeholderType) {
    AbstractInsnNode[] result = new AbstractInsnNode[2];
    FieldDescription.InDefinedShape member = fieldData.getMember();
    TypeDescription fieldType = member.getType().asErasure();
    boolean isStatic = member.isStatic();

    lineNumber = AsmUtils.printLineNumber(mv, lineNumber);

    if (isStatic) {
        mv.visitInsn(ACONST_NULL);/*from  w w  w.  ja  v  a 2s  .c o  m*/
    } else {
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 0);
    }

    AsmUtils.storeInt(mv, classData.getIndex());
    AsmUtils.storeInt(mv, fieldData.getIndex());

    switch (placeholderType) {
    case INVALID:
    case UNKNOWN:
    default:
        throw new IllegalStateException("Can't generate injection for invalid placeholders.");
    case NONNULL:
        mv.visitInsn(ICONST_1);
        break;
    case NULLABLE:
        mv.visitInsn(ICONST_0);
        break;
    }

    mv.visitMethodInsn(INVOKESTATIC, INJECTOR_CLASS, INJECTOR_FIELD, INJECTOR_FIELD_DESC, false);
    return lineNumber;
}

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

License:Open Source License

public static void generateMethodInjection(ControllerClassData classData, ControllerMethodData methodData,
        MethodNode mv, boolean printMethods, int lineNumber) {
    MethodDescription.InDefinedShape member = methodData.getMember();
    boolean isStatic = member.isStatic();

    if (printMethods) {
        lineNumber = printMethods(mv, classData.getType().getInternalName(), methodData.getBefore(), isStatic,
                lineNumber);//from  w  ww .ja v  a  2  s .co  m
    }
    lineNumber = AsmUtils.printLineNumber(mv, lineNumber);

    if (!isStatic) {
        mv.visitVarInsn(ALOAD, 0);
    }

    for (InjectValueData<?, Generic> valueData : methodData.getInjectValues()) {
        if (isStatic) {
            mv.visitInsn(ACONST_NULL);
        } else {
            mv.visitVarInsn(ALOAD, 0);
        }
        AsmUtils.storeInt(mv, classData.getIndex());
        AsmUtils.storeInt(mv, methodData.getIndex());
        AsmUtils.storeInt(mv, valueData.getIndex());
        mv.visitInsn(ICONST_0); // skip null checks in methods.
        lineNumber = AsmUtils.printLineNumber(mv, lineNumber);
        mv.visitMethodInsn(INVOKESTATIC, INJECTOR_CLASS, INJECTOR_METHOD, INJECTOR_METHOD_DESC, false);
        TypeDescription paramType = valueData.getType().asErasure();
        mv.visitTypeInsn(CHECKCAST, paramType.getInternalName()); // skip cast check?
    }

    lineNumber = AsmUtils.printLineNumber(mv, lineNumber);

    if (isStatic) {
        mv.visitMethodInsn(INVOKESTATIC, classData.getType().getInternalName(), member.getName(),
                member.getDescriptor(), false);
    } else {
        mv.visitMethodInsn(INVOKESPECIAL, classData.getType().getInternalName(), member.getName(),
                member.getDescriptor(), false);
    }

    if (printMethods) {
        printMethods(mv, classData.getType().getInternalName(), methodData.getAfter(), isStatic, lineNumber);
    }
}

From source file:org.jacoco.core.internal.analysis.filter.EnumEmptyConstructorFilterTest.java

License:Open Source License

@Test
public void should_filter() {
    final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, Opcodes.ACC_PRIVATE, "<init>",
            "(Ljava/lang/String;I)V", null, null);
    m.visitVarInsn(Opcodes.ALOAD, 0);/*from w w w  .ja va2 s.  c  o m*/
    m.visitVarInsn(Opcodes.ALOAD, 1);
    m.visitVarInsn(Opcodes.ILOAD, 2);
    m.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Enum", "<init>", "(Ljava/lang/String;I)V", false);
    m.visitInsn(Opcodes.RETURN);
    context.superClassName = "java/lang/Enum";

    filter.filter(m, context, output);

    assertIgnored(new Range(m.instructions.getFirst(), m.instructions.getLast()));
}