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:com.github.anba.es6draft.compiler.assembler.InstructionAssembler.java

License:Open Source License

public void anew(Type type) {
    methodVisitor.visitTypeInsn(Opcodes.NEW, type.internalName());
    stack.anew(type);
}

From source file:com.github.malamut2.low.AllocationMethodAdapter.java

License:Apache License

/**
 * new and anewarray bytecodes take a String operand for the type of
 * the object or array element so we hook them here.  Note that new doesn't
 * actually result in any instrumentation here; we just do a bit of
 * book-keeping and do the instrumentation following the constructor call
 * (because we're not allowed to touch the object until it is initialized).
 *///w  ww. j  av a 2  s  .  c o  m
@Override
public void visitTypeInsn(int opcode, String typeName) {
    if (opcode == Opcodes.NEW) {
        // We can't actually tag this object right after allocation because it
        // must be initialized with a ctor before we can touch it (Verifier
        // enforces this).  Instead, we just note it and tag following
        // initialization.
        super.visitTypeInsn(opcode, typeName);
        ++outstandingAllocs;
    } else if (opcode == Opcodes.ANEWARRAY) {
        super.visitInsn(Opcodes.DUP);
        super.visitTypeInsn(opcode, typeName);
        invokeRecordAllocation(typeName);
    } else {
        super.visitTypeInsn(opcode, typeName);
    }
}

From source file:com.google.code.jconts.instrument.gen.AsyncMethodAdapter.java

License:Apache License

@Override
public void visitMethodInsn(int opcode, String owner, String name, String desc) {
    checkProlog();//from www  . ja va 2s  . c  o  m

    if (opcode == Opcodes.INVOKESTATIC && ASYNC_NAME.equals(owner) && ARETURN_NAME.equals(name)) {

        if (ARETURN_VOID_DESC.equals(desc)) {
            mv.visitInsn(Opcodes.ACONST_NULL);
        }

        // state variable
        target.visitVarInsn(Opcodes.ALOAD, info.isStatic() ? 0 : 1);
        mv.visitFieldInsn(Opcodes.GETFIELD, info.stateClassName, CONTINUATION_FIELD, CONTINUATION_DESC);
        mv.visitInsn(Opcodes.SWAP);
        mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, CONTINUATION_NAME, CONTINUATION_INVOKE_NAME,
                CONTINUATION_INVOKE_DESC);

        // Will be dropped while replacing ARETURN with RETURN.
        // FIXME: Should verify this value is NOT used.
        mv.visitInsn(Opcodes.ACONST_NULL);
        return;
    }
    if (opcode == Opcodes.INVOKESTATIC && ASYNC_NAME.equals(owner) && AWAIT_NAME.equals(name)
            && AWAIT_DESC.equals(desc)) {

        // Computation<T> is on stack

        // FIXME: ...
        // if (stack.size() != 1) {
        // throw new IllegalStateException(
        // "Stack preserving is not supported!");
        // }

        int index = dispatchTable.size();

        // Save state
        List<Type> l = new ArrayList<Type>(locals);
        if (!info.isStatic()) {
            l.remove(0);
        }

        // state.varX = locX
        String[] vars = info.tracker.stateFields(l.toArray(new Type[0]));
        for (int i = 0; i < vars.length; ++i) {
            // state variable
            target.visitVarInsn(Opcodes.ALOAD, info.isStatic() ? 0 : 1);
            mv.visitVarInsn(l.get(i).getOpcode(Opcodes.ILOAD), i + info.thisOffset);
            mv.visitFieldInsn(Opcodes.PUTFIELD, info.stateClassName, vars[i], l.get(i).getDescriptor());
        }

        // Create instance of continuation
        // new Continuation([this, ]state, index);
        mv.visitTypeInsn(Opcodes.NEW, info.continuationClassName);
        mv.visitInsn(Opcodes.DUP);

        // "this' for new Continuation([this, ]state, index)
        if (!info.isStatic()) {
            mv.visitVarInsn(Opcodes.ALOAD, 0);
        }

        // state and index
        target.visitVarInsn(Opcodes.ALOAD, 0 + info.thisOffset);
        mv.visitIntInsn(Opcodes.BIPUSH, index);

        String ctorDesc;
        if (info.isStatic()) {
            ctorDesc = Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { info.stateType, Type.INT_TYPE });
        } else {
            ctorDesc = Type.getMethodDescriptor(Type.VOID_TYPE,
                    new Type[] { Type.getObjectType(info.owner), info.stateType, Type.INT_TYPE });
        }
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, info.continuationClassName, CTOR_NAME, ctorDesc);

        mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, COMPUTATION_NAME, COMPUTATION_EXECUTE_NAME,
                COMPUTATION_EXECUTE_DESC);
        super.visitInsn(Opcodes.RETURN);

        // Restore state
        // mv.visitFrame(Opcodes.F_SAME, 0, new Object[0], 0, new
        // Object[0]);
        Label label = new Label();

        int invokeIndex = dispatchTable.size();
        dispatchTable.add(label); // for invoke
        dispatchTable.add(label); // for setException
        mv.visitLabel(label);
        for (int i = 0; i < vars.length; ++i) {
            // state variable
            target.visitVarInsn(Opcodes.ALOAD, info.isStatic() ? 0 : 1);
            mv.visitFieldInsn(Opcodes.GETFIELD, info.stateClassName, vars[i], l.get(i).getDescriptor());
            mv.visitVarInsn(l.get(i).getOpcode(Opcodes.ISTORE), i + info.thisOffset);
        }

        // if (index == invokeIndex) goto invokeLabel;
        Label invokeLabel = new Label();
        target.visitVarInsn(Opcodes.ILOAD, 1 + info.thisOffset);
        mv.visitIntInsn(Opcodes.BIPUSH, invokeIndex);
        mv.visitJumpInsn(Opcodes.IF_ICMPEQ, invokeLabel);

        // Throw exception
        target.visitVarInsn(Opcodes.ALOAD, info.isStatic() ? 0 : 1);
        mv.visitFieldInsn(Opcodes.GETFIELD, info.stateClassName, "exception", THROWABLE_DESC);
        mv.visitInsn(Opcodes.ATHROW);

        // Push result value
        // invokeLabel:
        mv.visitLabel(invokeLabel);
        target.visitVarInsn(Opcodes.ALOAD, info.isStatic() ? 0 : 1);
        mv.visitFieldInsn(Opcodes.GETFIELD, info.stateClassName, "result", OBJECT_DESC);
        return;
    }
    super.visitMethodInsn(opcode, owner, name, desc);
}

