Example usage for org.objectweb.asm Opcodes ICONST_0

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

Introduction

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

Prototype

int ICONST_0

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

Click Source Link

Usage

From source file:com.github.malamut2.low.AllocationMethodAdapter.java

License:Apache License

void calculateArrayLengthAndDispatch(String typeName, int dimCount) {
    // Since the dimensions of the array are not known at instrumentation
    // time, we take the created multi-dimensional array and peel off nesting
    // levels from the left.  For each nesting layer we probe the array length
    // and accumulate a partial product which we can then feed the recording
    // function.//from   ww  w. j a v a 2 s . com

    // below we note the partial product of dimensions 1 to X-1 as productToX
    // (so productTo1 == 1 == no dimensions yet).  We denote by aref0 the
    // array reference at the current nesting level (the containing aref's [0]
    // element).  If we hit a level whose arraylength is 0 there's no point
    // continuing so we shortcut out.
    Label zeroDimension = new Label();
    super.visitInsn(Opcodes.DUP); // -> stack: ... origaref aref0
    super.visitLdcInsn(1); // -> stack: ... origaref aref0 productTo1
    for (int i = 0; i < dimCount; ++i) {
        // pre: stack: ... origaref aref0 productToI
        super.visitInsn(Opcodes.SWAP); // -> stack: ... origaref productToI aref
        super.visitInsn(Opcodes.DUP_X1);
        // -> stack: ... origaref aref0 productToI aref
        super.visitInsn(Opcodes.ARRAYLENGTH);
        // -> stack: ... origaref aref0 productToI dimI

        Label nonZeroDimension = new Label();
        super.visitInsn(Opcodes.DUP);
        // -> stack: ... origaref aref0 productToI dimI dimI
        super.visitJumpInsn(Opcodes.IFNE, nonZeroDimension);
        // -> stack: ... origaref aref0 productToI dimI
        super.visitInsn(Opcodes.POP);
        // -> stack: ... origaref aref0 productToI
        super.visitJumpInsn(Opcodes.GOTO, zeroDimension);
        super.visitLabel(nonZeroDimension);
        // -> stack: ... origaref aref0 productToI max(dimI,1)

        super.visitInsn(Opcodes.IMUL);
        // -> stack: ... origaref aref0 productTo{I+1}
        if (i < dimCount - 1) {
            super.visitInsn(Opcodes.SWAP);
            // -> stack: ... origaref productTo{I+1} aref0
            super.visitInsn(Opcodes.ICONST_0);
            // -> stack: ... origaref productTo{I+1} aref0 0
            super.visitInsn(Opcodes.AALOAD);
            // -> stack: ... origaref productTo{I+1} aref0'
            super.visitInsn(Opcodes.SWAP);
        }
        // post: stack: ... origaref aref0 productTo{I+1}
    }
    super.visitLabel(zeroDimension);

    super.visitInsn(Opcodes.SWAP); // -> stack: ... origaref product aref0
    super.visitInsn(Opcodes.POP); // -> stack: ... origaref product
    super.visitInsn(Opcodes.SWAP); // -> stack: ... product origaref
    invokeRecordAllocation(typeName);
}

From source file:com.github.wreulicke.bean.validation.ASMMethodParameterValidationInjector.java

License:Open Source License

private void inject(MethodNode node, ClassNode clazzNode) {
    if (Modifier.isStatic(node.access) || Modifier.isAbstract(node.access) || Modifier.isAbstract(node.access)
            || "<init>".equals(node.name) || "<clinit>".equals(node.name)) {
        return;/*from   w w  w  . java2 s. c  om*/
    }

    InsnList list = new InsnList();
    int index = node.localVariables.size();
    list.add(new MethodInsnNode(INVOKESTATIC, "javax/validation/Validation", "buildDefaultValidatorFactory",
            "()Ljavax/validation/ValidatorFactory;", false));
    list.add(new MethodInsnNode(INVOKEINTERFACE, "javax/validation/ValidatorFactory", "getValidator",
            "()Ljavax/validation/Validator;", true));
    list.add(new MethodInsnNode(INVOKEINTERFACE, "javax/validation/Validator", "forExecutables",
            "()Ljavax/validation/executable/ExecutableValidator;", true));

    list.add(new VarInsnNode(Opcodes.ASTORE, index));
    list.add(new VarInsnNode(Opcodes.ALOAD, index));
    list.add(new VarInsnNode(Opcodes.ALOAD, 0));

    list.add(new LdcInsnNode(Type.getType("L" + clazzNode.name + ";")));
    list.add(new LdcInsnNode(node.name));

    @SuppressWarnings("unchecked")
    List<LocalVariableNode> variables = node.localVariables;
    Type[] args = Type.getArgumentTypes(node.desc);

    list.add(new LdcInsnNode(args.length));
    list.add(new TypeInsnNode(Opcodes.ANEWARRAY, "java/lang/Class"));

    for (int i = 0; i < args.length; i++) {
        int paramIndex = 1 + i;
        list.add(new InsnNode(Opcodes.DUP));
        list.add(new LdcInsnNode(i));
        list.add(new LdcInsnNode(Type.getType(variables.get(paramIndex).desc)));
        list.add(new InsnNode(Opcodes.AASTORE));
    }

    list.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Class", "getMethod",
            "(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method;", false));

    list.add(new LdcInsnNode(args.length));
    list.add(new TypeInsnNode(Opcodes.ANEWARRAY, "java/lang/Object"));
    for (int i = 0; i < args.length; i++) {
        int paramIndex = i + 1;
        list.add(new InsnNode(Opcodes.DUP));
        list.add(new LdcInsnNode(i));
        list.add(new VarInsnNode(Opcodes.ALOAD, paramIndex));
        list.add(new InsnNode(Opcodes.AASTORE));
    }
    list.add(new InsnNode(Opcodes.ICONST_0));
    list.add(new TypeInsnNode(Opcodes.ANEWARRAY, "java/lang/Class"));
    list.add(new MethodInsnNode(INVOKEINTERFACE, "javax/validation/executable/ExecutableValidator",
            "validateParameters",
            "(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;[Ljava/lang/Class;)Ljava/util/Set;",
            true));
    list.add(new MethodInsnNode(INVOKESTATIC, "com/github/wreulicke/bean/validation/Constraints",
            "throwIfNeeded", "(Ljava/util/Set;)V", false));
    node.instructions.insert(list);
}

