Example usage for org.objectweb.asm Opcodes F_SAME

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

Introduction

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

Prototype

int F_SAME

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

Click Source Link

Document

A compressed frame with exactly the same locals as the previous frame and with an empty stack.

Usage

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 {//from w w  w .j  a v  a 2  s  .  c  om
        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[] transformAnvilChunkLoader(byte[] bytes) {
    /*try//from www  .  j  a v  a2 s  .com
     {
         FileOutputStream fos = new FileOutputStream("d:/AnvilChunkLoader.orig.class");
         fos.write(bytes);
       fos.close();
     } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
     }*/

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

    boolean ok = false, ok2 = false, ok3 = false, ok4 = false, ok6 = false, ok7 = 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("a") && m.desc.equals("(Ladq;Labv;Lbx;)V")) {
            int var1 = 9;
            for (int index = 0; index < m.instructions.size(); index++) {
                if (ok3)
                    break;
                if (m.instructions.get(index).getOpcode() == INVOKESPECIAL
                        && m.instructions.get(index).getType() == AbstractInsnNode.METHOD_INSN
                        && ((MethodInsnNode) m.instructions.get(index)).owner.equals("bx")
                        && ((MethodInsnNode) m.instructions.get(index)).name.equals("<init>")
                        && ((MethodInsnNode) m.instructions.get(index)).desc.equals("()V")) {
                    for (int index2 = index; index2 < m.instructions.size(); index2++) {
                        if (m.instructions.get(index2).getOpcode() == ASTORE) {
                            var1 = ((VarInsnNode) m.instructions.get(index2)).var;
                            ok3 = true;
                            break;
                        }
                    }
                }
            }

            int var2 = 11;
            for (int index = 0; index < m.instructions.size(); index++) {
                if (ok4)
                    break;
                if (m.instructions.get(index).getType() == AbstractInsnNode.LDC_INSN
                        && ((LdcInsnNode) m.instructions.get(index)).cst.equals("Data")) {
                    for (int index2 = index; index2 < m.instructions.size(); index2++) {
                        if (m.instructions.get(index2).getOpcode() == ALOAD) {
                            var2 = ((VarInsnNode) m.instructions.get(index2)).var;
                            ok4 = true;
                            break;
                        }
                    }
                }
            }

            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("cf")
                        && ((MethodInsnNode) m.instructions.get(index)).name.equals("a")
                        && ((MethodInsnNode) m.instructions.get(index)).desc.equals("(Lck;)V")) {
                    InsnList toInject = new InsnList();

                    toInject.add(new VarInsnNode(ALOAD, var1));
                    toInject.add(new LdcInsnNode("BPData"));
                    toInject.add(new VarInsnNode(ALOAD, var2));
                    toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "adr", "getBPdataArray", "()[B"));
                    toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "bx", "a", "(Ljava/lang/String;[B)V"));

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

                    ok = true;
                    break;
                }
            }
        } else if (m.name.equals("a") && m.desc.equals("(Labv;Lbx;)Ladq;")) {
            int var1 = 11;
            for (int index = 0; index < m.instructions.size(); index++) {

                if (ok6)
                    break;
                if (m.instructions.get(index).getOpcode() == INVOKEVIRTUAL
                        && m.instructions.get(index).getType() == AbstractInsnNode.METHOD_INSN
                        && ((MethodInsnNode) m.instructions.get(index)).owner.equals("cf")
                        && ((MethodInsnNode) m.instructions.get(index)).name.equals("b")
                        && ((MethodInsnNode) m.instructions.get(index)).desc.equals("(I)Lck;")) {
                    for (int index2 = index; index2 < m.instructions.size(); index2++) {
                        if (m.instructions.get(index2).getOpcode() == ASTORE) {
                            var1 = ((VarInsnNode) m.instructions.get(index2)).var;
                            ok6 = true;
                            break;
                        }
                    }
                }
            }

            int var2 = 13;
            for (int index = 0; index < m.instructions.size(); index++) {
                if (ok7)
                    break;
                if (m.instructions.get(index).getOpcode() == INVOKESPECIAL
                        && m.instructions.get(index).getType() == AbstractInsnNode.METHOD_INSN
                        && ((MethodInsnNode) m.instructions.get(index)).owner.equals("adr")
                        && ((MethodInsnNode) m.instructions.get(index)).name.equals("<init>")
                        && ((MethodInsnNode) m.instructions.get(index)).desc.equals("(IZ)V")) {
                    for (int index2 = index; index2 < m.instructions.size(); index2++) {
                        if (m.instructions.get(index2).getOpcode() == ASTORE) {
                            var2 = ((VarInsnNode) m.instructions.get(index2)).var;
                            ok7 = true;
                            break;
                        }
                    }
                }
            }

            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("adr")
                        && ((MethodInsnNode) m.instructions.get(index)).name.equals("e")
                        && ((MethodInsnNode) m.instructions.get(index)).desc.equals("()V")) {
                    InsnList toInject = new InsnList();

                    toInject.add(new VarInsnNode(ALOAD, var1));
                    toInject.add(new LdcInsnNode("BPData"));
                    toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "bx", "b", "(Ljava/lang/String;)Z"));
                    LabelNode l6 = new LabelNode();
                    toInject.add(new JumpInsnNode(IFEQ, l6));
                    toInject.add(new VarInsnNode(ALOAD, var2));
                    toInject.add(new VarInsnNode(ALOAD, var1));
                    toInject.add(new LdcInsnNode("BPData"));
                    toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "bx", "j", "(Ljava/lang/String;)[B"));
                    toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "adr", "setBPdataArray", "([B)V"));
                    LabelNode l7 = new LabelNode();
                    toInject.add(new JumpInsnNode(GOTO, l7));
                    toInject.add(l6);
                    toInject.add(new FrameNode(Opcodes.F_SAME, 0, null, 0, null));
                    toInject.add(new VarInsnNode(ALOAD, var2));
                    toInject.add(new IntInsnNode(SIPUSH, 4096));
                    toInject.add(new IntInsnNode(NEWARRAY, T_BYTE));
                    toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "adr", "setBPdataArray", "([B)V"));
                    toInject.add(l7);

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

                    ok2 = true;
                    break;
                }
            }
        }
    }

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

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

    /*try
    {
       FileOutputStream fos = new FileOutputStream("d:/AnvilChunkLoader.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  ww  . j  av a  2s  . co 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[] transformBlockPhysics(byte[] bytes) {
    /*try/*from  w w w  . j  a  v  a  2  s  . co m*/
    {
        FileOutputStream fos = new FileOutputStream("d:/BlockPhysics.orig.class");
        fos.write(bytes);
      fos.close();
    } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
    }*/

    try {
        Class.forName("org.bukkit.Bukkit");
        System.out.println("[BlockPhysics] Bukkit detected.");
    } catch (ClassNotFoundException e) {
        return bytes;
    }

    System.out.print("[BlockPhysics] Patching BlockPhysics.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("tickBlocksRandomMove") && m.desc.equals("(Ljr;)V")) {
            m.instructions.clear();
            m.localVariables.clear();

            InsnList toInject = new InsnList();

            toInject.add(new FieldInsnNode(GETSTATIC, "blockphysics/BlockPhysics", "skipMove", "Z"));
            LabelNode l0 = new LabelNode();
            toInject.add(new JumpInsnNode(IFEQ, l0));
            toInject.add(new InsnNode(RETURN));
            toInject.add(l0);
            toInject.add(new FrameNode(Opcodes.F_SAME, 0, null, 0, null));
            toInject.add(new VarInsnNode(ALOAD, 0));
            toInject.add(new FieldInsnNode(GETFIELD, "jr", "G", "Lgnu/trove/map/hash/TLongShortHashMap;"));
            toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "gnu/trove/map/hash/TLongShortHashMap", "iterator",
                    "()Lgnu/trove/iterator/TLongShortIterator;"));
            toInject.add(new VarInsnNode(ASTORE, 1));
            LabelNode l1 = new LabelNode();
            toInject.add(l1);
            toInject.add(new FrameNode(Opcodes.F_APPEND, 1,
                    new Object[] { "gnu/trove/iterator/TLongShortIterator" }, 0, null));
            toInject.add(new VarInsnNode(ALOAD, 1));
            toInject.add(new MethodInsnNode(INVOKEINTERFACE, "gnu/trove/iterator/TLongShortIterator", "hasNext",
                    "()Z"));
            LabelNode l2 = new LabelNode();
            toInject.add(new JumpInsnNode(IFEQ, l2));
            toInject.add(new VarInsnNode(ALOAD, 1));
            toInject.add(new MethodInsnNode(INVOKEINTERFACE, "gnu/trove/iterator/TLongShortIterator", "advance",
                    "()V"));
            toInject.add(new VarInsnNode(ALOAD, 1));
            toInject.add(
                    new MethodInsnNode(INVOKEINTERFACE, "gnu/trove/iterator/TLongShortIterator", "key", "()J"));
            toInject.add(new VarInsnNode(LSTORE, 2));
            toInject.add(new VarInsnNode(ALOAD, 0));
            toInject.add(new InsnNode(POP));
            toInject.add(new VarInsnNode(LLOAD, 2));
            toInject.add(new MethodInsnNode(INVOKESTATIC, "abv", "keyToX", "(J)I"));
            toInject.add(new VarInsnNode(ISTORE, 4));
            toInject.add(new VarInsnNode(ALOAD, 0));
            toInject.add(new InsnNode(POP));
            toInject.add(new VarInsnNode(LLOAD, 2));
            toInject.add(new MethodInsnNode(INVOKESTATIC, "abv", "keyToZ", "(J)I"));
            toInject.add(new VarInsnNode(ISTORE, 5));
            toInject.add(new VarInsnNode(ILOAD, 4));
            toInject.add(new IntInsnNode(BIPUSH, 16));
            toInject.add(new InsnNode(IMUL));
            toInject.add(new VarInsnNode(ISTORE, 6));
            toInject.add(new VarInsnNode(ILOAD, 5));
            toInject.add(new IntInsnNode(BIPUSH, 16));
            toInject.add(new InsnNode(IMUL));
            toInject.add(new VarInsnNode(ISTORE, 7));
            toInject.add(new VarInsnNode(ALOAD, 0));
            toInject.add(new VarInsnNode(ILOAD, 4));
            toInject.add(new VarInsnNode(ILOAD, 5));
            toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "jr", "e", "(II)Ladq;"));
            toInject.add(new VarInsnNode(ASTORE, 8));
            toInject.add(new VarInsnNode(ALOAD, 8));
            toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "adq", "i", "()[Ladr;"));
            toInject.add(new VarInsnNode(ASTORE, 12));
            toInject.add(new VarInsnNode(ALOAD, 12));
            toInject.add(new InsnNode(ARRAYLENGTH));
            toInject.add(new VarInsnNode(ISTORE, 9));
            toInject.add(new InsnNode(ICONST_0));
            toInject.add(new VarInsnNode(ISTORE, 10));
            LabelNode l3 = new LabelNode();
            toInject.add(l3);
            toInject.add(new FrameNode(Opcodes.F_FULL, 12,
                    new Object[] { "jr", "gnu/trove/iterator/TLongShortIterator", Opcodes.LONG, Opcodes.INTEGER,
                            Opcodes.INTEGER, Opcodes.INTEGER, Opcodes.INTEGER, "adq", Opcodes.INTEGER,
                            Opcodes.INTEGER, Opcodes.TOP, "[Ladr;" },
                    0, new Object[] {}));
            toInject.add(new VarInsnNode(ILOAD, 10));
            toInject.add(new VarInsnNode(ILOAD, 9));
            LabelNode l4 = new LabelNode();
            toInject.add(new JumpInsnNode(IF_ICMPGE, l4));
            toInject.add(new VarInsnNode(ALOAD, 12));
            toInject.add(new VarInsnNode(ILOAD, 10));
            toInject.add(new InsnNode(AALOAD));
            toInject.add(new VarInsnNode(ASTORE, 13));
            toInject.add(new VarInsnNode(ALOAD, 13));
            LabelNode l5 = new LabelNode();
            toInject.add(new JumpInsnNode(IFNULL, l5));
            toInject.add(new InsnNode(ICONST_0));
            toInject.add(new VarInsnNode(ISTORE, 14));
            LabelNode l6 = new LabelNode();
            toInject.add(l6);
            toInject.add(new FrameNode(Opcodes.F_APPEND, 2, new Object[] { "adr", Opcodes.INTEGER }, 0, null));
            toInject.add(new VarInsnNode(ILOAD, 14));
            toInject.add(new InsnNode(ICONST_3));
            toInject.add(new JumpInsnNode(IF_ICMPGE, l5));
            toInject.add(new FieldInsnNode(GETSTATIC, "blockphysics/BlockPhysics", "updateLCG", "I"));
            toInject.add(new InsnNode(ICONST_3));
            toInject.add(new InsnNode(IMUL));
            toInject.add(new LdcInsnNode(new Integer(1013904223)));
            toInject.add(new InsnNode(IADD));
            toInject.add(new FieldInsnNode(PUTSTATIC, "blockphysics/BlockPhysics", "updateLCG", "I"));
            toInject.add(new FieldInsnNode(GETSTATIC, "blockphysics/BlockPhysics", "updateLCG", "I"));
            toInject.add(new InsnNode(ICONST_2));
            toInject.add(new InsnNode(ISHR));
            toInject.add(new VarInsnNode(ISTORE, 11));
            toInject.add(new VarInsnNode(ILOAD, 11));
            toInject.add(new IntInsnNode(BIPUSH, 15));
            toInject.add(new InsnNode(IAND));
            toInject.add(new VarInsnNode(ISTORE, 15));
            toInject.add(new VarInsnNode(ILOAD, 11));
            toInject.add(new IntInsnNode(BIPUSH, 8));
            toInject.add(new InsnNode(ISHR));
            toInject.add(new IntInsnNode(BIPUSH, 15));
            toInject.add(new InsnNode(IAND));
            toInject.add(new VarInsnNode(ISTORE, 16));
            toInject.add(new VarInsnNode(ILOAD, 11));
            toInject.add(new IntInsnNode(BIPUSH, 16));
            toInject.add(new InsnNode(ISHR));
            toInject.add(new IntInsnNode(BIPUSH, 15));
            toInject.add(new InsnNode(IAND));
            toInject.add(new VarInsnNode(ISTORE, 17));
            toInject.add(new VarInsnNode(ALOAD, 13));
            toInject.add(new VarInsnNode(ILOAD, 15));
            toInject.add(new VarInsnNode(ILOAD, 17));
            toInject.add(new VarInsnNode(ILOAD, 16));
            toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "adr", "a", "(III)I"));
            toInject.add(new VarInsnNode(ISTORE, 18));
            toInject.add(new VarInsnNode(ALOAD, 13));
            toInject.add(new VarInsnNode(ILOAD, 15));
            toInject.add(new VarInsnNode(ILOAD, 17));
            toInject.add(new VarInsnNode(ILOAD, 16));
            toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "adr", "b", "(III)I"));
            toInject.add(new VarInsnNode(ISTORE, 19));
            toInject.add(new FieldInsnNode(GETSTATIC, "blockphysics/BlockPhysics", "blockSet",
                    "[[Lblockphysics/BlockDef;"));
            toInject.add(new VarInsnNode(ILOAD, 18));
            toInject.add(new InsnNode(AALOAD));
            toInject.add(new VarInsnNode(ILOAD, 19));
            toInject.add(new InsnNode(AALOAD));
            toInject.add(new FieldInsnNode(GETFIELD, "blockphysics/BlockDef", "randomtick", "Z"));
            LabelNode l7 = new LabelNode();
            toInject.add(new JumpInsnNode(IFEQ, l7));
            toInject.add(new VarInsnNode(ALOAD, 0));
            toInject.add(new VarInsnNode(ILOAD, 15));
            toInject.add(new VarInsnNode(ILOAD, 6));
            toInject.add(new InsnNode(IADD));
            toInject.add(new VarInsnNode(ILOAD, 17));
            toInject.add(new VarInsnNode(ALOAD, 13));
            toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "adr", "d", "()I"));
            toInject.add(new InsnNode(IADD));
            toInject.add(new VarInsnNode(ILOAD, 16));
            toInject.add(new VarInsnNode(ILOAD, 7));
            toInject.add(new InsnNode(IADD));
            toInject.add(new VarInsnNode(ILOAD, 18));
            toInject.add(new VarInsnNode(ILOAD, 19));
            toInject.add(new InsnNode(ICONST_0));
            toInject.add(new MethodInsnNode(INVOKESTATIC, "blockphysics/BlockPhysics", "tryToMove",
                    "(Labv;IIIIIZ)Z"));
            toInject.add(new InsnNode(POP));
            toInject.add(l7);
            toInject.add(new FrameNode(Opcodes.F_FULL, 14,
                    new Object[] { "jr", "gnu/trove/iterator/TLongShortIterator", Opcodes.LONG, Opcodes.INTEGER,
                            Opcodes.INTEGER, Opcodes.INTEGER, Opcodes.INTEGER, "adq", Opcodes.INTEGER,
                            Opcodes.INTEGER, Opcodes.INTEGER, "[Ladr;", "adr", Opcodes.INTEGER },
                    0, new Object[] {}));
            toInject.add(new IincInsnNode(14, 1));
            toInject.add(new JumpInsnNode(GOTO, l6));
            toInject.add(l5);
            toInject.add(new FrameNode(Opcodes.F_FULL, 12,
                    new Object[] { "jr", "gnu/trove/iterator/TLongShortIterator", Opcodes.LONG, Opcodes.INTEGER,
                            Opcodes.INTEGER, Opcodes.INTEGER, Opcodes.INTEGER, "adq", Opcodes.INTEGER,
                            Opcodes.INTEGER, Opcodes.TOP, "[Ladr;" },
                    0, new Object[] {}));
            toInject.add(new IincInsnNode(10, 1));
            toInject.add(new JumpInsnNode(GOTO, l3));
            toInject.add(l4);
            toInject.add(new FrameNode(Opcodes.F_FULL, 2,
                    new Object[] { "jr", "gnu/trove/iterator/TLongShortIterator" }, 0, new Object[] {}));
            toInject.add(new JumpInsnNode(GOTO, l1));
            toInject.add(l2);
            toInject.add(new FrameNode(Opcodes.F_CHOP, 1, null, 0, null));
            toInject.add(new InsnNode(RETURN));

            m.instructions.add(toInject);

            ok = true;
            break;
        }
    }

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

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

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

    return cw.toByteArray();
}

