Example usage for org.objectweb.asm Opcodes INVOKEINTERFACE

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

Introduction

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

Prototype

int INVOKEINTERFACE

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

Click Source Link

Usage

From source file:com.github.anba.es6draft.compiler.assembler.InstructionAssembler.java

License:Open Source License

public void invokeinterface(Type owner, String name, MethodTypeDescriptor desc) {
    methodVisitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, owner.internalName(), name, desc.descriptor(), true);
    stack.invokeinterface(desc);/*from   w w w  .  j  av a  2  s.  co  m*/
}

From source file:com.github.fge.grappa.misc.AsmUtils.java

License:Open Source License

public static boolean isCallOnContextAware(AbstractInsnNode insn) {
    Objects.requireNonNull(insn, "insn");
    return (insn.getOpcode() == Opcodes.INVOKEVIRTUAL //
            || insn.getOpcode() == Opcodes.INVOKEINTERFACE) //
            && isAssignableTo(((MethodInsnNode) insn).owner, ContextAware.class);//
}

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   w ww  . j  ava2  s  . c  om*/

    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.AsyncMethodAdapter.java

License:Apache License

@Override
public void visitMaxs(int maxStack, int maxLocals) {
    // Table switch at the end of the method
    mv.visitLabel(dispatchLabel);//w  w  w .  j  a v  a  2s  . c  o  m

    Label dflt = new Label();

    // Load index
    target.visitVarInsn(Opcodes.ILOAD, 1 + info.thisOffset);
    int[] keys = new int[dispatchTable.size()];
    for (int i = 0; i < keys.length; ++i) {
        keys[i] = i;
    }
    mv.visitLookupSwitchInsn(dflt, keys, dispatchTable.toArray(new Label[0]));

    // FIXME: ...throw exception
    mv.visitLabel(dflt);
    mv.visitInsn(Opcodes.RETURN);

    // catch block
    mv.visitLabel(catchLabel);

    // invoke Continuation#setException(Throwable t)
    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_SET_EXCEPTION_NAME,
            CONTINUATION_SET_EXCEPTION_DESC);
    mv.visitInsn(Opcodes.RETURN);

    // FIXME: evaluate properly
    super.visitMaxs(maxStack + 4 + info.thisOffset, maxLocals + 2);
}

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

License:Open Source License

@Override
public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) {
    if (opcode == Opcodes.INVOKESPECIAL && "<init>".equals(name)) {
        int argumentSize = (Type.getArgumentsAndReturnSizes(desc) >> 2);
        InferredType receiverType = getTypeOfOperandFromTop(argumentSize - 1);
        if (receiverType.isUninitialized()) {
            InferredType realType = InferredType.createNonUninitializedType('L' + owner + ';');
            replaceUninitializedTypeInStack(receiverType, realType);
        }/* ww w. ja v a  2s  .c  o m*/
    }
    switch (opcode) {
    case Opcodes.INVOKESPECIAL:
    case Opcodes.INVOKEVIRTUAL:
    case Opcodes.INVOKESTATIC:
    case Opcodes.INVOKEINTERFACE:
        popDescriptor(desc);
        if (opcode != Opcodes.INVOKESTATIC) {
            pop(); // Pop receiver.
        }
        pushDescriptor(desc);
        break;
    default:
        throw new RuntimeException(String.format("Unhandled opcode %s, owner=%s, name=%s, desc=%s, itf=%s",
                opcode, owner, name, desc, itf));
    }
    super.visitMethodInsn(opcode, owner, name, desc, itf);
}

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

License:Open Source License