From source file:com.google.devtools.build.android.desugar.BytecodeTypeInference.java

License:Open Source License

@Override
public void visitInsn(int opcode) {
    switch (opcode) {
    case Opcodes.NOP:
    case Opcodes.INEG:
    case Opcodes.LNEG:
    case Opcodes.FNEG:
    case Opcodes.DNEG:
    case Opcodes.I2B:
    case Opcodes.I2C:
    case Opcodes.I2S:
    case Opcodes.RETURN:
        break;// w  ww. j a  va2  s .c  om
    case Opcodes.ACONST_NULL:
        push(InferredType.NULL);
        break;
    case Opcodes.ICONST_M1:
    case Opcodes.ICONST_0:
    case Opcodes.ICONST_1:
    case Opcodes.ICONST_2:
    case Opcodes.ICONST_3:
    case Opcodes.ICONST_4:
    case Opcodes.ICONST_5:
        push(InferredType.INT);
        break;
    case Opcodes.LCONST_0:
    case Opcodes.LCONST_1:
        push(InferredType.LONG);
        push(InferredType.TOP);
        break;
    case Opcodes.FCONST_0:
    case Opcodes.FCONST_1:
    case Opcodes.FCONST_2:
        push(InferredType.FLOAT);
        break;
    case Opcodes.DCONST_0:
    case Opcodes.DCONST_1:
        push(InferredType.DOUBLE);
        push(InferredType.TOP);
        break;
    case Opcodes.IALOAD:
    case Opcodes.BALOAD:
    case Opcodes.CALOAD:
    case Opcodes.SALOAD:
        pop(2);
        push(InferredType.INT);
        break;
    case Opcodes.LALOAD:
    case Opcodes.D2L:
        pop(2);
        push(InferredType.LONG);
        push(InferredType.TOP);
        break;
    case Opcodes.DALOAD:
    case Opcodes.L2D:
        pop(2);
        push(InferredType.DOUBLE);
        push(InferredType.TOP);
        break;
    case Opcodes.AALOAD:
        InferredType arrayType = pop(2);
        InferredType elementType = arrayType.getElementTypeIfArrayOrThrow();
        push(elementType);
        break;
    case Opcodes.IASTORE:
    case Opcodes.BASTORE:
    case Opcodes.CASTORE:
    case Opcodes.SASTORE:
    case Opcodes.FASTORE:
    case Opcodes.AASTORE:
        pop(3);
        break;
    case Opcodes.LASTORE:
    case Opcodes.DASTORE:
        pop(4);
        break;
    case Opcodes.POP:
    case Opcodes.IRETURN:
    case Opcodes.FRETURN:
    case Opcodes.ARETURN:
    case Opcodes.ATHROW:
    case Opcodes.MONITORENTER:
    case Opcodes.MONITOREXIT:
        pop();
        break;
    case Opcodes.POP2:
    case Opcodes.LRETURN:
    case Opcodes.DRETURN:
        pop(2);
        break;
    case Opcodes.DUP:
        push(top());
        break;
    case Opcodes.DUP_X1: {
        InferredType top = pop();
        InferredType next = pop();
        push(top);
        push(next);
        push(top);
        break;
    }
    case Opcodes.DUP_X2: {
        InferredType top = pop();
        InferredType next = pop();
        InferredType bottom = pop();
        push(top);
        push(bottom);
        push(next);
        push(top);
        break;
    }
    case Opcodes.DUP2: {
        InferredType top = pop();
        InferredType next = pop();
        push(next);
        push(top);
        push(next);
        push(top);
        break;
    }
    case Opcodes.DUP2_X1: {
        InferredType top = pop();
        InferredType next = pop();
        InferredType bottom = pop();
        push(next);
        push(top);
        push(bottom);
        push(next);
        push(top);
        break;
    }
    case Opcodes.DUP2_X2: {
        InferredType t1 = pop();
        InferredType t2 = pop();
        InferredType t3 = pop();
        InferredType t4 = pop();
        push(t2);
        push(t1);
        push(t4);
        push(t3);
        push(t2);
        push(t1);
        break;
    }
    case Opcodes.SWAP: {
        InferredType top = pop();
        InferredType next = pop();
        push(top);
        push(next);
        break;
    }
    case Opcodes.IADD:
    case Opcodes.ISUB:
    case Opcodes.IMUL:
    case Opcodes.IDIV:
    case Opcodes.IREM:
    case Opcodes.ISHL:
    case Opcodes.ISHR:
    case Opcodes.IUSHR:
    case Opcodes.IAND:
    case Opcodes.IOR:
    case Opcodes.IXOR:
    case Opcodes.L2I:
    case Opcodes.D2I:
    case Opcodes.FCMPL:
    case Opcodes.FCMPG:
        pop(2);
        push(InferredType.INT);
        break;

    case Opcodes.LADD:
    case Opcodes.LSUB:
    case Opcodes.LMUL:
    case Opcodes.LDIV:
    case Opcodes.LREM:
    case Opcodes.LAND:
    case Opcodes.LOR:
    case Opcodes.LXOR:
        pop(4);
        push(InferredType.LONG);
        push(InferredType.TOP);
        break;

    case Opcodes.LSHL:
    case Opcodes.LSHR:
    case Opcodes.LUSHR:
        pop(3);
        push(InferredType.LONG);
        push(InferredType.TOP);
        break;
    case Opcodes.I2L:
    case Opcodes.F2L:
        pop();
        push(InferredType.LONG);
        push(InferredType.TOP);
        break;
    case Opcodes.I2F:
        pop();
        push(InferredType.FLOAT);
        break;

    case Opcodes.LCMP:
    case Opcodes.DCMPG:
    case Opcodes.DCMPL:
        pop(4);
        push(InferredType.INT);
        break;

    case Opcodes.I2D:
    case Opcodes.F2D:
        pop();
        push(InferredType.DOUBLE);
        push(InferredType.TOP);
        break;
    case Opcodes.F2I:
    case Opcodes.ARRAYLENGTH:
        pop();
        push(InferredType.INT);
        break;
    case Opcodes.FALOAD:
    case Opcodes.FADD:
    case Opcodes.FSUB:
    case Opcodes.FMUL:
    case Opcodes.FDIV:
    case Opcodes.FREM:
    case Opcodes.L2F:
    case Opcodes.D2F:
        pop(2);
        push(InferredType.FLOAT);
        break;

    case Opcodes.DADD:
    case Opcodes.DSUB:
    case Opcodes.DMUL:
    case Opcodes.DDIV:
    case Opcodes.DREM:
        pop(4);
        push(InferredType.DOUBLE);
        push(InferredType.TOP);
        break;
    default:
        throw new RuntimeException("Unhandled opcode " + opcode);
    }
    super.visitInsn(opcode);
}

