Example usage for org.objectweb.asm Opcodes INVOKEVIRTUAL

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

Introduction

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

Prototype

int INVOKEVIRTUAL

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

Click Source Link

Usage

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 w w w. j  a va 2 s. co  m
 */
@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.CoreLibrarySupportTest.java

License:Open Source License

@Test
public void testGetCoreInterfaceRewritingTarget_excludedMethodIgnored() throws Exception {
    CoreLibrarySupport support = new CoreLibrarySupport(new CoreLibraryRewriter(""),
            Thread.currentThread().getContextClassLoader(), ImmutableList.of(),
            ImmutableList.of("java/util/Collection"), ImmutableList.of(),
            ImmutableList.of("java/util/Collection#removeIf"));
    assertThat(support.getCoreInterfaceRewritingTarget(Opcodes.INVOKEINTERFACE, "java/util/List", "removeIf",
            "(Ljava/util/function/Predicate;)Z", true)).isNull();
    assertThat(support.getCoreInterfaceRewritingTarget(Opcodes.INVOKEVIRTUAL, "java/util/ArrayList", "removeIf",
            "(Ljava/util/function/Predicate;)Z", false)).isNull();
}

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:/* w ww .j  a  v  a  2s  .c om*/
        throw new UnsupportedOperationException("Don't know how to call " + invokedMethod);
    }
}

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

License:Apache License

/**
 * Logs the new build string with the J2ME Logger.
 *
 * @see PrintServant#finalizePrinting()//from   w w w .  j av  a 2  s. c o m
 */
@Override
protected void finalizePrinting() {
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, ClassNames.STRING_BUFFER, "toString",
            "()L" + ClassNames.JAVA_LANG_STRING + ";");
    mv.visitLdcInsn(logIdentifier);
    mv.visitInsn(Opcodes.SWAP);
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, J2meClassNames.LOG, "log",
            "(L" + ClassNames.JAVA_LANG_STRING + ";L" + ClassNames.JAVA_LANG_STRING + ";)V");
}

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

License:Apache License

/**
 * Prints the element on top of the stack by type.
 *
 * @see PrintServant#print(java.lang.String)
 *///  w ww  . j a  v a 2s.  co  m
@Override
public void print(String typeDesc) {
    String command = null;
    if (typeDesc.equals("")) {
        command = "L" + ClassNames.JAVA_LANG_STRING + ";";
    } else {
        command = typeToString(typeDesc);
    }

    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, ClassNames.STRING_BUFFER, "append",
            "(" + command + ")L" + ClassNames.STRING_BUFFER + ";");
}

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

License:Apache License

/**
 * Logs any call to commandAction by exposing the action fired.
 * //  w  w w.  j  a  v  a  2  s.c o m
 * @see org.objectweb.asm.MethodAdapter#visitCode()
 */
@Override
public void visitCode() {

    printServant.startPrinting();
    printServant.printString("[Listener] ");

    stackServant.loadThis();
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, ownerClass, "toString", "()Ljava/lang/String;");
    printServant.print("Ljava/lang/String;");

    printServant.printString(" Command = ");
    stackServant.loadArgumentAt(0);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, J2meClassNames.COMMAND, "getLabel", "()Ljava/lang/String;");
    printServant.print("Ljava/lang/String;");

    printServant.printString(" Target = ");
    // method = "getLabel";
    stackServant.loadArgumentAt(1);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, type, "toString", "()Ljava/lang/String;");
    printServant.println("Ljava/lang/String;");

    printServant.stopPrinting();
    mv.visitCode();
}

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

License:Apache License

/**
 * Injects methods call by logging invocation time and parameters.
 * /*w  w  w  .  jav a 2 s  .  c  o  m*/
 * @see org.objectweb.asm.MethodAdapter#visitCode()
 */
@Override
public void visitCode() {
    mv.visitInsn(Opcodes.ICONST_0);
    stackServant.store("I", accessedFieldOwnerIndexInStack);
    mv.visitInsn(Opcodes.DCONST_0);
    stackServant.store("D", accessedFieldValueIndexInStack);

    if (matchOwnerClass && matchMethod) {
        mv.visitInsn(Opcodes.LCONST_0);
        stackServant.store("J", timeIndexInStack);

        logger.info("\t[WhiteBox][Method call]: " + methodName + " " + description);

        printServant.startPrinting();
        printServant.printString("[Call]\t");

        // The init method has some problem with 'this' so it is considered static
        if (stackServant.isStatic() || methodName.equals("<init>")) {
            printServant.printString(ownerClass);
        } else {
            stackServant.loadThis();
            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, ownerClass, "toString",
                    "()L" + ClassNames.JAVA_LANG_STRING + ";");
            printServant.print("L" + ClassNames.JAVA_LANG_STRING + ";");
        }

        printServant.printString("." + methodName + " Arguments: ");

        for (int i = 0; i < stackServant.getArgumentSize(); i++) {
            stackServant.loadArgumentAt(i);
            printServant.print(stackServant.getArgumentTypeAt(i));
            printServant.printString("; ");
        }

        printServant.printString(" Invoked: ");
        timeServant.loadCurrentTimeMillis();
        printServant.println("J");

        // time
        timeServant.startCountingTime(timeIndexInStack);

        // saving and printing
        printServant.stopPrinting();
    }
    mv.visitCode();
}

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

