Example usage for org.objectweb.asm Opcodes NEW

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

Introduction

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

Prototype

int NEW

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

Click Source Link

Usage

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

License:Open Source License

private void findBlockBoundaries() {
    InsnList insns = methodNode.instructions;
    //We find the indices of any block-ending instruction and of any jump
    //target, sort, remove duplicates, then use pairs to define blocks. Note
    //these are end-exclusive indices, thus one after the block-enders, but
    //right on the jump targets (they're one-past-the-end of the preceding
    //block)./*  w  w w  . j  av a 2 s . c om*/
    List<Integer> indices = new ArrayList<>();
    indices.add(0);
    for (int i = 0; i < insns.size(); ++i) {
        AbstractInsnNode insn = insns.get(i);
        int opcode = insn.getOpcode();
        //Terminator opcodes end blocks.
        if (insn instanceof JumpInsnNode || insn instanceof LookupSwitchInsnNode
                || insn instanceof TableSwitchInsnNode || opcode == Opcodes.ATHROW || opcode == Opcodes.IRETURN
                || opcode == Opcodes.LRETURN || opcode == Opcodes.FRETURN || opcode == Opcodes.DRETURN
                || opcode == Opcodes.ARETURN || opcode == Opcodes.RETURN) {
            indices.add(i + 1);
        }
        //Jump targets of this instruction end blocks.
        if (insn instanceof JumpInsnNode)
            indices.add(insns.indexOf(((JumpInsnNode) insn).label));
        else if (insn instanceof LookupSwitchInsnNode) {
            indices.add(insns.indexOf(((LookupSwitchInsnNode) insn).dflt));
            for (Object label : ((LookupSwitchInsnNode) insn).labels)
                indices.add(insns.indexOf((LabelNode) label));
        } else if (insn instanceof TableSwitchInsnNode) {
            indices.add(insns.indexOf(((TableSwitchInsnNode) insn).dflt));
            for (Object label : ((TableSwitchInsnNode) insn).labels)
                indices.add(insns.indexOf((LabelNode) label));
        }

        //While we're scanning the instructions, make the UninitializedValue
        //values for 'new' opcodes.
        if (opcode == Opcodes.NEW) {
            Klass k = getKlassByInternalName(((TypeInsnNode) insn).desc);
            ReferenceType t = typeFactory.getReferenceType(k);
            newValues.put(insn, new UninitializedValue(t, "new" + (counter++)));
        }
    }

    //Remove duplicates and sort via TreeSet.
    indices = new ArrayList<>(new TreeSet<>(indices));
    for (int i = 1; i < indices.size(); ++i)
        blocks.add(new BBInfo(indices.get(i - 1), indices.get(i), i - 1));
}

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   w ww.  j  a va  2s  .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(CallInst i, InsnList insns) {
    Method m = i.getMethod();/*from  w  w  w . j  av a  2s.c om*/
    boolean callingSuperCtor = false;
    if (m.isConstructor()) {
        //If we're calling super(), load this.
        //TODO: this will get confused if we call a superclass constructor
        //for any reason other than our own initialization!
        if (method.isConstructor() && method.getParent().getSuperclass().equals(m.getParent())) {
            load(method.arguments().get(0), insns);
            callingSuperCtor = true;
        } else {
            insns.add(new TypeInsnNode(Opcodes.NEW, internalName(m.getType().getReturnType().getKlass())));
            insns.add(new InsnNode(Opcodes.DUP));
        }
    }
    int opcode;
    if (m.modifiers().contains(Modifier.STATIC))
        opcode = Opcodes.INVOKESTATIC;
    else if (m.isConstructor() || m.getAccess().equals(Access.PRIVATE) ||
    //We're calling a superclass method we've overridden.
            (Iterables.contains(method.getParent().superclasses(), m.getParent()))
                    && method.getParent().getMethodByVirtual(m.getName(), m.getType()) != m)
        opcode = Opcodes.INVOKESPECIAL;
    else if (m.getParent().modifiers().contains(Modifier.INTERFACE))
        //TODO: may not be correct?
        opcode = Opcodes.INVOKEINTERFACE;
    else
        opcode = Opcodes.INVOKEVIRTUAL;

    String owner = internalName(m.getParent());
    //hack to make cloning arrays work
    if (opcode == Opcodes.INVOKESPECIAL && m.getName().equals("clone")
            && i.getArgument(0).getType() instanceof ArrayType) {
        opcode = Opcodes.INVOKEVIRTUAL;
        owner = internalName(((ArrayType) i.getArgument(0).getType()).getKlass());
    }

    for (Value v : i.arguments())
        load(v, insns);
    insns.add(new MethodInsnNode(opcode, owner, m.getName(), i.callDescriptor()));

    if (!(i.getType() instanceof VoidType) && !callingSuperCtor)
        store(i, insns);
}