From source file:com.google.devtools.build.wireless.testing.java.injector.WhiteBoxMethodAdapter.java

License:Apache License

/**
 * Injects methods call by logging invocation time and parameters.
 * //w  w w  . jav a 2 s. co m
 * @see org.objectweb.asm.MethodAdapter#visitCode()
 */
@Override
public void visitCode() {
    mv.visitInsn(Opcodes.ICONST_0);
    stackServant.store("I", accessedFieldOwnerIndexInStack);
    mv.visitInsn(Opcodes.DCONST_0);
    stackServant.store("D", accessedFieldValueIndexInStack);

    if (matchOwnerClass && matchMethod) {
        mv.visitInsn(Opcodes.LCONST_0);
        stackServant.store("J", timeIndexInStack);

        logger.info("\t[WhiteBox][Method call]: " + methodName + " " + description);

        printServant.startPrinting();
        printServant.printString("[Call]\t");

        // The init method has some problem with 'this' so it is considered static
        if (stackServant.isStatic() || methodName.equals("<init>")) {
            printServant.printString(ownerClass);
        } else {
            stackServant.loadThis();
            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, ownerClass, "toString",
                    "()L" + ClassNames.JAVA_LANG_STRING + ";");
            printServant.print("L" + ClassNames.JAVA_LANG_STRING + ";");
        }

        printServant.printString("." + methodName + " Arguments: ");

        for (int i = 0; i < stackServant.getArgumentSize(); i++) {
            stackServant.loadArgumentAt(i);
            printServant.print(stackServant.getArgumentTypeAt(i));
            printServant.printString("; ");
        }

        printServant.printString(" Invoked: ");
        timeServant.loadCurrentTimeMillis();
        printServant.println("J");

        // time
        timeServant.startCountingTime(timeIndexInStack);

        // saving and printing
        printServant.stopPrinting();
    }
    mv.visitCode();
}

From source file:com.google.monitoring.runtime.instrumentation.adapters.AllocationMethodAdapter.java

License:Apache License

