Example usage for org.objectweb.asm Opcodes AALOAD

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

Introduction

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

Prototype

int AALOAD

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

Click Source Link

Usage

From source file:com.offbynull.coroutines.instrumenter.PackStateGenerators.java

License:Open Source License

public static InsnList unpackLocalsStorageArrays(MarkerType markerType, Frame<BasicValue> frame,
        Variable containerVar, StorageVariables localsStorageVars) {
    Validate.notNull(markerType);/*  w  w  w .j a v  a  2  s . co m*/
    Validate.notNull(containerVar);
    Validate.notNull(localsStorageVars);

    Variable localsIntsVar = localsStorageVars.getIntStorageVar();
    Variable localsFloatsVar = localsStorageVars.getFloatStorageVar();
    Variable localsLongsVar = localsStorageVars.getLongStorageVar();
    Variable localsDoublesVar = localsStorageVars.getDoubleStorageVar();
    Variable localsObjectsVar = localsStorageVars.getObjectStorageVar();

    StorageSizes localsSizes = LocalsStateGenerators.computeSizes(frame);

    // Why are we using size > 0 vs checking to see if var != null?
    //
    // REMEMBER THAT the analyzer will determine the variable slots to create for storage array based on its scan of EVERY
    // continuation/suspend point in the method. Imagine the method that we're instrumenting is this...
    //
    // public void example(Continuation c, String arg1) {
    //     String var1 = "hi";
    //     c.suspend();     
    //
    //     System.out.println(var1);
    //     int var2 = 5;
    //     c.suspend();
    //
    //     System.out.println(var1 + var2);
    // }
    //
    // There are two continuation/suspend points. The analyzer determines that method will need to assign variable slots for
    // localsObjectsVar+localsIntsVar. All the other locals vars will be null.
    //
    // If we ended up using var != null instead of size > 0, things would mess up on the first suspend(). The only variable initialized
    // at the first suspend is var1. As such, LocalStateGenerator ONLY CREATES AN ARRAY FOR localsObjectsVar. It doesn't touch
    // localsIntsVar because, at the first suspend(), var2 is UNINITALIZED. Nothing has been set to that variable slot.
    //
    //
    // The same thing applies to the operand stack. It doesn't make sense to create arrays for operand stack types that don't exist yet
    // at a continuation point, even though they may exist at other continuation points furhter down

    // Storage arrays from locals container
    return merge(
            debugMarker(markerType, "Unpacking storage arrays for locals and operand stack from an Object[]"),
            mergeIf(localsSizes.getIntsSize() > 0,
                    () -> new Object[] { debugMarker(markerType, "Getting locals ints from to container"),
                            new VarInsnNode(Opcodes.ALOAD, containerVar.getIndex()), // [Object[]]
                            new LdcInsnNode(0), // [Object[], 0]
                            new InsnNode(Opcodes.AALOAD), // [val]
                            new TypeInsnNode(Opcodes.CHECKCAST, localsIntsVar.getType().getInternalName()), // [val] REQ BY JVM SO TYPE IS KNOWN
                            new VarInsnNode(Opcodes.ASTORE, localsIntsVar.getIndex()), // []
                    }),
            mergeIf(localsSizes.getFloatsSize() > 0,
                    () -> new Object[] { debugMarker(markerType, "Getting locals floats from to container"),
                            new VarInsnNode(Opcodes.ALOAD, containerVar.getIndex()), // [Object[]]
                            new LdcInsnNode(1), // [Object[], 1]
                            new InsnNode(Opcodes.AALOAD), // [val]
                            new TypeInsnNode(Opcodes.CHECKCAST, localsFloatsVar.getType().getInternalName()), // [val] REQ BY JVM SO TYPE IS KNOWN
                            new VarInsnNode(Opcodes.ASTORE, localsFloatsVar.getIndex()), // []
                    }),
            mergeIf(localsSizes.getLongsSize() > 0,
                    () -> new Object[] { debugMarker(markerType, "Getting locals longs from to container"),
                            new VarInsnNode(Opcodes.ALOAD, containerVar.getIndex()), // [Object[]]
                            new LdcInsnNode(2), // [Object[], 2]
                            new InsnNode(Opcodes.AALOAD), // [val]
                            new TypeInsnNode(Opcodes.CHECKCAST, localsLongsVar.getType().getInternalName()), // [val] REQ BY JVM SO TYPE IS KNOWN
                            new VarInsnNode(Opcodes.ASTORE, localsLongsVar.getIndex()), // []
                    }),
            mergeIf(localsSizes.getDoublesSize() > 0,
                    () -> new Object[] { debugMarker(markerType, "Getting locals doubles from to container"),
                            new VarInsnNode(Opcodes.ALOAD, containerVar.getIndex()), // [Object[]]
                            new LdcInsnNode(3), // [Object[], 3]
                            new InsnNode(Opcodes.AALOAD), // [val]
                            new TypeInsnNode(Opcodes.CHECKCAST, localsDoublesVar.getType().getInternalName()), // [val] REQ BY JVM SO TYPE IS KNOWN
                            new VarInsnNode(Opcodes.ASTORE, localsDoublesVar.getIndex()), // []
                    }),
            mergeIf(localsSizes.getObjectsSize() > 0,
                    () -> new Object[] { debugMarker(markerType, "Getting locals objects from to container"),
                            new VarInsnNode(Opcodes.ALOAD, containerVar.getIndex()), // [Object[]]
                            new LdcInsnNode(4), // [Object[], 4]
                            new InsnNode(Opcodes.AALOAD), // [val]
                            new TypeInsnNode(Opcodes.CHECKCAST, localsObjectsVar.getType().getInternalName()), // [val] REQ BY JVM SO TYPE IS KNOWN
                            new VarInsnNode(Opcodes.ASTORE, localsObjectsVar.getIndex()), // []
                    }));
}

