Example usage for org.objectweb.asm Opcodes BIPUSH

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

Introduction

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

Prototype

int BIPUSH

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

Click Source Link

Usage

From source file:org.eclipse.objectteams.otredyn.bytecode.asm.AbstractTransformableClassNode.java

License:Open Source License

/**
 * Create an instruction for loading an integer constant,
 * using the most compact possible format. 
 *///  ww  w  . ja va  2s  .c o m
protected AbstractInsnNode createLoadIntConstant(int constant) {
    if (constant <= 5)
        return new InsnNode(Opcodes.ICONST_0 + constant);
    else if (constant < Byte.MAX_VALUE)
        return new IntInsnNode(Opcodes.BIPUSH, constant);
    else
        return new LdcInsnNode(constant);
}

From source file:org.eclipse.objectteams.otredyn.bytecode.asm.MoveCodeToCallOrigAdapter.java

License:Open Source License

/** To avoid infinite recursion, calls super.m(a1, a2) must be translated to super.callOrig(boundMethodId, new Object[] {a1, a2}). */
private void adjustSuperCalls(InsnList instructions, String selector, Type[] args, Type returnType,
        int boundMethodIdSlot) {
    // search:/*  w  w  w. j a  v a 2 s . c o m*/
    List<MethodInsnNode> toReplace = new ArrayList<MethodInsnNode>();
    ListIterator<AbstractInsnNode> orgMethodIter = instructions.iterator();
    while (orgMethodIter.hasNext()) {
        AbstractInsnNode orgMethodNode = orgMethodIter.next();
        if (orgMethodNode.getOpcode() == Opcodes.INVOKESPECIAL
                && ((MethodInsnNode) orgMethodNode).name.equals(selector))
            toReplace.add((MethodInsnNode) orgMethodNode);
    }
    if (toReplace.isEmpty())
        return;
    // replace:
    for (MethodInsnNode oldNode : toReplace) {
        // we need to insert into the loading sequence before the invocation, find the insertion points:
        AbstractInsnNode[] insertionPoints = StackBalanceAnalyzer.findInsertionPointsBefore(oldNode, args);
        AbstractInsnNode firstInsert = insertionPoints.length > 0 ? insertionPoints[0] : oldNode;

        // push first arg to _OT$callOrig():
        instructions.insertBefore(firstInsert, new IntInsnNode(Opcodes.ILOAD, boundMethodIdSlot));

        // prepare array as second arg to _OT$callOrig():
        instructions.insertBefore(firstInsert, new IntInsnNode(Opcodes.BIPUSH, args.length));
        instructions.insertBefore(firstInsert, new TypeInsnNode(Opcodes.ANEWARRAY, "java/lang/Object"));

        for (int i = 0; i < insertionPoints.length; i++) {
            // NB: each iteration has an even stack balance, where the top is the Object[].
            instructions.insertBefore(insertionPoints[i], new InsnNode(Opcodes.DUP));
            instructions.insertBefore(insertionPoints[i], new IntInsnNode(Opcodes.BIPUSH, i));
            // leave the original loading sequence in tact and continue at the next point:
            AbstractInsnNode insertAt = (i + 1 < insertionPoints.length) ? insertionPoints[i + 1] : oldNode;
            instructions.insertBefore(insertAt, AsmTypeHelper.getBoxingInstructionForType(args[i]));
            instructions.insertBefore(insertAt, new InsnNode(Opcodes.AASTORE));
        }

        if (returnType == Type.VOID_TYPE)
            instructions.insert(oldNode, new InsnNode(Opcodes.POP));
        else
            instructions.insert(oldNode, AsmTypeHelper.getUnboxingInstructionForType(returnType));

        instructions.set(oldNode, new MethodInsnNode(Opcodes.INVOKESPECIAL, ((MethodInsnNode) oldNode).owner,
                callOrig.getName(), callOrig.getSignature()));
    }
}

From source file:org.evosuite.graphs.cfg.ASMWrapper.java

License:Open Source License

/**
 * <p>// w w w . j a va  2s  .  c  o m
 * isConstant
 * </p>
 * 
 * @return a boolean.
 */
