Example usage for org.objectweb.asm Opcodes NEW

List of usage examples for org.objectweb.asm Opcodes NEW

Introduction

In this page you can find the example usage for org.objectweb.asm Opcodes NEW.

Prototype

int NEW

To view the source code for org.objectweb.asm Opcodes NEW.

Click Source Link

Usage

From source file:com.yahoo.yqlplus.engine.internal.compiler.CodeEmitter.java

public void emitNew(String typeInternalName, BytecodeExpression... constructorArguments) {
    methodVisitor.visitTypeInsn(Opcodes.NEW, typeInternalName);
    methodVisitor.visitInsn(Opcodes.DUP);
    String desc = Type.getMethodDescriptor(Type.VOID_TYPE);
    if (constructorArguments != null && constructorArguments.length > 0) {
        Type[] argumentTypes = new Type[constructorArguments.length];
        for (int i = 0; i < constructorArguments.length; ++i) {
            argumentTypes[i] = constructorArguments[i].getType().getJVMType();
        }/*from  w  ww.j a v a2s .  c o  m*/
        desc = Type.getMethodDescriptor(Type.VOID_TYPE, argumentTypes);
    }
    methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, typeInternalName, "<init>", desc, false);
}

From source file:com.yahoo.yqlplus.engine.internal.source.MissingRequiredFieldExpr.java

@Override
public void generate(CodeEmitter code) {
    MethodVisitor mv = code.getMethodVisitor();
    mv.visitTypeInsn(Opcodes.NEW, Type.getInternalName(IllegalArgumentException.class));
    mv.visitInsn(Opcodes.DUP);/* ww w . ja  v  a2  s  . c o m*/
    mv.visitLdcInsn(String.format("%s::%s Missing required property '%s' (%s)", className, methodName,
            propertyName, type.getTypeName()));
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(IllegalArgumentException.class), "<init>",
            Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(String.class)), false);
    mv.visitInsn(Opcodes.ATHROW);
}

From source file:de.dynamicfiles.projects.gradle.plugins.javafx.tasks.internal.MonkeyPatcher.java

License:Apache License

