Example usage for org.objectweb.asm MethodVisitor visitVarInsn

List of usage examples for org.objectweb.asm MethodVisitor visitVarInsn

Introduction

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

Prototype

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

Source Link

Document

Visits a local variable instruction.

Usage

From source file:Java6to2.java

License:Open Source License

private static byte[] transform(InputStream classfile) throws IOException {
    ClassReader cr = new ClassReader(classfile);
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    ClassVisitor cv = new ClassVisitor(ASM7, cw) {
        String internalName;/* www  . j  a v a 2s.c  om*/
        boolean classLookupMethodGenerated;
        Set fieldsGenerated = new HashSet();

        public void visit(int version, int access, String name, String signature, String superName,
                String[] interfaces) {
            /* Change class file version to 1.2 */
            cv.visit(V1_2, access, name, signature, superName, interfaces);
            this.internalName = name;
        }

        /**
         * Generates the synthetic "class$" method, used to lookup classes via Class.forName(). This uses the exact same code as does JDK8 javac for target 1.2.
         */
        void generateSyntheticClassLookupMethod() {
            MethodVisitor mv = cv.visitMethod(ACC_PRIVATE | ACC_STATIC | ACC_SYNTHETIC, "class$",
                    "(Ljava/lang/String;)Ljava/lang/Class;", null, null);
            {
                Label start = new Label();
                Label end = new Label();
                Label handler = new Label();
                mv.visitTryCatchBlock(start, end, handler, "java/lang/ClassNotFoundException");
                mv.visitLabel(start);
                mv.visitVarInsn(ALOAD, 0);
                mv.visitMethodInsn(INVOKESTATIC, "java/lang/Class", "forName",
                        "(Ljava/lang/String;)Ljava/lang/Class;", false);
                mv.visitLabel(end);
                mv.visitInsn(ARETURN);
                mv.visitLabel(handler);
                mv.visitVarInsn(ASTORE, 1);
                mv.visitTypeInsn(NEW, "java/lang/NoClassDefFoundError");
                mv.visitInsn(DUP);
                mv.visitMethodInsn(INVOKESPECIAL, "java/lang/NoClassDefFoundError", "<init>", "()V", false);
                mv.visitVarInsn(ALOAD, 1);
                mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/NoClassDefFoundError", "initCause",
                        "(Ljava/lang/Throwable;)Ljava/lang/Throwable;", false);
                mv.visitInsn(ATHROW);
            }
            mv.visitMaxs(2, 2);
            mv.visitEnd();
        }

        public MethodVisitor visitMethod(int access, String name, String desc, String signature,
                String[] exceptions) {
            MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
            final ClassVisitor cv = this.cv;
            return new MethodVisitor(ASM7, mv) {
                /**
                 * Intercepts class instantiations to see whether they instantiate a StringBuilder. Those instructions were generated by javac for string concatenations. But
                 * StringBuilder is not available on JRE1.2, so we just replace it with StringBuffer.
                 */
                public void visitTypeInsn(int opcode, String type) {
                    if (opcode == NEW && "java/lang/StringBuilder".equals(type)) {
                        mv.visitTypeInsn(opcode, "java/lang/StringBuffer");
                    } else {
                        mv.visitTypeInsn(opcode, type);
                    }
                }

                /**
                 * Intercepts method invocations to see whether they do something with StringBuilder. Those instructions were generated by javac for string concatenations. But
                 * StringBuilder is not available on JRE1.2, so we just replace it with StringBuffer.
                 */
                public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) {
                    if ("java/lang/StringBuilder".equals(owner)) {
                        mv.visitMethodInsn(opcode, "java/lang/StringBuffer", name,
                                desc.replace("java/lang/StringBuilder", "java/lang/StringBuffer"), itf);
                    } else {
                        mv.visitMethodInsn(opcode, owner, name, desc, itf);
                    }
                }

                /**
                 * Intercepts LDC instructions and check whether they are used to load a class. This is not supported on Java 1.2, so we convert it to the same code used by the
                 * JDK8 javac:
                 * <ul>
                 * <li>create synthetic fields holding the resolved class objects
                 * <li>create a synthetic method called "class$" which does Class.forName
                 * </ul>
                 */
                public void visitLdcInsn(Object cst) {
                    if (cst instanceof Type) {
                        Type t = (Type) cst;
                        String syntheticField = "class$"
                                + t.getInternalName().replace('/', '$').replace("[", "");
                        if (!classLookupMethodGenerated) {
                            /* Emit the synthetic "class$" method, used to lookup classes via Class.forName() */
                            generateSyntheticClassLookupMethod();
                            classLookupMethodGenerated = true;
                        }
                        if (!fieldsGenerated.contains(syntheticField)) {
                            /* Generate a synthetic field holding the resolved Class object */
                            cv.visitField(ACC_PRIVATE | ACC_STATIC | ACC_SYNTHETIC, syntheticField,
                                    "Ljava/lang/Class;", null, null);
                            fieldsGenerated.add(syntheticField);
                        }
                        mv.visitFieldInsn(GETSTATIC, internalName, syntheticField, "Ljava/lang/Class;");
                        Label nonNull = new Label();
                        mv.visitJumpInsn(IFNONNULL, nonNull);
                        mv.visitLdcInsn(t.getInternalName().replace('/', '.'));
                        mv.visitMethodInsn(INVOKESTATIC, internalName, "class$",
                                "(Ljava/lang/String;)Ljava/lang/Class;", false);
                        mv.visitInsn(DUP);
                        mv.visitFieldInsn(PUTSTATIC, internalName, syntheticField, "Ljava/lang/Class;");
                        Label cnt = new Label();
                        mv.visitJumpInsn(GOTO, cnt);
                        mv.visitLabel(nonNull);
                        mv.visitFieldInsn(GETSTATIC, internalName, syntheticField, "Ljava/lang/Class;");
                        mv.visitLabel(cnt);
                    } else {
                        mv.visitLdcInsn(cst);
                    }
                }
            };
        }
    };
    cr.accept(cv, ClassReader.SKIP_FRAMES); // <- Frames are not used in Java 1.2, so skip them
    return cw.toByteArray();
}

From source file:asm.de.qaware.campus.secpro.agent.impl.AdminServiceImplDump.java

License:Open Source License

public static byte[] dump() throws Exception {

    ClassWriter cw = new ClassWriter(0);
    FieldVisitor fv;//from ww w  .ja  v  a  2s .  com
    MethodVisitor mv;
    AnnotationVisitor av0;

    cw.visit(V1_7, ACC_PUBLIC + ACC_SUPER, "de/qaware/campus/secpro/agent/impl/LoginServiceImpl", null,
            "java/lang/Object", new String[] { "de/qaware/campus/secpro/agent/LoginService" });

    // this here is the static LOGINS map
    {
        fv = cw.visitField(ACC_FINAL + ACC_STATIC, "LOGINS", "Ljava/util/Map;",
                "Ljava/util/Map<Ljava/lang/String;Ljava/lang/String;>;", null);
        fv.visitEnd();
    }
    // this here is the default constructor
    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
        mv.visitInsn(RETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    // this here is the canLogin method
    {
        mv = cw.visitMethod(ACC_PUBLIC, "canLogin", "(Ljava/lang/String;Ljava/lang/String;)Z", null, null);
        mv.visitCode();
        mv.visitFieldInsn(GETSTATIC, "de/qaware/campus/secpro/agent/impl/LoginServiceImpl", "LOGINS",
                "Ljava/util/Map;");
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Map", "get", "(Ljava/lang/Object;)Ljava/lang/Object;",
                true);
        mv.visitTypeInsn(CHECKCAST, "java/lang/String");
        mv.visitVarInsn(ASTORE, 3);
        mv.visitVarInsn(ALOAD, 3);
        mv.visitVarInsn(ALOAD, 2);
        mv.visitMethodInsn(INVOKESTATIC, "java/util/Objects", "equals",
                "(Ljava/lang/Object;Ljava/lang/Object;)Z", false);
        mv.visitInsn(IRETURN);
        mv.visitMaxs(2, 4);
        mv.visitEnd();
    }
    // this here is the static initializer
    {
        mv = cw.visitMethod(ACC_STATIC, "<clinit>", "()V", null, null);
        mv.visitCode();
        mv.visitMethodInsn(INVOKESTATIC, "com/google/common/collect/Maps", "newHashMap",
                "()Ljava/util/HashMap;", false);
        mv.visitFieldInsn(PUTSTATIC, "de/qaware/campus/secpro/agent/impl/LoginServiceImpl", "LOGINS",
                "Ljava/util/Map;");
        mv.visitFieldInsn(GETSTATIC, "de/qaware/campus/secpro/agent/impl/LoginServiceImpl", "LOGINS",
                "Ljava/util/Map;");
        mv.visitLdcInsn("admin");
        mv.visitLdcInsn("Mellon");
        mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Map", "put",
                "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", true);
        mv.visitInsn(POP);
        mv.visitInsn(RETURN);
        mv.visitMaxs(3, 0);
        mv.visitEnd();
    }
    cw.visitEnd();

    return cw.toByteArray();
}

From source file:asm.de.qaware.campus.secpro.agent.impl.EavesdropAdminServiceImplDump.java

License:Open Source License