public boolean isConstant() {
    switch (asmNode.getOpcode()) {
    case Opcodes.LDC:
    case Opcodes.ICONST_0:
    case Opcodes.ICONST_1:
    case Opcodes.ICONST_2:
    case Opcodes.ICONST_3:
    case Opcodes.ICONST_4:
    case Opcodes.ICONST_5:
    case Opcodes.ICONST_M1:
    case Opcodes.LCONST_0:
    case Opcodes.LCONST_1:
    case Opcodes.DCONST_0:
    case Opcodes.DCONST_1:
    case Opcodes.FCONST_0:
    case Opcodes.FCONST_1:
    case Opcodes.FCONST_2:
    case Opcodes.BIPUSH:
    case Opcodes.SIPUSH:
        return true;
    default:
        return false;
    }
}

From source file:org.evosuite.graphs.cfg.BytecodeInstructionPool.java

License:Open Source License

/**
 * Determine how many bytes the current instruction occupies together with
 * its operands/*from w w w . j av  a2 s.co m*/
 * 
 * @return
 */
private int getBytecodeIncrement(AbstractInsnNode instructionNode) {
    int opcode = instructionNode.getOpcode();
    switch (opcode) {
    case Opcodes.ALOAD: // index
    case Opcodes.ASTORE: // index
    case Opcodes.DLOAD:
    case Opcodes.DSTORE:
    case Opcodes.FLOAD:
    case Opcodes.FSTORE:
    case Opcodes.ILOAD:
    case Opcodes.ISTORE:
    case Opcodes.LLOAD:
    case Opcodes.LSTORE:
        VarInsnNode varNode = (VarInsnNode) instructionNode;
        if (varNode.var > 3)
            return 1;
        else
            return 0;
    case Opcodes.BIPUSH: // byte
    case Opcodes.NEWARRAY:
    case Opcodes.RET:
        return 1;
    case Opcodes.LDC:
        LdcInsnNode ldcNode = (LdcInsnNode) instructionNode;
        if (ldcNode.cst instanceof Double || ldcNode.cst instanceof Long)
            return 2; // LDC2_W
        else
            return 1;
    case 19: //LDC_W
    case 20: //LDC2_W
        return 2;
    case Opcodes.ANEWARRAY: // indexbyte1, indexbyte2
    case Opcodes.CHECKCAST: // indexbyte1, indexbyte2
    case Opcodes.GETFIELD:
    case Opcodes.GETSTATIC:
    case Opcodes.GOTO:
    case Opcodes.IF_ACMPEQ:
    case Opcodes.IF_ACMPNE:
    case Opcodes.IF_ICMPEQ:
    case Opcodes.IF_ICMPNE:
    case Opcodes.IF_ICMPGE:
    case Opcodes.IF_ICMPGT:
    case Opcodes.IF_ICMPLE:
    case Opcodes.IF_ICMPLT:
    case Opcodes.IFLE:
    case Opcodes.IFLT:
    case Opcodes.IFGE:
    case Opcodes.IFGT:
    case Opcodes.IFNE:
    case Opcodes.IFEQ:
    case Opcodes.IFNONNULL:
    case Opcodes.IFNULL:
    case Opcodes.IINC:
    case Opcodes.INSTANCEOF:
    case Opcodes.INVOKESPECIAL:
    case Opcodes.INVOKESTATIC:
    case Opcodes.INVOKEVIRTUAL:
    case Opcodes.JSR:
    case Opcodes.NEW:
    case Opcodes.PUTFIELD:
    case Opcodes.PUTSTATIC:
    case Opcodes.SIPUSH:
        // case Opcodes.LDC_W
        // case Opcodes.LDC2_W

        return 2;
    case Opcodes.MULTIANEWARRAY:
        return 3;
    case Opcodes.INVOKEDYNAMIC:
    case Opcodes.INVOKEINTERFACE:
        return 4;

    case Opcodes.LOOKUPSWITCH:
    case Opcodes.TABLESWITCH:
        // TODO: Could be more
        return 4;
    // case Opcodes.GOTO_W 
    // case Opcodes.JSR_W
    }
    return 0;
}

From source file:org.evosuite.instrumentation.mutation.ReplaceConstant.java