From source file:cc.adf.metrics.agent.visitor.ApplicationModuleImplModifier.java

public static void addDetailNameMethod(ClassWriter cw) {
    MethodVisitor mv = cw.visitMethod(ACC_PRIVATE, "getDetailName", "()Ljava/lang/String;", null, null);
    mv.visitCode();//from   w ww .ja  va  2 s . c o  m
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKEVIRTUAL, "oracle/jbo/server/ApplicationModuleImpl", "isRoot", "()Z", false);
    Label l0 = new Label();
    mv.visitJumpInsn(IFEQ, l0);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESPECIAL, "oracle/jbo/server/ApplicationModuleImpl", "getName",
            "()Ljava/lang/String;", false);
    mv.visitInsn(ARETURN);
    mv.visitLabel(l0);
    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
    mv.visitTypeInsn(NEW, "java/lang/StringBuilder");
    mv.visitInsn(DUP);
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "()V", false);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESPECIAL, "oracle/jbo/server/ApplicationModuleImpl", "getRootApplicationModule",
            "()Loracle/jbo/server/ApplicationModuleImpl;", false);
    mv.visitMethodInsn(INVOKEVIRTUAL, "oracle/jbo/server/ApplicationModuleImpl", "getName",
            "()Ljava/lang/String;", false);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
            "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
    mv.visitLdcInsn("::");
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
            "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESPECIAL, "oracle/jbo/server/ApplicationModuleImpl", "getName",
            "()Ljava/lang/String;", false);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
            "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;", false);
    mv.visitInsn(ARETURN);
    mv.visitMaxs(2, 1);
    mv.visitEnd();
}