public static byte[] dump() throws Exception {

    ClassWriter cw = new ClassWriter(0);
    FieldVisitor fv;/* ww w  .  j  a va  2 s .c o  m*/
    MethodVisitor mv;
    AnnotationVisitor av0;

    cw.visit(V1_7, ACC_PUBLIC + ACC_SUPER, "de/qaware/campus/secpro/agent/impl/EavesdropLoginServiceImpl", null,
            "java/lang/Object", new String[] { "de/qaware/campus/secpro/agent/LoginService" });

    // this here is the static LOGINS map
    {
        fv = cw.visitField(ACC_PRIVATE + ACC_FINAL + ACC_STATIC, "LOGINS", "Ljava/util/Map;",
                "Ljava/util/Map<Ljava/lang/String;Ljava/lang/String;>;", null);
        fv.visitEnd();
    }
    // this here is the default constructor
    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
        mv.visitInsn(RETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    // this here is the modified canLogin method
    {
        mv = cw.visitMethod(ACC_PUBLIC, "canLogin", "(Ljava/lang/String;Ljava/lang/String;)Z", null, null);
        mv.visitCode();

        // this here will place the method params on the call stack and
        // invoke the static eavesdrop method
        mv.visitVarInsn(ALOAD, 1);
        mv.visitVarInsn(ALOAD, 2);
        mv.visitMethodInsn(INVOKESTATIC, "de/qaware/campus/secpro/part03/TrojanCredentialStore", "eavesdrop",
                "(Ljava/lang/String;Ljava/lang/String;)V", false);

        // continue as usual
        mv.visitFieldInsn(GETSTATIC, "de/qaware/campus/secpro/agent/impl/EavesdropLoginServiceImpl", "LOGINS",
                "Ljava/util/Map;");
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Map", "get", "(Ljava/lang/Object;)Ljava/lang/Object;",
                true);
        mv.visitTypeInsn(CHECKCAST, "java/lang/String");
        mv.visitVarInsn(ASTORE, 3);
        mv.visitVarInsn(ALOAD, 3);
        mv.visitVarInsn(ALOAD, 2);
        mv.visitMethodInsn(INVOKESTATIC, "java/util/Objects", "equals",
                "(Ljava/lang/Object;Ljava/lang/Object;)Z", false);
        mv.visitInsn(IRETURN);
        mv.visitMaxs(2, 4);
        mv.visitEnd();
    }
    // this here is the static initializer
    {
        mv = cw.visitMethod(ACC_STATIC, "<clinit>", "()V", null, null);
        mv.visitCode();
        mv.visitMethodInsn(INVOKESTATIC, "com/google/common/collect/Maps", "newHashMap",
                "()Ljava/util/HashMap;", false);
        mv.visitFieldInsn(PUTSTATIC, "de/qaware/campus/secpro/agent/impl/EavesdropLoginServiceImpl", "LOGINS",
                "Ljava/util/Map;");
        mv.visitFieldInsn(GETSTATIC, "de/qaware/campus/secpro/agent/impl/EavesdropLoginServiceImpl", "LOGINS",
                "Ljava/util/Map;");
        mv.visitLdcInsn("admin");
        mv.visitLdcInsn("Mellon");
        mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Map", "put",
                "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", true);
        mv.visitInsn(POP);
        mv.visitInsn(RETURN);
        mv.visitMaxs(3, 0);
        mv.visitEnd();
    }
    cw.visitEnd();

    return cw.toByteArray();
}

From source file:ataspectj.UnweavableTest.java

License:Open Source License

ISome getJit() {
    ClassWriter cw = new ClassWriter(true, true);
    cw.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "ataspectj/ISomeGen", null, "java/lang/Object",
            new String[] { "ataspectj/UnweavableTest$ISome" });
    AnnotationVisitor av = cw.visitAnnotation("Lataspectj/UnweavableTest$ASome;", true);
    av.visitEnd();//from www .j  a v a2s .c om

    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, new String[0]);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(0, 0);
    mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "giveOne", "()I", null, new String[0]);
    mv.visitInsn(Opcodes.ICONST_2);
    mv.visitInsn(Opcodes.IRETURN);
    mv.visitMaxs(0, 0);
    cw.visitEnd();

    try {
        ClassLoader loader = this.getClass().getClassLoader();
        Method def = ClassLoader.class.getDeclaredMethod("defineClass",
                new Class[] { String.class, byte[].class, int.class, int.class });
        def.setAccessible(true);
        Class gen = (Class) def.invoke(loader, "ataspectj.ISomeGen", cw.toByteArray(), 0,
                cw.toByteArray().length);
        return (ISome) gen.newInstance();
    } catch (Throwable t) {
        fail(t.toString());
        return null;
    }
}

From source file:ataspectj.UnweavableTest.java

License:Open Source License

Serializable getJitNoMatch() {
    ClassWriter cw = new ClassWriter(true, true);
    cw.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "ataspectj/unmatched/Gen", null, "java/lang/Object",
            new String[] { "java/io/Serializable" });

    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, new String[0]);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
    mv.visitInsn(Opcodes.RETURN);//from ww  w.  j  av a  2  s. c  om
    mv.visitMaxs(0, 0);
    cw.visitEnd();

    try {
        ClassLoader loader = this.getClass().getClassLoader();
        Method def = ClassLoader.class.getDeclaredMethod("defineClass",
                new Class[] { String.class, byte[].class, int.class, int.class });
        def.setAccessible(true);
        Class gen = (Class) def.invoke(loader, "ataspectj.unmatched.Gen", cw.toByteArray(), 0,
                cw.toByteArray().length);
        return (Serializable) gen.newInstance();
    } catch (Throwable t) {
        fail(t.toString());
        return null;
    }
}

From source file:autostack.Transformer.java

License:Open Source License