private void makeDispatchHelperMethod(ClassVisitor helper, EmulatedMethod method,
        ImmutableList<Class<?>> typechecks) {
    checkArgument(method.owner().isInterface());
    String owner = method.owner().getName().replace('.', '/');
    Type methodType = Type.getMethodType(method.descriptor());
    String companionDesc = InterfaceDesugaring.companionDefaultMethodDescriptor(owner, method.descriptor());
    MethodVisitor dispatchMethod = helper.visitMethod(method.access() | Opcodes.ACC_STATIC, method.name(),
            companionDesc, /*signature=*/ null, // signature is invalid due to extra "receiver" argument
            method.exceptions().toArray(EMPTY_LIST));

    dispatchMethod.visitCode();/*w  ww  .j av a  2  s.  c  o m*/
    {
        // See if the receiver might come with its own implementation of the method, and call it.
        // We do this by testing for the interface type created by EmulatedInterfaceRewriter
        Label fallthrough = new Label();
        String emulationInterface = renameCoreLibrary(owner);
        dispatchMethod.visitVarInsn(Opcodes.ALOAD, 0); // load "receiver"
        dispatchMethod.visitTypeInsn(Opcodes.INSTANCEOF, emulationInterface);
        dispatchMethod.visitJumpInsn(Opcodes.IFEQ, fallthrough);
        dispatchMethod.visitVarInsn(Opcodes.ALOAD, 0); // load "receiver"
        dispatchMethod.visitTypeInsn(Opcodes.CHECKCAST, emulationInterface);

        visitLoadArgs(dispatchMethod, methodType, 1 /* receiver already loaded above */);
        dispatchMethod.visitMethodInsn(Opcodes.INVOKEINTERFACE, emulationInterface, method.name(),
                method.descriptor(), /*itf=*/ true);
        dispatchMethod.visitInsn(methodType.getReturnType().getOpcode(Opcodes.IRETURN));

        dispatchMethod.visitLabel(fallthrough);
        // Trivial frame for the branch target: same empty stack as before
        dispatchMethod.visitFrame(Opcodes.F_SAME, 0, EMPTY_FRAME, 0, EMPTY_FRAME);
    }

    // Next, check for subtypes with specialized implementations and call them
    for (Class<?> tested : typechecks) {
        Label fallthrough = new Label();
        String testedName = tested.getName().replace('.', '/');
        // In case of a class this must be a member move; for interfaces use the companion.
        String target = tested.isInterface() ? InterfaceDesugaring.getCompanionClassName(testedName)
                : checkNotNull(memberMoves.get(rewriter.unprefix(testedName) + '#' + method.name()));
        dispatchMethod.visitVarInsn(Opcodes.ALOAD, 0); // load "receiver"
        dispatchMethod.visitTypeInsn(Opcodes.INSTANCEOF, testedName);
        dispatchMethod.visitJumpInsn(Opcodes.IFEQ, fallthrough);
        dispatchMethod.visitVarInsn(Opcodes.ALOAD, 0); // load "receiver"
        dispatchMethod.visitTypeInsn(Opcodes.CHECKCAST, testedName); // make verifier happy

        visitLoadArgs(dispatchMethod, methodType, 1 /* receiver already loaded above */);
        dispatchMethod.visitMethodInsn(Opcodes.INVOKESTATIC, target, method.name(),
                InterfaceDesugaring.companionDefaultMethodDescriptor(testedName, method.descriptor()),
                /*itf=*/ false);
        dispatchMethod.visitInsn(methodType.getReturnType().getOpcode(Opcodes.IRETURN));

        dispatchMethod.visitLabel(fallthrough);
        // Trivial frame for the branch target: same empty stack as before
        dispatchMethod.visitFrame(Opcodes.F_SAME, 0, EMPTY_FRAME, 0, EMPTY_FRAME);
    }

    // Call static type's default implementation in companion class
    dispatchMethod.visitVarInsn(Opcodes.ALOAD, 0); // load "receiver"
    visitLoadArgs(dispatchMethod, methodType, 1 /* receiver already loaded above */);
    dispatchMethod.visitMethodInsn(Opcodes.INVOKESTATIC, InterfaceDesugaring.getCompanionClassName(owner),
            method.name(), companionDesc, /*itf=*/ false);
    dispatchMethod.visitInsn(methodType.getReturnType().getOpcode(Opcodes.IRETURN));

    dispatchMethod.visitMaxs(0, 0);
    dispatchMethod.visitEnd();
}

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

License:Open Source License