From source file:ch.sourcepond.utils.podescoin.internal.method.InjectorMethodEnhancer.java

License:Apache License

@Override
public final void visitEnhance() {
    visitCode();/*from w ww .  ja v  a 2 s  . c o  m*/
    tryBlock();

    // Inserts a call to defaultWriteObject if a new writeObject method is
    // being generated.
    visitStreamDefaultCall();

    visitVarInsn(ALOAD, 0);
    visitMethodInsn(INVOKESTATIC, INJECTOR_INTERNAL_NAME, GET_CONTAINER_METHOD_NAME, GET_CONTAINER_METHOD_DESC,
            false);
    visitVarInsn(ASTORE, 2);
    visitLabel(l0);
    visitVarInsn(ALOAD, 0);

    int stackSize = MIN_STACK_SIZE;
    if (inspector.hasObjectInputStream()) {
        visitVarInsn(ALOAD, 1);
        stackSize++;
    }

    final String[][] components = inspector.getComponents();

    boolean increaseByOne = false;
    for (int i = 0; i < components.length; i++, stackSize++) {
        visitVarInsn(ALOAD, 2);
        if (components[i][0] != null) {
            visitLdcInsn(components[i][0]);
            visitLdcInsn(components[i][1]);
            pushByteConstant(mv, i);
            visitMethodInsn(INVOKEINTERFACE, CONTAINER_INTERNAL_NAME, GET_COMPONENT_BY_ID_NAME,
                    GET_COMPONENT_BY_ID_DESC, true);

            if (!increaseByOne) {
                increaseByOne = true;
            }
        } else {
            visitLdcInsn(components[i][1]);
            pushByteConstant(mv, i);
            visitMethodInsn(INVOKEINTERFACE, CONTAINER_INTERNAL_NAME, GET_COMPONENT_BY_TYPE_NAME_NAME,
                    GET_COMPONENT_BY_TYPE_DESC, true);
        }
        visitTypeInsn(CHECKCAST, components[i][1].replace('.', '/'));
    }

    visitMethodInsn(Opcodes.INVOKESPECIAL, inspector.getInternalClassName(), inspector.getInjectorMethodName(),
            inspector.getInjectorMethodDesc(), false);

    visitLabel(l1);
    catchBlock();

    visitFrame(Opcodes.F_SAME, 0, null, 0, null);
    visitInsn(RETURN);
    visitMaxs(increaseByOne ? stackSize + 1 : stackSize, 4);
}