License:Open Source License

private Object getValue(AbstractInsnNode constant) {
    switch (constant.getOpcode()) {
    case Opcodes.LDC:
        return ((LdcInsnNode) constant).cst;
    case Opcodes.ICONST_0:
        return 0;
    case Opcodes.ICONST_1:
        return 1;
    case Opcodes.ICONST_2:
        return 2;
    case Opcodes.ICONST_3:
        return 3;
    case Opcodes.ICONST_4:
        return 4;
    case Opcodes.ICONST_5:
        return 5;
    case Opcodes.ICONST_M1:
        return -1;
    case Opcodes.LCONST_0:
        return 0L;
    case Opcodes.LCONST_1:
        return 1L;
    case Opcodes.DCONST_0:
        return 0.0;
    case Opcodes.DCONST_1:
        return 1.0;
    case Opcodes.FCONST_0:
        return 0.0F;
    case Opcodes.FCONST_1:
        return 1.0F;
    case Opcodes.FCONST_2:
        return 2.0F;
    case Opcodes.SIPUSH:
        return ((IntInsnNode) constant).operand;
    case Opcodes.BIPUSH:
        return ((IntInsnNode) constant).operand;
    default:/* www.java 2  s.  c o m*/
        throw new RuntimeException("Unknown constant: " + constant.getOpcode());
    }
}

From source file:org.evosuite.instrumentation.ReturnValueAdapter.java

License:Open Source License

private void callLogPrototype(String traceMethod, PDType type) {
    if (type != PDType.LONG && type != PDType.DOUBLE) {
        this.visitInsn(Opcodes.DUP);
        if (type == PDType.FLOAT) {
            this.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Float", "floatToRawIntBits", "(F)I", false);
        }//from  ww w. ja  v a 2 s .c o m
    } else {
        this.visitInsn(Opcodes.DUP2);
        if (type == PDType.DOUBLE) {
            this.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Double", "doubleToRawLongBits", "(D)J",
                    false);
        }
        this.visitInsn(Opcodes.DUP2);
        this.visitIntInsn(Opcodes.BIPUSH, 32);
        this.visitInsn(Opcodes.LSHR);
        this.visitInsn(Opcodes.LXOR);
        this.visitInsn(Opcodes.L2I);
    }

    this.visitLdcInsn(className);
    this.visitLdcInsn(fullMethodName);
    this.visitMethodInsn(Opcodes.INVOKESTATIC, PackageInfo.getNameWithSlash(ExecutionTracer.class),
            "returnValue", "(ILjava/lang/String;Ljava/lang/String;)V", false);
}

From source file:org.evosuite.seeding.PrimitivePoolMethodAdapter.java

License:Open Source License

/** {@inheritDoc} */
@Override/*from www .  j a va 2  s .c o  m*/
public void visitIntInsn(int opcode, int operand) {
    if (opcode == Opcodes.BIPUSH || opcode == Opcodes.SIPUSH) {
        // constantPool.add(operand);
        if (DependencyAnalysis.isTargetClassName(className)) {
            poolManager.addSUTConstant(operand);
        } else {
            poolManager.addNonSUTConstant(operand);
        }
    }
    super.visitIntInsn(opcode, operand);
}

From source file:org.evosuite.testcarver.instrument.Instrumenter.java

License:Open Source License