From source file:com.offbynull.coroutines.instrumenter.PackStateGenerators.java

License:Open Source License

public static InsnList unpackOperandStackStorageArrays(MarkerType markerType, Frame<BasicValue> frame,
        Variable containerVar, StorageVariables operandStackStorageVars) {
    Validate.notNull(markerType);//from w ww .  ja  v a 2s.c o m
    Validate.notNull(containerVar);
    Validate.notNull(operandStackStorageVars);

    Variable stackIntsVar = operandStackStorageVars.getIntStorageVar();
    Variable stackFloatsVar = operandStackStorageVars.getFloatStorageVar();
    Variable stackLongsVar = operandStackStorageVars.getLongStorageVar();
    Variable stackDoublesVar = operandStackStorageVars.getDoubleStorageVar();
    Variable stackObjectsVar = operandStackStorageVars.getObjectStorageVar();

    StorageSizes stackSizes = OperandStackStateGenerators.computeSizes(frame, 0, frame.getStackSize());

    // Why are we using size > 0 vs checking to see if var != null?
    //
    // REMEMBER THAT the analyzer will determine the variable slots to create for storage array based on its scan of EVERY
    // continuation/suspend point in the method. Imagine the method that we're instrumenting is this...
    //
    // public void example(Continuation c, String arg1) {
    //     String var1 = "hi";
    //     c.suspend();     
    //
    //     System.out.println(var1);
    //     int var2 = 5;
    //     c.suspend();
    //
    //     System.out.println(var1 + var2);
    // }
    //
    // There are two continuation/suspend points. The analyzer determines that method will need to assign variable slots for
    // localsObjectsVar+localsIntsVar. All the other locals vars will be null.
    //
    // If we ended up using var != null instead of size > 0, things would mess up on the first suspend(). The only variable initialized
    // at the first suspend is var1. As such, LocalStateGenerator ONLY CREATES AN ARRAY FOR localsObjectsVar. It doesn't touch
    // localsIntsVar because, at the first suspend(), var2 is UNINITALIZED. Nothing has been set to that variable slot.
    //
    //
    // The same thing applies to the operand stack. It doesn't make sense to create arrays for operand stack types that don't exist yet
    // at a continuation point, even though they may exist at other continuation points furhter down

    // Storage arrays from locals container
    return merge(debugMarker(markerType, "Unpacking storage arrays for operand stack from an Object[]"),
            mergeIf(stackSizes.getIntsSize() > 0,
                    () -> new Object[] { debugMarker(markerType, "Getting stack ints from to container"),
                            new VarInsnNode(Opcodes.ALOAD, containerVar.getIndex()), // [Object[]]
                            new LdcInsnNode(5), // [Object[], 5]
                            new InsnNode(Opcodes.AALOAD), // [val]
                            new TypeInsnNode(Opcodes.CHECKCAST, stackIntsVar.getType().getInternalName()), // [val] REQ BY JVM SO TYPE IS KNOWN
                            new VarInsnNode(Opcodes.ASTORE, stackIntsVar.getIndex()), // []
                    }),
            mergeIf(stackSizes.getFloatsSize() > 0,
                    () -> new Object[] { debugMarker(markerType, "Getting stack floats from to container"),
                            new VarInsnNode(Opcodes.ALOAD, containerVar.getIndex()), // [Object[]]
                            new LdcInsnNode(6), // [Object[], 6]
                            new InsnNode(Opcodes.AALOAD), // [val]
                            new TypeInsnNode(Opcodes.CHECKCAST, stackFloatsVar.getType().getInternalName()), // [val] REQ BY JVM SO TYPE IS KNOWN
                            new VarInsnNode(Opcodes.ASTORE, stackFloatsVar.getIndex()), // []
                    }),
            mergeIf(stackSizes.getLongsSize() > 0,
                    () -> new Object[] { debugMarker(markerType, "Getting stack longs from to container"),
                            new VarInsnNode(Opcodes.ALOAD, containerVar.getIndex()), // [Object[]]
                            new LdcInsnNode(7), // [Object[], 7]
                            new InsnNode(Opcodes.AALOAD), // [val]
                            new TypeInsnNode(Opcodes.CHECKCAST, stackLongsVar.getType().getInternalName()), // [val] REQ BY JVM SO TYPE IS KNOWN
                            new VarInsnNode(Opcodes.ASTORE, stackLongsVar.getIndex()), // []
                    }),
            mergeIf(stackSizes.getDoublesSize() > 0,
                    () -> new Object[] { debugMarker(markerType, "Getting stack doubles from to container"),
                            new VarInsnNode(Opcodes.ALOAD, containerVar.getIndex()), // [Object[]]
                            new LdcInsnNode(8), // [Object[], 8]
                            new InsnNode(Opcodes.AALOAD), // [val]
                            new TypeInsnNode(Opcodes.CHECKCAST, stackDoublesVar.getType().getInternalName()), // [val] REQ BY JVM SO TYPE IS KNOWN
                            new VarInsnNode(Opcodes.ASTORE, stackDoublesVar.getIndex()), // []
                    }),
            mergeIf(stackSizes.getObjectsSize() > 0,
                    () -> new Object[] { debugMarker(markerType, "Getting stack objects from to container"),
                            new VarInsnNode(Opcodes.ALOAD, containerVar.getIndex()), // [Object[]]
                            new LdcInsnNode(9), // [Object[], 9]
                            new InsnNode(Opcodes.AALOAD), // [val]
                            new TypeInsnNode(Opcodes.CHECKCAST, stackObjectsVar.getType().getInternalName()), // [val] REQ BY JVM SO TYPE IS KNOWN
                            new VarInsnNode(Opcodes.ASTORE, stackObjectsVar.getIndex()), // []
                    }));
}