From source file:com.centimia.orm.jaqu.ext.asm.JaquClassAdapter.java

License:Open Source License

/**
 * Generates the new method body, copy the old to a new method and connect them.
 * /* w  ww. j av  a 2s .c  o  m*/
 * the structure of the new method is:<br>
 * <br><b><div style="background:lightgray">
 * <pre>
 * public [CollectionType] [getterName]() {
 *    if ([fieldName] == null){
 *       try {
 *          if (null == db || db.isClosed())
 *             throw new RuntimeException("Cannot initialize a 'Relation' outside an open session!!!. Try initializing the field directly within the class.");
 *          Method method = db.getClass().getDeclaredMethod("getRelationFromDb", String.class, Object.class, Class.class);
 *          method.setAccessible(true);
 *          children = (Collection<TestTable>)method.invoke(db, [fieldName], this, TestTable.class);
 *          method.setAccessible(false);
 *       }
 *       catch (Exception e) {
 *          if (e instanceof RuntimeException)
 *             throw (RuntimeException)e;
 *          throw new RuntimeException(e.getMessage(), e);
 *       }
 *    }
 * return $orig_[getterName]();
 * }
 * </pre>
 * </div>
 * 
 * @param access
 * @param desc
 * @param signature
 * @param exceptions
 * @param name
 * @param newName
 * @param fieldName
 */