@Test
public void testGetCoreInterfaceRewritingTarget_emulatedDefaultMethod() throws Exception {
    CoreLibrarySupport support = new CoreLibrarySupport(new CoreLibraryRewriter(""),
            Thread.currentThread().getContextClassLoader(), ImmutableList.of(),
            ImmutableList.of("java/util/Collection"), ImmutableList.of(), ImmutableList.of());
    assertThat(support.getCoreInterfaceRewritingTarget(Opcodes.INVOKEINTERFACE, "java/util/Collection",
            "removeIf", "(Ljava/util/function/Predicate;)Z", true)).isEqualTo(Collection.class);
    assertThat(support.getCoreInterfaceRewritingTarget(Opcodes.INVOKEVIRTUAL, "java/util/ArrayList", "removeIf",
            "(Ljava/util/function/Predicate;)Z", false)).isEqualTo(Collection.class);
    assertThat(support.getCoreInterfaceRewritingTarget(Opcodes.INVOKEINTERFACE,
            "com/google/HypotheticalListInterface", "removeIf", "(Ljava/util/function/Predicate;)Z", true))
                    .isNull();//ww  w  .ja  v  a2 s  .c  o  m
}

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

License:Open Source License

@Test
public void testGetCoreInterfaceRewritingTarget_emulatedImplementationMoved() throws Exception {
    CoreLibrarySupport support = new CoreLibrarySupport(new CoreLibraryRewriter(""),
            Thread.currentThread().getContextClassLoader(), ImmutableList.of("java/util/Moved"),
            ImmutableList.of("java/util/Map"),
            ImmutableList.of("java/util/LinkedHashMap#forEach->java/util/Moved"), ImmutableList.of());
    assertThat(support.getCoreInterfaceRewritingTarget(Opcodes.INVOKEINTERFACE, "java/util/Map", "forEach",
            "(Ljava/util/function/BiConsumer;)V", true)).isEqualTo(Map.class);
    assertThat(support.getCoreInterfaceRewritingTarget(Opcodes.INVOKESPECIAL, "java/util/Map", "forEach",
            "(Ljava/util/function/BiConsumer;)V", true)).isEqualTo(Map.class);
    assertThat(support.getCoreInterfaceRewritingTarget(Opcodes.INVOKEVIRTUAL, "java/util/LinkedHashMap",
            "forEach", "(Ljava/util/function/BiConsumer;)V", false)).isEqualTo(Map.class);
    assertThat(support.getCoreInterfaceRewritingTarget(Opcodes.INVOKESPECIAL, "java/util/LinkedHashMap",
            "forEach", "(Ljava/util/function/BiConsumer;)V", false)).isEqualTo(LinkedHashMap.class);
    assertThat(support.getCoreInterfaceRewritingTarget(Opcodes.INVOKESPECIAL, "java/util/HashMap", "forEach",
            "(Ljava/util/function/BiConsumer;)V", false)).isEqualTo(Map.class);
}

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

License:Open Source License

@Test
public void testGetCoreInterfaceRewritingTarget_abstractMethod() throws Exception {
    CoreLibrarySupport support = new CoreLibrarySupport(new CoreLibraryRewriter(""),
            Thread.currentThread().getContextClassLoader(), ImmutableList.of(),
            ImmutableList.of("java/util/Collection"), ImmutableList.of(), ImmutableList.of());
    assertThat(support.getCoreInterfaceRewritingTarget(Opcodes.INVOKEINTERFACE, "java/util/Collection", "size",
            "()I", true)).isNull();
    assertThat(support.getCoreInterfaceRewritingTarget(Opcodes.INVOKEVIRTUAL, "java/util/ArrayList", "size",
            "()I", false)).isNull();
}

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

License:Open Source License

@Test
public void testGetCoreInterfaceRewritingTarget_emulatedDefaultOverride() throws Exception {
    CoreLibrarySupport support = new CoreLibrarySupport(new CoreLibraryRewriter(""),
            Thread.currentThread().getContextClassLoader(), ImmutableList.of(),
            ImmutableList.of("java/util/Map"), ImmutableList.of(), ImmutableList.of());
    assertThat(support.getCoreInterfaceRewritingTarget(Opcodes.INVOKEINTERFACE, "java/util/Map", "putIfAbsent",
            "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", true)).isEqualTo(Map.class);
    assertThat(support.getCoreInterfaceRewritingTarget(Opcodes.INVOKEINTERFACE,
            "java/util/concurrent/ConcurrentMap", "putIfAbsent",
            "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", true)).isNull(); // putIfAbsent is default in Map but abstract in ConcurrentMap
    assertThat(support.getCoreInterfaceRewritingTarget(Opcodes.INVOKEINTERFACE,
            "java/util/concurrent/ConcurrentMap", "getOrDefault",
            "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", true)).isEqualTo(ConcurrentMap.class); // conversely, getOrDefault is overridden as default method
}