private InsnList addCaptureCall(final boolean isStatic, final String internalClassName, final String methodName,
        final String methodDesc, final Type[] argTypes) {
    // construction of  
    //   Capturer.capture(final Object receiver, final String methodName, final Object[] methodParams)
    // call/*from www .  ja  v  a  2s .  co  m*/
    final InsnList il = new InsnList();

    il.add(new LdcInsnNode(this.captureId));

    // --- load receiver argument
    int varIndex;
    if (isStatic) {
        // static method invocation
        il.add(new LdcInsnNode(internalClassName));
        il.add(new MethodInsnNode(Opcodes.INVOKESTATIC, PackageInfo.getNameWithSlash(CaptureUtil.class),
                "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;"));

        varIndex = 0;
    } else {
        // non-static method call
        il.add(new VarInsnNode(Opcodes.ALOAD, 0));
        varIndex = 1;
    }

    // --- load method name argument

    il.add(new LdcInsnNode(methodName));

    // --- load method description argument

    il.add(new LdcInsnNode(methodDesc));

    // --- load methodParams arguments

    // load methodParams length
    // TODO ICONST_1 to ICONST_5 would be more efficient
    il.add(new IntInsnNode(Opcodes.BIPUSH, argTypes.length));

    // create array object
    il.add(new TypeInsnNode(Opcodes.ANEWARRAY, "java/lang/Object"));

    // fill the array

    for (int i = 0; i < argTypes.length; i++) {
        il.add(new InsnNode(Opcodes.DUP));

        // TODO ICONST_1 to ICONST_5 would be more efficient
        il.add(new IntInsnNode(Opcodes.BIPUSH, i));

        //check for primitives
        this.loadAndConvertToObject(il, argTypes[i], varIndex++);
        il.add(new InsnNode(Opcodes.AASTORE));

        // long/double take two registers
        if (argTypes[i].equals(Type.LONG_TYPE) || argTypes[i].equals(Type.DOUBLE_TYPE)) {
            varIndex++;
        }
    }

    // --- construct Capture.capture() call

    il.add(new MethodInsnNode(Opcodes.INVOKESTATIC,
            PackageInfo.getNameWithSlash(org.evosuite.testcarver.capture.Capturer.class), "capture",
            "(ILjava/lang/Object;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;)V"));

    return il;
}

From source file:org.fabric3.implementation.bytecode.proxy.common.ProxyFactoryImpl.java

License:Open Source License

