Example usage for org.objectweb.asm Opcodes ANEWARRAY

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

Introduction

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

Prototype

int ANEWARRAY

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

Click Source Link

Usage

From source file:org.brutusin.instrumentation.Instrumentator.java

License:Apache License

private void addGetMethodInvocation(InsnList il) {
    il.add(TreeInstructions.getPushInstruction(this.methodArguments.length));
    il.add(new TypeInsnNode(Opcodes.ANEWARRAY, "java/lang/Class"));
    int parameterClassesIndex = getFistAvailablePosition();
    il.add(new VarInsnNode(Opcodes.ASTORE, parameterClassesIndex));
    this.mn.maxLocals++;
    for (int i = 0; i < this.methodArguments.length; i++) {
        il.add(new VarInsnNode(Opcodes.ALOAD, parameterClassesIndex));
        il.add(TreeInstructions.getPushInstruction(i));
        il.add(TreeInstructions.getClassReferenceInstruction(methodArguments[i], cn.version & 0xFFFF));
        il.add(new InsnNode(Opcodes.AASTORE));
    }/* w w w. j  a  v  a2 s  .c o m*/
    il.add(TreeInstructions.getClassConstantReference(this.classType, cn.version & 0xFFFF));
    il.add(new LdcInsnNode(this.mn.name));
    il.add(new VarInsnNode(Opcodes.ALOAD, parameterClassesIndex));
    il.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "org/brutusin/instrumentation/utils/Helper", "getSource",
            "(Ljava/lang/Class;Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/Object;", false));
}

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

License:Open Source License

@Override
public void visitTypeInsn(final int opcode, final String type) {
    assert type != null;
    final T t = getDu().getT(type);

    switch (opcode) {
    /********//  ww w .  j a  va2s  .c o  m
     * CAST *
     ********/
    case Opcodes.CHECKCAST:
        add(new CAST(this.ops.size(), opcode, this.line, T.REF, t));
        break;
    /**************
     * INSTANCEOF *
     **************/
    case Opcodes.INSTANCEOF:
        add(new INSTANCEOF(this.ops.size(), opcode, this.line, t));
        break;
    /*******
     * NEW *
     *******/
    case Opcodes.NEW:
        add(new NEW(this.ops.size(), opcode, this.line, t));
        break;
    /************
     * NEWARRAY *
     ************/
    case Opcodes.ANEWARRAY:
        add(new NEWARRAY(this.ops.size(), opcode, this.line, getDu().getArrayT(t), 1));
        break;
    default:
        log.warn(getM() + ": Unknown var insn opcode '" + opcode + "'!");
    }
}

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

License:Open Source License

/**
 * Returns instructions, that are needed to pack all arguments of a method
 * in an {@link Object} Array//from   ww w .j  a v a2 s  . c  om
 * @param args The Types of the arguments
 * @param isStatic is this method static or not
 * @return
 */
protected InsnList getBoxingInstructions(Type[] args, boolean isStatic) {
    int firstArgIndex = 1;
    if (isStatic) {
        firstArgIndex = 0;
    }
    InsnList instructions = new InsnList();
    instructions.add(createLoadIntConstant(args.length));
    instructions.add(new TypeInsnNode(Opcodes.ANEWARRAY, ClassNames.OBJECT_SLASH));
    for (int i = 0, slot = 0; i < args.length; slot += args[i++].getSize()) {
        instructions.add(new InsnNode(Opcodes.DUP));
        instructions.add(createLoadIntConstant(i));
        instructions.add(new IntInsnNode(args[i].getOpcode(Opcodes.ILOAD), slot + firstArgIndex));
        if (args[i].getSort() != Type.OBJECT && args[i].getSort() != Type.ARRAY) {
            instructions.add(AsmTypeHelper.getBoxingInstructionForType(args[i]));
        }
        instructions.add(new InsnNode(Opcodes.AASTORE));
    }

    return instructions;
}

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://from   www.j  ava 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.BytecodeInstruction.java

License:Open Source License

/**
 * <p>//from   w w w .jav  a 2s  . c o  m
 * getASMNodeString
 * </p>
 * 
 * @return a {@link java.lang.String} object.
 */