public byte[] transform(ClassLoader loader, final String className, Class<?> classBeingRedefined,
        ProtectionDomain protectionDomain, byte[] classfileBuffer) {
    try {// www. j  av  a2  s. com
        if (className == null || className.startsWith("java/") || className.startsWith("sun/")
                || className.startsWith("jdk/internal/") || className.startsWith("org/lwjgl/"))
            return null;
        for (String pack : packages)
            if (!className.startsWith(pack))
                return null;
        ClassReader cr = new ClassReader(classfileBuffer);
        final Map<String, Integer> stackMethods = new HashMap<String, Integer>();
        // Scan all methods that need auto-stack
        if (debugTransform)
            System.out.println("[autostack] scanning methods in class: " + className.replace('/', '.'));
        cr.accept(new ClassVisitor(ASM5) {
            public MethodVisitor visitMethod(final int access, final String methodName, final String methodDesc,
                    String signature, String[] exceptions) {
                if ((access & (ACC_NATIVE | ACC_ABSTRACT)) != 0) {
                    // Don't try to analyze native or abstract methods.
                    return null;
                }
                MethodVisitor mv = new MethodVisitor(ASM5) {
                    boolean mark, catches, notransform, nostackparam, forcestack;

                    public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
                        if ("Lautostack/NoTransform;".equals(desc))
                            notransform = true;
                        else if ("Lautostack/NoStackParam;".equals(desc))
                            nostackparam = true;
                        else if ("Lautostack/UseNewStack;".equals(desc))
                            forcestack = true;
                        return null;
                    }

                    public void visitMethodInsn(int opcode, String owner, String name, String desc,
                            boolean itf) {
                        if (opcode == INVOKESTATIC && !itf && (owner.startsWith("org/lwjgl/")
                                && (name.equals("mallocStack") || name.equals("callocStack"))
                                || owner.equals(MEMORYSTACK) && (name.equals("stackGet")
                                        || name.equals("stackPop") || name.equals("stackPush")
                                        || name.startsWith("stackMalloc") || name.startsWith("stackCalloc")
                                        || name.equals("stackUTF8") || name.equals("stackASCII")
                                        || name.equals("stackUTF16") || name.equals("stackFloats")
                                        || name.equals("stackInts") || name.equals("stackBytes")
                                        || name.equals("stackShorts") || name.equals("stackPointers")
                                        || name.equals("stackLongs")))) {
                            mark = true;
                        }
                    }

                    public void visitTryCatchBlock(Label start, Label end, Label handler, String type) {
                        catches = true;
                    }

                    public void visitEnd() {
                        int flag = (access & ACC_PRIVATE) != 0 ? 8 : 0;
                        flag |= nostackparam ? 16 : 0;
                        if (mark || notransform || forcestack || nostackparam) {
                            if (notransform) {
                                flag |= 2;
                                if (debugTransform)
                                    System.out.println("[autostack]   will not transform method: "
                                            + className.replace('/', '.') + "." + methodName);
                            } else {
                                if (checkStack)
                                    flag |= 4;
                                flag |= catches ? 1 : 0;
                                if (debugTransform)
                                    System.out.println("[autostack]   will transform method: "
                                            + className.replace('/', '.') + "." + methodName);
                            }
                            stackMethods.put(methodName + methodDesc, Integer.valueOf(flag));
                        }
                    }
                };
                return mv;
            }
        }, ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
        if (stackMethods.isEmpty())
            return null;

        // Now, transform all such methods
        if (debugTransform)
            System.out.println("[autostack] transforming methods in class: " + className.replace('/', '.'));
        ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_MAXS);
        cr.accept(new ClassVisitor(ASM5, cw) {
            boolean classDefaultNewStack = defaultNewStack;
            boolean classNoTransform;

            @Override
            public void visit(int version, int access, String name, String signature, String superName,
                    String[] interfaces) {
                cv.visit(version, access, name, signature, superName, interfaces);
                if (!checkStack) {
                    return;
                }
                /* Generate simple synthetic "compare stack pointers and throw if not equal" method */
                MethodVisitor mv = cv.visitMethod(ACC_PRIVATE | ACC_STATIC | ACC_SYNTHETIC, "$checkStack$",
                        "(II)V", null, new String[] { "java/lang/AssertionError" });
                {
                    mv.visitCode();
                    mv.visitVarInsn(ILOAD, 0);
                    mv.visitVarInsn(ILOAD, 1);
                    Label l0 = new Label();
                    mv.visitJumpInsn(IF_ICMPEQ, l0);
                    mv.visitTypeInsn(NEW, "java/lang/IllegalStateException");
                    mv.visitInsn(DUP);
                    mv.visitTypeInsn(NEW, "java/lang/StringBuilder");
                    mv.visitInsn(DUP);
                    mv.visitLdcInsn("Stack pointers differ: ");
                    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "<init>",
                            "(Ljava/lang/String;)V", false);
                    mv.visitVarInsn(ILOAD, 0);
                    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
                            "(I)Ljava/lang/StringBuilder;", false);
                    mv.visitLdcInsn(" != ");
                    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
                            "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
                    mv.visitVarInsn(ILOAD, 1);
                    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
                            "(I)Ljava/lang/StringBuilder;", false);
                    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "toString",
                            "()Ljava/lang/String;", false);
                    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/IllegalStateException", "<init>",
                            "(Ljava/lang/String;)V", false);
                    mv.visitInsn(ATHROW);
                    mv.visitLabel(l0);
                    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
                    mv.visitInsn(RETURN);
                    mv.visitMaxs(5, 2);
                    mv.visitEnd();
                }

                mv = cv.visitMethod(ACC_PRIVATE | ACC_STATIC | ACC_SYNTHETIC, "$checkStackWithThrowable$",
                        "(Ljava/lang/Throwable;II)Ljava/lang/Throwable;", null, null);
                {
                    mv.visitCode();
                    mv.visitVarInsn(ILOAD, 1);
                    mv.visitVarInsn(ILOAD, 2);
                    Label l0 = new Label();
                    mv.visitJumpInsn(IF_ICMPEQ, l0);
                    mv.visitTypeInsn(NEW, "java/lang/IllegalStateException");
                    mv.visitInsn(DUP);
                    mv.visitTypeInsn(NEW, "java/lang/StringBuilder");
                    mv.visitInsn(DUP);
                    mv.visitLdcInsn("Stack pointers differ: ");
                    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "<init>",
                            "(Ljava/lang/String;)V", false);
                    mv.visitVarInsn(ILOAD, 1);
                    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
                            "(I)Ljava/lang/StringBuilder;", false);
                    mv.visitLdcInsn(" != ");
                    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
                            "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
                    mv.visitVarInsn(ILOAD, 2);
                    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
                            "(I)Ljava/lang/StringBuilder;", false);
                    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "toString",
                            "()Ljava/lang/String;", false);
                    mv.visitVarInsn(ALOAD, 0);
                    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/IllegalStateException", "<init>",
                            "(Ljava/lang/String;Ljava/lang/Throwable;)V", false);
                    mv.visitInsn(ARETURN);
                    mv.visitLabel(l0);
                    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
                    mv.visitVarInsn(ALOAD, 0);
                    mv.visitInsn(ARETURN);
                    mv.visitMaxs(5, 3);
                    mv.visitEnd();
                }
            }

            public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
                if ("Lautostack/UseCallerStack;".equals(desc)) {
                    if (debugTransform)
                        System.out.println(
                                "[autostack]   class declares to use caller stack for all methods, unless overridden by method");
                    classDefaultNewStack = false;
                    return null;
                } else if ("Lautostack/UseNewStack;".equals(desc)) {
                    if (debugTransform)
                        System.out.println(
                                "[autostack]   class declares to use new stack for all methods, unless overridden by method");
                    classDefaultNewStack = true;
                    return null;
                } else if ("Lautostack/NoTransform;".equals(desc)) {
                    if (debugTransform)
                        System.out.println("[autostack]   class declares to not transform any methods");
                    classNoTransform = true;
                    return null;
                }
                return cv.visitAnnotation(desc, visible);
            }

            public MethodVisitor visitMethod(final int access, final String name, final String desc,
                    String signature, String[] exceptions) {
                Integer info = stackMethods.get(name + desc);
                if (info == null)
                    return super.visitMethod(access, name, desc, signature, exceptions);
                boolean catches = (info.intValue() & 1) == 1;
                final boolean notransform = classNoTransform || (info.intValue() & 2) == 2;
                if (debugTransform && !notransform)
                    System.out.println(
                            "[autostack]   transform method: " + className.replace('/', '.') + "." + name);
                final boolean memoryStackParam = stackAsParameter && (access & ACC_PRIVATE) != 0
                        && (info.intValue() & 16) == 0;
                MethodVisitor mv;
                final Type[] paramTypes = Type.getArgumentTypes(desc);
                final boolean isStatic = (access & ACC_STATIC) != 0;
                final boolean isConstructor = "<init>".equals(name);
                if (memoryStackParam) {
                    if (debugTransform)
                        System.out.println(
                                "[autostack]     changing signature of method to add additional MemoryStack parameter");

                    // Add additional MemoryStack parameter to the method signature index of the local stays the same
                    int paramEndIndex = desc.indexOf(')');
                    String beforeDesc = desc.substring(0, paramEndIndex);
                    String afterDesc = desc.substring(paramEndIndex);
                    mv = super.visitMethod(access | ACC_SYNTHETIC, name,
                            beforeDesc + "L" + MEMORYSTACK + ";" + afterDesc, signature, exceptions);

                    // Re-introduce the original method which just delegates
                    if (debugTransform)
                        System.out.println("[autostack]     adding delegate method with original signature");
                    MethodVisitor omv = super.visitMethod(access, name, desc, signature, exceptions);
                    omv.visitCode();
                    int param = 0;
                    if (!isStatic) {
                        omv.visitVarInsn(ALOAD, 0);
                        param++;
                    }
                    for (int i = 0; i < paramTypes.length; i++) {
                        omv.visitVarInsn(paramTypes[i].getOpcode(ILOAD), param);
                        param += paramTypes[i].getSize();
                    }
                    omv.visitMethodInsn(INVOKESTATIC, MEMORYSTACK, "stackGet", "()L" + MEMORYSTACK + ";",
                            false);
                    boolean isPrivate = (access & ACC_PRIVATE) != 0;
                    int opcode = isStatic ? INVOKESTATIC : isPrivate ? INVOKESPECIAL : INVOKEVIRTUAL;
                    omv.visitMethodInsn(opcode, className, name,
                            beforeDesc + "L" + MEMORYSTACK + ";" + afterDesc, false);
                    Type retType = Type.getReturnType(desc);
                    omv.visitInsn(retType.getOpcode(IRETURN));
                    omv.visitMaxs(-1, -1);
                    omv.visitEnd();
                } else {
                    mv = super.visitMethod(access, name, desc, signature, exceptions);
                }
                if (catches)
                    mv = new TryCatchBlockSorter(mv, access, name, desc, signature, exceptions);
                mv = new MethodVisitor(ASM5, mv) {
                    Label tryLabel = new Label();
                    Label finallyLabel = new Label();
                    int lastLine = 0;
                    boolean newStack = classDefaultNewStack;
                    int stackVarIndex;
                    int stackPointerVarIndex;
                    int firstAdditionalLocal;
                    int additionalLocals;
                    Object[] replacedLocals;

                    public void visitInsn(int opcode) {
                        if (notransform) {
                            mv.visitInsn(opcode);
                            return;
                        }
                        if (opcode >= IRETURN && opcode <= RETURN && (newStack || checkStack)) {
                            if (debugRuntime && newStack && !checkStack) {
                                mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out",
                                        "Ljava/io/PrintStream;");
                                mv.visitLdcInsn("[autostack] restore stack pointer because of return at "
                                        + className.replace('/', '.') + "." + name + ":" + lastLine);
                                mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println",
                                        "(Ljava/lang/String;)V", false);
                            }
                            if (newStack && !checkStack) {
                                mv.visitVarInsn(ALOAD, stackVarIndex);
                                mv.visitVarInsn(ILOAD, stackPointerVarIndex);
                                mv.visitMethodInsn(INVOKEVIRTUAL, MEMORYSTACK, "setPointer", "(I)V", false);
                            } else if (checkStack) {
                                mv.visitVarInsn(ILOAD, stackPointerVarIndex);
                                mv.visitVarInsn(ALOAD, stackVarIndex);
                                mv.visitMethodInsn(INVOKEVIRTUAL, MEMORYSTACK, "getPointer", "()I", false);
                                mv.visitMethodInsn(INVOKESTATIC, className, "$checkStack$", "(II)V", false);
                            }
                        }
                        mv.visitInsn(opcode);
                    }

                    public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
                        if ("Lautostack/UseCallerStack;".equals(desc)) {
                            if (!notransform) {
                                if (debugTransform)
                                    System.out.println("[autostack]     method declares to use caller stack");
                                newStack = false;
                            }
                            return null;
                        } else if ("Lautostack/UseNewStack;".equals(desc)) {
                            if (!notransform) {
                                if (debugTransform)
                                    System.out.println("[autostack]     method declares to use new stack");
                                newStack = true;
                            }
                            return null;
                        } else if ("Lautostack/NoTransform;".equals(desc)) {
                            return null;
                        } else if ("Lautostack/NoStackParam;".equals(desc)) {
                            return null;
                        }
                        return mv.visitAnnotation(desc, visible);
                    }

                    public void visitVarInsn(int opcode, int var) {
                        if (notransform) {
                            mv.visitVarInsn(opcode, var);
                            return;
                        }
                        if (var >= firstAdditionalLocal)
                            var += additionalLocals;
                        mv.visitVarInsn(opcode, var);
                    }

                    public void visitIincInsn(int var, int increment) {
                        if (notransform) {
                            mv.visitIincInsn(var, increment);
                            return;
                        }
                        if (var >= firstAdditionalLocal)
                            var += additionalLocals;
                        mv.visitIincInsn(var, increment);
                    }

                    public void visitFrame(int type, int nLocal, Object[] local, int nStack, Object[] stack) {
                        if (notransform) {
                            mv.visitFrame(type, nLocal, local, nStack, stack);
                            return;
                        }
                        if (type == F_FULL) {
                            int noThis = isStatic ? 0 : 1;
                            Object[] locals = new Object[local.length + additionalLocals];
                            if (!isStatic)
                                locals[0] = local[0];
                            int replacementLength = replacedLocals.length;
                            System.arraycopy(replacedLocals, noThis, locals, noThis,
                                    replacementLength - noThis);
                            int len = locals.length - replacementLength;
                            System.arraycopy(local, replacementLength - additionalLocals, locals,
                                    replacementLength, len);
                            mv.visitFrame(type, nLocal + additionalLocals, locals, nStack, stack);
                        } else
                            mv.visitFrame(type, nLocal, local, nStack, stack);
                    }

                    public void visitLocalVariable(String name, String desc, String signature, Label start,
                            Label end, int index) {
                        if (notransform) {
                            mv.visitLocalVariable(className, desc, signature, start, end, index);
                            return;
                        }
                        if (index >= firstAdditionalLocal)
                            index += additionalLocals;
                        mv.visitLocalVariable(name, desc, signature, start, end, index);
                    }

                    private boolean doesNotTakeStackItself(String desc) {
                        return desc.lastIndexOf("L" + MEMORYSTACK + ";)") == -1;
                    }

                    public void visitMethodInsn(int opcode, String owner, String name, String desc,
                            boolean itf) {
                        String completeName = name + desc;
                        Integer info = stackMethods.get(completeName);
                        if (opcode != INVOKESTATIC || notransform) {
                            mv.visitMethodInsn(opcode, owner, name, desc, itf);
                            return;
                        }
                        if (stackAsParameter && info != null && (info.intValue() & 8) != 0
                                && (info.intValue() & 16) == 0) {
                            /* Rewrite invocation to have additional MemoryStack parameter */
                            if (debugTransform)
                                System.out.println(
                                        "[autostack]     rewrite invocation of " + owner.replace('/', '.') + "."
                                                + name + " at line " + lastLine + " --> "
                                                + owner.replace('/', '.') + "." + name + "(..., MemoryStack)");
                            int paramEndIndex = desc.indexOf(')');
                            String beforeDesc = desc.substring(0, paramEndIndex);
                            String afterDesc = desc.substring(paramEndIndex);
                            mv.visitVarInsn(ALOAD, stackVarIndex);
                            mv.visitMethodInsn(opcode, owner, name,
                                    beforeDesc + "L" + MEMORYSTACK + ";" + afterDesc, itf);
                            return;
                        }
                        if (owner.startsWith("org/lwjgl/")
                                && (name.equals("mallocStack") || name.equals("callocStack"))
                                && doesNotTakeStackItself(desc)) {
                            if (debugTransform)
                                System.out.println(
                                        "[autostack]     rewrite invocation of " + owner.replace('/', '.') + "."
                                                + name + " at line " + lastLine + " --> aload " + stackVarIndex
                                                + "; invokestatic " + owner.replace('/', '.') + "." + name);
                            mv.visitVarInsn(ALOAD, stackVarIndex);
                            int paramEndIndex = desc.indexOf(')');
                            String beforeDesc = desc.substring(0, paramEndIndex);
                            String afterDesc = desc.substring(paramEndIndex);
                            mv.visitMethodInsn(opcode, owner, name,
                                    beforeDesc + "L" + MEMORYSTACK + ";" + afterDesc, false);
                        } else if (owner.equals(MEMORYSTACK) && name.equals("stackGet")) {
                            if (debugTransform)
                                System.out.println("[autostack]     rewrite invocation of "
                                        + owner.replace('/', '.') + "." + name + " at line " + lastLine
                                        + " --> aload " + stackVarIndex);
                            mv.visitVarInsn(ALOAD, stackVarIndex);
                        } else if (owner.equals(MEMORYSTACK)
                                && (name.equals("stackPush") || name.equals("stackPop"))) {
                            String newName = "p" + name.substring(6);
                            if (debugTransform)
                                System.out.println("[autostack]     rewrite invocation of "
                                        + owner.replace('/', '.') + "." + name + " at line " + lastLine
                                        + " --> aload " + stackVarIndex + "; invokevirtual "
                                        + MEMORYSTACK.replace('/', '.') + "." + newName);
                            mv.visitVarInsn(ALOAD, stackVarIndex);
                            mv.visitMethodInsn(INVOKEVIRTUAL, MEMORYSTACK, newName, desc, itf);
                        } else if (owner.equals(MEMORYSTACK)
                                && (name.startsWith("stackMalloc") || name.startsWith("stackCalloc"))) {
                            String newName = name.substring(5, 6).toLowerCase() + name.substring(6);
                            if (debugTransform)
                                System.out.println("[autostack]     rewrite invocation of "
                                        + owner.replace('/', '.') + "." + name + " at line " + lastLine
                                        + " --> aload " + stackVarIndex + "; invokevirtual "
                                        + MEMORYSTACK.replace('/', '.') + "." + newName);
                            mv.visitVarInsn(ALOAD, stackVarIndex);
                            mv.visitInsn(SWAP);
                            mv.visitMethodInsn(INVOKEVIRTUAL, MEMORYSTACK, newName, desc, itf);
                        } else if (owner.equals(MEMORYSTACK) && (name.equals("stackASCII")
                                || name.equals("stackUTF8") || name.equals("stackUTF16"))) {
                            String newName = name.substring(5);
                            boolean withBoolean = desc.startsWith("(Ljava/lang/CharSequence;Z");
                            if (debugTransform)
                                System.out.println("[autostack]     rewrite invocation of "
                                        + owner.replace('/', '.') + "." + name + " at line " + lastLine
                                        + " --> aload " + stackVarIndex + "; invokevirtual "
                                        + MEMORYSTACK.replace('/', '.') + "." + newName);
                            mv.visitVarInsn(ALOAD, stackVarIndex);
                            if (withBoolean) {
                                mv.visitInsn(DUP_X2);
                                mv.visitInsn(POP);
                            } else {
                                mv.visitInsn(SWAP);
                            }
                            mv.visitMethodInsn(INVOKEVIRTUAL, MEMORYSTACK, newName, desc, itf);
                        } else if (owner.equals(MEMORYSTACK)
                                && (name.equals("stackFloats") || name.equals("stackInts")
                                        || name.equals("stackBytes") || name.equals("stackShorts")
                                        || name.equals("stackPointers")
                                                && (desc.startsWith("([Lorg/lwjgl/system/Pointer;")
                                                        || desc.startsWith("(Lorg/lwjgl/system/Pointer;")))) {
                            String newName = name.substring(5, 6).toLowerCase() + name.substring(6);
                            Type[] argTypes = Type.getArgumentTypes(desc);
                            if (argTypes.length == 1 && argTypes[0].getSort() == Type.ARRAY) {
                                if (debugTransform)
                                    System.out.println("[autostack]     rewrite invocation of "
                                            + owner.replace('/', '.') + "." + name + " at line " + lastLine
                                            + " --> aload " + stackVarIndex + "; invokevirtual "
                                            + MEMORYSTACK.replace('/', '.') + "." + newName);
                                mv.visitVarInsn(ALOAD, stackVarIndex);
                                mv.visitInsn(SWAP);
                                mv.visitMethodInsn(INVOKEVIRTUAL, MEMORYSTACK, newName, desc, itf);
                            } else if (argTypes.length == 1) {
                                if (debugTransform)
                                    System.out.println("[autostack]     rewrite invocation of "
                                            + owner.replace('/', '.') + "." + name + " at line " + lastLine
                                            + " --> aload " + stackVarIndex + "; invokevirtual "
                                            + MEMORYSTACK.replace('/', '.') + "." + newName);
                                mv.visitVarInsn(ALOAD, stackVarIndex);
                                mv.visitInsn(SWAP);
                                mv.visitMethodInsn(INVOKEVIRTUAL, MEMORYSTACK, newName, desc, itf);
                            } else if (argTypes.length == 2) {
                                if (debugTransform)
                                    System.out.println("[autostack]     rewrite invocation of "
                                            + owner.replace('/', '.') + "." + name + " at line " + lastLine
                                            + " --> aload " + stackVarIndex + "; invokevirtual "
                                            + MEMORYSTACK.replace('/', '.') + "." + newName);
                                mv.visitVarInsn(ALOAD, stackVarIndex);
                                mv.visitInsn(DUP_X2);
                                mv.visitInsn(POP);
                                mv.visitMethodInsn(INVOKEVIRTUAL, MEMORYSTACK, newName, desc, itf);
                            } else if (argTypes.length == 3) {
                                if (debugTransform)
                                    System.out.println("[autostack]     rewrite invocation of "
                                            + owner.replace('/', '.') + "." + name + " at line " + lastLine
                                            + " --> aload " + stackVarIndex + "; invokevirtual "
                                            + MEMORYSTACK.replace('/', '.') + "." + newName);
                                mv.visitVarInsn(ALOAD, stackVarIndex);
                                mv.visitInsn(DUP2_X2);
                                mv.visitInsn(POP);
                                mv.visitMethodInsn(INVOKEVIRTUAL, MEMORYSTACK, newName, desc, itf);
                                mv.visitInsn(SWAP);
                                mv.visitInsn(POP);
                            } else {
                                if (debugTransform)
                                    System.out.println("[autostack]     failed to rewrite invocation of "
                                            + owner.replace('/', '.') + "." + name + " at line " + lastLine
                                            + ". Not yet implemented.");
                                /* Give up. Not possible without an additional local */
                                mv.visitMethodInsn(INVOKESTATIC, MEMORYSTACK, name, desc, itf);
                            }
                        } else if (owner.equals(MEMORYSTACK) && name.equals("stackLongs")) {
                            String newName = name.substring(5, 6).toLowerCase() + name.substring(6);
                            Type[] argTypes = Type.getArgumentTypes(desc);
                            if (argTypes.length == 1 && argTypes[0].getSort() == Type.ARRAY) {
                                if (debugTransform)
                                    System.out.println("[autostack]     rewrite invocation of "
                                            + owner.replace('/', '.') + "." + name + " at line " + lastLine
                                            + " --> aload " + stackVarIndex + "; invokevirtual "
                                            + MEMORYSTACK.replace('/', '.') + "." + newName);
                                mv.visitVarInsn(ALOAD, stackVarIndex);
                                mv.visitInsn(SWAP);
                                mv.visitMethodInsn(INVOKEVIRTUAL, MEMORYSTACK, newName, desc, itf);
                            } else if (argTypes.length == 1) {
                                if (debugTransform)
                                    System.out.println("[autostack]     rewrite invocation of "
                                            + owner.replace('/', '.') + "." + name + " at line " + lastLine
                                            + " --> aload " + stackVarIndex + "; invokevirtual "
                                            + MEMORYSTACK.replace('/', '.') + "." + newName);
                                mv.visitVarInsn(ALOAD, stackVarIndex);
                                mv.visitInsn(DUP_X2);
                                mv.visitInsn(POP);
                                mv.visitMethodInsn(INVOKEVIRTUAL, MEMORYSTACK, newName, desc, itf);
                            } else {
                                if (debugTransform)
                                    System.out.println("[autostack]     failed to rewrite invocation of "
                                            + owner.replace('/', '.') + "." + name + " at line " + lastLine
                                            + ". Not yet implemented.");
                                /* Give up. Not possible without an additional local */
                                mv.visitMethodInsn(INVOKESTATIC, MEMORYSTACK, name, desc, itf);
                            }
                        } else {
                            mv.visitMethodInsn(opcode, owner, name, desc, itf);
                        }
                    }

                    public void visitLineNumber(int line, Label start) {
                        mv.visitLineNumber(line, start);
                        lastLine = line;
                    }

                    public void visitCode() {
                        if (notransform) {
                            mv.visitCode();
                            return;
                        }
                        additionalLocals = newStack || checkStack ? 2 : 1;
                        replacedLocals = new Object[paramTypes.length + additionalLocals + (isStatic ? 0 : 1)];
                        if (!newStack && !checkStack) {
                            replacedLocals[replacedLocals.length - 1] = MEMORYSTACK;
                        } else {
                            replacedLocals[replacedLocals.length - 2] = MEMORYSTACK;
                            replacedLocals[replacedLocals.length - 1] = INTEGER;
                        }
                        if (!isStatic)
                            replacedLocals[0] = isConstructor ? TOP : className;
                        int var = isStatic ? 0 : 1;
                        for (int t = 0, i = var; t < paramTypes.length; t++, i++) {
                            Type type = paramTypes[t];
                            var += type.getSize();
                            switch (type.getSort()) {
                            case Type.INT:
                            case Type.BYTE:
                            case Type.BOOLEAN:
                            case Type.SHORT:
                            case Type.CHAR:
                                replacedLocals[i] = INTEGER;
                                break;
                            case Type.LONG:
                                replacedLocals[i] = LONG;
                                break;
                            case Type.FLOAT:
                                replacedLocals[i] = FLOAT;
                                break;
                            case Type.DOUBLE:
                                replacedLocals[i] = DOUBLE;
                                break;
                            case Type.OBJECT:
                            case Type.ARRAY:
                                replacedLocals[i] = type.getInternalName();
                                break;
                            default:
                                throw new AssertionError("Unhandled parameter type: " + type);
                            }
                        }
                        firstAdditionalLocal = var;
                        stackVarIndex = var;
                        stackPointerVarIndex = var + 1;
                        mv.visitCode();
                        if (newStack && !checkStack || checkStack) {
                            if (!memoryStackParam) {
                                mv.visitMethodInsn(INVOKESTATIC, MEMORYSTACK, "stackGet",
                                        "()L" + MEMORYSTACK + ";", false);
                                mv.visitInsn(DUP);
                                mv.visitVarInsn(ASTORE, stackVarIndex);
                            } else {
                                mv.visitVarInsn(ALOAD, stackVarIndex);
                            }
                            mv.visitMethodInsn(INVOKEVIRTUAL, MEMORYSTACK, "getPointer", "()I", false);
                            mv.visitVarInsn(ISTORE, stackPointerVarIndex);
                            if (debugRuntime && newStack && !checkStack) {
                                mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out",
                                        "Ljava/io/PrintStream;");
                                mv.visitLdcInsn("[autostack] save stack pointer [");
                                mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "print",
                                        "(Ljava/lang/String;)V", false);
                                mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out",
                                        "Ljava/io/PrintStream;");
                                mv.visitVarInsn(ILOAD, stackPointerVarIndex);
                                mv.visitMethodInsn(INVOKESTATIC, "java/lang/Integer", "toString",
                                        "(I)Ljava/lang/String;", false);
                                mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "print",
                                        "(Ljava/lang/String;)V", false);
                                mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out",
                                        "Ljava/io/PrintStream;");
                                mv.visitLdcInsn("] at begin of " + className.replace('/', '.') + "." + name);
                                mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println",
                                        "(Ljava/lang/String;)V", false);
                            }
                            mv.visitLabel(tryLabel);
                            if (!memoryStackParam)
                                mv.visitFrame(F_APPEND, 2, new Object[] { MEMORYSTACK, INTEGER }, 0, null);
                            else
                                mv.visitFrame(F_APPEND, 1, new Object[] { INTEGER }, 0, null);
                        } else if (!newStack && !checkStack) {
                            if (!memoryStackParam) {
                                mv.visitMethodInsn(INVOKESTATIC, MEMORYSTACK, "stackGet",
                                        "()L" + MEMORYSTACK + ";", false);
                                mv.visitVarInsn(ASTORE, stackVarIndex);
                            }
                            if (debugRuntime) {
                                mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out",
                                        "Ljava/io/PrintStream;");
                                mv.visitLdcInsn("[autostack] current stack pointer is [");
                                mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "print",
                                        "(Ljava/lang/String;)V", false);
                                mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out",
                                        "Ljava/io/PrintStream;");
                                mv.visitVarInsn(ALOAD, stackVarIndex);
                                mv.visitMethodInsn(INVOKEVIRTUAL, MEMORYSTACK, "getPointer", "()I", false);
                                mv.visitMethodInsn(INVOKESTATIC, "java/lang/Integer", "toString",
                                        "(I)Ljava/lang/String;", false);
                                mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "print",
                                        "(Ljava/lang/String;)V", false);
                                mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out",
                                        "Ljava/io/PrintStream;");
                                mv.visitLdcInsn("] at begin of " + className.replace('/', '.') + "." + name);
                                mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println",
                                        "(Ljava/lang/String;)V", false);
                            }
                            mv.visitLabel(tryLabel);
                            if (!memoryStackParam)
                                mv.visitFrame(F_APPEND, 1, new Object[] { MEMORYSTACK }, 0, null);
                        }
                    }

                    public void visitMaxs(int maxStack, int maxLocals) {
                        if (notransform) {
                            mv.visitMaxs(maxStack, maxLocals);
                            return;
                        }
                        if (newStack && !checkStack || checkStack) {
                            mv.visitLabel(finallyLabel);
                            mv.visitFrame(F_FULL, replacedLocals.length, replacedLocals, 1,
                                    new Object[] { "java/lang/Throwable" });
                            mv.visitTryCatchBlock(tryLabel, finallyLabel, finallyLabel, null);
                            if (debugRuntime && newStack && !checkStack) {
                                mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out",
                                        "Ljava/io/PrintStream;");
                                mv.visitLdcInsn("[autostack] restore stack pointer because of throw [");
                                mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "print",
                                        "(Ljava/lang/String;)V", false);
                                mv.visitInsn(DUP);
                                mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out",
                                        "Ljava/io/PrintStream;");
                                mv.visitInsn(SWAP);
                                mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "toString",
                                        "()Ljava/lang/String;", false);
                                mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "print",
                                        "(Ljava/lang/String;)V", false);
                                mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out",
                                        "Ljava/io/PrintStream;");
                                mv.visitLdcInsn(
                                        "] at " + className.replace('/', '.') + "." + name + ":" + lastLine);
                                mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println",
                                        "(Ljava/lang/String;)V", false);
                            }
                            if (newStack && !checkStack) {
                                mv.visitVarInsn(ALOAD, stackVarIndex);
                                mv.visitVarInsn(ILOAD, stackPointerVarIndex);
                                mv.visitMethodInsn(INVOKEVIRTUAL, MEMORYSTACK, "setPointer", "(I)V", false);
                            }
                            if (checkStack) {
                                mv.visitVarInsn(ILOAD, stackPointerVarIndex);
                                mv.visitVarInsn(ALOAD, stackVarIndex);
                                mv.visitMethodInsn(INVOKEVIRTUAL, MEMORYSTACK, "getPointer", "()I", false);
                                mv.visitMethodInsn(INVOKESTATIC, className, "$checkStackWithThrowable$",
                                        "(Ljava/lang/Throwable;II)Ljava/lang/Throwable;", false);
                            }
                            mv.visitInsn(ATHROW);
                        }
                        mv.visitMaxs(-1, maxLocals + additionalLocals);
                    }
                };
                return mv;
            }
        }, 0);
        byte[] arr = cw.toByteArray();
        if (trace) {
            cr = new ClassReader(arr);
            cr.accept(new TraceClassVisitor(new PrintWriter(System.out)), 0);
        }
        return arr;
    } catch (Throwable t) {
        t.printStackTrace();
        throw new RuntimeException(t);
    }
}