private void pushProductOfIntArrayOnStack() {
    final Label beginScopeLabel = new Label();
    final Label endScopeLabel = new Label();

    final int dimsArrayIndex = newLocal("[I", beginScopeLabel, endScopeLabel);
    final int counterIndex = newLocal("I", beginScopeLabel, endScopeLabel);
    final int productIndex = newLocal("I", beginScopeLabel, endScopeLabel);
    final Label loopLabel = new Label();
    final Label endLabel = new Label();

    super.visitLabel(beginScopeLabel);

    // stack: ... intArray
    super.visitVarInsn(Opcodes.ASTORE, dimsArrayIndex);
    // -> stack: ...

    // counter = 0
    super.visitInsn(Opcodes.ICONST_0);
    super.visitVarInsn(Opcodes.ISTORE, counterIndex);
    // product = 1
    super.visitInsn(Opcodes.ICONST_1);
    super.visitVarInsn(Opcodes.ISTORE, productIndex);
    // loop:/*  www.j a  v a  2 s  .  co m*/
    super.visitLabel(loopLabel);
    // if index >= arraylength goto end:
    super.visitVarInsn(Opcodes.ILOAD, counterIndex);
    super.visitVarInsn(Opcodes.ALOAD, dimsArrayIndex);
    super.visitInsn(Opcodes.ARRAYLENGTH);
    super.visitJumpInsn(Opcodes.IF_ICMPGE, endLabel);
    // product = product * max(array[counter],1)
    super.visitVarInsn(Opcodes.ALOAD, dimsArrayIndex);
    super.visitVarInsn(Opcodes.ILOAD, counterIndex);
    super.visitInsn(Opcodes.IALOAD);
    super.visitInsn(Opcodes.DUP);
    final Label nonZeroDimension = new Label();
    super.visitJumpInsn(Opcodes.IFNE, nonZeroDimension);
    super.visitInsn(Opcodes.POP);
    super.visitInsn(Opcodes.ICONST_1);
    super.visitLabel(nonZeroDimension);
    super.visitVarInsn(Opcodes.ILOAD, productIndex);
    super.visitInsn(Opcodes.IMUL); // if overflow happens it happens.
    super.visitVarInsn(Opcodes.ISTORE, productIndex);
    // iinc counter 1
    super.visitIincInsn(counterIndex, 1);
    // goto loop
    super.visitJumpInsn(Opcodes.GOTO, loopLabel);
    // end:
    super.visitLabel(endLabel);
    // re-push dimensions array
    super.visitVarInsn(Opcodes.ALOAD, dimsArrayIndex);
    // push product
    super.visitVarInsn(Opcodes.ILOAD, productIndex);

    super.visitLabel(endScopeLabel);
}

From source file:com.google.monitoring.runtime.instrumentation.adapters.AllocationMethodAdapter.java

License:Apache License

void calculateArrayLengthAndDispatch(final String typeName, final int dimCount) {
    // Since the dimensions of the array are not known at instrumentation
    // time, we take the created multi-dimensional array and peel off nesting
    // levels from the left.  For each nesting layer we probe the array length
    // and accumulate a partial product which we can then feed the recording
    // function.//from w  w w. ja  v  a2  s. co  m

    // below we note the partial product of dimensions 1 to X-1 as productToX
    // (so productTo1 == 1 == no dimensions yet).  We denote by aref0 the
    // array reference at the current nesting level (the containing aref's [0]
    // element).  If we hit a level whose arraylength is 0 there's no point
    // continuing so we shortcut out.
    final Label zeroDimension = new Label();
    super.visitInsn(Opcodes.DUP); // -> stack: ... origaref aref0
    super.visitLdcInsn(1); // -> stack: ... origaref aref0 productTo1
    for (int i = 0; i < dimCount; ++i) {
        // pre: stack: ... origaref aref0 productToI
        super.visitInsn(Opcodes.SWAP); // -> stack: ... origaref productToI aref
        super.visitInsn(Opcodes.DUP_X1);
        // -> stack: ... origaref aref0 productToI aref
        super.visitInsn(Opcodes.ARRAYLENGTH);
        // -> stack: ... origaref aref0 productToI dimI

        final Label nonZeroDimension = new Label();
        super.visitInsn(Opcodes.DUP);
        // -> stack: ... origaref aref0 productToI dimI dimI
        super.visitJumpInsn(Opcodes.IFNE, nonZeroDimension);
        // -> stack: ... origaref aref0 productToI dimI
        super.visitInsn(Opcodes.POP);
        // -> stack: ... origaref aref0 productToI
        super.visitJumpInsn(Opcodes.GOTO, zeroDimension);
        super.visitLabel(nonZeroDimension);
        // -> stack: ... origaref aref0 productToI max(dimI,1)

        super.visitInsn(Opcodes.IMUL);
        // -> stack: ... origaref aref0 productTo{I+1}
        if (i < dimCount - 1) {
            super.visitInsn(Opcodes.SWAP);
            // -> stack: ... origaref productTo{I+1} aref0
            super.visitInsn(Opcodes.ICONST_0);
            // -> stack: ... origaref productTo{I+1} aref0 0
            super.visitInsn(Opcodes.AALOAD);
            // -> stack: ... origaref productTo{I+1} aref0'
            super.visitInsn(Opcodes.SWAP);
        }
        // post: stack: ... origaref aref0 productTo{I+1}
    }
    super.visitLabel(zeroDimension);

    super.visitInsn(Opcodes.SWAP); // -> stack: ... origaref product aref0
    super.visitInsn(Opcodes.POP); // -> stack: ... origaref product
    super.visitInsn(Opcodes.SWAP); // -> stack: ... product origaref
    invokeRecordAllocation(typeName);
}