From source file:com.google.code.jconts.instrument.gen.TramplineMethodGenerator.java

License:Apache License

public void accept(ClassVisitor cv) {
    MethodVisitor mv = cv.visitMethod(info.access, info.name, info.desc, info.signature, info.exceptions);
    mv.visitCode();/*from w w w  .j av a  2 s. c o m*/
    mv.visitTypeInsn(Opcodes.NEW, info.computationClassName);
    mv.visitInsn(Opcodes.DUP);

    // "this' for new Computation(this, state)
    if (!info.isStatic()) {
        mv.visitVarInsn(Opcodes.ALOAD, 0);
    }

    // new State()
    mv.visitTypeInsn(Opcodes.NEW, info.stateClassName);
    mv.visitInsn(Opcodes.DUP);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, info.stateClassName, CTOR_NAME, DEFAULT_CTOR_DESC);

    // state.varX = argX
    String[] names = info.entryLocalsVars;
    for (int i = 0; i < info.entryLocals.length; ++i) {
        mv.visitInsn(Opcodes.DUP);
        mv.visitVarInsn(info.entryLocals[i].getOpcode(Opcodes.ILOAD), i + info.thisOffset);
        mv.visitFieldInsn(Opcodes.PUTFIELD, info.stateClassName, names[i], info.entryLocals[i].getDescriptor());
    }

    // new Computation(this, state);
    String ctorDesc;
    if (info.isStatic()) {
        ctorDesc = Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { info.stateType });
    } else {
        ctorDesc = Type.getMethodDescriptor(Type.VOID_TYPE,
                new Type[] { Type.getObjectType(info.owner), info.stateType });
    }
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, info.computationClassName, CTOR_NAME, ctorDesc);

    mv.visitInsn(Opcodes.ARETURN);
    mv.visitMaxs(info.isStatic() ? 5 : 6,
            info.isStatic() ? info.entryLocals.length : info.entryLocals.length + 1);
    mv.visitEnd();
}