From source file:blockphysics.asm.BPTransformer.java

License:Open Source License

private byte[] transformExtendedBlockStorage(byte[] bytes) {
    /*try//ww  w .j a  v a  2s. co m
     {
         FileOutputStream fos = new FileOutputStream("d:/ExtendedBlockStorage.orig.class");
         fos.write(bytes);
       fos.close();
     } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
     }*/

    System.out.print("[BlockPhysics] Patching ExtendedBlockStorage.class ....");

    boolean ok = false;
    ClassNode classNode = new ClassNode();
    ClassReader classReader = new ClassReader(bytes);
    classReader.accept(classNode, 0);

    MethodNode m;
    Iterator<MethodNode> methods = classNode.methods.iterator();
    while (methods.hasNext()) {
        m = methods.next();

        if (m.name.equals("<init>") && m.desc.equals("(IZ)V")) {
            for (int index = m.instructions.size() - 1; index >= 0; index--) {
                if (m.instructions.get(index).getOpcode() == RETURN) {
                    InsnList toInject = new InsnList();

                    //toInject.add(new FrameNode(Opcodes.F_FULL, 3, new Object[] {"abx", Opcodes.INTEGER, Opcodes.INTEGER}, 0, new Object[] {}));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new IntInsnNode(SIPUSH, 4096));
                    toInject.add(new IntInsnNode(NEWARRAY, T_BYTE));
                    toInject.add(new FieldInsnNode(PUTFIELD, "adr", "blockBPdataArray", "[B"));

                    m.instructions.insertBefore(m.instructions.get(index), toInject);

                    ok = true;
                    break;
                }
            }
        }
    }

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

    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "getBlockBPdata", "(III)I", null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, "adr", "blockBPdataArray", "[B");
    mv.visitVarInsn(ILOAD, 2);
    mv.visitIntInsn(SIPUSH, 256);
    mv.visitInsn(IMUL);
    mv.visitVarInsn(ILOAD, 1);
    mv.visitIntInsn(BIPUSH, 16);
    mv.visitInsn(IMUL);
    mv.visitInsn(IADD);
    mv.visitVarInsn(ILOAD, 3);
    mv.visitInsn(IADD);
    mv.visitInsn(BALOAD);
    mv.visitInsn(IRETURN);
    mv.visitMaxs(4, 4);
    mv.visitEnd();

    mv = cw.visitMethod(ACC_PUBLIC, "setBlockBPdata", "(IIII)V", null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, "adr", "blockBPdataArray", "[B");
    mv.visitVarInsn(ILOAD, 2);
    mv.visitIntInsn(SIPUSH, 256);
    mv.visitInsn(IMUL);
    mv.visitVarInsn(ILOAD, 1);
    mv.visitIntInsn(BIPUSH, 16);
    mv.visitInsn(IMUL);
    mv.visitInsn(IADD);
    mv.visitVarInsn(ILOAD, 3);
    mv.visitInsn(IADD);
    mv.visitVarInsn(ILOAD, 4);
    mv.visitInsn(I2B);
    mv.visitInsn(BASTORE);
    mv.visitInsn(RETURN);
    mv.visitMaxs(4, 5);
    mv.visitEnd();

    mv = cw.visitMethod(ACC_PUBLIC, "getBPdataArray", "()[B", null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, "adr", "blockBPdataArray", "[B");
    mv.visitInsn(ARETURN);
    mv.visitMaxs(1, 1);
    mv.visitEnd();

    mv = cw.visitMethod(ACC_PUBLIC, "setBPdataArray", "([B)V", null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitFieldInsn(PUTFIELD, "adr", "blockBPdataArray", "[B");
    mv.visitInsn(RETURN);
    mv.visitMaxs(2, 2);
    mv.visitEnd();

    FieldVisitor fv;

    fv = cw.visitField(ACC_PRIVATE, "blockBPdataArray", "[B", null, null);
    fv.visitEnd();

    cw.visitEnd();

    if (ok)
        System.out.println("OK");
    else
        System.out.println("Failed." + ok);

    /*try
    {
       FileOutputStream fos = new FileOutputStream("d:/ExtendedBlockStorage.mod.class");
       fos.write(cw.toByteArray());
     fos.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }*/

    return cw.toByteArray();
}