From source file:com.trigersoft.jaque.expression.ExpressionMethodVisitor.java

License:Apache License

@Override
public void visitInsn(int opcode) {
    Expression e;//from   w ww. j  av a  2  s  .c om
    Expression first;
    Expression second;
    switch (opcode) {
    case Opcodes.ARRAYLENGTH:
        e = Expression.arrayLength(_exprStack.pop());
        break;
    case Opcodes.ACONST_NULL:
        e = Expression.constant(null, Object.class);
        break;
    case Opcodes.IALOAD:
    case Opcodes.LALOAD:
    case Opcodes.FALOAD:
    case Opcodes.DALOAD:
    case Opcodes.AALOAD:
    case Opcodes.BALOAD:
    case Opcodes.CALOAD:
    case Opcodes.SALOAD:
        first = _exprStack.pop();
        second = _exprStack.pop();
        e = Expression.arrayIndex(second, first);
        break;
    case Opcodes.DCONST_0:
        e = Expression.constant(0d, Double.TYPE);
        break;
    case Opcodes.DCONST_1:
        e = Expression.constant(1d, Double.TYPE);
        break;
    case Opcodes.FCMPG:
    case Opcodes.FCMPL:
    case Opcodes.DCMPG:
    case Opcodes.DCMPL:
    case Opcodes.LCMP:
        first = _exprStack.pop();
        second = _exprStack.pop();
        e = Expression.subtract(second, first);
        break;
    case Opcodes.FCONST_0:
        e = Expression.constant(0f, Float.TYPE);
        break;
    case Opcodes.FCONST_1:
        e = Expression.constant(1f, Float.TYPE);
        break;
    case Opcodes.FCONST_2:
        e = Expression.constant(2f, Float.TYPE);
        break;
    case Opcodes.ICONST_M1:
        e = Expression.constant(-1, Integer.TYPE);
        break;
    case Opcodes.ICONST_0:
        e = Expression.constant(0, Integer.TYPE);
        break;
    case Opcodes.ICONST_1:
        e = Expression.constant(1, Integer.TYPE);
        break;
    case Opcodes.ICONST_2:
        e = Expression.constant(2, Integer.TYPE);
        break;
    case Opcodes.ICONST_3:
        e = Expression.constant(3, Integer.TYPE);
        break;
    case Opcodes.ICONST_4:
        e = Expression.constant(4, Integer.TYPE);
        break;
    case Opcodes.ICONST_5:
        e = Expression.constant(5, Integer.TYPE);
        break;
    case Opcodes.LCONST_0:
        e = Expression.constant(0l, Long.TYPE);
        break;
    case Opcodes.LCONST_1:
        e = Expression.constant(1l, Long.TYPE);
        break;
    case Opcodes.IADD:
    case Opcodes.LADD:
    case Opcodes.FADD:
    case Opcodes.DADD:
        first = _exprStack.pop();
        second = _exprStack.pop();
        e = Expression.add(second, first);
        break;
    case Opcodes.ISUB:
    case Opcodes.LSUB:
    case Opcodes.FSUB:
    case Opcodes.DSUB:
        first = _exprStack.pop();
        second = _exprStack.pop();
        e = Expression.subtract(second, first);
        break;
    case Opcodes.IMUL:
    case Opcodes.LMUL:
    case Opcodes.FMUL:
    case Opcodes.DMUL:
        first = _exprStack.pop();
        second = _exprStack.pop();
        e = Expression.multiply(second, first);
        break;
    case Opcodes.IDIV:
    case Opcodes.LDIV:
    case Opcodes.FDIV:
    case Opcodes.DDIV:
        first = _exprStack.pop();
        second = _exprStack.pop();
        e = Expression.divide(second, first);
        break;
    case Opcodes.IREM:
    case Opcodes.LREM:
    case Opcodes.FREM:
    case Opcodes.DREM:
        first = _exprStack.pop();
        second = _exprStack.pop();
        e = Expression.modulo(second, first);
        break;
    case Opcodes.INEG:
    case Opcodes.LNEG:
    case Opcodes.FNEG:
    case Opcodes.DNEG:
        first = _exprStack.pop();
        e = Expression.negate(first);
        break;
    case Opcodes.ISHL:
    case Opcodes.LSHL:
        first = _exprStack.pop();
        second = _exprStack.pop();
        e = Expression.leftShift(second, first);
        break;
    case Opcodes.ISHR:
    case Opcodes.LSHR:
        first = _exprStack.pop();
        second = _exprStack.pop();
        e = Expression.rightShift(second, first);
        break;
    case Opcodes.IUSHR:
    case Opcodes.LUSHR:
        first = _exprStack.pop();
        second = _exprStack.pop();
        e = Expression.rightShift(second, first);
        break;
    case Opcodes.IAND:
    case Opcodes.LAND:
        first = _exprStack.pop();
        second = _exprStack.pop();
        e = Expression.bitwiseAnd(second, first);
        break;
    case Opcodes.IOR:
    case Opcodes.LOR:
        first = _exprStack.pop();
        second = _exprStack.pop();
        e = Expression.bitwiseOr(second, first);
        break;
    case Opcodes.IXOR:
    case Opcodes.LXOR:
        first = _exprStack.pop();
        second = _exprStack.pop();
        e = Expression.exclusiveOr(second, first);
        break;
    case Opcodes.I2B:
    case Opcodes.I2C:
    case Opcodes.I2S:
        first = _exprStack.pop();
        e = Expression.convert(first, NumericTypeLookup2[opcode - Opcodes.I2B]);
        break;
    case Opcodes.I2L:
    case Opcodes.I2F:
    case Opcodes.I2D:
        first = _exprStack.pop();
        e = Expression.convert(first, NumericTypeLookup[opcode - Opcodes.I2L + 1]);
        break;
    case Opcodes.L2I:
    case Opcodes.L2F:
    case Opcodes.L2D:
        int l2l = opcode > Opcodes.L2I ? 1 : 0;
        first = _exprStack.pop();
        e = Expression.convert(first, NumericTypeLookup[opcode - Opcodes.L2I + l2l]);
        break;
    case Opcodes.F2I:
    case Opcodes.F2L:
    case Opcodes.F2D:
        int f2f = opcode == Opcodes.F2D ? 1 : 0;
        first = _exprStack.pop();
        e = Expression.convert(first, NumericTypeLookup[opcode - Opcodes.F2I + f2f]);
        break;
    case Opcodes.D2I:
    case Opcodes.D2L:
    case Opcodes.D2F:
        first = _exprStack.pop();
        e = Expression.convert(first, NumericTypeLookup[opcode - Opcodes.D2I]);
        break;
    case Opcodes.IRETURN:
    case Opcodes.LRETURN:
    case Opcodes.FRETURN:
    case Opcodes.DRETURN:
    case Opcodes.ARETURN:

        go(null);

        return;
    case Opcodes.SWAP:
        first = _exprStack.pop();
        second = _exprStack.pop();
        _exprStack.push(first);
        _exprStack.push(second);
    case Opcodes.DUP:
    case Opcodes.DUP_X1:
    case Opcodes.DUP_X2:
    case Opcodes.DUP2:
    case Opcodes.DUP2_X1:
    case Opcodes.DUP2_X2:
        // our stack is not divided to words
        int base = (opcode - Opcodes.DUP) % 3;
        base++;
        dup(_exprStack, base, base - 1);
        return;
    case Opcodes.NOP:
        return;
    case Opcodes.RETURN:
    default:
        throw notLambda(opcode);
    }

    _exprStack.push(e);
}

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