From source file:com.google.test.metric.asm.MethodVisitorBuilder.java

License:Apache License

public void visitInsn(final int opcode) {
    switch (opcode) {
    case Opcodes.ACONST_NULL:
        recorder.add(new Runnable() {
            public void run() {
                block.addOp(new Load(lineNumber, new Constant(null, JavaType.OBJECT)));
            }/*from   w  w  w  . java 2  s  .c  o m*/
        });
        break;
    case Opcodes.ICONST_M1:
    case Opcodes.ICONST_0:
    case Opcodes.ICONST_1:
    case Opcodes.ICONST_2:
    case Opcodes.ICONST_3:
    case Opcodes.ICONST_4:
    case Opcodes.ICONST_5:
        loadConstant(opcode - Opcodes.ICONST_M1 - 1, JavaType.INT);
        break;
    case Opcodes.LCONST_0:
    case Opcodes.LCONST_1:
        loadConstant(opcode - Opcodes.LCONST_0, JavaType.LONG);
        break;
    case Opcodes.FCONST_0:
    case Opcodes.FCONST_1:
    case Opcodes.FCONST_2:
        loadConstant(opcode - Opcodes.FCONST_0, JavaType.FLOAT);
        break;
    case Opcodes.DCONST_0:
    case Opcodes.DCONST_1:
        loadConstant(opcode - Opcodes.DCONST_0, JavaType.DOUBLE);
        break;
    case Opcodes.IALOAD:
        recordArrayLoad(JavaType.INT);
        break;
    case Opcodes.LALOAD:
        recordArrayLoad(JavaType.LONG);
        break;
    case Opcodes.FALOAD:
        recordArrayLoad(JavaType.FLOAT);
        break;
    case Opcodes.DALOAD:
        recordArrayLoad(JavaType.DOUBLE);
        break;
    case Opcodes.AALOAD:
        recordArrayLoad(JavaType.OBJECT);
        break;
    case Opcodes.BALOAD:
        recordArrayLoad(JavaType.BYTE);
        break;
    case Opcodes.CALOAD:
        recordArrayLoad(JavaType.CHAR);
        break;
    case Opcodes.SALOAD:
        recordArrayLoad(JavaType.SHORT);
        break;

    case Opcodes.IASTORE:
        recordArrayStore(JavaType.INT);
        break;
    case Opcodes.LASTORE:
        recordArrayStore(JavaType.LONG);
        break;
    case Opcodes.FASTORE:
        recordArrayStore(JavaType.FLOAT);
        break;
    case Opcodes.DASTORE:
        recordArrayStore(JavaType.DOUBLE);
        break;
    case Opcodes.AASTORE:
        recordArrayStore(JavaType.OBJECT);
        break;
    case Opcodes.BASTORE:
        recordArrayStore(JavaType.BYTE);
        break;
    case Opcodes.CASTORE:
        recordArrayStore(JavaType.CHAR);
        break;
    case Opcodes.SASTORE:
        recordArrayStore(JavaType.SHORT);
        break;
    case Opcodes.POP:
    case Opcodes.POP2:
        recorder.add(new Runnable() {
            public void run() {
                block.addOp(new Pop(lineNumber, opcode - Opcodes.POP + 1));
            }
        });
        break;
    case Opcodes.DUP:
    case Opcodes.DUP_X1:
    case Opcodes.DUP_X2:
        recorder.add(new Runnable() {
            public void run() {
                int offset = opcode - Opcodes.DUP;
                block.addOp(new Duplicate(lineNumber, offset));
            }
        });
        break;
    case Opcodes.DUP2:
    case Opcodes.DUP2_X1:
    case Opcodes.DUP2_X2:
        recorder.add(new Runnable() {
            public void run() {
                block.addOp(new Duplicate2(lineNumber, opcode - Opcodes.DUP2));
            }
        });
        break;
    case Opcodes.SWAP:
        recorder.add(new Runnable() {
            public void run() {
                block.addOp(new Swap(lineNumber));
            }
        });
        break;
    case Opcodes.IRETURN:
        _return(JavaType.INT);
        break;
    case Opcodes.FRETURN:
        _return(JavaType.FLOAT);
        break;
    case Opcodes.ARETURN:
        _return(JavaType.OBJECT);
        break;
    case Opcodes.LRETURN:
        _return(JavaType.LONG);
        break;
    case Opcodes.DRETURN:
        _return(JavaType.DOUBLE);
        break;
    case Opcodes.ATHROW:
        recorder.add(new Runnable() {
            public void run() {
                block.addOp(new Throw(lineNumber));
            }
        });
        break;
    case Opcodes.RETURN:
        _return(JavaType.VOID);
        break;
    case Opcodes.LCMP:
        operation("cmp", JavaType.LONG, JavaType.LONG, JavaType.INT);
        break;
    case Opcodes.FCMPL:
        operation("cmpl", JavaType.FLOAT, JavaType.FLOAT, JavaType.INT);
        break;
    case Opcodes.FCMPG:
        operation("cmpg", JavaType.FLOAT, JavaType.FLOAT, JavaType.INT);
        break;
    case Opcodes.DCMPL:
        operation("cmpl", JavaType.DOUBLE, JavaType.DOUBLE, JavaType.INT);
        break;
    case Opcodes.DCMPG:
        operation("cmpg", JavaType.DOUBLE, JavaType.DOUBLE, JavaType.INT);
        break;
    case Opcodes.LSHL:
        operation("shl", JavaType.LONG, JavaType.INT, JavaType.LONG);
        break;
    case Opcodes.LSHR:
        operation("shr", JavaType.LONG, JavaType.INT, JavaType.LONG);
        break;
    case Opcodes.LUSHR:
        operation("ushr", JavaType.LONG, JavaType.INT, JavaType.LONG);
        break;
    case Opcodes.LADD:
        operation("add", JavaType.LONG, JavaType.LONG, JavaType.LONG);
        break;
    case Opcodes.LSUB:
        operation("sub", JavaType.LONG, JavaType.LONG, JavaType.LONG);
        break;
    case Opcodes.LDIV:
        operation("div", JavaType.LONG, JavaType.LONG, JavaType.LONG);
        break;
    case Opcodes.LREM:
        operation("rem", JavaType.LONG, JavaType.LONG, JavaType.LONG);
        break;
    case Opcodes.LAND:
        operation("and", JavaType.LONG, JavaType.LONG, JavaType.LONG);
        break;
    case Opcodes.LOR:
        operation("or", JavaType.LONG, JavaType.LONG, JavaType.LONG);
        break;
    case Opcodes.LXOR:
        operation("xor", JavaType.LONG, JavaType.LONG, JavaType.LONG);
        break;
    case Opcodes.LMUL:
        operation("mul", JavaType.LONG, JavaType.LONG, JavaType.LONG);
        break;
    case Opcodes.FADD:
        operation("add", JavaType.FLOAT, JavaType.FLOAT, JavaType.FLOAT);
        break;
    case Opcodes.FSUB:
        operation("sub", JavaType.FLOAT, JavaType.FLOAT, JavaType.FLOAT);
        break;
    case Opcodes.FMUL:
        operation("mul", JavaType.FLOAT, JavaType.FLOAT, JavaType.FLOAT);
        break;
    case Opcodes.FREM:
        operation("rem", JavaType.FLOAT, JavaType.FLOAT, JavaType.FLOAT);
        break;
    case Opcodes.FDIV:
        operation("div", JavaType.FLOAT, JavaType.FLOAT, JavaType.FLOAT);
        break;
    case Opcodes.ISHL:
        operation("shl", JavaType.INT, JavaType.INT, JavaType.INT);
        break;
    case Opcodes.ISHR:
        operation("shr", JavaType.INT, JavaType.INT, JavaType.INT);
        break;
    case Opcodes.IUSHR:
        operation("ushr", JavaType.INT, JavaType.INT, JavaType.INT);
        break;
    case Opcodes.IADD:
        operation("add", JavaType.INT, JavaType.INT, JavaType.INT);
        break;
    case Opcodes.ISUB:
        operation("sub", JavaType.INT, JavaType.INT, JavaType.INT);
        break;
    case Opcodes.IMUL:
        operation("mul", JavaType.INT, JavaType.INT, JavaType.INT);
        break;
    case Opcodes.IDIV:
        operation("div", JavaType.INT, JavaType.INT, JavaType.INT);
        break;
    case Opcodes.IREM:
        operation("rem", JavaType.INT, JavaType.INT, JavaType.INT);
        break;
    case Opcodes.IAND:
        operation("and", JavaType.INT, JavaType.INT, JavaType.INT);
        break;
    case Opcodes.IOR:
        operation("or", JavaType.INT, JavaType.INT, JavaType.INT);
        break;
    case Opcodes.IXOR:
        operation("xor", JavaType.INT, JavaType.INT, JavaType.INT);
        break;
    case Opcodes.DSUB:
        operation("sub", JavaType.DOUBLE, JavaType.DOUBLE, JavaType.DOUBLE);
        break;
    case Opcodes.DADD:
        operation("add", JavaType.DOUBLE, JavaType.DOUBLE, JavaType.DOUBLE);
        break;
    case Opcodes.DMUL:
        operation("mul", JavaType.DOUBLE, JavaType.DOUBLE, JavaType.DOUBLE);
        break;
    case Opcodes.DDIV:
        operation("div", JavaType.DOUBLE, JavaType.DOUBLE, JavaType.DOUBLE);
        break;
    case Opcodes.DREM:
        operation("rem", JavaType.DOUBLE, JavaType.DOUBLE, JavaType.DOUBLE);
        break;
    case Opcodes.L2I:
        convert(JavaType.LONG, JavaType.INT);
        break;
    case Opcodes.L2F:
        convert(JavaType.LONG, JavaType.FLOAT);
        break;
    case Opcodes.L2D:
        convert(JavaType.LONG, JavaType.DOUBLE);
        break;
    case Opcodes.LNEG:
        operation("neg", JavaType.LONG, null, JavaType.LONG);
        break;
    case Opcodes.F2I:
        convert(JavaType.FLOAT, JavaType.INT);
        break;
    case Opcodes.F2L:
        convert(JavaType.FLOAT, JavaType.LONG);
        break;
    case Opcodes.FNEG:
        operation("neg", JavaType.FLOAT, null, JavaType.FLOAT);
        break;
    case Opcodes.F2D:
        convert(JavaType.FLOAT, JavaType.DOUBLE);
        break;
    case Opcodes.D2I:
        convert(JavaType.DOUBLE, JavaType.INT);
        break;
    case Opcodes.D2L:
        convert(JavaType.DOUBLE, JavaType.LONG);
        break;
    case Opcodes.D2F:
        convert(JavaType.DOUBLE, JavaType.FLOAT);
        break;
    case Opcodes.DNEG:
        operation("neg", JavaType.DOUBLE, null, JavaType.DOUBLE);
        break;
    case Opcodes.I2L:
        convert(JavaType.INT, JavaType.LONG);
        break;
    case Opcodes.I2F:
        convert(JavaType.INT, JavaType.FLOAT);
        break;
    case Opcodes.I2D:
        convert(JavaType.INT, JavaType.DOUBLE);
        break;
    case Opcodes.I2B:
        convert(JavaType.INT, JavaType.BYTE);
        break;
    case Opcodes.I2C:
        convert(JavaType.INT, JavaType.CHAR);
        break;
    case Opcodes.I2S:
        convert(JavaType.INT, JavaType.SHORT);
        break;
    case Opcodes.INEG:
        operation("neg", JavaType.INT, null, JavaType.INT);
        break;
    case Opcodes.ARRAYLENGTH:
        operation("arraylength", JavaType.OBJECT.toArray(), null, JavaType.INT);
        break;
    case Opcodes.MONITORENTER:
        recorder.add(new Runnable() {
            public void run() {
                block.addOp(new MonitorEnter(lineNumber));
            }
        });
        break;
    case Opcodes.MONITOREXIT:
        recorder.add(new Runnable() {
            public void run() {
                block.addOp(new MonitorExit(lineNumber));
            }
        });
        break;
    case Opcodes.NOP:
        recorder.add(new Runnable() {
            public void run() {
                block.addOp(new Transform(lineNumber, "NOP", null, null, null));
            }
        });
    }
}