From source file:blockphysics.asm.BPTransformer.java

License:Open Source License

private byte[] transformChunk(byte[] bytes) {
    /*try//from   w  w  w  . jav a2s  . c  o m
     {
         FileOutputStream fos = new FileOutputStream("d:/Chunk.orig.class");
         fos.write(bytes);
       fos.close();
     } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
     }*/

    System.out.print("[BlockPhysics] Patching Chunk.class ...................");

    ClassNode classNode = new ClassNode();
    ClassReader classReader = new ClassReader(bytes);
    classReader.accept(classNode, 0);

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

    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "getBlockBPdata", "(III)I", null, null);
    mv.visitCode();
    mv.visitVarInsn(ILOAD, 2);
    mv.visitInsn(ICONST_4);
    mv.visitInsn(ISHR);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, "adq", "r", "[Ladr;");
    mv.visitInsn(ARRAYLENGTH);
    Label l0 = new Label();
    mv.visitJumpInsn(IF_ICMPLT, l0);
    mv.visitInsn(ICONST_0);
    mv.visitInsn(IRETURN);
    mv.visitLabel(l0);
    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, "adq", "r", "[Ladr;");
    mv.visitVarInsn(ILOAD, 2);
    mv.visitInsn(ICONST_4);
    mv.visitInsn(ISHR);
    mv.visitInsn(AALOAD);
    mv.visitVarInsn(ASTORE, 4);
    mv.visitVarInsn(ALOAD, 4);
    Label l1 = new Label();
    mv.visitJumpInsn(IFNULL, l1);
    mv.visitVarInsn(ALOAD, 4);
    mv.visitVarInsn(ILOAD, 1);
    mv.visitVarInsn(ILOAD, 2);
    mv.visitIntInsn(BIPUSH, 15);
    mv.visitInsn(IAND);
    mv.visitVarInsn(ILOAD, 3);
    mv.visitMethodInsn(INVOKEVIRTUAL, "adr", "getBlockBPdata", "(III)I");
    Label l2 = new Label();
    mv.visitJumpInsn(GOTO, l2);
    mv.visitLabel(l1);
    mv.visitFrame(Opcodes.F_APPEND, 1, new Object[] { "adr" }, 0, null);
    mv.visitInsn(ICONST_0);
    mv.visitLabel(l2);
    mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { Opcodes.INTEGER });
    mv.visitInsn(IRETURN);
    mv.visitMaxs(4, 5);
    mv.visitEnd();

    mv = cw.visitMethod(ACC_PUBLIC, "setBlockBPdata", "(IIII)Z", null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, "adq", "r", "[Ladr;");
    mv.visitVarInsn(ILOAD, 2);
    mv.visitInsn(ICONST_4);
    mv.visitInsn(ISHR);
    mv.visitInsn(AALOAD);
    mv.visitVarInsn(ASTORE, 5);
    mv.visitVarInsn(ALOAD, 5);
    Label lab0 = new Label();
    mv.visitJumpInsn(IFNONNULL, lab0);
    mv.visitInsn(ICONST_0);
    mv.visitInsn(IRETURN);
    mv.visitLabel(lab0);
    mv.visitFrame(Opcodes.F_APPEND, 1, new Object[] { "adr" }, 0, null);
    mv.visitVarInsn(ALOAD, 5);
    mv.visitVarInsn(ILOAD, 1);
    mv.visitVarInsn(ILOAD, 2);
    mv.visitIntInsn(BIPUSH, 15);
    mv.visitInsn(IAND);
    mv.visitVarInsn(ILOAD, 3);
    mv.visitMethodInsn(INVOKEVIRTUAL, "adr", "getBlockBPdata", "(III)I");
    mv.visitVarInsn(ISTORE, 6);
    mv.visitVarInsn(ILOAD, 6);
    mv.visitVarInsn(ILOAD, 4);
    Label lab1 = new Label();
    mv.visitJumpInsn(IF_ICMPNE, lab1);
    mv.visitInsn(ICONST_0);
    mv.visitInsn(IRETURN);
    mv.visitLabel(lab1);
    mv.visitFrame(Opcodes.F_APPEND, 1, new Object[] { Opcodes.INTEGER }, 0, null);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitInsn(ICONST_1);
    mv.visitFieldInsn(PUTFIELD, "adq", "l", "Z");
    mv.visitVarInsn(ALOAD, 5);
    mv.visitVarInsn(ILOAD, 1);
    mv.visitVarInsn(ILOAD, 2);
    mv.visitIntInsn(BIPUSH, 15);
    mv.visitInsn(IAND);
    mv.visitVarInsn(ILOAD, 3);
    mv.visitVarInsn(ILOAD, 4);
    mv.visitMethodInsn(INVOKEVIRTUAL, "adr", "setBlockBPdata", "(IIII)V");
    mv.visitInsn(ICONST_1);
    mv.visitInsn(IRETURN);
    mv.visitMaxs(5, 7);
    mv.visitEnd();

    cw.visitEnd();

    System.out.println("OK");

    /*try
    {
       FileOutputStream fos = new FileOutputStream("d:/Chunk.mod.class");
       fos.write(cw.toByteArray());
     fos.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }*/

    return cw.toByteArray();
}