License:Apache License

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

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

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

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

            };
        }

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

From source file:de.kandid.model.Emitter.java

License:Apache License

/**
 * Assembles a class that implements the given interface by generating the byte code.
 * @param interfaze the interface to implement
 * @return the class//from w  ww  .  j  av  a  2  s .co m
 */
private static Class<? extends Emitter<?>> makeClass(Class<?> interfaze) {
    String nameClass = _nameEmitter + '$' + interfaze.getName().replace('.', '$');
    String nameInterface = Type.getInternalName(interfaze);
    // ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
    ClassWriter cw = new ClassWriter(0);
    cw.visit(V1_4, ACC_PUBLIC + ACC_SUPER, nameClass, null, _nameEmitter, new String[] { name(interfaze) });

    // Generate default construcotor
    MethodVisitor cv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
    cv.visitVarInsn(ALOAD, 0);
    cv.visitMethodInsn(INVOKESPECIAL, _nameEmitter, "<init>", "()V");
    cv.visitInsn(RETURN);
    cv.visitMaxs(1, 1);

    // Generate methods
    Method[] methods = interfaze.getMethods();
    for (int i = 0; i < methods.length; ++i) {
        final Method m = methods[i];
        if (m.getReturnType() != void.class)
            throw new IllegalArgumentException("Method " + m.toGenericString() + " must not return a value");
        final String descMethod = Type.getMethodDescriptor(m);
        final MethodVisitor mw = cw.visitMethod(ACC_PUBLIC + ACC_SYNCHRONIZED, m.getName(), descMethod, null,
                null);
        final Type[] argTypes = Type.getArgumentTypes(m);

        // for (int i = 0; i < _end; i += 2)
        //    ((Listener) _listeners[i]).send(...);

        int localStart = 1; // give one for "this"
        for (Type at : argTypes)
            localStart += at.getSize();
        Label entry = new Label();
        Label exit = new Label();
        mw.visitLabel(entry);

        // _isFiring = true;
        mw.visitVarInsn(ALOAD, 0);
        mw.visitInsn(Opcodes.ICONST_1);
        mw.visitFieldInsn(Opcodes.PUTFIELD, nameClass, "_isFiring", Type.BOOLEAN_TYPE.getDescriptor());

        // setup local variables: i, _listeners, _end
        mw.visitLocalVariable("i", Type.INT_TYPE.getDescriptor(), null, entry, exit, localStart);
        mw.visitInsn(Opcodes.ICONST_0);
        mw.visitIntInsn(Opcodes.ISTORE, localStart);

        mw.visitLocalVariable("listeners", _descObjectArray, null, entry, exit, localStart + 1);
        mw.visitVarInsn(ALOAD, 0);
        mw.visitFieldInsn(GETFIELD, nameClass, "_listeners", _descObjectArray);
        mw.visitIntInsn(Opcodes.ASTORE, localStart + 1);

        mw.visitLocalVariable("end", Type.INT_TYPE.getDescriptor(), null, entry, exit, localStart + 2);
        mw.visitVarInsn(ALOAD, 0);
        mw.visitFieldInsn(GETFIELD, nameClass, "_end", Type.INT_TYPE.getDescriptor());
        mw.visitIntInsn(Opcodes.ISTORE, localStart + 2);

        final Label condition = new Label();
        mw.visitJumpInsn(GOTO, condition);

        final Label loop = new Label();
        mw.visitLabel(loop);

        //((Listener) _listeners[i]).doSomething()
        mw.visitIntInsn(Opcodes.ALOAD, localStart + 1);
        mw.visitIntInsn(Opcodes.ILOAD, localStart);
        mw.visitInsn(Opcodes.AALOAD);
        mw.visitTypeInsn(CHECKCAST, nameInterface);
        int offs = 1; // give one for "this"
        for (Type at : argTypes) {
            mw.visitVarInsn(at.getOpcode(ILOAD), offs);
            offs += at.getSize();
        }
        mw.visitMethodInsn(INVOKEINTERFACE, nameInterface, m.getName(), descMethod);

        // i += 2
        mw.visitIincInsn(localStart, 2);

        // if (i < end) goto loop
        mw.visitLabel(condition);
        mw.visitIntInsn(Opcodes.ILOAD, localStart);
        mw.visitIntInsn(Opcodes.ILOAD, localStart + 2);
        mw.visitJumpInsn(Opcodes.IF_ICMPLT, loop);

        // _isFiring = false;
        mw.visitVarInsn(ALOAD, 0);
        mw.visitInsn(Opcodes.ICONST_0);
        mw.visitFieldInsn(Opcodes.PUTFIELD, nameClass, "_isFiring", Type.BOOLEAN_TYPE.getDescriptor());

        mw.visitLabel(exit);
        mw.visitInsn(RETURN);
        mw.visitMaxs(localStart + 2, localStart + 3);
        mw.visitEnd();
    }
    cw.visitEnd();
    return _loader.loadClass(cw, nameClass.replace('/', '.'));
}

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