private void generateNewMethodBody(int access, String desc, String signature, String[] exceptions, String name,
        String newName, String fieldName) {
    MethodVisitor mv = cv.visitMethod(access, name, desc, signature, exceptions);
    String fieldSignature = signature.substring(signature.indexOf(')') + 1, signature.lastIndexOf('<')) + ";";
    String type = signature.substring(signature.indexOf('<') + 1, signature.indexOf('>'));
    String cast = desc.substring(desc.indexOf("java/"), desc.indexOf(';'));

    mv.visitCode();
    Label l0 = new Label();
    Label l1 = new Label();
    Label l2 = new Label();
    mv.visitTryCatchBlock(l0, l1, l2, "java/lang/Exception");
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, className, fieldName, fieldSignature);
    Label l3 = new Label();
    mv.visitJumpInsn(IFNONNULL, l3);
    mv.visitLabel(l0);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, className, "db", "Lcom/centimia/orm/jaqu/Db;");
    Label l4 = new Label();
    mv.visitJumpInsn(IFNULL, l4);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, className, "db", "Lcom/centimia/orm/jaqu/Db;");
    mv.visitMethodInsn(INVOKEVIRTUAL, "com/centimia/orm/jaqu/Db", "isClosed", "()Z", false);
    Label l5 = new Label();
    mv.visitJumpInsn(IFEQ, l5);
    mv.visitLabel(l4);
    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
    mv.visitTypeInsn(NEW, "java/lang/RuntimeException");
    mv.visitInsn(DUP);
    mv.visitLdcInsn(
            "Cannot initialize a 'Relation' outside an open session!!!. Try initializing the field directly within the class.");
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/RuntimeException", "<init>", "(Ljava/lang/String;)V", false);
    mv.visitInsn(ATHROW);
    mv.visitLabel(l5);
    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, className, "db", "Lcom/centimia/orm/jaqu/Db;");
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;", false);
    mv.visitLdcInsn("getRelationFromDb");
    mv.visitInsn(ICONST_3);
    mv.visitTypeInsn(ANEWARRAY, "java/lang/Class");
    mv.visitInsn(DUP);
    mv.visitInsn(ICONST_0);
    mv.visitLdcInsn(Type.getType("Ljava/lang/String;"));
    mv.visitInsn(AASTORE);
    mv.visitInsn(DUP);
    mv.visitInsn(ICONST_1);
    mv.visitLdcInsn(Type.getType("Ljava/lang/Object;"));
    mv.visitInsn(AASTORE);
    mv.visitInsn(DUP);
    mv.visitInsn(ICONST_2);
    mv.visitLdcInsn(Type.getType("Ljava/lang/Class;"));
    mv.visitInsn(AASTORE);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getDeclaredMethod",
            "(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method;", false);
    mv.visitVarInsn(ASTORE, 1);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitInsn(ICONST_1);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/Method", "setAccessible", "(Z)V", false);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, className, "db", "Lcom/centimia/orm/jaqu/Db;");
    mv.visitInsn(ICONST_3);
    mv.visitTypeInsn(ANEWARRAY, "java/lang/Object");
    mv.visitInsn(DUP);
    mv.visitInsn(ICONST_0);
    mv.visitLdcInsn(fieldName);
    mv.visitInsn(AASTORE);
    mv.visitInsn(DUP);
    mv.visitInsn(ICONST_1);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitInsn(AASTORE);
    mv.visitInsn(DUP);
    mv.visitInsn(ICONST_2);
    mv.visitLdcInsn(Type.getType(type));
    mv.visitInsn(AASTORE);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/Method", "invoke",
            "(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;", false);
    mv.visitTypeInsn(CHECKCAST, cast);
    mv.visitFieldInsn(PUTFIELD, className, fieldName, fieldSignature);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitInsn(ICONST_0);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/Method", "setAccessible", "(Z)V", false);
    mv.visitLabel(l1);
    mv.visitJumpInsn(GOTO, l3);
    mv.visitLabel(l2);
    mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/Exception" });
    mv.visitVarInsn(ASTORE, 1);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitTypeInsn(INSTANCEOF, "java/lang/RuntimeException");
    Label l6 = new Label();
    mv.visitJumpInsn(IFEQ, l6);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitTypeInsn(CHECKCAST, "java/lang/RuntimeException");
    mv.visitInsn(ATHROW);
    mv.visitLabel(l6);
    mv.visitFrame(Opcodes.F_APPEND, 1, new Object[] { "java/lang/Exception" }, 0, null);
    mv.visitTypeInsn(NEW, "java/lang/RuntimeException");
    mv.visitInsn(DUP);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Exception", "getMessage", "()Ljava/lang/String;", false);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/RuntimeException", "<init>",
            "(Ljava/lang/String;Ljava/lang/Throwable;)V", false);
    mv.visitInsn(ATHROW);
    mv.visitLabel(l3);
    mv.visitFrame(Opcodes.F_CHOP, 1, null, 0, null);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESPECIAL, className, newName, desc, false);
    mv.visitInsn(ARETURN);
    mv.visitMaxs(7, 2);
    mv.visitEnd();
}