public <T, D extends ProxyDispatcher> T createProxy(URI classLoaderKey, Class<T> type, Method[] methods,
        Class<D> dispatcherInt, Class<? extends D> dispatcherTmpl, boolean wrapped) throws ProxyException {

    String className = type.getName() + "_Proxy_" + dispatcherInt.getSimpleName(); // ensure multiple dispatchers can be defined for the same interface

    // check if the proxy class has already been created
    BytecodeClassLoader generationLoader = getClassLoader(classLoaderKey);
    try {//  w w  w  .  j a  v a2 s.  c om
        Class<T> proxyClass = (Class<T>) generationLoader.loadClass(className);
        return proxyClass.newInstance();
    } catch (ClassNotFoundException e) {
        // ignore
    } catch (InstantiationException e) {
        throw new ProxyException(e);
    } catch (IllegalAccessException e) {
        throw new ProxyException(e);
    }

    verifyTemplate(dispatcherTmpl);

    String classNameInternal = Type.getInternalName(type) + "_Proxy_" + dispatcherInt.getSimpleName();
    String thisDescriptor = "L" + classNameInternal + ";";
    String dispatcherIntName = Type.getInternalName(dispatcherInt);

    //Important to use class version of template class that will be copied. If class compiled with JDK 1.5 but copied 
    //to class version 1.7 there will be errors since 1.7 enforces frame stackmaps which were not present in 1.5

    int version = getClassVersion(generationLoader, dispatcherTmpl);
    ClassWriter cw = new ClassWriter(0);
    MethodVisitor mv;

    if (type.isInterface()) {
        String interfazeName = Type.getInternalName(type);
        cw.visit(version, ACC_PUBLIC + ACC_SUPER, classNameInternal, null, "java/lang/Object",
                new String[] { dispatcherIntName, interfazeName });
        cw.visitSource(type.getName() + "Proxy.java", null);
        String baseName = Type.getInternalName(Object.class);
        // write the ctor
        writeConstructor(baseName, thisDescriptor, cw);
    } else {
        verifyBaseClass(type, methods);
        String baseTypeName = Type.getInternalName(type);
        cw.visit(version, ACC_PUBLIC + ACC_SUPER, classNameInternal, null, baseTypeName,
                new String[] { dispatcherIntName });

        cw.visitSource(type.getName() + "Proxy.java", null);
        String baseName = Type.getInternalName(type);
        // write the ctor
        writeConstructor(baseName, thisDescriptor, cw);
    }

    copyTemplate(generationLoader, classNameInternal, dispatcherTmpl, cw);

    // write the methods
    int methodIndex = 0;
    for (Method method : methods) {
        //if the method is not overridable do not generate a bytecode method for it. This means any invocation of the class will directly act upon the 
        //the base class or proxy class but since these methods should not be visible anyway it shouldn't matter. The exception could be equals/hashcode/toString/clone 
        if (!isOverridableMethod(method)) {
            methodIndex++;
            continue;
        }
        String methodSignature = Type.getMethodDescriptor(method);
        String[] exceptions = new String[method.getExceptionTypes().length];
        for (int i = 0; i < exceptions.length; i++) {
            exceptions[i] = Type.getInternalName(method.getExceptionTypes()[i]);
        }
        int visibility = Modifier.isPublic(method.getModifiers()) ? ACC_PUBLIC
                : Modifier.isProtected(method.getModifiers()) ? ACC_PROTECTED : 0;
        mv = cw.visitMethod(visibility, method.getName(), methodSignature, null, exceptions);
        mv.visitCode();

        List<Label> exceptionLabels = new ArrayList<Label>();
        Label label2 = new Label();
        Label label3 = new Label();

        for (String exception : exceptions) {
            Label endLabel = new Label();
            exceptionLabels.add(endLabel);
            mv.visitTryCatchBlock(label2, label3, endLabel, exception);

        }

        mv.visitLabel(label2);
        mv.visitVarInsn(ALOAD, 0);

        // set the method index used to dispatch on
        if (methodIndex >= 0 && methodIndex <= 5) {
            // use an integer constant if within range
            mv.visitInsn(Opcodes.ICONST_0 + methodIndex);
        } else {
            mv.visitIntInsn(Opcodes.BIPUSH, methodIndex);
        }
        methodIndex++;

        int[] index = new int[1];
        index[0] = 0;
        int[] stack = new int[1];
        stack[0] = 1;

        if (method.getParameterTypes().length == 0) {
            // no params, load null
            mv.visitInsn(Opcodes.ACONST_NULL);
        } else {
            if (wrapped) {
                emitWrappedParameters(method, mv, index, stack);
            } else {
                emitUnWrappedParameters(method, mv, index, stack);
            }
        }

        mv.visitMethodInsn(INVOKEVIRTUAL, classNameInternal, "_f3_invoke",
                "(ILjava/lang/Object;)Ljava/lang/Object;");

        // handle return values
        writeReturn(method, label3, mv);

        // implement catch blocks
        index[0] = 0;
        for (String exception : exceptions) {
            Label endLabel = exceptionLabels.get(index[0]);
            mv.visitLabel(endLabel);
            mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { exception });
            mv.visitVarInsn(ASTORE, wrapped ? stack[0] : 1);
            Label label6 = new Label();
            mv.visitLabel(label6);
            mv.visitVarInsn(ALOAD, wrapped ? stack[0] : 1);
            mv.visitInsn(ATHROW);
            index[0]++;
        }

        Label label7 = new Label();
        mv.visitLabel(label7);
        mv.visitMaxs(7, 5);
        mv.visitEnd();

    }
    cw.visitEnd();

    byte[] data = cw.toByteArray();
    //ClassReader classReader = new ClassReader(data);
    //classReader.accept(new org.objectweb.asm.util.TraceClassVisitor(null, new org.objectweb.asm.util.ASMifier(), new java.io.PrintWriter(System.out)), 0);
    Class<?> proxyClass = generationLoader.defineClass(className, data);
    try {
        return (T) proxyClass.newInstance();
    } catch (InstantiationException e) {
        throw new ProxyException(e);
    } catch (IllegalAccessException e) {
        throw new ProxyException(e);
    }

}