License:Creative Commons License

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

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

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

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

    method.instructions.insertBefore(insertPt, newInstr);

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

From source file:de.tuberlin.uebb.jbop.optimizer.array.AbstractLocalArrayOptimizer.java

License:Open Source License

/**
 * Register get array./*from ww w.j  av a 2s .  c  om*/
 * 
 * @param currentNode
 *          the current node
 * @param knownArrays
 *          the known arrays
 * @return true, if successful
 * @throws JBOPClassException
 *           the jBOP class exception
 */
protected int registerGetArray(final AbstractInsnNode currentNode, final Map<Integer, Object> knownArrays)
        throws JBOPClassException {
    final List<AbstractInsnNode> previous = new ArrayList<>();
    final List<AbstractInsnNode> previous2 = new ArrayList<>();
    AbstractInsnNode previous2x = currentNode;
    while (true) {
        final AbstractInsnNode previousx = NodeHelper.getPrevious(previous2x);
        if (previousx.getOpcode() != Opcodes.AALOAD) {
            knownArrays.remove(Integer.valueOf(NodeHelper.getVarIndex(currentNode)));
            return 0;
        }
        previous.add(previousx);
        previous2x = NodeHelper.getPrevious(previousx);
        if (!NodeHelper.isNumberNode(previous2x)) {
            knownArrays.remove(Integer.valueOf(NodeHelper.getVarIndex(currentNode)));
            return 0;
        }
        previous2.add(previous2x);
        final AbstractInsnNode previous2xtmp = NodeHelper.getPrevious(previous2x);
        if ((previous2xtmp instanceof FieldInsnNode) || NodeHelper.isAload(previous2xtmp)) {
            break;
        }
    }
    final AbstractInsnNode previous3 = NodeHelper.getPrevious(previous2.get(previous2.size() - 1));
    Object array;
    if (previous3 instanceof VarInsnNode) {
        array = knownArrays.get(Integer.valueOf(((VarInsnNode) previous3).var));
        if (array == null) {
            knownArrays.remove(Integer.valueOf(NodeHelper.getVarIndex(currentNode)));
            return 0;
        }
    } else {
        if (!(previous3 instanceof FieldInsnNode)) {
            knownArrays.remove(Integer.valueOf(NodeHelper.getVarIndex(currentNode)));
            return 0;
        }
        final AbstractInsnNode previous4 = NodeHelper.getPrevious(previous3);
        if (!NodeHelper.isAload0(previous4)) {
            knownArrays.remove(Integer.valueOf(NodeHelper.getVarIndex(currentNode)));
            return 0;
        }
        final String fieldName = ((FieldInsnNode) previous3).name;

        array = ClassAccessor.getCurrentValue(input, fieldName);
    }
    final Integer varIndex = Integer.valueOf(((VarInsnNode) currentNode).var);

    for (int i = previous2.size() - 1; i >= 0; i--) {
        int index1;
        if (previous2.size() <= i) {
            index1 = 0;
        } else {
            final Number arrIndex = NodeHelper.getNumberValue(previous2.get(i));
            index1 = arrIndex.intValue();
        }
        array = Array.get(array, index1);
    }
    knownArrays.put(varIndex, array);
    return 1;
}

