Example usage for org.objectweb.asm Opcodes INVOKESPECIAL

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

Introduction

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

Prototype

int INVOKESPECIAL

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

Click Source Link

Usage

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

/**
 * Tests that call sites of renamed core libraries are treated like call sites in regular
 * {@link InterfaceDesugaring}./*from   www.  j a  v  a  2  s .  c om*/
 */
@Test
public void testGetCoreInterfaceRewritingTarget_renamed() throws Exception {
    CoreLibrarySupport support = new CoreLibrarySupport(new CoreLibraryRewriter(""),
            Thread.currentThread().getContextClassLoader(), ImmutableList.of("java/util/"), ImmutableList.of(),
            ImmutableList.of(), ImmutableList.of());

    // regular invocations of default methods: ignored
    assertThat(support.getCoreInterfaceRewritingTarget(Opcodes.INVOKEINTERFACE, "java/util/Collection",
            "removeIf", "(Ljava/util/function/Predicate;)Z", true)).isNull();
    assertThat(support.getCoreInterfaceRewritingTarget(Opcodes.INVOKEVIRTUAL, "java/util/ArrayList", "removeIf",
            "(Ljava/util/function/Predicate;)Z", false)).isNull();

    // abstract methods: ignored
    assertThat(support.getCoreInterfaceRewritingTarget(Opcodes.INVOKEINTERFACE, "java/util/Collection", "size",
            "()I", true)).isNull();

    // static interface method
    assertThat(support.getCoreInterfaceRewritingTarget(Opcodes.INVOKESTATIC, "java/util/Comparator",
            "reverseOrder", "()Ljava/util/Comparator;", true)).isEqualTo(Comparator.class);

    // invokespecial for default methods: find nearest definition
    assertThat(support.getCoreInterfaceRewritingTarget(Opcodes.INVOKESPECIAL, "java/util/List", "removeIf",
            "(Ljava/util/function/Predicate;)Z", true)).isEqualTo(Collection.class);
    // invokespecial on a class: ignore (even if there's an inherited default method)
    assertThat(support.getCoreInterfaceRewritingTarget(Opcodes.INVOKESPECIAL, "java/util/ArrayList", "removeIf",
            "(Ljava/util/function/Predicate;)Z", false)).isNull();
}

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  . j  a va2s .c  o  m
        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

static int invokeOpcode(Handle invokedMethod) {
    switch (invokedMethod.getTag()) {
    case Opcodes.H_INVOKESTATIC:
        return Opcodes.INVOKESTATIC;
    case Opcodes.H_INVOKEVIRTUAL:
        return Opcodes.INVOKEVIRTUAL;
    case Opcodes.H_INVOKESPECIAL:
    case Opcodes.H_NEWINVOKESPECIAL: // Must be preceded by NEW
        return Opcodes.INVOKESPECIAL;
    case Opcodes.H_INVOKEINTERFACE:
        return Opcodes.INVOKEINTERFACE;
    default:// ww  w .j av  a2  s  .com
        throw new UnsupportedOperationException("Don't know how to call " + invokedMethod);
    }
}

From source file:com.google.devtools.build.android.resources.RClassGenerator.java

License:Open Source License

private static void writeConstructor(ClassWriter classWriter) {
    MethodVisitor constructor = classWriter.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V",
            null, /* signature */
            null /* exceptions */);
    constructor.visitCode();/*from w  ww .ja va  2s  .c  o m*/
    constructor.visitVarInsn(Opcodes.ALOAD, 0);
    constructor.visitMethodInsn(Opcodes.INVOKESPECIAL, SUPER_CLASS, "<init>", "()V", false);
    constructor.visitInsn(Opcodes.RETURN);
    constructor.visitMaxs(1, 1);
    constructor.visitEnd();
}

From source file:com.google.devtools.build.android.resources.RClassWriter.java

License:Open Source License

private static void writeConstructor(ClassWriter classWriter) {
    MethodVisitor constructor = classWriter.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
    constructor.visitCode();/*from  ww  w .j  a  va2  s . co m*/
    constructor.visitVarInsn(Opcodes.ALOAD, 0);
    constructor.visitMethodInsn(Opcodes.INVOKESPECIAL, SUPER_CLASS, "<init>", "()V", false);
    constructor.visitInsn(Opcodes.RETURN);
    constructor.visitMaxs(1, 1);
    constructor.visitEnd();
}

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

License:Apache License