From source file:com.google.devtools.build.android.desugar.BytecodeTypeInference.java

License:Open Source License

@Override
public void visitTypeInsn(int opcode, String type) {
    String descriptor = convertToDescriptor(type);
    switch (opcode) {
    case Opcodes.NEW:
        pushDescriptor(descriptor); // This should be UNINITIALIZED(label). Okay for type inference.
        break;//from  w w  w . j  a v  a2 s  .c o m
    case Opcodes.ANEWARRAY:
        pop();
        pushDescriptor('[' + descriptor);
        break;
    case Opcodes.CHECKCAST:
        pop();
        pushDescriptor(descriptor);
        break;
    case Opcodes.INSTANCEOF:
        pop();
        push(InferredType.INT);
        break;
    default:
        throw new RuntimeException("Unhandled opcode " + opcode);
    }
    super.visitTypeInsn(opcode, type);
}

From source file:com.google.devtools.build.android.desugar.LambdaClassFixer.java

License:Open Source License

@Override
public void visitEnd() {
    checkState(!hasState || hasFactory, "Expected factory method for capturing lambda %s", getInternalName());
    if (!hasState) {
        checkState(signature == null, "Didn't expect generic constructor signature %s %s", getInternalName(),
                signature);/*from w  w w  . java2 s .c om*/
        checkState(lambdaInfo.factoryMethodDesc().startsWith("()"),
                "Expected 0-arg factory method for %s but found %s", getInternalName(),
                lambdaInfo.factoryMethodDesc());
        // Since this is a stateless class we populate and use a static singleton field "$instance".
        // Field is package-private so we can read it from the class that had the invokedynamic.
        String singletonFieldDesc = lambdaInfo.factoryMethodDesc().substring("()".length());
        super.visitField(Opcodes.ACC_STATIC | Opcodes.ACC_FINAL, SINGLETON_FIELD_NAME, singletonFieldDesc,
                (String) null, (Object) null).visitEnd();

        MethodVisitor codeBuilder = super.visitMethod(Opcodes.ACC_STATIC, "<clinit>", "()V", (String) null,
                new String[0]);
        codeBuilder.visitTypeInsn(Opcodes.NEW, getInternalName());
        codeBuilder.visitInsn(Opcodes.DUP);
        codeBuilder.visitMethodInsn(Opcodes.INVOKESPECIAL, getInternalName(), "<init>",
                checkNotNull(desc, "didn't see a constructor for %s", getInternalName()), /*itf*/ false);
        codeBuilder.visitFieldInsn(Opcodes.PUTSTATIC, getInternalName(), SINGLETON_FIELD_NAME,
                singletonFieldDesc);
        codeBuilder.visitInsn(Opcodes.RETURN);
        codeBuilder.visitMaxs(2, 0); // two values are pushed onto the stack
        codeBuilder.visitEnd();
    }

    copyRewrittenLambdaMethods();
    if (!allowDefaultMethods) {
        copyBridgeMethods(interfaces);
    }
    super.visitEnd();
}

From source file:com.google.devtools.build.android.desugar.LambdaDesugaring.java

License:Open Source License