From source file:com.google.test.metric.asm.MethodVisitorBuilderTest.java

License:Apache License

public void testSwap() throws Exception {
    ClassInfo classInfo = new ClassInfo("TestClass", false, null, null, null);
    MethodVisitorBuilder builder = new MethodVisitorBuilder(null, classInfo, "test", "()V", null, null, true,
            false, Visibility.PUBLIC);
    builder.visitInsn(Opcodes.ICONST_0);
    builder.visitInsn(Opcodes.ICONST_0);
    builder.visitInsn(Opcodes.SWAP);/* w w w .j  ava  2 s  .c o m*/
    builder.visitEnd();
}

From source file:com.liferay.alloy.mvc.jsonwebservice.AlloyControllerInvokerManager.java

License:Open Source License

protected byte[] generateAlloyControllerInvokerClassData(Class<?> controllerClass,
        String alloyControllerInvokerClassName) throws NoClassNecessaryException {

    boolean jsonWebServiceMethodsPresent = false;

    ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES);

    String alloyControllerInvokerClassBinaryName = getClassBinaryName(alloyControllerInvokerClassName);

    String baseAlloyControllerInvokerClassBinaryName = getClassBinaryName(
            BaseAlloyControllerInvokerImpl.class.getName());

    classWriter.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER,
            alloyControllerInvokerClassBinaryName, null, baseAlloyControllerInvokerClassBinaryName, null);

    MethodVisitor methodVisitor = classWriter.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);

    methodVisitor.visitCode();/* w w  w .j av a2s  .c  o  m*/
    methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
    methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, baseAlloyControllerInvokerClassBinaryName, "<init>",
            "()V");
    methodVisitor.visitInsn(Opcodes.RETURN);
    methodVisitor.visitMaxs(1, 1);
    methodVisitor.visitEnd();

    Method[] methods = controllerClass.getDeclaredMethods();

    for (Method method : methods) {
        if (!Modifier.isPublic(method.getModifiers())) {
            continue;
        }

        JSONWebServiceMethod jsonWebServiceMethod = method.getAnnotation(JSONWebServiceMethod.class);

        if (jsonWebServiceMethod == null) {
            continue;
        }

        jsonWebServiceMethodsPresent = true;

        Class<?>[] parameterTypes = jsonWebServiceMethod.parameterTypes();

        StringBundler sb = new StringBundler(parameterTypes.length + 3);

        sb.append(StringPool.OPEN_PARENTHESIS);

        for (Class<?> parameterType : parameterTypes) {
            sb.append(Type.getDescriptor(parameterType));
        }

        sb.append(StringPool.CLOSE_PARENTHESIS);
        sb.append(Type.getDescriptor(JSONSerializable.class));

        String methodDescriptor = sb.toString();

        methodVisitor = classWriter.visitMethod(Opcodes.ACC_PUBLIC, method.getName(), methodDescriptor, null,
                new String[] { getClassBinaryName(Exception.class.getName()) });

        methodVisitor.visitCode();

        for (int i = 0; i < parameterTypes.length; i++) {
            String parameterName = jsonWebServiceMethod.parameterNames()[i];
            Class<?> parameterType = parameterTypes[i];

            methodVisitor.visitLocalVariable(parameterName, Type.getDescriptor(parameterType), null,
                    new Label(), new Label(), i + 1);
        }

        methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
        methodVisitor.visitLdcInsn(jsonWebServiceMethod.lifecycle());

        methodVisitor.visitIntInsn(Opcodes.BIPUSH, parameterTypes.length * 2 + 2);
        methodVisitor.visitTypeInsn(Opcodes.ANEWARRAY, getClassBinaryName(Object.class.getName()));

        methodVisitor.visitInsn(Opcodes.DUP);
        methodVisitor.visitInsn(Opcodes.ICONST_0);
        methodVisitor.visitLdcInsn("action");
        methodVisitor.visitInsn(Opcodes.AASTORE);

        methodVisitor.visitInsn(Opcodes.DUP);
        methodVisitor.visitInsn(Opcodes.ICONST_1);
        methodVisitor.visitLdcInsn(method.getName());
        methodVisitor.visitInsn(Opcodes.AASTORE);

        for (int i = 0; i < parameterTypes.length; i++) {
            String parameterName = jsonWebServiceMethod.parameterNames()[i];

            methodVisitor.visitInsn(Opcodes.DUP);
            methodVisitor.visitIntInsn(Opcodes.BIPUSH, (i + 1) * 2);
            methodVisitor.visitLdcInsn(parameterName);
            methodVisitor.visitInsn(Opcodes.AASTORE);

            methodVisitor.visitInsn(Opcodes.DUP);
            methodVisitor.visitIntInsn(Opcodes.BIPUSH, (i + 1) * 2 + 1);
            methodVisitor.visitVarInsn(Opcodes.ALOAD, i + 1);
            methodVisitor.visitInsn(Opcodes.AASTORE);
        }

        sb = new StringBundler(5);

        sb.append(StringPool.OPEN_PARENTHESIS);
        sb.append(Type.getDescriptor(String.class));
        sb.append(Type.getDescriptor(Object[].class));
        sb.append(StringPool.CLOSE_PARENTHESIS);
        sb.append(Type.getDescriptor(JSONSerializable.class));

        methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, alloyControllerInvokerClassBinaryName,
                "invokeAlloyController", sb.toString());

        methodVisitor.visitInsn(Opcodes.ARETURN);

        methodVisitor.visitMaxs(-1, -1);
        methodVisitor.visitEnd();
    }

    classWriter.visitEnd();

    if (!jsonWebServiceMethodsPresent) {
        throw new NoClassNecessaryException();
    }

    return classWriter.toByteArray();
}