/**
 * Initialize a string buffer.// w w  w .ja v  a  2 s .com
 *
 * @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.gwt.jvm.asm.ConstructorDollarMethodVistor.java

License:Apache License

@Override
public void visitMethodInsn(int opcode, String owner, String name, String desc) {
    if (isOpcode(opcode, Opcodes.INVOKESPECIAL) && typePredicate.isOverlayDesc(owner)) {
        super.visitMethodInsn(opcode, owner + "$", name, desc);
    } else {/*from  w w  w.  j a  v a  2  s  .  co m*/
        super.visitMethodInsn(opcode, owner, name, desc);
    }
}

From source file:com.google.gwt.jvm.asm.NativeMethodDelegatingVisitor.java

License:Apache License

private void assertThisNotNull() {
    delegate.visitVarInsn(Opcodes.ALOAD, 0);
    Label notNull = new Label();
    delegate.visitJumpInsn(Opcodes.IFNONNULL, notNull);
    delegate.visitTypeInsn(Opcodes.NEW, NULL_POINTER_EXCEPTION);
    delegate.visitInsn(Opcodes.DUP);/*ww w  . j a  v  a  2  s .c  om*/
    delegate.visitMethodInsn(Opcodes.INVOKESPECIAL, NULL_POINTER_EXCEPTION, "<init>", "()V");
    delegate.visitInsn(Opcodes.ATHROW);
    delegate.visitLabel(notNull);
}

From source file:com.google.monitoring.runtime.instrumentation.adapters.AllocationMethodAdapter.java

License:Apache License

/**
 * Reflection-based allocation (@see java.lang.reflect.Array#newInstance) is
 * triggered with a static method call (INVOKESTATIC), so we hook it here.
 * Class initialization is triggered with a constructor call (INVOKESPECIAL)
 * so we hook that here too as a proxy for the new bytecode which leaves an
 * uninitialized object on the stack that we're not allowed to touch.
 * {@link java.lang.Object#clone} is also a call to INVOKESPECIAL,
 * and is hooked here.  {@link java.lang.Class#newInstance} and
 * {@link java.lang.reflect.Constructor#newInstance} are both
 * INVOKEVIRTUAL calls, so they are hooked here, as well.
 *//*from ww  w  .  j a v a2  s .  c  o  m*/