License:Apache License

/**
 * Prints the type of the current class if static or invoke 
 * the <code>toStrin()</code> method, if virtual.
 *//*from ww  w.j  av  a2  s. co  m*/
private void printCurrentClass() {
    if (stackServant.isStatic()) {
        printServant.printString(ownerClass);
    } else {
        stackServant.loadThis();
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, ownerClass, "toString",
                "()L" + ClassNames.JAVA_LANG_STRING + ";");
        printServant.print("L" + ClassNames.JAVA_LANG_OBJECT + ";");
    }
}

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

License:Apache License

/**
 * Injects visit putfield and putstatic instructions by logging the new value
 * which is stored into the field.//from ww w . jav  a2  s.  co m
 */
@Override
public void visitFieldInsn(int opcode, String owner, String name, String desc) {
    boolean instrument = false;

    Matcher fieldMatcher = fieldInclusionPattern.matcher(name);
    boolean matchField = fieldMatcher.find();

    if (matchOwnerClass && matchField && (isField(opcode) || isStaticField(opcode))) {
        instrument = true;
        logger.info("\t[SetField]: " + owner + "." + name + " desc " + desc);
    }

    if (instrument && isField(opcode)) {
        // duplicate and store the value
        stackServant.store(desc, accessedFieldValueIndexInStack);
        stackServant.duplicateAndStore("L" + owner + ";", accessedFieldOwnerIndexInStack);
        stackServant.load(desc, accessedFieldValueIndexInStack);
    }

    mv.visitFieldInsn(opcode, owner, name, desc);

    if (instrument) {
        printServant.startPrinting();
        printServant.printString("[WhiteBox][Set]\t");

        if (isField(opcode)) {
            stackServant.load("L" + owner + ";", accessedFieldOwnerIndexInStack);
            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, owner, "toString",
                    "()L" + ClassNames.JAVA_LANG_STRING + ";");
            printServant.print("L" + ClassNames.JAVA_LANG_STRING + ";");
        } else {
            printServant.printString(owner);
        }

        printServant.printString("." + name + " Invoked=");

        timeServant.loadCurrentTimeMillis();
        printServant.print("J");

        // add value type to output string
        printServant.printString(" value=");

        // if the field is not static we must retrieve a pointer to the owner
        int code = -1;
        if (isField(opcode)) {
            stackServant.load("L" + owner + ";", accessedFieldOwnerIndexInStack);
            code = Opcodes.GETFIELD;
        } else {
            code = Opcodes.GETSTATIC;
        }
        mv.visitFieldInsn(code, owner, name, desc);
        printServant.println(desc);

        // save and print
        printServant.stopPrinting();
    }
}

From source file:com.google.gwt.dev.shell.rewrite.RewriteSingleJsoImplDispatches.java

License:Apache License

/**
 * For regular Java objects that implement a SingleJsoImpl interface, write
 * instance trampoline dispatchers for mangled method names to the
 * implementing method.// w w  w .j  av a 2 s  . c  om
 */
private void writeTrampoline(String stubIntr) {
    /*
     * This is almost the same kind of trampoline as the ones generated in
     * WriteJsoImpl, however there are enough small differences between the
     * semantics of the dispatches that would make a common implementation far
     * more awkward than the duplication of code.
     */
    for (String mangledName : toImplement(stubIntr)) {
        for (Method method : jsoData.getDeclarations(mangledName)) {

            Method toCall = new Method(method.getName(), method.getDescriptor());

            // Must not be final
            MethodVisitor mv = super.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC, mangledName,
                    method.getDescriptor(), null, null);
            if (mv != null) {
                mv.visitCode();

                /*
                 * It just so happens that the stack and local variable sizes are the
                 * same, but they're kept distinct to aid in clarity should the
                 * dispatch logic change.
                 *
                 * These start at 1 because we need to load "this" onto the stack
                 */
                int var = 1;
                int size = 1;

                // load this
                mv.visitVarInsn(Opcodes.ALOAD, 0);

                // then the rest of the arguments
                for (Type t : toCall.getArgumentTypes()) {
                    size += t.getSize();
                    mv.visitVarInsn(t.getOpcode(Opcodes.ILOAD), var);
                    var += t.getSize();
                }

                // Make sure there's enough room for the return value
                size = Math.max(size, toCall.getReturnType().getSize());

                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, currentTypeName, toCall.getName(),
                        toCall.getDescriptor(), false);
                mv.visitInsn(toCall.getReturnType().getOpcode(Opcodes.IRETURN));
                mv.visitMaxs(size, var);
                mv.visitEnd();
            }
        }
    }
}