private static void doMonkeyPatchFileHandleLeak(ClassReader classReader, ClassWriter classWriter) {
    classReader.accept(new ClassVisitor(Opcodes.ASM5, classWriter) {
        @Override//from   w w  w .ja v a 2s.  c o  m
        public MethodVisitor visitMethod(int access, String name, String desc, String signature,
                String[] exceptions) {
            if (!(name.equals(METHOD_TO_MONKEY_PATCH) && desc.equals(METHOD_SIGNATURE_TO_MONKEY_PATCH))) {
                return super.visitMethod(access, name, desc, signature, exceptions);
            }
            // helpful source: http://web.cs.ucla.edu/~msb/cs239-tutorial/
            // "We will do this using the Adapter Pattern. Adapters wrap an object, overriding some of its methods, and delegating to the others."
            // ugly adapter-pattern ... took me more time than I really can tell here <.<
            return getMonkeyPatchedFileHandleLeakMethodVisitor(access, name, desc, signature, exceptions);
        }

        private MethodVisitor getMonkeyPatchedFileHandleLeakMethodVisitor(int access, String name, String desc,
                String signature, String[] exceptions) {
            return new MethodVisitor(Opcodes.ASM5,
                    super.visitMethod(access, name, desc, signature, exceptions)) {

                /*
                 TODO improve detection of lambda-positions, numbers might vary on different compile-versions
                 */
                @Override
                public void visitCode() {
                    // This mostly got generated from ASM itself, except some adjustments for lambda-IDs and removed "visitMaxs()"-call
                    String javalangThrowable = "java/lang/Throwable";
                    String javaioFile = "java/io/File";
                    String javalangString = "java/lang/String";

                    Label l0 = new Label();
                    Label l1 = new Label();
                    Label l2 = new Label();
                    mv.visitTryCatchBlock(l0, l1, l2, javalangThrowable);
                    Label l3 = new Label();
                    Label l4 = new Label();
                    Label l5 = new Label();
                    mv.visitTryCatchBlock(l3, l4, l5, javalangThrowable);
                    Label l6 = new Label();
                    mv.visitTryCatchBlock(l3, l4, l6, null);
                    Label l7 = new Label();
                    Label l8 = new Label();
                    Label l9 = new Label();
                    mv.visitTryCatchBlock(l7, l8, l9, javalangThrowable);
                    Label l10 = new Label();
                    mv.visitTryCatchBlock(l5, l10, l6, null);
                    mv.visitInsn(Opcodes.ACONST_NULL);
                    mv.visitVarInsn(Opcodes.ASTORE, 3);
                    mv.visitVarInsn(Opcodes.ALOAD, 2);
                    Label l11 = new Label();
                    mv.visitJumpInsn(Opcodes.IFNULL, l11);
                    mv.visitVarInsn(Opcodes.ALOAD, 2);
                    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, javaioFile, "isDirectory", "()Z", false);
                    Label l12 = new Label();
                    mv.visitJumpInsn(Opcodes.IFNE, l12);
                    mv.visitLabel(l11);
                    mv.visitFrame(Opcodes.F_APPEND, 1, new Object[] { javalangString }, 0, null);
                    mv.visitTypeInsn(Opcodes.NEW, javaioFile);
                    mv.visitInsn(Opcodes.DUP);
                    mv.visitLdcInsn("java.home");
                    mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/System", "getProperty",
                            "(Ljava/lang/String;)Ljava/lang/String;", false);
                    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, javaioFile, "<init>", "(Ljava/lang/String;)V",
                            false);
                    mv.visitVarInsn(Opcodes.ASTORE, 2);
                    mv.visitLabel(l12);
                    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
                    mv.visitFieldInsn(Opcodes.GETSTATIC, "com/oracle/tools/packager/windows/WinAppBundler",
                            "VS_VERS", "[Ljava/lang/String;");
                    mv.visitVarInsn(Opcodes.ASTORE, 4);
                    mv.visitVarInsn(Opcodes.ALOAD, 4);
                    mv.visitInsn(Opcodes.ARRAYLENGTH);
                    mv.visitVarInsn(Opcodes.ISTORE, 5);
                    mv.visitInsn(Opcodes.ICONST_0);
                    mv.visitVarInsn(Opcodes.ISTORE, 6);
                    Label l13 = new Label();
                    mv.visitLabel(l13);
                    mv.visitFrame(Opcodes.F_APPEND, 3,
                            new Object[] { "[Ljava/lang/String;", Opcodes.INTEGER, Opcodes.INTEGER }, 0, null);
                    mv.visitVarInsn(Opcodes.ILOAD, 6);
                    mv.visitVarInsn(Opcodes.ILOAD, 5);
                    Label l14 = new Label();
                    mv.visitJumpInsn(Opcodes.IF_ICMPGE, l14);
                    mv.visitVarInsn(Opcodes.ALOAD, 4);
                    mv.visitVarInsn(Opcodes.ILOAD, 6);
                    mv.visitInsn(Opcodes.AALOAD);
                    mv.visitVarInsn(Opcodes.ASTORE, 7);
                    mv.visitVarInsn(Opcodes.ALOAD, 0);
                    mv.visitVarInsn(Opcodes.ALOAD, 1);
                    mv.visitVarInsn(Opcodes.ALOAD, 7);
                    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "com/oracle/tools/packager/windows/WinAppBundler",
                            "copyMSVCDLLs", "(Ljava/io/File;Ljava/lang/String;)Z", false);
                    Label l15 = new Label();
                    mv.visitJumpInsn(Opcodes.IFEQ, l15);
                    mv.visitVarInsn(Opcodes.ALOAD, 7);
                    mv.visitVarInsn(Opcodes.ASTORE, 3);
                    mv.visitJumpInsn(Opcodes.GOTO, l14);
                    mv.visitLabel(l15);
                    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
                    mv.visitIincInsn(6, 1);
                    mv.visitJumpInsn(Opcodes.GOTO, l13);
                    mv.visitLabel(l14);
                    mv.visitFrame(Opcodes.F_CHOP, 3, null, 0, null);
                    mv.visitVarInsn(Opcodes.ALOAD, 3);
                    Label l16 = new Label();
                    mv.visitJumpInsn(Opcodes.IFNONNULL, l16);
                    mv.visitTypeInsn(Opcodes.NEW, "java/lang/RuntimeException");
                    mv.visitInsn(Opcodes.DUP);
                    mv.visitLdcInsn("Not found MSVC dlls");
                    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/RuntimeException", "<init>",
                            "(Ljava/lang/String;)V", false);
                    mv.visitInsn(Opcodes.ATHROW);
                    mv.visitLabel(l16);
                    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
                    mv.visitTypeInsn(Opcodes.NEW, "java/util/concurrent/atomic/AtomicReference");
                    mv.visitInsn(Opcodes.DUP);
                    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/util/concurrent/atomic/AtomicReference",
                            "<init>", "()V", false);
                    mv.visitVarInsn(Opcodes.ASTORE, 4);
                    mv.visitVarInsn(Opcodes.ALOAD, 3);
                    mv.visitVarInsn(Opcodes.ASTORE, 5);
                    mv.visitVarInsn(Opcodes.ALOAD, 2);
                    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, javaioFile, "toPath", "()Ljava/nio/file/Path;",
                            false);
                    mv.visitLdcInsn("bin");
                    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/nio/file/Path", "resolve",
                            "(Ljava/lang/String;)Ljava/nio/file/Path;", true);
                    mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/nio/file/Files", "list",
                            "(Ljava/nio/file/Path;)Ljava/util/stream/Stream;", false);
                    mv.visitVarInsn(Opcodes.ASTORE, 6);
                    mv.visitInsn(Opcodes.ACONST_NULL);
                    mv.visitVarInsn(Opcodes.ASTORE, 7);
                    mv.visitLabel(l3);
                    mv.visitVarInsn(Opcodes.ALOAD, 6);
                    mv.visitInvokeDynamicInsn("test", "()Ljava/util/function/Predicate;", new Handle(
                            Opcodes.H_INVOKESTATIC, "java/lang/invoke/LambdaMetafactory", "metafactory",
                            "(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;"),
                            new Object[] { Type.getType("(Ljava/lang/Object;)Z"),
                                    new Handle(Opcodes.H_INVOKESTATIC,
                                            "com/oracle/tools/packager/windows/WinAppBundler",
                                            "lambda$copyMSVCDLLs$261", "(Ljava/nio/file/Path;)Z"),
                                    Type.getType("(Ljava/nio/file/Path;)Z") }); // modified lambda-name
                    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/stream/Stream", "filter",
                            "(Ljava/util/function/Predicate;)Ljava/util/stream/Stream;", true);
                    mv.visitVarInsn(Opcodes.ALOAD, 5);
                    mv.visitInvokeDynamicInsn("test", "(Ljava/lang/String;)Ljava/util/function/Predicate;",
                            new Handle(Opcodes.H_INVOKESTATIC, "java/lang/invoke/LambdaMetafactory",
                                    "metafactory",
                                    "(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;"),
                            new Object[] { Type.getType("(Ljava/lang/Object;)Z"), new Handle(
                                    Opcodes.H_INVOKESTATIC, "com/oracle/tools/packager/windows/WinAppBundler",
                                    "lambda$copyMSVCDLLs$262", "(Ljava/lang/String;Ljava/nio/file/Path;)Z"),
                                    Type.getType("(Ljava/nio/file/Path;)Z") }); // modified lambda-name
                    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/stream/Stream", "filter",
                            "(Ljava/util/function/Predicate;)Ljava/util/stream/Stream;", true);
                    mv.visitVarInsn(Opcodes.ALOAD, 1);
                    mv.visitVarInsn(Opcodes.ALOAD, 4);
                    mv.visitInvokeDynamicInsn("accept",
                            "(Ljava/io/File;Ljava/util/concurrent/atomic/AtomicReference;)Ljava/util/function/Consumer;",
                            new Handle(Opcodes.H_INVOKESTATIC, "java/lang/invoke/LambdaMetafactory",
                                    "metafactory",
                                    "(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;"),
                            new Object[] { Type.getType("(Ljava/lang/Object;)V"), new Handle(
                                    Opcodes.H_INVOKESTATIC, "com/oracle/tools/packager/windows/WinAppBundler",
                                    "lambda$copyMSVCDLLs$263",
                                    "(Ljava/io/File;Ljava/util/concurrent/atomic/AtomicReference;Ljava/nio/file/Path;)V"),
                                    Type.getType("(Ljava/nio/file/Path;)V") }); // modified lambda-name
                    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/stream/Stream", "forEach",
                            "(Ljava/util/function/Consumer;)V", true);
                    mv.visitLabel(l4);
                    mv.visitVarInsn(Opcodes.ALOAD, 6);
                    Label l17 = new Label();
                    mv.visitJumpInsn(Opcodes.IFNULL, l17);
                    mv.visitVarInsn(Opcodes.ALOAD, 7);
                    Label l18 = new Label();
                    mv.visitJumpInsn(Opcodes.IFNULL, l18);
                    mv.visitLabel(l0);
                    mv.visitVarInsn(Opcodes.ALOAD, 6);
                    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/stream/Stream", "close", "()V",
                            true);
                    mv.visitLabel(l1);
                    mv.visitJumpInsn(Opcodes.GOTO, l17);
                    mv.visitLabel(l2);
                    mv.visitFrame(Opcodes.F_FULL, 8,
                            new Object[] { "com/oracle/tools/packager/windows/WinAppBundler", javaioFile,
                                    javaioFile, javalangString, "java/util/concurrent/atomic/AtomicReference",
                                    javalangString, "java/util/stream/Stream", javalangThrowable },
                            1, new Object[] { javalangThrowable });
                    mv.visitVarInsn(Opcodes.ASTORE, 8);
                    mv.visitVarInsn(Opcodes.ALOAD, 7);
                    mv.visitVarInsn(Opcodes.ALOAD, 8);
                    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, javalangThrowable, "addSuppressed",
                            "(Ljava/lang/Throwable;)V", false);
                    mv.visitJumpInsn(Opcodes.GOTO, l17);
                    mv.visitLabel(l18);
                    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
                    mv.visitVarInsn(Opcodes.ALOAD, 6);
                    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/stream/Stream", "close", "()V",
                            true);
                    mv.visitJumpInsn(Opcodes.GOTO, l17);
                    mv.visitLabel(l5);
                    mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { javalangThrowable });
                    mv.visitVarInsn(Opcodes.ASTORE, 8);
                    mv.visitVarInsn(Opcodes.ALOAD, 8);
                    mv.visitVarInsn(Opcodes.ASTORE, 7);
                    mv.visitVarInsn(Opcodes.ALOAD, 8);
                    mv.visitInsn(Opcodes.ATHROW);
                    mv.visitLabel(l6);
                    mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { javalangThrowable });
                    mv.visitVarInsn(Opcodes.ASTORE, 9);
                    mv.visitLabel(l10);
                    mv.visitVarInsn(Opcodes.ALOAD, 6);
                    Label l19 = new Label();
                    mv.visitJumpInsn(Opcodes.IFNULL, l19);
                    mv.visitVarInsn(Opcodes.ALOAD, 7);
                    Label l20 = new Label();
                    mv.visitJumpInsn(Opcodes.IFNULL, l20);
                    mv.visitLabel(l7);
                    mv.visitVarInsn(Opcodes.ALOAD, 6);
                    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/stream/Stream", "close", "()V",
                            true);
                    mv.visitLabel(l8);
                    mv.visitJumpInsn(Opcodes.GOTO, l19);
                    mv.visitLabel(l9);
                    mv.visitFrame(Opcodes.F_FULL, 10,
                            new Object[] { "com/oracle/tools/packager/windows/WinAppBundler", javaioFile,
                                    javaioFile, javalangString, "java/util/concurrent/atomic/AtomicReference",
                                    javalangString, "java/util/stream/Stream", javalangThrowable, Opcodes.TOP,
                                    javalangThrowable },
                            1, new Object[] { javalangThrowable });
                    mv.visitVarInsn(Opcodes.ASTORE, 10);
                    mv.visitVarInsn(Opcodes.ALOAD, 7);
                    mv.visitVarInsn(Opcodes.ALOAD, 10);
                    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, javalangThrowable, "addSuppressed",
                            "(Ljava/lang/Throwable;)V", false);
                    mv.visitJumpInsn(Opcodes.GOTO, l19);
                    mv.visitLabel(l20);
                    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
                    mv.visitVarInsn(Opcodes.ALOAD, 6);
                    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/stream/Stream", "close", "()V",
                            true);
                    mv.visitLabel(l19);
                    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
                    mv.visitVarInsn(Opcodes.ALOAD, 9);
                    mv.visitInsn(Opcodes.ATHROW);
                    mv.visitLabel(l17);
                    mv.visitFrame(Opcodes.F_FULL, 6,
                            new Object[] { "com/oracle/tools/packager/windows/WinAppBundler", javaioFile,
                                    javaioFile, javalangString, "java/util/concurrent/atomic/AtomicReference",
                                    javalangString },
                            0, new Object[] {});
                    mv.visitVarInsn(Opcodes.ALOAD, 4);
                    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/util/concurrent/atomic/AtomicReference",
                            "get", "()Ljava/lang/Object;", false);
                    mv.visitTypeInsn(Opcodes.CHECKCAST, "java/io/IOException");
                    mv.visitVarInsn(Opcodes.ASTORE, 6);
                    mv.visitVarInsn(Opcodes.ALOAD, 6);
                    Label l21 = new Label();
                    mv.visitJumpInsn(Opcodes.IFNULL, l21);
                    mv.visitVarInsn(Opcodes.ALOAD, 6);
                    mv.visitInsn(Opcodes.ATHROW);
                    mv.visitLabel(l21);
                    mv.visitFrame(Opcodes.F_APPEND, 1, new Object[] { "java/io/IOException" }, 0, null);
                    mv.visitInsn(Opcodes.RETURN);
                }

            };
        }

    }, ClassReader.EXPAND_FRAMES); // ClassReader.EXPAND_FRAMES required for Java 8
}

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