From source file:de.tuberlin.uebb.jbop.optimizer.array.LocalArrayLengthInlinerTest.java

License:Open Source License

/**
 * Tests that LocalArrayLengthInliner is working correctly.
 * //from w w w . ja v a  2s  .c  o m
 * @throws Exception
 *           the exception
 */
@Test
public void test_newarrayAndSubarray() throws Exception {
    // INIT
    final String owner = "de.tuberlin.uebb.jbop.optimizer.array.LocalArrayLengthTestClass";
    final ClassNodeBuilder builder = ClassNodeBuilder.createClass(owner).//
            addField("doubleArray", "[[D").initArray(15, 42)
            .withModifiers(Opcodes.ACC_PRIVATE, Opcodes.ACC_FINAL).//
            addMethod("getArrayLength", "()I").withAnnotation(Optimizable.class).//
            addArray("[D", 15).// 3 -> 3
            addInsn(new VarInsnNode(Opcodes.ALOAD, 1)).// 1 -> 0|
            addInsn(new InsnNode(Opcodes.ARRAYLENGTH)).// 1 -> 0|1
            addGetClassField("doubleArray").// 2 -> 2
            addInsn(new InsnNode(Opcodes.ICONST_0)).// 1 -> 1
            addInsn(new InsnNode(Opcodes.AALOAD)).// 1 -> 1
            addInsn(new VarInsnNode(Opcodes.ASTORE, 2)).// 1 -> 1
            addInsn(new VarInsnNode(Opcodes.ALOAD, 2)).// 1 -> 0|
            addInsn(new InsnNode(Opcodes.ARRAYLENGTH)).// 1 -> 0|1
            addInsn(new InsnNode(Opcodes.IADD)).// 1 -> 1
            addInsn(new InsnNode(Opcodes.IRETURN));// 1 -> 1
    // 14 -> 12
    final LocalArrayLengthInliner inliner = new LocalArrayLengthInliner();
    inliner.setInputObject(builder.toClass().instance());

    // RUN STEP 1
    final MethodNode method = builder.getMethod("getArrayLength");
    assertEquals(14, method.instructions.size());
    final InsnList optimized = inliner.optimize(method.instructions, method);
    method.instructions = optimized;

    // ASSERT STEP 1
    assertEquals(12, optimized.size());
    assertEquals(15, NodeHelper.getNumberValue(optimized.get(3)).intValue());
    assertEquals(42, NodeHelper.getNumberValue(optimized.get(9)).intValue());

    // RUN STEP 2
    final InsnList optimized2 = inliner.optimize(method.instructions, method);

    // ASSERT STEP 2
    assertEquals(12, optimized2.size());
}

