Example usage for org.objectweb.asm Opcodes INSTANCEOF

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

Introduction

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

Prototype

int INSTANCEOF

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

Click Source Link

Usage

From source file:edu.mit.streamjit.util.bytecode.MethodResolver.java

License:Open Source License

private void interpret(TypeInsnNode insn, FrameState frame, BBInfo block) {
    Klass k = getKlassByInternalName(insn.desc);
    ReferenceType t = typeFactory.getReferenceType(k);
    switch (insn.getOpcode()) {
    case Opcodes.NEW:
        UninitializedValue newValue = newValues.get(insn);
        assert newValue != null;
        assert newValue.getType().equals(t);
        frame.stack.push(newValue);/*from  ww w  . j  a v  a2 s .com*/
        break;
    case Opcodes.CHECKCAST:
        CastInst c = new CastInst(t, frame.stack.pop());
        block.block.instructions().add(c);
        frame.stack.push(c);
        break;
    case Opcodes.INSTANCEOF:
        InstanceofInst ioi = new InstanceofInst(t, frame.stack.pop());
        block.block.instructions().add(ioi);
        frame.stack.push(ioi);
        break;
    case Opcodes.ANEWARRAY:
        ArrayType at = typeFactory.getArrayType(module.getArrayKlass(k, 1));
        NewArrayInst nai = new NewArrayInst(at, frame.stack.pop());
        block.block.instructions().add(nai);
        frame.stack.push(nai);
        break;
    default:
        throw new UnsupportedOperationException("" + insn.getOpcode());
    }
}

From source file:edu.mit.streamjit.util.bytecode.MethodUnresolver.java

License:Open Source License

private void emit(InstanceofInst i, InsnList insns) {
    load(i.getOperand(0), insns);/*from   ww w  .  ja va2s .c  om*/
    insns.add(new TypeInsnNode(Opcodes.INSTANCEOF, internalName(i.getTestType().getKlass())));
    store(i, insns);
}

From source file:erjang.ETuple.java

License:Apache License

private static void create_cast2(ClassAdapter cw, int n) {
    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "cast",
            "(L" + Type.getInternalName(EObject.class) + ";)L" + ETUPLE_NAME + n + ";", null, null);
    mv.visitCode();//ww  w . j  a  v a2 s .c o m

    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitInsn(Opcodes.DUP);
    mv.visitTypeInsn(Opcodes.INSTANCEOF, ETUPLE_NAME + n);

    Label fail = new Label();

    mv.visitJumpInsn(Opcodes.IFEQ, fail);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitTypeInsn(Opcodes.CHECKCAST, ETUPLE_NAME + n);
    mv.visitInsn(Opcodes.ARETURN);

    mv.visitLabel(fail);
    mv.visitInsn(Opcodes.ACONST_NULL);
    mv.visitInsn(Opcodes.ARETURN);

    mv.visitMaxs(2, 2);
    mv.visitEnd();
}

From source file:org.actorsguildframework.internal.codegenerator.ActorProxyCreator.java

License:Apache License

/**
 * Writes a proxy method for messages./*w  w w  .j av a  2s  .  co m*/
 * @param classNameInternal the internal class name
 * @param classNameDescriptor the class name descriptor
 * @param cw the ClassWriter
 * @param index the message index
 * @param type the ActorState type to use
 * @param concurrencyModel the concurrency model of the message
 * @param messageDescriptor the message's descriptor
 * @param method the method to override
 * @param simpleDescriptor a simple descriptor of the message
 * @param genericSignature the signature of the message
 */