License:Creative Commons License

private static byte[] transformEnderman(byte[] bytes) {
    ClassNode clazz = ASMHelper.createClassNode(bytes);
    MethodNode method = ASMHelper.findMethod(clazz, ASMNames.MD_ENDERMAN_SHOULD_ATTACK_PLAYER);

    InsnList needle = new InsnList();
    needle.add(new VarInsnNode(Opcodes.ALOAD, 1));
    needle.add(ASMHelper.getFieldInsnNode(Opcodes.GETFIELD, ASMNames.FD_PLAYER_INVENTORY));
    needle.add(ASMHelper.getFieldInsnNode(Opcodes.GETFIELD, ASMNames.FD_INVPLAYER_ARMOR_INVENTORY));
    needle.add(new InsnNode(Opcodes.ICONST_3));
    needle.add(new InsnNode(Opcodes.AALOAD));
    needle.add(new VarInsnNode(Opcodes.ASTORE, 2));

    AbstractInsnNode insertPt = ASMHelper.findFirstNodeFromNeedle(method.instructions, needle);

    InsnList newInstr = new InsnList();
    newInstr.add(ASMHelper.getFieldInsnNode(Opcodes.GETSTATIC, ASMNames.FD_SAPUTILS_EVENT_BUS));
    newInstr.add(new TypeInsnNode(Opcodes.NEW, ASMNames.CL_ENDER_FACING_EVENT));
    newInstr.add(new InsnNode(Opcodes.DUP));
    newInstr.add(new VarInsnNode(Opcodes.ALOAD, 1));
    newInstr.add(new VarInsnNode(Opcodes.ALOAD, 0));
    newInstr.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKESPECIAL, ASMNames.MD_ENDERFACINGEVENT_INIT, false));
    newInstr.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_EVENT_BUS_POST, false));
    LabelNode l1 = new LabelNode();
    newInstr.add(new JumpInsnNode(Opcodes.IFEQ, l1));
    newInstr.add(new InsnNode(Opcodes.ICONST_0));
    newInstr.add(new InsnNode(Opcodes.IRETURN));
    newInstr.add(l1);//from ww w . j  ava2s  .  c om

    method.instructions.insertBefore(insertPt, newInstr);

    return ASMHelper.createBytes(clazz, /*ClassWriter.COMPUTE_FRAMES |*/ ClassWriter.COMPUTE_MAXS);
}

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