From source file:edu.ubc.mirrors.holograms.FrameVerifier.java

License:Open Source License

@Override
public FrameValue newOperation(AbstractInsnNode insn) throws AnalyzerException {
    BasicValue result = simplerVerifier.newOperation(insn);
    if (insn.getOpcode() == Opcodes.NEW) {
        return new FrameValue(result, insn);
    } else {//from   www. j  a  va  2 s. co m
        return FrameValue.fromBasicValue(result);
    }
}

From source file:edu.ubc.mirrors.holograms.HologramMethodGenerator.java

License:Open Source License

@Override
public void visitTypeInsn(int opcode, String type) {
    if (opcode == Opcodes.ANEWARRAY) {
        String originalTypeName = getOriginalInternalClassName(type);
        Type arrayType = Reflection.makeArrayType(1, Type.getObjectType(originalTypeName));

        getClassMirror(Type.getObjectType(type));
        swap();/*from w ww . j a v a 2 s  . c  o m*/
        invokeinterface(classMirrorType.getInternalName(), "newArray",
                Type.getMethodDescriptor(Type.getType(ArrayMirror.class), Type.INT_TYPE));

        // Instantiate the hologram class
        Type hologramArrayType = getHologramType(arrayType, true);
        anew(hologramArrayType);
        dupX1();
        swap();

        invokespecial(hologramArrayType.getInternalName(), "<init>",
                Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(ObjectArrayMirror.class)));
        return;
    }

    if (opcode == Opcodes.NEW && type.equals(Type.getInternalName(Hologram.class))) {
        type = objectHologramType.getInternalName();
    }

    super.visitTypeInsn(opcode, type);
}

From source file:erjang.EDouble.java

License:Apache License

@Override
public org.objectweb.asm.Type emit_const(MethodVisitor fa) {

    Type type = EDOUBLE_TYPE;/*from w  w w  .  j a  v  a  2 s  .  c o m*/

    fa.visitTypeInsn(Opcodes.NEW, type.getInternalName());
    fa.visitInsn(Opcodes.DUP);
    fa.visitLdcInsn(new Double(value));
    fa.visitMethodInsn(Opcodes.INVOKESPECIAL, type.getInternalName(), "<init>", "(D)V");

    return type;
}

From source file:erjang.EList.java

License:Apache License

@Override
public Type emit_const(MethodVisitor fa) {
    Type type = ELIST_TYPE;//from   w w  w. j  ava2  s .co  m

    fa.visitTypeInsn(Opcodes.NEW, type.getInternalName());
    fa.visitInsn(Opcodes.DUP);

    ((EObject) head).emit_const(fa);
    ((EObject) tail).emit_const(fa);

    fa.visitMethodInsn(Opcodes.INVOKESPECIAL, type.getInternalName(), "<init>", CONSTRUCTOR_DESC);

    return type;
}

From source file:erjang.EPair.java

License:Apache License

@Override
public Type emit_const(MethodVisitor fa) {
    Type type = EPAIR_TYPE;/*from   w ww. j a  va 2  s.c  o  m*/

    fa.visitTypeInsn(Opcodes.NEW, type.getInternalName());
    fa.visitInsn(Opcodes.DUP);

    ((EObject) head).emit_const(fa);
    ((EObject) tail).emit_const(fa);

    fa.visitMethodInsn(Opcodes.INVOKESPECIAL, type.getInternalName(), "<init>", CONSTRUCTOR_DESC);

    return type;
}

From source file:erjang.ESmall.java

License:Apache License

@Override
public org.objectweb.asm.Type emit_const(MethodVisitor fa) {

    Type type = ESMALL_TYPE;/*from w w  w.j av a2 s.c  o m*/

    fa.visitTypeInsn(Opcodes.NEW, type.getInternalName());
    fa.visitInsn(Opcodes.DUP);
    fa.visitLdcInsn(new Integer(value));
    fa.visitMethodInsn(Opcodes.INVOKESPECIAL, type.getInternalName(), "<init>", "(I)V");

    return type;
}

From source file:erjang.ETuple.java

License:Apache License

private static void create_tuple_copy(int i, ClassAdapter cw, String this_class_name, String super_class_name) {
    MethodVisitor mv;/*from   ww w.  j a  va 2 s .  c o  m*/
    make_blank_bridge(cw, this_class_name, super_class_name);

    mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "blank", "()L" + this_class_name + ";", null, null);
    mv.visitCode();
    mv.visitTypeInsn(Opcodes.NEW, this_class_name);
    mv.visitInsn(Opcodes.DUP);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, this_class_name, "<init>", "()V");

    mv.visitInsn(Opcodes.ARETURN);

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