From source file:de.tuberlin.uebb.jbop.optimizer.array.LocalArrayValueInlinerTest.java

License:Open Source License

/**
 * Tests that LocalArrayValueInliner is working correctly.
 * //from   ww w.  ja  v  a  2  s.  c o m
 * @throws Exception
 *           the exception
 */
@Test
public void testLocalArrayValueInliner() throws Exception {
    // INIT
    final String owner = "de.tuberlin.uebb.jbop.optimizer.array.LocalArrayValueTestClass";
    final ClassNodeBuilder builder = ClassNodeBuilder.createClass(owner).//
            addField("doubleArray", "[[D").//
            withAnnotation(ImmutableArray.class).//
            withModifiers(Opcodes.ACC_PRIVATE, Opcodes.ACC_FINAL).//
            initArray(2, 2).//
            initMultiArrayWith(1.0, 0, 0).//
            initMultiArrayWith(2.0, 0, 1).//
            initMultiArrayWith(3.0, 1, 0).//
            initMultiArrayWith(4.0, 1, 1).//
            addMethod("getArrayValue", "()D").withAnnotation(Optimizable.class).//
            addGetClassField("doubleArray").// 2 -> 2
            addInsn(new InsnNode(Opcodes.ICONST_0)).// 1 -> 1
            addInsn(new InsnNode(Opcodes.AALOAD)).// 1 -> 1
            addInsn(new VarInsnNode(Opcodes.ASTORE, 2)).// 1 -> 1
            addInsn(new VarInsnNode(Opcodes.ALOAD, 2)).// 1 -> 0|
            addInsn(new InsnNode(Opcodes.ICONST_1)).// 1 -> 0|
            addInsn(new InsnNode(Opcodes.DALOAD)).// 1 -> 0| 1
            addInsn(new InsnNode(Opcodes.DRETURN));// 1 -> 1
    // 14 -> 12
    final LocalArrayValueInliner inliner = new LocalArrayValueInliner();
    inliner.setInputObject(builder.toClass().instance());

    // RUN STEP 1
    final MethodNode method = builder.getMethod("getArrayValue");
    assertEquals(9, method.instructions.size());
    final InsnList optimized = inliner.optimize(method.instructions, method);

    // ASSERT STEP 1

    assertEquals(7, optimized.size());
    assertEquals(2.0, NodeHelper.getNumberValue(optimized.get(5)).doubleValue(), .0001);

    // RUN STEP 2
    final InsnList optimized2 = inliner.optimize(method.instructions, method);

    // ASSERT STEP 2
    assertEquals(7, optimized2.size());
}

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