License:Creative Commons License

private static byte[] transformWorld(byte[] bytes) {
    ClassNode clazz = ASMHelper.createClassNode(bytes);
    MethodNode method = ASMHelper.findMethod(clazz, ASMNames.MD_WORLD_GET_COLLIDING_BB);

    InsnList needle = new InsnList();
    LabelNode ln = new LabelNode();
    needle.add(ln);/*from  w w w.  j  av  a 2 s  . co m*/
    needle.add(new LineNumberNode(-1, ln));
    needle.add(new VarInsnNode(Opcodes.ALOAD, 0));
    needle.add(new VarInsnNode(Opcodes.ALOAD, 1));
    needle.add(new VarInsnNode(Opcodes.ALOAD, 2));
    needle.add(new VarInsnNode(Opcodes.DLOAD, -1));
    needle.add(new VarInsnNode(Opcodes.DLOAD, -1));
    needle.add(new VarInsnNode(Opcodes.DLOAD, -1));
    needle.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_AABB_EXPAND, false));
    needle.add(
            ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_WORLD_GET_ENTITIES_EXCLUDE, false));
    needle.add(new VarInsnNode(Opcodes.ASTORE, -1));

    VarInsnNode insertPoint = (VarInsnNode) ASMHelper.findLastNodeFromNeedle(method.instructions, needle);

    InsnList injectList = new InsnList();
    injectList.add(new LabelNode());
    injectList.add(ASMHelper.getFieldInsnNode(Opcodes.GETSTATIC, ASMNames.FD_SAPUTILS_EVENT_BUS));
    injectList.add(new TypeInsnNode(Opcodes.NEW, ASMNames.CL_COLLIDING_ENTITY_CHECK_EVENT));
    injectList.add(new InsnNode(Opcodes.DUP));
    injectList.add(new VarInsnNode(Opcodes.ALOAD, 0));
    injectList.add(new VarInsnNode(Opcodes.ALOAD, insertPoint.var));
    injectList.add(new VarInsnNode(Opcodes.ALOAD, 1));
    injectList.add(new VarInsnNode(Opcodes.ALOAD, 2));
    injectList.add(
            ASMHelper.getMethodInsnNode(Opcodes.INVOKESPECIAL, ASMNames.MD_SAP_COLLENTITYCHKEVT_INIT, false));
    injectList.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_EVENT_BUS_POST, false));
    injectList.add(new InsnNode(Opcodes.POP));

    method.instructions.insert(insertPoint, injectList);

    // insert entity-sensitive bounding box method

    needle = new InsnList();
    ln = new LabelNode();
    needle.add(ln);
    needle.add(new LineNumberNode(-1, ln));
    needle.add(new VarInsnNode(Opcodes.ALOAD, 11));
    needle.add(new VarInsnNode(Opcodes.ILOAD, 12));
    needle.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKEINTERFACE, ASMNames.MD_LIST_GET, true));
    needle.add(new TypeInsnNode(Opcodes.CHECKCAST, ASMNames.CL_ENTITY));
    needle.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_ENTITY_GET_BOUNDING_BOX, false));
    needle.add(new VarInsnNode(Opcodes.ASTORE, -1));

    insertPoint = (VarInsnNode) ASMHelper.findLastNodeFromNeedle(method.instructions, needle);

    injectList = new InsnList();
    injectList.add(new LabelNode());
    injectList.add(new VarInsnNode(Opcodes.ALOAD, 11));
    injectList.add(new VarInsnNode(Opcodes.ILOAD, 12));
    injectList.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKEINTERFACE, ASMNames.MD_LIST_GET, true));
    injectList.add(new TypeInsnNode(Opcodes.CHECKCAST, ASMNames.CL_ENTITY));
    injectList.add(new VarInsnNode(Opcodes.ALOAD, 1));
    injectList.add(new VarInsnNode(Opcodes.ALOAD, insertPoint.var));
    injectList.add(
            ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_SAP_ENTITY_GET_BOUNDING_BOX, false));
    injectList.add(new VarInsnNode(Opcodes.ASTORE, insertPoint.var));

    method.instructions.insert(insertPoint, injectList);

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

    return bytes;
}

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 . ja  v  a 2  s .c  o m
    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:de.sanandrew.core.manpack.transformer.TransformHorseArmor.java