@Override
public void visitMethodInsn(final int opcode, final String owner, final String name, final String signature,
        final boolean itf) {
    if (opcode == Opcodes.INVOKESTATIC &&
    // Array does its own native allocation.  Grr.
            owner.equals("java/lang/reflect/Array") && name.equals("newInstance")) {
        if (signature.equals("(Ljava/lang/Class;I)Ljava/lang/Object;")) {

            final Label beginScopeLabel = new Label();
            final Label endScopeLabel = new Label();
            super.visitLabel(beginScopeLabel);

            // stack: ... class count
            final int countIndex = newLocal("I", beginScopeLabel, endScopeLabel);
            super.visitVarInsn(Opcodes.ISTORE, countIndex);
            // -> stack: ... class
            pushClassNameOnStack();
            // -> stack: ... class className
            final int typeNameIndex = newLocal("Ljava/lang/String;", beginScopeLabel, endScopeLabel);
            super.visitVarInsn(Opcodes.ASTORE, typeNameIndex);
            // -> stack: ... class
            super.visitVarInsn(Opcodes.ILOAD, countIndex);
            // -> stack: ... class count
            super.visitMethodInsn(opcode, owner, name, signature, itf);
            // -> stack: ... newobj
            super.visitInsn(Opcodes.DUP);
            // -> stack: ... newobj newobj
            super.visitVarInsn(Opcodes.ILOAD, countIndex);
            // -> stack: ... newobj newobj count
            super.visitInsn(Opcodes.SWAP);
            // -> stack: ... newobj count newobj
            super.visitVarInsn(Opcodes.ALOAD, typeNameIndex);
            super.visitLabel(endScopeLabel);
            // -> stack: ... newobj count newobj className
            super.visitInsn(Opcodes.SWAP);
            // -> stack: ... newobj count className newobj
            super.visitMethodInsn(Opcodes.INVOKESTATIC, recorderClass, recorderMethod, RECORDER_SIGNATURE,
                    false);
            // -> stack: ... newobj
            return;
        } else if (signature.equals("(Ljava/lang/Class;[I)Ljava/lang/Object;")) {
            final Label beginScopeLabel = new Label();
            final Label endScopeLabel = new Label();
            super.visitLabel(beginScopeLabel);

            final int dimsArrayIndex = newLocal("[I", beginScopeLabel, endScopeLabel);
            // stack: ... class dimsArray
            pushProductOfIntArrayOnStack();
            // -> stack: ... class dimsArray product
            final int productIndex = newLocal("I", beginScopeLabel, endScopeLabel);
            super.visitVarInsn(Opcodes.ISTORE, productIndex);
            // -> stack: ... class dimsArray

            super.visitVarInsn(Opcodes.ASTORE, dimsArrayIndex);
            // -> stack: ... class
            pushClassNameOnStack();
            // -> stack: ... class className
            final int typeNameIndex = newLocal("Ljava/lang/String;", beginScopeLabel, endScopeLabel);
            super.visitVarInsn(Opcodes.ASTORE, typeNameIndex);
            // -> stack: ... class
            super.visitVarInsn(Opcodes.ALOAD, dimsArrayIndex);
            // -> stack: ... class dimsArray
            super.visitMethodInsn(opcode, owner, name, signature, itf);
            // -> stack: ... newobj

            super.visitInsn(Opcodes.DUP);
            // -> stack: ... newobj newobj
            super.visitVarInsn(Opcodes.ILOAD, productIndex);
            // -> stack: ... newobj newobj product
            super.visitInsn(Opcodes.SWAP);
            // -> stack: ... newobj product newobj
            super.visitVarInsn(Opcodes.ALOAD, typeNameIndex);
            super.visitLabel(endScopeLabel);
            // -> stack: ... newobj product newobj className
            super.visitInsn(Opcodes.SWAP);
            // -> stack: ... newobj product className newobj
            super.visitMethodInsn(Opcodes.INVOKESTATIC, recorderClass, recorderMethod, RECORDER_SIGNATURE,
                    false);
            // -> stack: ... newobj
            return;
        }
    }

    if (opcode == Opcodes.INVOKEVIRTUAL) {
        if ("clone".equals(name) && owner.startsWith("[")) {
            super.visitMethodInsn(opcode, owner, name, signature, itf);

            int i = 0;
            while (i < owner.length()) {
                if (owner.charAt(i) != '[') {
                    break;
                }
                i++;
            }
            if (i > 1) {
                // -> stack: ... newobj
                super.visitTypeInsn(Opcodes.CHECKCAST, owner);
                // -> stack: ... arrayref
                calculateArrayLengthAndDispatch(owner.substring(i), i);
            } else {
                // -> stack: ... newobj
                super.visitInsn(Opcodes.DUP);
                // -> stack: ... newobj newobj
                super.visitTypeInsn(Opcodes.CHECKCAST, owner);
                // -> stack: ... newobj arrayref
                super.visitInsn(Opcodes.ARRAYLENGTH);
                // -> stack: ... newobj length
                super.visitInsn(Opcodes.SWAP);
                // -> stack: ... length newobj
                invokeRecordAllocation(owner.substring(i));
            }
            return;
        } else if ("newInstance".equals(name)) {
            if ("java/lang/Class".equals(owner) && "()Ljava/lang/Object;".equals(signature)) {
                super.visitInsn(Opcodes.DUP);
                // -> stack: ... Class Class
                super.visitMethodInsn(opcode, owner, name, signature, itf);
                // -> stack: ... Class newobj
                super.visitInsn(Opcodes.DUP_X1);
                // -> stack: ... newobj Class newobj
                super.visitMethodInsn(Opcodes.INVOKESTATIC, recorderClass, recorderMethod, CLASS_RECORDER_SIG,
                        false);
                // -> stack: ... newobj
                return;
            } else if ("java/lang/reflect/Constructor".equals(owner)
                    && "([Ljava/lang/Object;)Ljava/lang/Object;".equals(signature)) {
                buildRecorderFromObject(opcode, owner, name, signature, itf);
                return;
            }
        }
    }

    if (opcode == Opcodes.INVOKESPECIAL) {
        if ("clone".equals(name) && "java/lang/Object".equals(owner)) {
            buildRecorderFromObject(opcode, owner, name, signature, itf);
            return;
        } else if ("<init>".equals(name) && outstandingAllocs > 0) {
            // Tricky because superclass initializers mean there can be more calls
            // to <init> than calls to NEW; hence outstandingAllocs.
            --outstandingAllocs;

            // Most of the time (i.e. in bytecode generated by javac) it is the case
            // that following an <init> call the top of the stack has a reference ot
            // the newly-initialized object.  But nothing in the JVM Spec requires
            // this, so we need to play games with the stack to make an explicit
            // extra copy (and then discard it).

            dupStackElementBeforeSignatureArgs(signature);
            super.visitMethodInsn(opcode, owner, name, signature, itf);
            super.visitLdcInsn(-1);
            super.visitInsn(Opcodes.SWAP);
            invokeRecordAllocation(owner);
            super.visitInsn(Opcodes.POP);
            return;
        }
    }

    super.visitMethodInsn(opcode, owner, name, signature, itf);
}