Example usage for org.objectweb.asm Opcodes NEWARRAY

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

Introduction

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

Prototype

int NEWARRAY

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

Click Source Link

Usage

From source file:org.adjective.stout.operation.CreateArrayExpression.java

License:Apache License

public void getInstructions(ExecutionStack stack, InstructionCollector collector) {
    addInstruction(collector, ConstantIntegerExpression.getInstruction(_elements.length));

    Instruction store;//from  w  w w .  j  a  va 2s .co m
    Class<?> componentClass = _componentType.getRawClass();
    if (componentClass.isPrimitive()) {
        addInstruction(collector, new IntInstruction(Opcodes.NEWARRAY, getTypeCode(componentClass)));
        store = new GenericInstruction(Type.getType(componentClass).getOpcode(Opcodes.IASTORE));
    } else {
        addInstruction(collector, new TypeInstruction(Opcodes.ANEWARRAY, _componentType.getInternalName()));
        store = new GenericInstruction(Opcodes.AASTORE);
    }

    for (int i = 0; i < _elements.length; i++) {
        addInstruction(collector, DUPLICATE);
        addInstruction(collector, ConstantIntegerExpression.getInstruction(i));
        _elements[i].getInstructions(stack, collector);
        addInstruction(collector, store);
    }

}

From source file:org.decojer.cavaj.readers.asm.ReadMethodVisitor.java

License:Open Source License

@Override
public void visitIntInsn(final int opcode, final int operand) {
    switch (opcode) {
    /********//from  w w  w.  ja  va  2  s.  c om
     * PUSH *
     ********/
    case Opcodes.BIPUSH:
    case Opcodes.SIPUSH:
        add(new PUSH(this.ops.size(), opcode, this.line, T.getJvmIntT(operand), operand));
        break;
    /************
     * NEWARRAY *
     ************/
    case Opcodes.NEWARRAY: {
        final T t = T.TYPES[operand];
        assert t != null;
        add(new NEWARRAY(this.ops.size(), opcode, this.line, getDu().getArrayT(t), 1));
        break;
    }
    default:
        log.warn(getM() + ": Unknown int insn opcode '" + opcode + "'!");
    }
}

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// ww  w. ja va  2s .  c  o  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.ArrayAllocationLimitMethodAdapter.java

License:Open Source License

/** {@inheritDoc} */
@Override/*from   www.  j  a v  a  2s  .  c o  m*/
public void visitIntInsn(int opcode, int operand) {
    if (opcode == Opcodes.NEWARRAY) {
        Label origTarget = new Label();
        visitInsn(Opcodes.DUP);
        visitFieldInsn(Opcodes.GETSTATIC, PackageInfo.getNameWithSlash(org.evosuite.Properties.class),
                "ARRAY_LIMIT", "I");
        super.visitJumpInsn(Opcodes.IF_ICMPLT, origTarget);
        super.visitTypeInsn(Opcodes.NEW, PackageInfo.getNameWithSlash(TestCaseExecutor.TimeoutExceeded.class));
        super.visitInsn(Opcodes.DUP);
        super.visitMethodInsn(Opcodes.INVOKESPECIAL,
                PackageInfo.getNameWithSlash(TestCaseExecutor.TimeoutExceeded.class), "<init>", "()V", false);
        super.visitInsn(Opcodes.ATHROW);
        super.visitLabel(origTarget);

    }
    super.visitIntInsn(opcode, operand);
}

From source file:org.evosuite.instrumentation.error.ArrayInstrumentation.java

License:Open Source License

@Override
public void visitIntInsn(int opcode, int operand) {
    if (opcode == Opcodes.NEWARRAY) {
        mv.visitInsn(Opcodes.DUP);// w w w  .ja v  a 2s .c  o  m
        insertBranch(Opcodes.IFGE, "java/lang/NegativeArraySizeException");
    }
}

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

License:Open Source License

@SuppressWarnings("unchecked")
private void addFieldRegistryRegisterCall(final MethodNode methodNode) {
    AbstractInsnNode ins = null;//  w  w w. j a va  2s.  c  om
    ListIterator<AbstractInsnNode> iter = methodNode.instructions.iterator();

    int numInvokeSpecials = 0; // number of invokespecial calls before actual constructor call

    while (iter.hasNext()) {
        ins = iter.next();

        if (ins instanceof MethodInsnNode) {
            MethodInsnNode mins = (MethodInsnNode) ins;
            if (ins.getOpcode() == Opcodes.INVOKESPECIAL) {
                if (mins.name.startsWith("<init>")) {
                    if (numInvokeSpecials == 0) {
                        break;
                    } else {
                        numInvokeSpecials--;
                    }
                }
            }
        } else if (ins instanceof TypeInsnNode) {
            TypeInsnNode typeIns = (TypeInsnNode) ins;
            if (typeIns.getOpcode() == Opcodes.NEW || typeIns.getOpcode() == Opcodes.NEWARRAY) {
                numInvokeSpecials++;
            }
        }
    }

    final InsnList instructions = new InsnList();

    instructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
    instructions.add(new MethodInsnNode(Opcodes.INVOKESTATIC, PackageInfo.getNameWithSlash(FieldRegistry.class),
            "register", "(Ljava/lang/Object;)V"));

    methodNode.instructions.insert(ins, instructions);
}

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