From source file:com.liferay.portal.nio.intraband.proxy.IntrabandProxyUtilTest.java

License:Open Source License

@Test
public void testRewriteGetProxyMethodSignaturesMethodNode() {
    class TestClass {

        @SuppressWarnings("unused")
        public final String[] PROXY_METHOD_SIGNATURES = _getProxyMethodSignatures();

        private String[] _getProxyMethodSignatures() {
            return new String[0];
        }//from  ww w. j  av a2  s.c  o m
    }

    ClassNode classNode = _loadClass(TestClass.class);

    String[] proxyMethodSignatures = { "testSignature1", "testSignature2", "testSignature3" };

    IntrabandProxyUtil.rewriteGetProxyMethodSignaturesMethodNode(classNode, proxyMethodSignatures);

    MethodNode methodNode = ASMUtil.findMethodNode(classNode.methods, "_getProxyMethodSignatures",
            Type.getType(String[].class));

    InsnList insnList = methodNode.instructions;

    Iterator<AbstractInsnNode> iterator = insnList.iterator();

    _assertInsnNode(iterator.next(), Opcodes.ICONST_3);

    _assertTypeInsnNode(iterator.next(), Opcodes.ANEWARRAY, String.class);

    for (int i = 0; i < proxyMethodSignatures.length; i++) {
        _assertInsnNode(iterator.next(), Opcodes.DUP);
        _assertInsnNode(iterator.next(), Opcodes.ICONST_0 + i);
        _assertLdcInsnNode(iterator.next(), Opcodes.LDC, proxyMethodSignatures[i]);
        _assertInsnNode(iterator.next(), Opcodes.AASTORE);
    }

    _assertInsnNode(iterator.next(), Opcodes.ARETURN);

    Assert.assertFalse(iterator.hasNext());
}