License:Creative Commons License

private static void transformArmorTexture(MethodNode method) {
    InsnList needle = new InsnList();
    needle.add(new VarInsnNode(Opcodes.ALOAD, 0));
    needle.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_HORSE_FUNC110241CB, false));
    needle.add(new VarInsnNode(Opcodes.ISTORE, 3));

    AbstractInsnNode node = ASMHelper.findLastNodeFromNeedle(method.instructions, needle);

    InsnList newInstr = new InsnList();
    LabelNode l17 = new LabelNode();
    newInstr.add(l17);//from w w  w. ja  v  a 2  s  . c  o m
    newInstr.add(new VarInsnNode(Opcodes.ALOAD, 0));
    newInstr.add(
            ASMHelper.getMethodInsnNode(Opcodes.INVOKESPECIAL, ASMNames.MD_SAP_GET_CUSTOM_ARMOR_ITEM, false));
    newInstr.add(new VarInsnNode(Opcodes.ASTORE, 4));
    LabelNode l18 = new LabelNode();
    newInstr.add(l18);
    newInstr.add(new VarInsnNode(Opcodes.ALOAD, 4));
    newInstr.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_ITEMSTACK_GET_ITEM, false));
    newInstr.add(new TypeInsnNode(Opcodes.INSTANCEOF, ASMNames.CL_ITEM_HORSE_ARMOR));
    LabelNode l19 = new LabelNode();
    newInstr.add(new JumpInsnNode(Opcodes.IFEQ, l19));
    LabelNode l20 = new LabelNode();
    newInstr.add(l20);
    newInstr.add(new VarInsnNode(Opcodes.ALOAD, 0));
    newInstr.add(ASMHelper.getFieldInsnNode(Opcodes.GETFIELD, ASMNames.FD_HORSE_FIELD110280BR));
    newInstr.add(new InsnNode(Opcodes.ICONST_2));
    newInstr.add(new VarInsnNode(Opcodes.ALOAD, 4));
    newInstr.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_ITEMSTACK_GET_ITEM, false));
    newInstr.add(new TypeInsnNode(Opcodes.CHECKCAST, ASMNames.CL_ITEM_HORSE_ARMOR));
    newInstr.add(new VarInsnNode(Opcodes.ALOAD, 0));
    newInstr.add(new VarInsnNode(Opcodes.ALOAD, 4));
    newInstr.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_SAP_GET_ARMOR_TEXTURE, false));
    newInstr.add(new InsnNode(Opcodes.AASTORE));
    LabelNode l21 = new LabelNode();
    newInstr.add(l21);
    newInstr.add(new VarInsnNode(Opcodes.ALOAD, 0));
    newInstr.add(new TypeInsnNode(Opcodes.NEW, ASMNames.CL_STRING_BUILDER));
    newInstr.add(new InsnNode(Opcodes.DUP));
    newInstr.add(new VarInsnNode(Opcodes.ALOAD, 0));
    newInstr.add(ASMHelper.getFieldInsnNode(Opcodes.GETFIELD, ASMNames.FD_HORSE_FIELD110286BQ));
    newInstr.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKESTATIC, ASMNames.MD_STRING_VALUE_OF, false));
    newInstr.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKESPECIAL, ASMNames.MD_STRINGBUILDER_INIT, false));
    newInstr.add(new LdcInsnNode("cst-"));
    newInstr.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_STRINGBUILDER_APPEND, false));
    newInstr.add(new VarInsnNode(Opcodes.ALOAD, 4));
    newInstr.add(
            ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_ITEMSTACK_GET_UNLOC_NAME, false));
    newInstr.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_STRINGBUILDER_APPEND, false));
    newInstr.add(
            ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_STRINGBUILDER_TO_STRING, false));
    newInstr.add(ASMHelper.getFieldInsnNode(Opcodes.PUTFIELD, ASMNames.FD_HORSE_FIELD110286BQ));
    newInstr.add(new InsnNode(Opcodes.RETURN));
    newInstr.add(l19);

    method.instructions.insert(node, newInstr);
}

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