License:Open Source License

/**
 *    public int myMethod(int i)/*from   w w w .j  a v a2 s. c o  m*/
   {
 try
 {
    return _sw_prototype_original_myMethod(i)
 }
 finally
 {
    Capturer.enable();
 }
   }
        
 * @param classNode
 * @param className
 * @param methodNode
 */
@SuppressWarnings("unchecked")
private MethodNode wrapMethod(final ClassNode classNode, final String className, final MethodNode methodNode) {
    methodNode.maxStack += 4;

    // create wrapper for original method
    final MethodNode wrappingMethodNode = new MethodNode(methodNode.access, methodNode.name, methodNode.desc,
            methodNode.signature,
            (String[]) methodNode.exceptions.toArray(new String[methodNode.exceptions.size()]));
    wrappingMethodNode.maxStack = methodNode.maxStack;

    // assign annotations to wrapping method
    wrappingMethodNode.visibleAnnotations = methodNode.visibleAnnotations;
    wrappingMethodNode.visibleParameterAnnotations = methodNode.visibleParameterAnnotations;

    // remove annotations from wrapped method to avoid wrong behavior controlled by annotations
    methodNode.visibleAnnotations = null;
    methodNode.visibleParameterAnnotations = null;

    // rename original method
    methodNode.access = TransformerUtil.modifyVisibility(methodNode.access, Opcodes.ACC_PRIVATE);

    final LabelNode l0 = new LabelNode();
    final LabelNode l1 = new LabelNode();
    final LabelNode l2 = new LabelNode();

    final InsnList wInstructions = wrappingMethodNode.instructions;

    if ("<init>".equals(methodNode.name)) {
        // wrap a constructor 

        methodNode.name = WRAP_NAME_PREFIX + "init" + WRAP_NAME_PREFIX;

        // move call to other constructors to new method
        AbstractInsnNode ins = null;
        ListIterator<AbstractInsnNode> iter = methodNode.instructions.iterator();

        int numInvokeSpecials = 0; // number of invokespecial calls before actual constructor call

        while (iter.hasNext()) {
            ins = iter.next();
            iter.remove();
            wInstructions.add(ins);

            if (ins instanceof MethodInsnNode) {
                MethodInsnNode mins = (MethodInsnNode) ins;
                if (ins.getOpcode() == Opcodes.INVOKESPECIAL) {
                    if (mins.name.startsWith("<init>")) {
                        if (numInvokeSpecials == 0) {
                            break;
                        } else {
                            numInvokeSpecials--;
                        }
                    }
                }
            } else if (ins instanceof TypeInsnNode) {
                TypeInsnNode typeIns = (TypeInsnNode) ins;
                if (typeIns.getOpcode() == Opcodes.NEW || typeIns.getOpcode() == Opcodes.NEWARRAY) {
                    numInvokeSpecials++;
                }
            }
        }
    } else {
        methodNode.name = WRAP_NAME_PREFIX + methodNode.name;
    }

    int varReturnValue = 0;

    final Type returnType = Type.getReturnType(methodNode.desc);

    if (returnType.equals(Type.VOID_TYPE)) {
        wrappingMethodNode.tryCatchBlocks.add(new TryCatchBlockNode(l0, l1, l1, "java/lang/Throwable"));

    } else {

        wrappingMethodNode.tryCatchBlocks.add(new TryCatchBlockNode(l0, l1, l2, "java/lang/Throwable"));

        //--- create "Object returnValue = null;"

        if (!TransformerUtil.isStatic(methodNode.access)) {
            // load "this"
            varReturnValue++;
        }

        // consider method arguments to find right variable index
        final Type[] argTypes = Type.getArgumentTypes(methodNode.desc);
        for (int i = 0; i < argTypes.length; i++) {
            varReturnValue++;

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

        // push NULL on the stack and initialize variable for return value for it
        wInstructions.add(new InsnNode(Opcodes.ACONST_NULL));
        wInstructions.add(new VarInsnNode(Opcodes.ASTORE, varReturnValue));
    }

    int var = 0;

    // --- L0
    wInstructions.add(l0);

    wInstructions.add(this.addCaptureCall(TransformerUtil.isStatic(methodNode.access), className,
            wrappingMethodNode.name, wrappingMethodNode.desc, Type.getArgumentTypes(methodNode.desc)));

    // --- construct call to wrapped methode

    if (!TransformerUtil.isStatic(methodNode.access)) {
        // load "this" to call method
        wInstructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
        var++;
    }

    final Type[] argTypes = Type.getArgumentTypes(methodNode.desc);
    for (int i = 0; i < argTypes.length; i++) {
        this.addLoadInsn(wInstructions, argTypes[i], var++);

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

    if (TransformerUtil.isStatic(methodNode.access)) {
        wInstructions.add(
                new MethodInsnNode(Opcodes.INVOKESTATIC, classNode.name, methodNode.name, methodNode.desc));
    } else {
        wInstructions.add(
                new MethodInsnNode(Opcodes.INVOKEVIRTUAL, classNode.name, methodNode.name, methodNode.desc));
    }

    var++;

    if (returnType.equals(Type.VOID_TYPE)) {
        wInstructions.add(new JumpInsnNode(Opcodes.GOTO, l2));

        // --- L1

        wInstructions.add(l1);

        wInstructions.add(new FrameNode(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/Throwable" }));

        wInstructions.add(new VarInsnNode(Opcodes.ASTORE, --var));

        this.addCaptureEnableStatement(className, methodNode, wInstructions, -1);

        wInstructions.add(new VarInsnNode(Opcodes.ALOAD, var));
        wInstructions.add(new InsnNode(Opcodes.ATHROW));

        // FIXME <--- DUPLICATE CODE

        // --- L2

        wInstructions.add(l2);
        wInstructions.add(new FrameNode(Opcodes.F_SAME, 0, null, 0, null));

        this.addCaptureEnableStatement(className, methodNode, wInstructions, -1);

        wInstructions.add(new InsnNode(Opcodes.RETURN));
    } else {
        // construct store of the wrapped method call's result

        this.addBoxingStmt(wInstructions, returnType);

        wInstructions.add(new VarInsnNode(Opcodes.ASTORE, varReturnValue));
        wInstructions.add(new VarInsnNode(Opcodes.ALOAD, varReturnValue));

        this.addUnBoxingStmt(wInstructions, returnType);

        final int storeOpcode = returnType.getOpcode(Opcodes.ISTORE);
        wInstructions.add(new VarInsnNode(storeOpcode, ++var)); // might be only var

        // --- L1

        wInstructions.add(l1);

        this.addCaptureEnableStatement(className, methodNode, wInstructions, varReturnValue);

        // construct load of the wrapped method call's result
        int loadOpcode = returnType.getOpcode(Opcodes.ILOAD);
        wInstructions.add(new VarInsnNode(loadOpcode, var));

        // construct return of the wrapped method call's result
        this.addReturnInsn(wInstructions, returnType);

        //---- L2

        wInstructions.add(l2);

        wInstructions.add(
                new FrameNode(Opcodes.F_FULL, 2, new Object[] { className, this.getInternalName(returnType) },
                        1, new Object[] { "java/lang/Throwable" }));
        wInstructions.add(new VarInsnNode(Opcodes.ASTORE, --var));

        this.addCaptureEnableStatement(className, methodNode, wInstructions, varReturnValue);

        wInstructions.add(new VarInsnNode(Opcodes.ALOAD, var));
        wInstructions.add(new InsnNode(Opcodes.ATHROW));
    }
    transformWrapperCalls(methodNode);
    return wrappingMethodNode;
}

From source file:org.jacoco.core.internal.instr.FrameTracker.java

License:Open Source License

@Override
public void visitIntInsn(final int opcode, final int operand) {
    switch (opcode) {
    case Opcodes.BIPUSH:
    case Opcodes.SIPUSH:
        push(Opcodes.INTEGER);/* w ww  . j ava  2s  .co  m*/
        break;
    case Opcodes.NEWARRAY:
        pop(1);
        switch (operand) {
        case Opcodes.T_BOOLEAN:
            push("[Z");
            break;
        case Opcodes.T_CHAR:
            push("[C");
            break;
        case Opcodes.T_FLOAT:
            push("[F");
            break;
        case Opcodes.T_DOUBLE:
            push("[D");
            break;
        case Opcodes.T_BYTE:
            push("[B");
            break;
        case Opcodes.T_SHORT:
            push("[S");
            break;
        case Opcodes.T_INT:
            push("[I");
            break;
        case Opcodes.T_LONG:
            push("[J");
            break;
        default:
            throw new IllegalArgumentException();
        }
        break;
    default:
        throw new IllegalArgumentException();
    }
    mv.visitIntInsn(opcode, operand);
}

From source file:org.jboss.byteman.agent.adapter.RuleGeneratorAdapter.java

License:Open Source License

/**
 * Generates the instruction to create a new array.
 *
 * @param type the type of the array elements.
 *//*from ww w  .jav  a2s  .com*/
public void newArray(final Type type) {
    int typ;
    switch (type.getSort()) {
    case Type.BOOLEAN:
        typ = Opcodes.T_BOOLEAN;
        break;
    case Type.CHAR:
        typ = Opcodes.T_CHAR;
        break;
    case Type.BYTE:
        typ = Opcodes.T_BYTE;
        break;
    case Type.SHORT:
        typ = Opcodes.T_SHORT;
        break;
    case Type.INT:
        typ = Opcodes.T_INT;
        break;
    case Type.FLOAT:
        typ = Opcodes.T_FLOAT;
        break;
    case Type.LONG:
        typ = Opcodes.T_LONG;
        break;
    case Type.DOUBLE:
        typ = Opcodes.T_DOUBLE;
        break;
    default:
        typeInsn(Opcodes.ANEWARRAY, type);
        return;
    }
    visitIntInsn(Opcodes.NEWARRAY, typ);
}

From source file:org.jboss.byteman.rule.expression.ArrayInitExpression.java

License:Open Source License

public void compile(MethodVisitor mv, CompileContext compileContext) throws CompileException {
    // make sure we are at the right source line
    compileContext.notifySourceLine(line);

    Type baseType = getType().getBaseType();
    int currentStack = compileContext.getStackCount();
    int expected = 1;
    int length = elements.size();

    // stack array size and then create the array
    mv.visitLdcInsn(length);//from www  . ja  v a  2s.c o  m
    compileContext.addStackCount(1);
    // new array pops count and pushes array so no change to stack size
    if (baseType.isArray()) {
        mv.visitMultiANewArrayInsn(getType().getInternalName(), 1);
    } else if (baseType.isObject()) {
        mv.visitTypeInsn(Opcodes.ANEWARRAY, baseType.getInternalName());
    } else {
        int operand = 0;
        if (baseType.equals(Type.Z)) {
            operand = Opcodes.T_BOOLEAN;
        } else if (baseType.equals(Type.B)) {
            operand = Opcodes.T_BYTE;
        } else if (baseType.equals(Type.S)) {
            operand = Opcodes.T_SHORT;
        } else if (baseType.equals(Type.C)) {
            operand = Opcodes.T_CHAR;
        } else if (baseType.equals(Type.I)) {
            operand = Opcodes.T_INT;
        } else if (baseType.equals(Type.J)) {
            operand = Opcodes.T_LONG;
        } else if (baseType.equals(Type.F)) {
            operand = Opcodes.T_FLOAT;
        } else if (baseType.equals(Type.D)) {
            operand = Opcodes.T_DOUBLE;
        }
        mv.visitIntInsn(Opcodes.NEWARRAY, operand);
    }

    int idx = 0;
    boolean isTwoWords = (baseType.getNBytes() > 4);

    for (Expression element : elements) {
        int toPop = 0;
        // copy array so we can assign it -- adds one to height
        mv.visitInsn(Opcodes.DUP);
        // compile expression index -- adds 1 to height
        mv.visitLdcInsn(idx++);
        compileContext.addStackCount(2);
        // compile value -- adds one or two words to height
        element.compile(mv, compileContext);
        // ensure we have the correct value type
        compileContext.compileTypeConversion(element.type, baseType);
        // now we can do the array store
        if (baseType.isObject() || baseType.isArray()) {
            // compile load object - pops 3
            mv.visitInsn(Opcodes.AASTORE);
            toPop = -3;
        } else if (baseType == Type.Z || baseType == Type.B) {
            // compile load byte - pops 3
            mv.visitInsn(Opcodes.BASTORE);
            toPop = -3;
        } else if (baseType == Type.S) {
            // compile load short - pops 3
            mv.visitInsn(Opcodes.SASTORE);
            toPop = -3;
        } else if (baseType == Type.C) {
            // compile load char - pops 3
            mv.visitInsn(Opcodes.CASTORE);
            toPop = -3;
        } else if (baseType == Type.I) {
            // compile load int - pops 3
            mv.visitInsn(Opcodes.IASTORE);
            toPop = -3;
        } else if (baseType == Type.J) {
            // compile load long - pops 4
            mv.visitInsn(Opcodes.LASTORE);
            toPop = -4;
        } else if (baseType == Type.F) {
            // compile load float - pops 3
            mv.visitInsn(Opcodes.FASTORE);
            toPop = -3;
        } else if (baseType == Type.D) {
            // compile load double - pops 4
            mv.visitInsn(Opcodes.DASTORE);
            toPop = -4;
        }
        // pop the appropriate number of elements off the stack
        compileContext.addStackCount(toPop);
    }

    // check stack height
    if (compileContext.getStackCount() != currentStack + expected) {
        throw new CompileException("ArrayInitExpression.compile : invalid stack height "
                + compileContext.getStackCount() + " expecting " + (currentStack + expected));
    }

    // no need to update stack max
}