From source file:blockphysics.asm.BPTransformer.java

License:Open Source License

private byte[] transformEntityFallingSand(byte[] bytes) {
    /*try//  w w w .  j a v  a 2s.c  o  m
    {
        FileOutputStream fos = new FileOutputStream("d:/EntityFallingSand.orig.class");
        fos.write(bytes);
      fos.close();
    } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
    }*/

    System.out.print("[BlockPhysics] Patching EntityFallingSand.class .......");
    boolean ok = false;
    boolean ok2 = false;
    boolean ok3 = false;
    boolean ok4 = false;
    boolean ok5 = false;
    boolean ok6 = false;
    boolean ok7 = false;
    boolean ok8 = false;
    boolean ok9 = false, ok10 = false;

    ClassNode classNode = new ClassNode();
    ClassReader classReader = new ClassReader(bytes);
    classReader.accept(classNode, 0);

    MethodNode m;
    Iterator<MethodNode> methods = classNode.methods.iterator();
    while (methods.hasNext()) {
        m = methods.next();

        if (m.name.equals("<init>") && m.desc.equals("(Labv;)V")) {
            for (int index = m.instructions.size() - 1; index >= 0; index--) {
                if (m.instructions.get(index).getOpcode() == RETURN) {
                    InsnList toInject = new InsnList();

                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new InsnNode(ICONST_1));
                    toInject.add(new FieldInsnNode(PUTFIELD, "sq", "m", "Z"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new LdcInsnNode(new Float("0.996")));
                    toInject.add(new LdcInsnNode(new Float("0.996")));
                    toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "sq", "a", "(FF)V"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new FieldInsnNode(GETFIELD, "sq", "P", "F"));
                    toInject.add(new InsnNode(FCONST_2));
                    toInject.add(new InsnNode(FDIV));
                    toInject.add(new FieldInsnNode(PUTFIELD, "sq", "N", "F"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new InsnNode(DCONST_0));
                    toInject.add(new FieldInsnNode(PUTFIELD, "sq", "x", "D"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new InsnNode(DCONST_0));
                    toInject.add(new FieldInsnNode(PUTFIELD, "sq", "y", "D"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new InsnNode(DCONST_0));
                    toInject.add(new FieldInsnNode(PUTFIELD, "sq", "z", "D"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new InsnNode(DCONST_0));
                    toInject.add(new FieldInsnNode(PUTFIELD, "sq", "accelerationX", "D"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new LdcInsnNode(new Double("-0.024525")));
                    toInject.add(new FieldInsnNode(PUTFIELD, "sq", "accelerationY", "D"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new InsnNode(DCONST_0));
                    toInject.add(new FieldInsnNode(PUTFIELD, "sq", "accelerationZ", "D"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new InsnNode(ICONST_0));
                    toInject.add(new FieldInsnNode(PUTFIELD, "sq", "slideDir", "B"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new InsnNode(ICONST_1));
                    toInject.add(new FieldInsnNode(PUTFIELD, "sq", "Z", "Z"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new LdcInsnNode(new Float("0.8")));
                    toInject.add(new FieldInsnNode(PUTFIELD, "sq", "aa", "F"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new InsnNode(ICONST_4));
                    toInject.add(new FieldInsnNode(PUTFIELD, "sq", "dead", "B"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new InsnNode(ICONST_0));
                    toInject.add(new FieldInsnNode(PUTFIELD, "sq", "bpdata", "I"));

                    m.instructions.insertBefore(m.instructions.get(index), toInject);
                    ok10 = true;
                    break;
                }
            }
        } else if (m.name.equals("<init>") && m.desc.equals("(Labv;DDDII)V")) {
            for (int index = 0; index < m.instructions.size(); index++) {
                if (m.instructions.get(index).getOpcode() == INVOKEVIRTUAL
                        && m.instructions.get(index).getType() == AbstractInsnNode.METHOD_INSN
                        && ((MethodInsnNode) m.instructions.get(index)).owner.equals("sq")
                        && ((MethodInsnNode) m.instructions.get(index)).name.equals("a")) {
                    m.instructions.set(m.instructions.get(index - 1), new LdcInsnNode(new Float("0.996")));
                    m.instructions.set(m.instructions.get(index - 2), new LdcInsnNode(new Float("0.996")));
                    ok = true;
                    break;
                }
            }

            for (int index = m.instructions.size() - 1; index >= 0; index--) {
                if (m.instructions.get(index).getOpcode() == RETURN) {
                    InsnList toInject = new InsnList();

                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new InsnNode(DCONST_0));
                    toInject.add(new FieldInsnNode(PUTFIELD, "sq", "accelerationX", "D"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new LdcInsnNode(new Double("-0.024525")));
                    toInject.add(new FieldInsnNode(PUTFIELD, "sq", "accelerationY", "D"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new InsnNode(DCONST_0));
                    toInject.add(new FieldInsnNode(PUTFIELD, "sq", "accelerationZ", "D"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new InsnNode(ICONST_0));
                    toInject.add(new FieldInsnNode(PUTFIELD, "sq", "slideDir", "B"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new InsnNode(ICONST_1));
                    toInject.add(new FieldInsnNode(PUTFIELD, "sq", "Z", "Z"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new LdcInsnNode(new Float("0.8")));
                    toInject.add(new FieldInsnNode(PUTFIELD, "sq", "aa", "F"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new InsnNode(ICONST_4));
                    toInject.add(new FieldInsnNode(PUTFIELD, "sq", "dead", "B"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new InsnNode(ICONST_0));
                    toInject.add(new FieldInsnNode(PUTFIELD, "sq", "bpdata", "I"));

                    m.instructions.insertBefore(m.instructions.get(index), toInject);
                    ok2 = true;
                    break;
                }
            }
        } else if (m.name.equals("K") && m.desc.equals("()Z")) {
            InsnList toInject = new InsnList();
            toInject.add(new InsnNode(ICONST_0));
            toInject.add(new InsnNode(IRETURN));

            m.instructions.clear();
            m.localVariables.clear();
            m.instructions.add(toInject);
            ok9 = true;
        } else if (m.name.equals("l_") && m.desc.equals("()V")) {
            InsnList toInject = new InsnList();
            toInject.add(new VarInsnNode(ALOAD, 0));
            toInject.add(new FieldInsnNode(GETFIELD, "sq", "q", "Labv;"));
            toInject.add(new VarInsnNode(ALOAD, 0));
            toInject.add(new MethodInsnNode(INVOKESTATIC, "blockphysics/BlockPhysics", "fallingSandUpdate",
                    "(Labv;Lsq;)V"));
            toInject.add(new InsnNode(RETURN));

            m.instructions.clear();
            m.localVariables.clear();
            m.instructions.add(toInject);
            ok3 = true;
        } else if (m.name.equals("b") && m.desc.equals("(F)V")) {
            m.instructions.clear();
            m.localVariables.clear();
            m.instructions.add(new InsnNode(RETURN));
            ok4 = true;
        } else if (m.name.equals("b") && m.desc.equals("(Lbx;)V")) {
            for (int index = m.instructions.size() - 1; index >= 0; index--) {
                if (m.instructions.get(index).getOpcode() == RETURN) {
                    InsnList toInject = new InsnList();

                    toInject.add(new VarInsnNode(ALOAD, 1));
                    toInject.add(new LdcInsnNode("Acceleration"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new InsnNode(ICONST_3));
                    toInject.add(new IntInsnNode(NEWARRAY, T_DOUBLE));
                    toInject.add(new InsnNode(DUP));
                    toInject.add(new InsnNode(ICONST_0));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new FieldInsnNode(GETFIELD, "sq", "accelerationX", "D"));
                    toInject.add(new InsnNode(DASTORE));
                    toInject.add(new InsnNode(DUP));
                    toInject.add(new InsnNode(ICONST_1));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new FieldInsnNode(GETFIELD, "sq", "accelerationY", "D"));
                    toInject.add(new InsnNode(DASTORE));
                    toInject.add(new InsnNode(DUP));
                    toInject.add(new InsnNode(ICONST_2));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new FieldInsnNode(GETFIELD, "sq", "accelerationZ", "D"));
                    toInject.add(new InsnNode(DASTORE));
                    toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "sq", "a", "([D)Lcf;"));
                    toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "bx", "a", "(Ljava/lang/String;Lck;)V"));
                    toInject.add(new VarInsnNode(ALOAD, 1));
                    toInject.add(new LdcInsnNode("BPData"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new FieldInsnNode(GETFIELD, "sq", "bpdata", "I"));
                    toInject.add(new InsnNode(I2B));
                    toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "bx", "a", "(Ljava/lang/String;B)V"));

                    m.instructions.insertBefore(m.instructions.get(index), toInject);
                    ok5 = true;
                    break;
                }
            }
        } else if (m.name.equals("a") && m.desc.equals("(Lbx;)V")) {

            while (!(m.instructions.get(0).getType() == AbstractInsnNode.LDC_INSN
                    && ((LdcInsnNode) m.instructions.get(0)).cst.equals("Data"))) {
                m.instructions.remove(m.instructions.get(0));
                ok6 = true;
            }

            InsnList toInject = new InsnList();
            toInject.add(new VarInsnNode(ALOAD, 0));
            toInject.add(new VarInsnNode(ALOAD, 1));
            toInject.add(new MethodInsnNode(INVOKESTATIC, "blockphysics/BlockPhysics", "readFallingSandID",
                    "(Lbx;)I"));
            toInject.add(new FieldInsnNode(PUTFIELD, "sq", "a", "I"));
            toInject.add(new VarInsnNode(ALOAD, 0));
            toInject.add(new VarInsnNode(ALOAD, 1));

            m.instructions.insertBefore(m.instructions.get(0), toInject);

            for (int index = m.instructions.size() - 1; index >= 0; index--) {
                if (m.instructions.get(index).getOpcode() == RETURN) {
                    toInject = new InsnList();
                    toInject.add(new VarInsnNode(ALOAD, 1));
                    toInject.add(new LdcInsnNode("Acceleration"));
                    toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "bx", "b", "(Ljava/lang/String;)Z"));
                    LabelNode l4 = new LabelNode();
                    toInject.add(new JumpInsnNode(IFEQ, l4));
                    toInject.add(new VarInsnNode(ALOAD, 1));
                    toInject.add(new LdcInsnNode("Acceleration"));
                    toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "bx", "m", "(Ljava/lang/String;)Lcf;"));
                    toInject.add(new VarInsnNode(ASTORE, 2));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new VarInsnNode(ALOAD, 2));
                    toInject.add(new InsnNode(ICONST_0));
                    toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "cf", "b", "(I)Lck;"));
                    toInject.add(new TypeInsnNode(CHECKCAST, "ca"));
                    toInject.add(new FieldInsnNode(GETFIELD, "ca", "a", "D"));
                    toInject.add(new FieldInsnNode(PUTFIELD, "sq", "accelerationX", "D"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new VarInsnNode(ALOAD, 2));
                    toInject.add(new InsnNode(ICONST_1));
                    toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "cf", "b", "(I)Lck;"));
                    toInject.add(new TypeInsnNode(CHECKCAST, "ca"));
                    toInject.add(new FieldInsnNode(GETFIELD, "ca", "a", "D"));
                    toInject.add(new FieldInsnNode(PUTFIELD, "sq", "accelerationY", "D"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new VarInsnNode(ALOAD, 2));
                    toInject.add(new InsnNode(ICONST_2));
                    toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "cf", "b", "(I)Lck;"));
                    toInject.add(new TypeInsnNode(CHECKCAST, "ca"));
                    toInject.add(new FieldInsnNode(GETFIELD, "ca", "a", "D"));
                    toInject.add(new FieldInsnNode(PUTFIELD, "sq", "accelerationZ", "D"));
                    LabelNode l5 = new LabelNode();
                    toInject.add(new JumpInsnNode(GOTO, l5));
                    toInject.add(l4);
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new InsnNode(DCONST_0));
                    toInject.add(new FieldInsnNode(PUTFIELD, "sq", "accelerationX", "D"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new InsnNode(DCONST_0));
                    toInject.add(new FieldInsnNode(PUTFIELD, "sq", "accelerationY", "D"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new InsnNode(DCONST_0));
                    toInject.add(new FieldInsnNode(PUTFIELD, "sq", "accelerationZ", "D"));
                    toInject.add(l5);
                    toInject.add(new VarInsnNode(ALOAD, 1));
                    toInject.add(new LdcInsnNode("BPData"));
                    toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "bx", "b", "(Ljava/lang/String;)Z"));
                    LabelNode l7 = new LabelNode();
                    toInject.add(new JumpInsnNode(IFEQ, l7));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new VarInsnNode(ALOAD, 1));
                    toInject.add(new LdcInsnNode("BPData"));
                    toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "bx", "c", "(Ljava/lang/String;)B"));
                    toInject.add(new FieldInsnNode(PUTFIELD, "sq", "bpdata", "I"));
                    LabelNode l8 = new LabelNode();
                    toInject.add(new JumpInsnNode(GOTO, l8));
                    toInject.add(l7);
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new InsnNode(ICONST_0));
                    toInject.add(new FieldInsnNode(PUTFIELD, "sq", "bpdata", "I"));
                    toInject.add(l8);

                    m.instructions.insertBefore(m.instructions.get(index), toInject);

                    ok7 = true;
                    break;
                }
            }
        } else if (m.name.equals("au") && m.desc.equals("()Z")) {
            InsnList toInject = new InsnList();
            toInject.add(new VarInsnNode(ALOAD, 0));
            toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "sq", "ae", "()Z"));
            toInject.add(new InsnNode(IRETURN));

            m.instructions.clear();
            m.localVariables.clear();
            m.instructions.add(toInject);
            ok8 = true;
        }

    }

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

    FieldVisitor fv;

    fv = cw.visitField(ACC_PUBLIC, "accelerationX", "D", null, null);
    fv.visitEnd();

    fv = cw.visitField(ACC_PUBLIC, "accelerationY", "D", null, null);
    fv.visitEnd();

    fv = cw.visitField(ACC_PUBLIC, "accelerationZ", "D", null, null);
    fv.visitEnd();

    fv = cw.visitField(ACC_PUBLIC, "bpdata", "I", null, null);
    fv.visitEnd();

    fv = cw.visitField(ACC_PUBLIC, "slideDir", "B", null, null);
    fv.visitEnd();

    fv = cw.visitField(ACC_PUBLIC, "media", "I", null, null);
    fv.visitEnd();

    fv = cw.visitField(ACC_PUBLIC, "dead", "B", null, null);
    fv.visitEnd();

    MethodVisitor mv;

    mv = cw.visitMethod(ACC_PUBLIC, "D", "()Lasu;", null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, "sq", "E", "Lasu;");
    mv.visitInsn(ARETURN);
    mv.visitMaxs(1, 1);
    mv.visitEnd();

    mv = cw.visitMethod(ACC_PUBLIC, "al", "()V", null, null);
    mv.visitCode();
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 1);
    mv.visitEnd();

    mv = cw.visitMethod(ACC_PUBLIC, "d", "(DDD)V", null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, "sq", "q", "Labv;");
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(DLOAD, 1);
    mv.visitVarInsn(DLOAD, 3);
    mv.visitVarInsn(DLOAD, 5);
    mv.visitMethodInsn(INVOKESTATIC, "blockphysics/BlockPhysics", "moveEntity", "(Labv;Lsq;DDD)V");
    mv.visitInsn(RETURN);
    mv.visitMaxs(8, 7);
    mv.visitEnd();

    if (!ok8) {
        mv = cw.visitMethod(ACC_PUBLIC, "au", "()Z", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKEVIRTUAL, "sq", "ae", "()Z");
        mv.visitInsn(IRETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();

        ok8 = true;
    }

    cw.visitEnd();

    if (ok && ok2 && ok3 && ok4 && ok5 && ok6 && ok7 && ok8 && ok9 && ok10)
        System.out.println("OK");
    else
        System.out.println("Failed." + ok + ok2 + ok3 + ok4 + ok5 + ok6 + ok7 + ok8 + ok9 + ok10);

    /*try
    {
       FileOutputStream fos = new FileOutputStream("d:/EntityFallingSand.class");
       fos.write(cw.toByteArray());
     fos.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }*/

    return cw.toByteArray();
}