License:Creative Commons License

private static void transformEntityInit(MethodNode method) {
    InsnList needle = new InsnList();
    needle.add(new VarInsnNode(Opcodes.ALOAD, 0));
    needle.add(ASMHelper.getFieldInsnNode(Opcodes.GETFIELD, ASMNames.FD_HORSE_DATAWATCHER));
    needle.add(new IntInsnNode(Opcodes.BIPUSH, 22));
    needle.add(new InsnNode(Opcodes.ICONST_0));
    needle.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKESTATIC, ASMNames.MD_INTEGER_VALUE_OF, false));
    needle.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_DATAWATCHER_ADD_OBJECT, false));

    AbstractInsnNode pointer = ASMHelper.findLastNodeFromNeedle(method.instructions, needle);

    InsnList newInstr = new InsnList();
    newInstr.add(new LabelNode());
    newInstr.add(new VarInsnNode(Opcodes.ALOAD, 0));
    newInstr.add(ASMHelper.getFieldInsnNode(Opcodes.GETFIELD, ASMNames.FD_HORSE_DATAWATCHER));
    newInstr.add(new IntInsnNode(Opcodes.BIPUSH, 23));
    newInstr.add(new TypeInsnNode(Opcodes.NEW, ASMNames.CL_ITEM_STACK));
    newInstr.add(new InsnNode(Opcodes.DUP));
    newInstr.add(ASMHelper.getFieldInsnNode(Opcodes.GETSTATIC, ASMNames.FD_ITEMS_IRON_SHOVEL));
    newInstr.add(new InsnNode(Opcodes.ICONST_0));
    newInstr.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKESPECIAL, ASMNames.MD_ITEMSTACK_INIT, false));
    newInstr.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_DATAWATCHER_ADD_OBJECT, false));

    method.instructions.insert(pointer, newInstr);
}