From source file:com.centimia.orm.jaqu.ext.asm.JaquClassAdapter.java

License:Open Source License

/**
 * Generates the new method body, copy the old to a new method and connect them.
 * the structure of the new method is:<br>
 * <br><b><div style="background:lightgray">
 * <pre>//from   w  w  w .j  a va 2s  . c  om
 * public [entityType] [getterName]() {
 *   if ([field] == null) {
 *      try {
 *         if (null == db)
 *            throw new RuntimeException("Cannot initialize 'Relation' outside an open session!!!. Try initializing field directly within the class.");
 *         
 *         [parentType] parent = this.getClass().newInstance();
 *         [entityType] desc = TestB.class.newInstance();
 *         
 *         // get the primary key
 *         Object pk = db.getPrimaryKey(this);
 *         
 *         // get the object
 *         [entityValue] = db.from(desc).innerJoin(parent).on(parent.[entityValue]).is(desc).where(db.getPrimaryKey(parent)).is(pk).selectFirst();
 *      }
 *      catch (Exception e) {
 *         if (e instanceof RuntimeException)
 *            throw (RuntimeException)e;
 *         throw new RuntimeException(e.getMessage(), e);
 *      }
 *   }
 *   return $orig_[getterName]();
 *  }
 * </pre>
 * </div>
 * 
 * @param access
 * @param desc
 * @param exceptions
 * @param name - current method name
 * @param newName - new method name (the orig...)
 * @param fieldName
 */