public String getASMNodeString() {
    String type = getType();
    String opcode = getInstructionType();

    String stack = "";
    if (frame == null)
        stack = "null";
    else
        for (int i = 0; i < frame.getStackSize(); i++) {
            stack += frame.getStack(i) + ",";
        }

    if (asmNode instanceof LabelNode) {
        return "LABEL " + ((LabelNode) asmNode).getLabel().toString();
    } else if (asmNode instanceof FieldInsnNode)
        return "Field" + " " + ((FieldInsnNode) asmNode).owner + "." + ((FieldInsnNode) asmNode).name + " Type="
                + type + ", Opcode=" + opcode;
    else if (asmNode instanceof FrameNode)
        return "Frame" + " " + asmNode.getOpcode() + " Type=" + type + ", Opcode=" + opcode;
    else if (asmNode instanceof IincInsnNode)
        return "IINC " + ((IincInsnNode) asmNode).var + " Type=" + type + ", Opcode=" + opcode;
    else if (asmNode instanceof InsnNode)
        return "" + opcode;
    else if (asmNode instanceof IntInsnNode)
        return "INT " + ((IntInsnNode) asmNode).operand + " Type=" + type + ", Opcode=" + opcode;
    else if (asmNode instanceof MethodInsnNode)
        return opcode + " " + ((MethodInsnNode) asmNode).owner + "." + ((MethodInsnNode) asmNode).name
                + ((MethodInsnNode) asmNode).desc;
    else if (asmNode instanceof JumpInsnNode)
        return "JUMP " + ((JumpInsnNode) asmNode).label.getLabel() + " Type=" + type + ", Opcode=" + opcode
                + ", Stack: " + stack + " - Line: " + lineNumber;
    else if (asmNode instanceof LdcInsnNode)
        return "LDC " + ((LdcInsnNode) asmNode).cst + " Type=" + type; // +
    // ", Opcode=";
    // + opcode; // cst starts with mutationid if
    // this is location of mutation
    else if (asmNode instanceof LineNumberNode)
        return "LINE " + " " + ((LineNumberNode) asmNode).line;
    else if (asmNode instanceof LookupSwitchInsnNode)
        return "LookupSwitchInsnNode" + " " + asmNode.getOpcode() + " Type=" + type + ", Opcode=" + opcode;
    else if (asmNode instanceof MultiANewArrayInsnNode)
        return "MULTIANEWARRAY " + " " + asmNode.getOpcode() + " Type=" + type + ", Opcode=" + opcode;
    else if (asmNode instanceof TableSwitchInsnNode)
        return "TableSwitchInsnNode" + " " + asmNode.getOpcode() + " Type=" + type + ", Opcode=" + opcode;
    else if (asmNode instanceof TypeInsnNode) {
        switch (asmNode.getOpcode()) {
        case Opcodes.NEW:
            return "NEW " + ((TypeInsnNode) asmNode).desc;
        case Opcodes.ANEWARRAY:
            return "ANEWARRAY " + ((TypeInsnNode) asmNode).desc;
        case Opcodes.CHECKCAST:
            return "CHECKCAST " + ((TypeInsnNode) asmNode).desc;
        case Opcodes.INSTANCEOF:
            return "INSTANCEOF " + ((TypeInsnNode) asmNode).desc;
        default:
            return "Unknown node" + " Type=" + type + ", Opcode=" + opcode;
        }
    }
    // return "TYPE " + " " + node.getOpcode() + " Type=" + type
    // + ", Opcode=" + opcode;
    else if (asmNode instanceof VarInsnNode)
        return opcode + " " + ((VarInsnNode) asmNode).var;
    else
        return "Unknown node" + " Type=" + type + ", 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/*from   w w w  . jav a 2 s . 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   ww w . ja  v  a 2  s.com
public void visitTypeInsn(int opcode, String type) {

    if (opcode == Opcodes.ANEWARRAY) {
        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.visitTypeInsn(opcode, type);
}

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

License:Open Source License

@Override
public void visitTypeInsn(int opcode, String type) {
    if (opcode == Opcodes.ANEWARRAY) {
        mv.visitInsn(Opcodes.DUP);/*from  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

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/* www  . j a va 2 s.com*/
    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.glassfish.pfl.tf.spi.Util.java

License:Open Source License

public void wrapArgs(MethodVisitor mv, int access, String desc) {
    info(2, "Wrapping args for descriptor " + desc);

    Type[] atypes = Type.getArgumentTypes(desc);
    emitIntConstant(mv, atypes.length);/*from   w w  w  . ja  v a  2  s .c  o m*/
    mv.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/Object");

    int argIndex;
    if ((access & Opcodes.ACC_STATIC) == Opcodes.ACC_STATIC) {
        argIndex = 0;
    } else {
        argIndex = 1;
    }

    for (int ctr = 0; ctr < atypes.length; ctr++) {
        mv.visitInsn(Opcodes.DUP);
        emitIntConstant(mv, ctr);
        argIndex = wrapArg(mv, argIndex, atypes[ctr]);
        mv.visitInsn(Opcodes.AASTORE);
    }
}