private static void writeProxyMethod(String classNameInternal, String classNameDescriptor, ClassWriter cw,
        int index, Type actorState, ConcurrencyModel concurrencyModel, MessageImplDescriptor messageDescriptor,
        Method method, String simpleDescriptor, String genericSignature) throws NoSuchMethodException {
    MethodVisitor mv;
    {
        mv = cw.visitMethod(Opcodes.ACC_PUBLIC, method.getName(), simpleDescriptor, genericSignature, null);
        mv.visitCode();
        Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitIntInsn(Opcodes.BIPUSH, method.getParameterTypes().length);
        mv.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/Object");
        for (int j = 0; j < method.getParameterTypes().length; j++) {
            mv.visitInsn(Opcodes.DUP);
            mv.visitIntInsn(Opcodes.BIPUSH, j);
            Class<?> paraType = method.getParameterTypes()[j];
            if (paraType.isPrimitive()) {
                String wrapperClass = GenerationUtils.getWrapperInternalName(paraType);
                Type primType = Type.getType(paraType);
                mv.visitVarInsn(primType.getOpcode(Opcodes.ILOAD), j + 1);
                mv.visitMethodInsn(Opcodes.INVOKESTATIC, wrapperClass, "valueOf",
                        "(" + primType.getDescriptor() + ")" + "L" + wrapperClass + ";");
            } else if (isArgumentFreezingRequired(method, j, paraType)) {
                mv.visitVarInsn(Opcodes.ALOAD, j + 1);
                mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(SerializableFreezer.class),
                        "freeze",
                        Type.getMethodDescriptor(SerializableFreezer.class.getMethod("freeze", Object.class)));
            } else if (paraType.isInterface()) {
                mv.visitVarInsn(Opcodes.ALOAD, j + 1);
                mv.visitInsn(Opcodes.DUP);
                mv.visitTypeInsn(Opcodes.INSTANCEOF, "org/actorsguildframework/Actor");
                Label lEndif = new Label();
                mv.visitJumpInsn(Opcodes.IFNE, lEndif);
                mv.visitTypeInsn(Opcodes.NEW, Type.getInternalName(ActorRuntimeException.class));
                mv.visitInsn(Opcodes.DUP);
                mv.visitLdcInsn(String.format(
                        "Argument %d is an non-Serializable interface, but you did not give an Actor. If a message's argument type is an interface that does not extend Serializable, only Actors are acceptable as argument.",
                        j));
                mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(ActorRuntimeException.class),
                        "<init>", "(Ljava/lang/String;)V");
                mv.visitInsn(Opcodes.ATHROW);
                mv.visitLabel(lEndif);
            } else
                mv.visitVarInsn(Opcodes.ALOAD, j + 1);

            mv.visitInsn(Opcodes.AASTORE);
        }
        Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitVarInsn(Opcodes.ASTORE, method.getParameterTypes().length + 1); // method.getParameterTypes().length+1 ==> 'args' local variable
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitFieldInsn(Opcodes.GETFIELD, classNameInternal, "actorState__ACTORPROXY",
                actorState.getDescriptor());
        mv.visitFieldInsn(Opcodes.GETSTATIC, classNameInternal,
                String.format(MESSAGE_CALLER_NAME_FORMAT, index),
                "Lorg/actorsguildframework/internal/MessageCaller;");
        mv.visitFieldInsn(Opcodes.GETSTATIC, "org/actorsguildframework/annotations/ThreadUsage",
                messageDescriptor.getThreadUsage().name(),
                "Lorg/actorsguildframework/annotations/ThreadUsage;");
        mv.visitVarInsn(Opcodes.ALOAD, method.getParameterTypes().length + 1);
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, actorState.getInternalName(), "queueMessage",
                "(Lorg/actorsguildframework/internal/MessageCaller;Lorg/actorsguildframework/annotations/ThreadUsage;[Ljava/lang/Object;)Lorg/actorsguildframework/internal/AsyncResultImpl;");
        mv.visitInsn(Opcodes.ARETURN);
        Label l4 = new Label();
        mv.visitLabel(l4);
        mv.visitLocalVariable("this", classNameDescriptor, null, l0, l4, 0);
        for (int j = 0; j < method.getParameterTypes().length; j++)
            mv.visitLocalVariable("arg" + j, Type.getDescriptor(method.getParameterTypes()[j]),
                    GenericTypeHelper.getSignatureIfGeneric(method.getGenericParameterTypes()[j]), l0, l4,
                    j + 1);
        mv.visitLocalVariable("args", "[Ljava/lang/Object;", null, l1, l4,
                method.getParameterTypes().length + 1);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
}

From source file:org.copperengine.core.instrument.TryCatchBlockHandler.java

License:Apache License