public void generateLazyRelation(int access, String desc, String[] exceptions, String name, String newName,
        String fieldName) {
    MethodVisitor mv = cv.visitMethod(access, name, desc, null, exceptions);
    String fieldSignature = desc.substring(desc.indexOf(')') + 1);
    String fieldClassName = desc.substring(desc.indexOf(')') + 2, desc.length() - 1);

    mv.visitCode();
    Label l0 = new Label();
    Label l1 = new Label();
    Label l2 = new Label();
    mv.visitTryCatchBlock(l0, l1, l2, "java/lang/Exception");
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, className, fieldName, fieldSignature);
    Label l3 = new Label();
    mv.visitJumpInsn(IFNULL, l3);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, className, fieldName, fieldSignature);
    mv.visitFieldInsn(GETFIELD, fieldClassName, "isLazy", "Z");
    mv.visitJumpInsn(IFEQ, l3);
    mv.visitLabel(l0);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, className, "db", "Lcom/centimia/orm/jaqu/Db;");
    Label l4 = new Label();
    mv.visitJumpInsn(IFNULL, l4);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, className, "db", "Lcom/centimia/orm/jaqu/Db;");
    mv.visitMethodInsn(INVOKEVIRTUAL, "com/centimia/orm/jaqu/Db", "isClosed", "()Z", false);
    Label l5 = new Label();
    mv.visitJumpInsn(IFEQ, l5);
    mv.visitLabel(l4);
    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
    mv.visitTypeInsn(NEW, "java/lang/RuntimeException");
    mv.visitInsn(DUP);
    mv.visitLdcInsn(
            "Cannot initialize 'Relation' outside an open session!!!. Try initializing field directly within the class.");
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/RuntimeException", "<init>", "(Ljava/lang/String;)V", false);
    mv.visitInsn(ATHROW);
    mv.visitLabel(l5);
    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;", false);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "newInstance", "()Ljava/lang/Object;", false);
    mv.visitTypeInsn(CHECKCAST, className);
    mv.visitVarInsn(ASTORE, 1);
    mv.visitLdcInsn(Type.getType(fieldSignature));
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "newInstance", "()Ljava/lang/Object;", false);
    mv.visitTypeInsn(CHECKCAST, fieldClassName);
    mv.visitVarInsn(ASTORE, 2);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, className, "db", "Lcom/centimia/orm/jaqu/Db;");
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKEVIRTUAL, "com/centimia/orm/jaqu/Db", "getPrimaryKey",
            "(Ljava/lang/Object;)Ljava/lang/Object;", false);
    mv.visitVarInsn(ASTORE, 3);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, className, "db", "Lcom/centimia/orm/jaqu/Db;");
    mv.visitVarInsn(ALOAD, 2);
    mv.visitMethodInsn(INVOKEVIRTUAL, "com/centimia/orm/jaqu/Db", "from",
            "(Ljava/lang/Object;)Lcom/centimia/orm/jaqu/QueryInterface;", false);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEINTERFACE, "com/centimia/orm/jaqu/QueryInterface", "innerJoin",
            "(Ljava/lang/Object;)Lcom/centimia/orm/jaqu/QueryJoin;", true);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitFieldInsn(GETFIELD, className, fieldName, fieldSignature);
    mv.visitMethodInsn(INVOKEVIRTUAL, "com/centimia/orm/jaqu/QueryJoin", "on",
            "(Ljava/lang/Object;)Lcom/centimia/orm/jaqu/QueryJoinCondition;", false);
    mv.visitVarInsn(ALOAD, 2);
    mv.visitMethodInsn(INVOKEVIRTUAL, "com/centimia/orm/jaqu/QueryJoinCondition", "is",
            "(Ljava/lang/Object;)Lcom/centimia/orm/jaqu/QueryJoinWhere;", false);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, className, "db", "Lcom/centimia/orm/jaqu/Db;");
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEVIRTUAL, "com/centimia/orm/jaqu/Db", "getPrimaryKey",
            "(Ljava/lang/Object;)Ljava/lang/Object;", false);
    mv.visitMethodInsn(INVOKEVIRTUAL, "com/centimia/orm/jaqu/QueryJoinWhere", "where",
            "(Ljava/lang/Object;)Lcom/centimia/orm/jaqu/QueryCondition;", false);
    mv.visitVarInsn(ALOAD, 3);
    mv.visitMethodInsn(INVOKEVIRTUAL, "com/centimia/orm/jaqu/QueryCondition", "is",
            "(Ljava/lang/Object;)Lcom/centimia/orm/jaqu/QueryWhere;", false);
    mv.visitMethodInsn(INVOKEVIRTUAL, "com/centimia/orm/jaqu/QueryWhere", "selectFirst", "()Ljava/lang/Object;",
            false);
    mv.visitTypeInsn(CHECKCAST, fieldClassName);
    mv.visitFieldInsn(PUTFIELD, className, fieldName, fieldSignature);
    mv.visitLabel(l1);
    mv.visitJumpInsn(GOTO, l3);
    mv.visitLabel(l2);
    mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/Exception" });
    mv.visitVarInsn(ASTORE, 1);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitTypeInsn(INSTANCEOF, "java/lang/RuntimeException");
    Label l6 = new Label();
    mv.visitJumpInsn(IFEQ, l6);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitTypeInsn(CHECKCAST, "java/lang/RuntimeException");
    mv.visitInsn(ATHROW);
    mv.visitLabel(l6);
    mv.visitFrame(Opcodes.F_APPEND, 1, new Object[] { "java/lang/Exception" }, 0, null);
    mv.visitTypeInsn(NEW, "java/lang/RuntimeException");
    mv.visitInsn(DUP);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Exception", "getMessage", "()Ljava/lang/String;", false);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/RuntimeException", "<init>",
            "(Ljava/lang/String;Ljava/lang/Throwable;)V", false);
    mv.visitInsn(ATHROW);
    mv.visitLabel(l3);
    mv.visitFrame(Opcodes.F_CHOP, 1, null, 0, null);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKEVIRTUAL, className, newName, desc, false);
    mv.visitInsn(ARETURN);
    mv.visitMaxs(4, 4);
    mv.visitEnd();
}

