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

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

Introduction

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

Prototype

@Override
    public void visitVarInsn(final int opcode, final int var) 

Source Link

Usage

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

License:Creative Commons License

private static MethodNode injectMethodSetCustomArmorItem() {
    MethodNode method = ASMHelper.getMethodNode(Opcodes.ACC_PRIVATE, ASMNames.MD_SAP_SET_CUSTOM_ARMOR_ITEM);
    method.visitCode();//  www . j  a  v  a2  s  .  c om
    Label l0 = new Label();
    method.visitLabel(l0);
    method.visitVarInsn(Opcodes.ALOAD, 1);
    Label l1 = new Label();
    method.visitJumpInsn(Opcodes.IFNONNULL, l1);
    method.visitTypeInsn(Opcodes.NEW, ASMNames.CL_ITEM_STACK);
    method.visitInsn(Opcodes.DUP);
    ASMHelper.visitFieldInsn(method, Opcodes.GETSTATIC, ASMNames.FD_ITEMS_IRON_SHOVEL);
    method.visitInsn(Opcodes.ICONST_0);
    ASMHelper.visitMethodInsn(method, Opcodes.INVOKESPECIAL, ASMNames.MD_ITEMSTACK_INIT, false);
    method.visitVarInsn(Opcodes.ASTORE, 1);
    method.visitLabel(l1);
    method.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
    method.visitVarInsn(Opcodes.ALOAD, 0);
    ASMHelper.visitFieldInsn(method, Opcodes.GETFIELD, ASMNames.FD_HORSE_DATAWATCHER);
    method.visitIntInsn(Opcodes.BIPUSH, 23);
    method.visitVarInsn(Opcodes.ALOAD, 1);
    ASMHelper.visitMethodInsn(method, Opcodes.INVOKEVIRTUAL, ASMNames.MD_DATAWATCHER_UPDATE_OBJ, false);
    Label l3 = new Label();
    method.visitLabel(l3);
    method.visitInsn(Opcodes.RETURN);
    Label l4 = new Label();
    method.visitLabel(l4);
    method.visitLocalVariable("this", ASMNames.CL_T_ENTITY_HORSE, null, l0, l3, 0);
    method.visitLocalVariable("stack", ASMNames.CL_T_ITEM_STACK, null, l0, l3, 1);
    method.visitMaxs(5, 2);
    method.visitEnd();

    return method;
}

From source file:naftoreiclag.dontdigleft.transformer.ResizePlayerBoudingBox.java

License:Open Source License

@Override
public byte[] transform(String name, String transformedName, byte[] basicClass) {
    if (name.equals("net.minecraft.entity.player.EntityPlayer") || name.equals("xl")) {
        ClassNode classNode = new ClassNode();
        ClassReader classReader = new ClassReader(basicClass);

        classReader.accept(classNode, 0);

        MethodNode mv = new MethodNode(ASM4, ACC_PROTECTED, "setSize", "(FF)V", null, null);

        mv.visitCode();/* w  w w  . jav a 2 s  .c om*/
        Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitVarInsn(FLOAD, 1);
        mv.visitLdcInsn(new Float("0.6"));
        mv.visitInsn(FCMPL);
        Label l1 = new Label();
        mv.visitJumpInsn(IFNE, l1);
        mv.visitVarInsn(FLOAD, 2);
        mv.visitLdcInsn(new Float("1.8"));
        mv.visitInsn(FCMPL);
        mv.visitJumpInsn(IFNE, l1);
        Label l2 = new Label();
        mv.visitLabel(l2);
        mv.visitLdcInsn(new Float("0.6"));
        mv.visitVarInsn(FSTORE, 1);
        Label l3 = new Label();
        mv.visitLabel(l3);
        mv.visitLdcInsn(new Float("0.6"));
        mv.visitVarInsn(FSTORE, 2);
        mv.visitLabel(l1);
        mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(FLOAD, 1);
        mv.visitVarInsn(FLOAD, 2);
        mv.visitMethodInsn(INVOKESPECIAL, "net/minecraft/entity/Entity", "setSize", "(FF)V");
        Label l4 = new Label();
        mv.visitLabel(l4);
        mv.visitInsn(RETURN);
        Label l5 = new Label();
        mv.visitLabel(l5);
        mv.visitLocalVariable("this", "Lnet/minecraft/entity/player/EntityPlayer;", null, l0, l5, 0);
        mv.visitLocalVariable("par1", "F", null, l0, l5, 1);
        mv.visitLocalVariable("par2", "F", null, l0, l5, 2);
        mv.visitMaxs(3, 3);
        mv.visitEnd();

        ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
        classNode.accept(cw);
        mv.accept(cw);

        return cw.toByteArray();
    } else {
        return basicClass;
    }
}

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   ww w . ja  v a  2  s .c  om

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