@SuppressWarnings("unchecked")
public void instrument(ClassNode cn) {
    // if (1 == 1) return;

    for (MethodNode m : (List<MethodNode>) cn.methods) {
        if (!m.exceptions.contains(INTERRUPT_EXCEPTION_NAME) || m.tryCatchBlocks.isEmpty()) {
            continue;
        }//  ww  w  . j  ava2s.  co  m
        logger.info("Instrument " + cn.name + "." + m.name);
        HashSet<Label> labels = new HashSet<Label>();
        for (TryCatchBlockNode catchNode : (List<TryCatchBlockNode>) m.tryCatchBlocks) {
            if (labels.contains(catchNode.handler.getLabel())) {
                // some handlers share their handling code - check it out to prevent double instrumentation
                logger.info("skipping node");
                continue;
            }
            labels.add(catchNode.handler.getLabel());

            LabelNode labelNode = catchNode.handler;
            AbstractInsnNode lineNumberNode = labelNode.getNext() instanceof LineNumberNode
                    ? labelNode.getNext()
                    : labelNode;
            FrameNode frameNode = (FrameNode) lineNumberNode.getNext();
            VarInsnNode varInsnNode = (VarInsnNode) frameNode.getNext();
            AbstractInsnNode insertPoint = varInsnNode;

            if (catchNode.type == null) {
                // this is probably a finally block;
                if (insertPoint.getNext() != null && insertPoint.getNext() instanceof LabelNode) {
                    insertPoint = insertPoint.getNext();
                }
            }

            LabelNode labelNode4ifeg = new LabelNode();
            InsnList newCode = new InsnList();
            newCode.add(new VarInsnNode(Opcodes.ALOAD, varInsnNode.var));
            newCode.add(new TypeInsnNode(Opcodes.INSTANCEOF, INTERRUPT_EXCEPTION_NAME));
            newCode.add(new JumpInsnNode(Opcodes.IFEQ, labelNode4ifeg));
            newCode.add(new VarInsnNode(Opcodes.ALOAD, varInsnNode.var));
            newCode.add(new TypeInsnNode(Opcodes.CHECKCAST, INTERRUPT_EXCEPTION_NAME));
            newCode.add(new InsnNode(Opcodes.ATHROW));
            newCode.add(labelNode4ifeg);
            m.instructions.insert(insertPoint, newCode);
        }
    }
}

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) {
    /********//  w ww .j  a v  a2s .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.evosuite.graphs.cfg.BytecodeInstruction.java

License:Open Source License

/**
 * <p>//w w w . jav  a2  s. co  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   www  .  ja  v a  2 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.BooleanValueInterpreter.java

License:Open Source License

/** {@inheritDoc} */
@Override/*from w  w  w.j  av  a  2 s  .  co  m*/
public BasicValue unaryOperation(AbstractInsnNode insn, BasicValue value) throws AnalyzerException {
    if (insn.getOpcode() == Opcodes.INSTANCEOF) {
        return BOOLEAN_VALUE;
    } else if (insn.getOpcode() == Opcodes.GETFIELD) {
        FieldInsnNode fieldNode = (FieldInsnNode) insn;
        if (BooleanTestabilityTransformation.isTransformedField(fieldNode.owner, fieldNode.name,
                fieldNode.desc))
            return BOOLEAN_VALUE;
    }
    return super.unaryOperation(insn, value);
}

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

License:Open Source License

@Override
public void visitTypeInsn(int opcode, String type) {

    if (opcode == Opcodes.CHECKCAST) {
        Label origTarget = new Label();
        // Label origTarget = new AnnotatedLabel();
        // origTarget.info = Boolean.FALSE;
        mv.visitInsn(Opcodes.DUP);// w w w  .j av  a2s . c  om
        mv.tagBranch();
        mv.visitJumpInsn(Opcodes.IFNULL, origTarget);
        mv.visitInsn(Opcodes.DUP);
        mv.visitTypeInsn(Opcodes.INSTANCEOF, type);
        mv.tagBranch();
        mv.visitJumpInsn(Opcodes.IFNE, origTarget);
        mv.visitTypeInsn(Opcodes.NEW, "java/lang/ClassCastException");
        mv.visitInsn(Opcodes.DUP);
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/ClassCastException", "<init>", "()V", false);
        mv.visitInsn(Opcodes.ATHROW);
        mv.visitLabel(origTarget);
        mv.tagBranchExit();
    }
}