From source file:com.gargoylesoftware.js.nashorn.internal.ir.debug.NashornTextifier.java

License:Open Source License

@Override
public void visitFrame(final int type, final int nLocal, final Object[] local, final int nStack,
        final Object[] stack) {
    final StringBuilder sb = new StringBuilder();
    sb.append("frame ");
    switch (type) {
    case Opcodes.F_NEW:
    case Opcodes.F_FULL:
        sb.append("full [");
        appendFrameTypes(sb, nLocal, local);
        sb.append("] [");
        appendFrameTypes(sb, nStack, stack);
        sb.append(']');
        break;//  ww w.  ja va2s. c  o  m
    case Opcodes.F_APPEND:
        sb.append("append [");
        appendFrameTypes(sb, nLocal, local);
        sb.append(']');
        break;
    case Opcodes.F_CHOP:
        sb.append("chop ").append(nLocal);
        break;
    case Opcodes.F_SAME:
        sb.append("same");
        break;
    case Opcodes.F_SAME1:
        sb.append("same1 ");
        appendFrameTypes(sb, 1, stack);
        break;
    default:
        assert false;
        break;
    }
    sb.append('\n');
    sb.append('\n');
    addText(sb);
}

From source file:com.github.fge.grappa.transform.CodeBlock.java

License:Apache License

/**
 * adds a compressed frame to the stack/*w w w  .  ja va2s.c  om*/
 *
 * @param stackArguments the argument types on the stack, represented as
 * "class path names" e.g java/lang/RuntimeException
 */
public CodeBlock frame_same(Object... stackArguments) {
    int type;

    switch (stackArguments.length) {
    case 0:
        type = Opcodes.F_SAME;
        break;
    case 1:
        type = Opcodes.F_SAME1;
        break;
    default:
        throw new IllegalArgumentException("same frame should have 0" + " or 1 arguments on stack");
    }

    instructionList.add(new FrameNode(type, 0, null, stackArguments.length, stackArguments));
    return this;
}