From source file:de.tuberlin.uebb.jbop.optimizer.ClassNodeBuilder.java

License:Open Source License

/**
 * Creates Instruction to instantiate a new Object of type "desc".
 * /*from   ww w. ja v a 2  s.  com*/
 * @param desc
 *          the desc
 * @return the insn list
 */
private InsnList newObject(final String desc) {
    final InsnList list = new InsnList();
    final String fieldDesc = StringUtils.removeEnd(StringUtils.removeStart(desc, "L"), ";");
    final TypeInsnNode node = new TypeInsnNode(Opcodes.NEW, fieldDesc);
    list.add(node);
    list.add(new InsnNode(DUP));
    return list;
}

From source file:de.tuberlin.uebb.jbop.optimizer.controlflow.ConstantIfInlinerTest.java

License:Open Source License

/**
 * Tests that constantIfInliner is working correctly.
 * /*from   www . j a va  2 s . com*/
 * Input is
 * 
 * <pre>
 * if(a==null)
 * ...
 * </pre>
 * 
 * where a is not null
 * 
 * @throws JBOPClassException
 *           the jBOP class exception
 */
@Test
public void testConstantIfInlinerIFNULL() throws JBOPClassException {
    // INIT
    when(nonNullValue.is(Matchers.<AbstractInsnNode>any(), Matchers.<AbstractInsnNode>any(),
            Matchers.<List<AbstractInsnNode>>any(), Matchers.<List<AbstractInsnNode>>any()))
                    .thenReturn(Boolean.valueOf(true));
    final LabelNode label = new LabelNode();
    builder.addInsn(new TypeInsnNode(Opcodes.NEW, Type.getDescriptor(Object.class))).//
            addInsn(new JumpInsnNode(Opcodes.IFNULL, label)).//
            addInsn(new InsnNode(Opcodes.NOP)).//
            addInsn(label).//
            addInsn(new InsnNode(Opcodes.RETURN));

    // RUN
    assertEquals(5, method.instructions.size());
    final InsnList optimized = constantIfInliner.optimize(method.instructions, method);

    // ASSERT
    assertEquals(3, optimized.size());
    assertEquals(Opcodes.NOP, optimized.get(0).getOpcode());
}