From source file:org.fabric3.implementation.bytecode.proxy.common.ProxyFactoryImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
public void emitWrappedParameters(Method method, MethodVisitor mv, int[] index, int[] stack) {
    int numberOfParameters = method.getParameterTypes().length;

    // create the Object[] used to pass the parameters to _f3_invoke and push it on the stack
    if (numberOfParameters >= 0 && numberOfParameters <= 5) {
        // use an integer constant if within range
        mv.visitInsn(Opcodes.ICONST_0 + numberOfParameters);
    } else {//from   w w w . j  a  v a2s.c  om
        mv.visitIntInsn(Opcodes.BIPUSH, numberOfParameters);
    }
    mv.visitTypeInsn(ANEWARRAY, "java/lang/Object");
    mv.visitInsn(DUP);

    for (Class<?> param : method.getParameterTypes()) {
        if (Integer.TYPE.equals(param)) {
            mv.visitInsn(Opcodes.ICONST_0 + index[0]);
            mv.visitVarInsn(Opcodes.ILOAD, stack[0]);
            mv.visitMethodInsn(INVOKESTATIC, "java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;");
            mv.visitInsn(AASTORE);
            if (index[0] < numberOfParameters - 1) {
                mv.visitInsn(DUP);
            }
        } else if (Float.TYPE.equals(param)) {
            mv.visitInsn(Opcodes.ICONST_0 + index[0]);
            mv.visitVarInsn(Opcodes.FLOAD, stack[0]);
            mv.visitMethodInsn(INVOKESTATIC, "java/lang/Float", "valueOf", "(F)Ljava/lang/Float;");
            mv.visitInsn(AASTORE);
            if (index[0] < numberOfParameters - 1) {
                mv.visitInsn(DUP);
            }
        } else if (Boolean.TYPE.equals(param)) {
            mv.visitInsn(Opcodes.ICONST_0 + index[0]);
            mv.visitVarInsn(Opcodes.ILOAD, stack[0]);
            mv.visitMethodInsn(INVOKESTATIC, "java/lang/Boolean", "valueOf", "(Z)Ljava/lang/Boolean;");
            mv.visitInsn(AASTORE);
            if (index[0] < numberOfParameters - 1) {
                mv.visitInsn(DUP);
            }
        } else if (Short.TYPE.equals(param)) {
            mv.visitInsn(Opcodes.ICONST_0 + index[0]);
            mv.visitVarInsn(Opcodes.ILOAD, stack[0]);
            mv.visitMethodInsn(INVOKESTATIC, "java/lang/Short", "valueOf", "(S)Ljava/lang/Short;");
            mv.visitInsn(AASTORE);
            if (index[0] < numberOfParameters - 1) {
                mv.visitInsn(DUP);
            }
        } else if (Byte.TYPE.equals(param)) {
            mv.visitInsn(Opcodes.ICONST_0 + index[0]);
            mv.visitVarInsn(Opcodes.ILOAD, stack[0]);
            mv.visitMethodInsn(INVOKESTATIC, "java/lang/Byte", "valueOf", "(B)Ljava/lang/Byte;");
            mv.visitInsn(AASTORE);
            if (index[0] < numberOfParameters - 1) {
                mv.visitInsn(DUP);
            }
        } else if (Double.TYPE.equals(param)) {
            mv.visitInsn(Opcodes.ICONST_0 + index[0]);
            mv.visitVarInsn(Opcodes.DLOAD, stack[0]);
            mv.visitMethodInsn(INVOKESTATIC, "java/lang/Double", "valueOf", "(D)Ljava/lang/Double;");
            mv.visitInsn(AASTORE);
            if (index[0] < numberOfParameters - 1) {
                mv.visitInsn(DUP);
            }
            stack[0]++; // double occupies two positions

        } else if (Long.TYPE.equals(param)) {
            mv.visitInsn(Opcodes.ICONST_0 + index[0]);
            mv.visitVarInsn(Opcodes.LLOAD, stack[0]);
            mv.visitMethodInsn(INVOKESTATIC, "java/lang/Long", "valueOf", "(J)Ljava/lang/Long;");
            mv.visitInsn(AASTORE);
            if (index[0] < numberOfParameters - 1) {
                mv.visitInsn(DUP);
            }
            stack[0]++; // long occupies two positions
        } else {
            // object type
            mv.visitInsn(Opcodes.ICONST_0 + index[0]);
            mv.visitVarInsn(ALOAD, stack[0]);
            mv.visitInsn(AASTORE);
            if (index[0] < numberOfParameters - 1) {
                mv.visitInsn(DUP);
            }
        }
        index[0]++;
    }
    // TODO other primitive types
    stack[0]++;

}