License:Open Source License

/**
 * Inits the multi array with./*from   w  w w. j  av  a2  s.  c o m*/
 * 
 * @param value
 *          the value
 * @param indexes
 *          the indexes
 * @return the class node builder
 */
public ClassNodeBuilder initMultiArrayWith(final Object value, final int... indexes) {
    if (isInterface) {
        return this;
    }
    final InsnList list = new InsnList();
    list.add(new VarInsnNode(Opcodes.ALOAD, 0));
    list.add(new FieldInsnNode(Opcodes.GETFIELD, classNode.name, lastField.name, lastField.desc));
    for (int i = 0; i < indexes.length - 1; ++i) {
        list.add(NodeHelper.getInsnNodeFor(indexes[i]));
        list.add(new InsnNode(Opcodes.AALOAD));
    }
    list.add(NodeHelper.getInsnNodeFor(indexes[indexes.length - 1]));
    list.add(NodeHelper.getInsnNodeFor(value));
    final Type elementType = Type.getType(lastField.desc).getElementType();
    if (elementType.getDescriptor().startsWith("L")) {
        list.add(new InsnNode(Opcodes.AASTORE));
    } else {
        list.add(new InsnNode(toPrimitive(Type.getType(value.getClass())).getOpcode(Opcodes.IASTORE)));

    }
    addToConstructor(list);
    return this;
}