From source file:net.petercashel.jmsDd.util.ASMTransformer.java

License:Apache License

public static byte[] transform(String name, byte[] bytes) {
    if (debug)//  ww  w.  ja va  2s  . c  om
        System.out.println(bytes.length);
    ClassNode classNode = new ClassNode();
    String classNameASM = name.replace('.', '/');
    ClassReader classReader = new ClassReader(bytes);
    classReader.accept(classNode, 0);
    boolean DoModInit = false;
    boolean HasInit = false;
    String initDesc = "()V";
    boolean lockDesc = false;

    try {
        try {
            for (int i = 0; i < classNode.visibleAnnotations.size(); i++) {
                AnnotationNode ann = (AnnotationNode) classNode.visibleAnnotations.get(i);
                if (ann.desc.equalsIgnoreCase("Lnet/petercashel/jmsDd/module/Module;")) {
                    try {
                        if (debug)
                            System.out.println("ANNOTE!");
                        DoModInit = true;
                        Map<String, Object> values = asmList2Map(ann.values);
                        ModuleSystem.modulesToLoad.put(
                                values.get("ModuleName").toString().replace("[", "").replace("]", ""),
                                classNode.name.replace("/", "."));
                    } catch (Exception e) {
                        e.printStackTrace();

                    }
                }
            }
        } catch (Exception e) {
        }

        try {
            if (DoModInit) {

                for (int i = 0; i < classNode.methods.size(); i++) {
                    MethodNode m = (MethodNode) classNode.methods.get(i);
                    if (m.name.contentEquals("<init>")) {
                        initDesc = m.desc;
                        if (m.desc.contentEquals("()V")) {
                            HasInit = true;
                            if (debug)
                                System.out.println("Found <init>");
                        }
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (debug)
        System.out.println("Still alive?");
    try {

        //          L0
        //             LINENUMBER 43 L0
        //             ALOAD 0
        //             INVOKESPECIAL // CLASSNAME FOR ASM  // ()V    ////// This is effectically a super() call but to the discovered constructor
        //            L1
        //             LINENUMBER 44 L1
        //             INVOKESTATIC net/petercashel/jmsDd/API/API$Impl.getAPI ()Lnet/petercashel/jmsDd/API/API;
        //             ALOAD 0
        //             INVOKEINTERFACE net/petercashel/jmsDd/API/API.registerEventBus (Ljava/lang/Object;)V
        //            L2
        //             LINENUMBER 46 L2
        //             RETURN
        //            L3
        //             LOCALVARIABLE this // "L" + CLASSNAME FOR ASM + ";" // L0 L3 0
        //             LOCALVARIABLE e Lnet/petercashel/jmsDd/event/module/DummyEvent; L0 L3 1
        //             MAXSTACK = 2
        //             MAXLOCALS = 2
        if (DoModInit) {
            if (HasInit) {
                if (debug)
                    System.out.println("Adding Extra Constructor to " + name);
                MethodNode constructor = new MethodNode(Opcodes.ACC_PUBLIC, "<init>",
                        "(Lnet/petercashel/jmsDd/event/module/DummyEvent;)V", null, null);

                Label L0 = new Label();
                constructor.visitLabel(L0);
                constructor.visitVarInsn(Opcodes.ALOAD, 0);
                constructor.visitMethodInsn(Opcodes.INVOKESPECIAL, classNameASM, "<init>", initDesc);

                Label L1 = new Label();
                constructor.visitLabel(L1);
                constructor.visitMethodInsn(Opcodes.INVOKESTATIC, "net/petercashel/jmsDd/API/API$Impl",
                        "getAPI", "()Lnet/petercashel/jmsDd/API/API;");
                constructor.visitVarInsn(Opcodes.ALOAD, 0);
                constructor.visitMethodInsn(Opcodes.INVOKEINTERFACE, "net/petercashel/jmsDd/API/API",
                        "registerEventBus", "(Ljava/lang/Object;)V");

                Label L2 = new Label();
                constructor.visitLabel(L2);
                constructor.visitInsn(Opcodes.RETURN);

                Label L3 = new Label();
                constructor.visitLabel(L3);
                constructor.visitLocalVariable("this", "L" + classNameASM + ";", null, L0, L3, 0);
                constructor.visitLocalVariable("e", "Lnet/petercashel/jmsDd/event/module/DummyEvent;", null, L0,
                        L3, 1);
                constructor.visitMaxs(2, 2);
                constructor.visitEnd();
                classNode.methods.add(constructor);

            } else {
                System.err.println("WARNING! " + name
                        + " Doesn't have a default no-args constructor.  Module loader cannot chain load the constructor. \n If you are recieving this error and your module has no constructors defined, or a no-args constructor defined, \n please report the bug to the author of JMSDd.");
                MethodNode constructor = new MethodNode(Opcodes.ACC_PUBLIC, "<init>",
                        "(Lnet/petercashel/jmsDd/event/module/DummyEvent;)V", null, null);

                Label L0 = new Label();
                constructor.visitLabel(L0);
                constructor.visitVarInsn(Opcodes.ALOAD, 0);
                //INVOKESPECIAL java/lang/Object.<init> ()V ////// There is no other constructor, call super() to Object.
                constructor.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V");

                Label L1 = new Label();
                constructor.visitLabel(L1);
                constructor.visitMethodInsn(Opcodes.INVOKESTATIC, "net/petercashel/jmsDd/API/API$Impl",
                        "getAPI", "()Lnet/petercashel/jmsDd/API/API;");
                constructor.visitVarInsn(Opcodes.ALOAD, 0);
                constructor.visitMethodInsn(Opcodes.INVOKEINTERFACE, "net/petercashel/jmsDd/API/API",
                        "registerEventBus", "(Ljava/lang/Object;)V");

                Label L2 = new Label();
                constructor.visitLabel(L2);
                constructor.visitInsn(Opcodes.RETURN);

                Label L3 = new Label();
                constructor.visitLabel(L3);
                constructor.visitLocalVariable("this", "L" + classNameASM + ";", null, L0, L3, 0);
                constructor.visitLocalVariable("e", "Lnet/petercashel/jmsDd/event/module/DummyEvent;", null, L0,
                        L3, 1);
                constructor.visitMaxs(2, 2);
                constructor.visitEnd();
                classNode.methods.add(constructor);
            }
            classNode.visitEnd();
            ClassWriter wr = new ClassWriter(0);
            classNode.accept(wr);

            bytes = wr.toByteArray();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (debug)
        System.out.println("Still alive.");
    if (debug)
        System.out.println(bytes.length);
    if (plugins.size() > 0) {
        for (ASMPlugin p : plugins) {
            try {
                bytes = p.transform(name, bytes);
            } catch (Exception e) {

            }
        }
    }

    return bytes;
}

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 .j  av  a 2 s  . co  m*/
    } 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  v  a  2  s. 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 . j  a v  a  2 s. com
    }
    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 .  j a va2s. com
    } 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 ww w. 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());
        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);
    m.visitVarInsn(Opcodes.ALOAD, 1);/*ww  w  .  ja  v a 2  s.com*/
    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()));
}