@Override
public void visitEnd() {
    for (Map.Entry<Handle, MethodReferenceBridgeInfo> bridge : bridgeMethods.entrySet()) {
        Handle original = bridge.getKey();
        Handle neededMethod = bridge.getValue().bridgeMethod();
        checkState(//  w w w.jav a 2 s  .  co m
                neededMethod.getTag() == Opcodes.H_INVOKESTATIC
                        || neededMethod.getTag() == Opcodes.H_INVOKEVIRTUAL,
                "Cannot generate bridge method %s to reach %s", neededMethod, original);
        checkState(bridge.getValue().referenced() != null, "Need referenced method %s to generate bridge %s",
                original, neededMethod);

        int access = Opcodes.ACC_BRIDGE | Opcodes.ACC_SYNTHETIC | Opcodes.ACC_FINAL;
        if (neededMethod.getTag() == Opcodes.H_INVOKESTATIC) {
            access |= Opcodes.ACC_STATIC;
        }
        MethodVisitor bridgeMethod = super.visitMethod(access, neededMethod.getName(), neededMethod.getDesc(),
                (String) null, toInternalNames(bridge.getValue().referenced().getExceptionTypes()));

        // Bridge is a factory method calling a constructor
        if (original.getTag() == Opcodes.H_NEWINVOKESPECIAL) {
            bridgeMethod.visitTypeInsn(Opcodes.NEW, original.getOwner());
            bridgeMethod.visitInsn(Opcodes.DUP);
        }

        int slot = 0;
        if (neededMethod.getTag() != Opcodes.H_INVOKESTATIC) {
            bridgeMethod.visitVarInsn(Opcodes.ALOAD, slot++);
        }
        Type neededType = Type.getMethodType(neededMethod.getDesc());
        for (Type arg : neededType.getArgumentTypes()) {
            bridgeMethod.visitVarInsn(arg.getOpcode(Opcodes.ILOAD), slot);
            slot += arg.getSize();
        }
        bridgeMethod.visitMethodInsn(invokeOpcode(original), original.getOwner(), original.getName(),
                original.getDesc(), original.isInterface());
        bridgeMethod.visitInsn(neededType.getReturnType().getOpcode(Opcodes.IRETURN));

        bridgeMethod.visitMaxs(0, 0); // rely on class writer to compute these
        bridgeMethod.visitEnd();
    }
    super.visitEnd();
}

From source file:com.google.devtools.build.lib.syntax.compiler.NewObject.java

License:Open Source License

@Override
public Size apply(MethodVisitor methodVisitor, Context implementationContext) {
    methodVisitor.visitTypeInsn(Opcodes.NEW, constructor.getDeclaringType().getInternalName());
    return new StackManipulation.Compound(Duplication.SINGLE, arguments, MethodInvocation.invoke(constructor))
            .apply(methodVisitor, implementationContext);
}

From source file:com.google.devtools.build.wireless.testing.java.injector.j2me.J2mePrintServant.java

License:Apache License

/**
 * Initialize a string buffer./*from w w w.  j  a  v a 2s.c  o  m*/
 *
 * @see PrintServant#initializePrinting()
 */
@Override
protected void initializePrinting() {
    mv.visitTypeInsn(Opcodes.NEW, ClassNames.STRING_BUFFER);
    mv.visitInsn(Opcodes.DUP);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, ClassNames.STRING_BUFFER, "<init>", "()V");
}

From source file:com.google.devtools.build.wireless.testing.java.injector.WrapperClassAdapterTest.java

License:Apache License

/**
 * Tests /*from  w  w w.  j a  v  a2  s.  co  m*/
 * {@link WrapperClassAdapter#visitMethod(int, String, String, String, String[])}
 * .
 * 
 * <p>This test checks that the operator new has been invoked on the wrapped 
 * type when necessary.
 */
public void testWrappingOperatorNew() {
    MethodVisitor mv = wrapper.visitMethod(Opcodes.ACC_PUBLIC, "Method", "()V", null, null);
    assertTrue("Wrong MethodVisitor.", mv instanceof WrapperMethodAdapter);

    for (String key : classMap.keySet()) {
        mv.visitTypeInsn(Opcodes.NEW, key);
        assertEquals("The given classname should have been wrapped", classMap.get(key),
                spyVisitor.ownerClassName);
    }

    mv.visitTypeInsn(Opcodes.NEW, fakeKey);
    assertEquals("The given classname should NOT have been wrapped", fakeKey, spyVisitor.ownerClassName);
}