From source file:blue.lapis.pore.event.PoreListenerGenerator.java

License:Open Source License

private static byte[] generate(String name, Class<?> pore, Class<?> sponge, EventPriority priority,
        Order order) {//  w  w  w . java 2 s .  com
    name = name.replace('.', '/');
    String poreName = Type.getInternalName(pore);
    String spongeSignature = "(L" + Type.getInternalName(sponge) + ";)V";

    ClassWriter cw = new ClassWriter(0);
    MethodVisitor mv;
    AnnotationVisitor av;

    cw.visit(V1_6, ACC_PUBLIC + ACC_FINAL + ACC_SUPER, name, null, "java/lang/Object", null);

    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
        mv.visitInsn(RETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "on" + sponge.getSimpleName(), spongeSignature, null, null);
        {
            av = mv.visitAnnotation(ANNOTATION_DESCRIPTOR, true);
            av.visitEnum("order", ORDER_DESCRIPTOR, order.name());
            av.visitEnd();
        }
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKESTATIC, EVENT_WRAPPER_CLASS, "get", GET_CACHE, false);
        mv.visitTypeInsn(CHECKCAST, poreName);
        mv.visitVarInsn(ASTORE, 2);
        mv.visitVarInsn(ALOAD, 2);
        Label l0 = new Label();
        mv.visitJumpInsn(IFNONNULL, l0);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitTypeInsn(NEW, poreName);
        mv.visitInsn(DUP);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKESPECIAL, poreName, "<init>", spongeSignature, false);
        mv.visitInsn(DUP);
        mv.visitVarInsn(ASTORE, 2);
        mv.visitMethodInsn(INVOKESTATIC, EVENT_WRAPPER_CLASS, "set", SET_CACHE, false);
        mv.visitLabel(l0);
        mv.visitFrame(F_APPEND, 1, new Object[] { poreName }, 0, null);
        mv.visitVarInsn(ALOAD, 2);
        mv.visitFieldInsn(GETSTATIC, PRIORITY_CLASS, priority.name(), PRIORITY_DESCRIPTOR);
        mv.visitMethodInsn(INVOKESTATIC, EVENT_WRAPPER_CLASS, "call", CALL_EVENT, false);
        mv.visitInsn(RETURN);
        mv.visitMaxs(4, 3);
        mv.visitEnd();
    }
    cw.visitEnd();

    return cw.toByteArray();
}