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:org.adjective.stout.tools.StackVisualiserMethodVisitor.java

License:Apache License

public void visitMethodInsn(int opcode, String owner, String name, String desc) {

    Type[] argumentTypes = Type.getArgumentTypes(desc);
    Type returnType = Type.getReturnType(desc);

    StringBuilder args = new StringBuilder();

    for (int i = argumentTypes.length; i > 0; i--) {
        args.insert(0, pop(argumentTypes[i - 1]).description + ",");
    }/* w w w.j av  a 2 s .c  o  m*/
    switch (opcode) {
    case Opcodes.INVOKESTATIC:
        break;
    case Opcodes.INVOKEINTERFACE:
    case Opcodes.INVOKEVIRTUAL:
    case Opcodes.INVOKESPECIAL:
        pop(Type.getObjectType(owner));
        break;
    default:
        throw new IllegalArgumentException("Unsupported opcode " + OPCODES[opcode]);
    }

    String description = simpleName(owner) + "." + name + "(" + args + ")";
    if (!returnType.equals(Type.VOID_TYPE)) {
        push(description, returnType);
    }
    print(opcode, description + " {" + desc + "}");
}

From source file:org.apache.commons.javaflow.bytecode.transformation.asm.ContinuationMethodAnalyzer.java

License:Apache License

boolean needsFrameGuard(int opcode, String owner, String name, String desc) {
    /* TODO: need to customize a way enchancer skips classes/methods
    if (owner.startsWith("java/")) {/*from   ww  w. j  ava  2 s  .  co m*/
        System.out.println("SKIP:: " + owner + "." + name + desc);
        return false;
    }
    */

    if (opcode == Opcodes.INVOKEINTERFACE || (opcode == Opcodes.INVOKESPECIAL && !"<init>".equals(name))
            || opcode == Opcodes.INVOKESTATIC || opcode == Opcodes.INVOKEVIRTUAL) {
        return true;
    }
    return false;
}

From source file:org.apache.commons.javaflow.providers.asm3.ContinuableMethodNode.java

License:Apache License

boolean needsFrameGuard(int opcode, String owner, String name, String desc) {
    if (owner.startsWith("java/") || owner.startsWith("javax/")) {
        //System.out.println("SKIP:: " + owner + "." + name + desc);
        return false;
    }//www .j av  a  2 s  .  co m

    // Always create save-point before Continuation methods (like suspend)
    if (CONTINUATION_CLASS_INTERNAL_NAME.equals(owner)) {
        return CONTINUATION_CLASS_CONTINUABLE_METHODS.contains(name);
    }

    // No need to create save-point before constructors -- it's forbidden to suspend in constructors anyway
    if (opcode == Opcodes.INVOKESPECIAL && "<init>".equals(name)) {
        return false;
    }

    if (opcode == Opcodes.INVOKEINTERFACE || opcode == Opcodes.INVOKESPECIAL || opcode == Opcodes.INVOKESTATIC
            || opcode == Opcodes.INVOKEVIRTUAL) {
        final ContinuableClassInfo classInfo;
        try {
            classInfo = cciResolver.resolve(owner);
        } catch (final IOException ex) {
            throw new RuntimeException(ex);
        }
        return null != classInfo && classInfo.isContinuableMethod(opcode, name, desc, desc);
    }
    return false;
}

From source file:org.apache.commons.javaflow.providers.asm4.ContinuableMethodNode.java

License:Apache License

boolean needsFrameGuard(int opcode, String owner, String name, String desc) {
    if (owner.startsWith("java/") || owner.startsWith("javax/")) {
        //System.out.println("SKIP:: " + owner + "." + name + desc);
        return false;
    }// ww w .  j  a v a  2  s . c o m

    // Always create save-point before Continuation methods (like suspend)
    if (CONTINUATION_CLASS_INTERNAL_NAME.equals(owner)) {
        return CONTINUATION_CLASS_CONTINUABLE_METHODS.contains(name);
    }

    // No need to create save-point before constructors -- it's forbidden to suspend in constructors anyway
    if (opcode == Opcodes.INVOKESPECIAL && "<init>".equals(name)) {
        return false;
    }

    if (opcode == Opcodes.INVOKEDYNAMIC) {
        // TODO verify CallSite to be continuable?
        return true;
    }

    if (opcode == Opcodes.INVOKEINTERFACE || opcode == Opcodes.INVOKESPECIAL || opcode == Opcodes.INVOKESTATIC
            || opcode == Opcodes.INVOKEVIRTUAL) {
        final ContinuableClassInfo classInfo;
        try {
            classInfo = cciResolver.resolve(owner);
        } catch (final IOException ex) {
            throw new RuntimeException(ex);
        }
        return null != classInfo && classInfo.isContinuableMethod(opcode, name, desc, desc);
    }
    return false;
}

From source file:org.apache.cxf.jaxb.WrapperHelperCompiler.java

License:Apache License

private boolean addCreateWrapperObject(String newClassName, Class<?> objectFactoryClass) {

    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "createWrapperObject",
            "(Ljava/util/List;)Ljava/lang/Object;", "(Ljava/util/List<*>;)Ljava/lang/Object;",
            new String[] { "org/apache/cxf/interceptor/Fault" });
    mv.visitCode();//from   w ww.java  2s .com
    Label lBegin = new Label();
    mv.visitLabel(lBegin);
    mv.visitLineNumber(104, lBegin);

    mv.visitTypeInsn(Opcodes.NEW, periodToSlashes(wrapperType.getName()));
    mv.visitInsn(Opcodes.DUP);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, periodToSlashes(wrapperType.getName()), "<init>", "()V");
    mv.visitVarInsn(Opcodes.ASTORE, 2);

    for (int x = 0; x < setMethods.length; x++) {
        if (getMethods[x] == null) {
            if (setMethods[x] == null && fields[x] == null) {
                // null placeholder
                continue;
            } else {
                return false;
            }
        }
        Class<?> tp = getMethods[x].getReturnType();
        mv.visitVarInsn(Opcodes.ALOAD, 2);

        if (List.class.isAssignableFrom(tp)) {
            doCollection(mv, x);
        } else {
            if (JAXBElement.class.isAssignableFrom(tp)) {
                mv.visitVarInsn(Opcodes.ALOAD, 0);
                mv.visitFieldInsn(Opcodes.GETFIELD, periodToSlashes(newClassName), "factory",
                        "L" + periodToSlashes(objectFactoryClass.getName()) + ";");
            }
            mv.visitVarInsn(Opcodes.ALOAD, 1);
            mv.visitIntInsn(Opcodes.BIPUSH, x);
            mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/List", "get", "(I)Ljava/lang/Object;");

            if (tp.isPrimitive()) {
                mv.visitTypeInsn(Opcodes.CHECKCAST, NONPRIMITIVE_MAP.get(tp));
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, NONPRIMITIVE_MAP.get(tp), tp.getName() + "Value",
                        "()" + PRIMITIVE_MAP.get(tp));
            } else if (JAXBElement.class.isAssignableFrom(tp)) {
                mv.visitTypeInsn(Opcodes.CHECKCAST,
                        periodToSlashes(jaxbMethods[x].getParameterTypes()[0].getName()));
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, periodToSlashes(objectFactoryClass.getName()),
                        jaxbMethods[x].getName(), getMethodSignature(jaxbMethods[x]));
            } else if (tp.isArray()) {
                mv.visitTypeInsn(Opcodes.CHECKCAST, getClassCode(tp));
            } else {
                mv.visitTypeInsn(Opcodes.CHECKCAST, periodToSlashes(tp.getName()));
            }
            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, periodToSlashes(wrapperType.getName()),
                    setMethods[x].getName(), "(" + getClassCode(tp) + ")V");
        }
    }

    mv.visitVarInsn(Opcodes.ALOAD, 2);
    mv.visitInsn(Opcodes.ARETURN);

    Label lEnd = new Label();
    mv.visitLabel(lEnd);
    mv.visitLocalVariable("this", "L" + newClassName + ";", null, lBegin, lEnd, 0);
    mv.visitLocalVariable("lst", "Ljava/util/List;", "Ljava/util/List<*>;", lBegin, lEnd, 1);
    mv.visitLocalVariable("ok", "L" + periodToSlashes(wrapperType.getName()) + ";", null, lBegin, lEnd, 2);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    return true;
}

From source file:org.apache.cxf.jaxb.WrapperHelperCompiler.java

License:Apache License

private void doCollection(MethodVisitor mv, int x) {
    // List aVal = obj.getA();
    // List newA = (List)lst.get(99);
    // if (aVal == null) {
    // obj.setA(newA);
    // } else if (newA != null) {
    // aVal.addAll(newA);
    // }// w  w  w.  jav  a  2 s  . co  m

    Label l3 = new Label();
    mv.visitLabel(l3);
    mv.visitLineNumber(114, l3);

    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, periodToSlashes(wrapperType.getName()), getMethods[x].getName(),
            getMethodSignature(getMethods[x]));
    mv.visitVarInsn(Opcodes.ASTORE, 3);
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitIntInsn(Opcodes.BIPUSH, x);
    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/List", "get", "(I)Ljava/lang/Object;");
    mv.visitTypeInsn(Opcodes.CHECKCAST, "java/util/List");
    mv.visitVarInsn(Opcodes.ASTORE, 4);
    mv.visitVarInsn(Opcodes.ALOAD, 3);
    Label nonNullLabel = new Label();
    mv.visitJumpInsn(Opcodes.IFNONNULL, nonNullLabel);

    if (setMethods[x] == null) {
        mv.visitTypeInsn(Opcodes.NEW, "java/lang/RuntimeException");
        mv.visitInsn(Opcodes.DUP);
        mv.visitLdcInsn(getMethods[x].getName() + " returned null and there isn't a set method.");
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/RuntimeException", "<init>",
                "(Ljava/lang/String;)V");
        mv.visitInsn(Opcodes.ATHROW);
    } else {
        mv.visitVarInsn(Opcodes.ALOAD, 2);
        mv.visitVarInsn(Opcodes.ALOAD, 4);
        mv.visitTypeInsn(Opcodes.CHECKCAST, getMethods[x].getReturnType().getName().replace('.', '/'));
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, periodToSlashes(wrapperType.getName()),
                setMethods[x].getName(), getMethodSignature(setMethods[x]));
    }
    Label jumpOverLabel = new Label();
    mv.visitJumpInsn(Opcodes.GOTO, jumpOverLabel);
    mv.visitLabel(nonNullLabel);
    mv.visitLineNumber(106, nonNullLabel);

    mv.visitVarInsn(Opcodes.ALOAD, 4);
    mv.visitJumpInsn(Opcodes.IFNULL, jumpOverLabel);
    mv.visitVarInsn(Opcodes.ALOAD, 3);
    mv.visitVarInsn(Opcodes.ALOAD, 4);
    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/List", "addAll", "(Ljava/util/Collection;)Z");
    mv.visitInsn(Opcodes.POP);
    mv.visitLabel(jumpOverLabel);
    mv.visitLineNumber(107, jumpOverLabel);
}

From source file:org.apache.cxf.jaxb.WrapperHelperCompiler.java

License:Apache License

private static boolean addGetWrapperParts(String newClassName, Class<?> wrapperClass, Method getMethods[],
        Field fields[], ClassWriter cw) {
    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "getWrapperParts",
            "(Ljava/lang/Object;)Ljava/util/List;", "(Ljava/lang/Object;)Ljava/util/List<Ljava/lang/Object;>;",
            new String[] { "org/apache/cxf/interceptor/Fault" });
    mv.visitCode();/*  w  w w . j a v a2  s .  co  m*/
    Label lBegin = new Label();
    mv.visitLabel(lBegin);
    mv.visitLineNumber(108, lBegin);

    // the ret List
    mv.visitTypeInsn(Opcodes.NEW, "java/util/ArrayList");
    mv.visitInsn(Opcodes.DUP);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/util/ArrayList", "<init>", "()V");
    mv.visitVarInsn(Opcodes.ASTORE, 2);

    // cast the Object to the wrapperType type
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitTypeInsn(Opcodes.CHECKCAST, periodToSlashes(wrapperClass.getName()));
    mv.visitVarInsn(Opcodes.ASTORE, 3);

    for (int x = 0; x < getMethods.length; x++) {
        Method method = getMethods[x];
        if (method == null && fields[x] != null) {
            // fallback to reflection mode
            return false;
        }

        if (method == null) {
            Label l3 = new Label();
            mv.visitLabel(l3);
            mv.visitLineNumber(200 + x, l3);

            mv.visitVarInsn(Opcodes.ALOAD, 2);
            mv.visitInsn(Opcodes.ACONST_NULL);
            mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/List", "add", "(Ljava/lang/Object;)Z");
            mv.visitInsn(Opcodes.POP);
        } else {
            Label l3 = new Label();
            mv.visitLabel(l3);
            mv.visitLineNumber(250 + x, l3);

            mv.visitVarInsn(Opcodes.ALOAD, 2);
            mv.visitVarInsn(Opcodes.ALOAD, 3);
            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, periodToSlashes(wrapperClass.getName()), method.getName(),
                    getMethodSignature(method));
            if (method.getReturnType().isPrimitive()) {
                // wrap into Object type
                createObjectWrapper(mv, method.getReturnType());
            }
            if (JAXBElement.class.isAssignableFrom(method.getReturnType())) {
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "javax/xml/bind/JAXBElement", "getValue",
                        "()Ljava/lang/Object;");
            }

            mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/List", "add", "(Ljava/lang/Object;)Z");
            mv.visitInsn(Opcodes.POP);
        }
    }

    // return the list
    Label l2 = new Label();
    mv.visitLabel(l2);
    mv.visitLineNumber(108, l2);
    mv.visitVarInsn(Opcodes.ALOAD, 2);
    mv.visitInsn(Opcodes.ARETURN);

    Label lEnd = new Label();
    mv.visitLabel(lEnd);
    mv.visitLocalVariable("this", "L" + newClassName + ";", null, lBegin, lEnd, 0);
    mv.visitLocalVariable("o", "Ljava/lang/Object;", null, lBegin, lEnd, 1);
    mv.visitLocalVariable("ret", "Ljava/util/List;", "Ljava/util/List<Ljava/lang/Object;>;", lBegin, lEnd, 2);
    mv.visitLocalVariable("ok", "L" + periodToSlashes(wrapperClass.getName()) + ";", null, lBegin, lEnd, 3);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    return true;
}

From source file:org.apache.drill.exec.compile.DrillInitMethodVisitor.java

License:Apache License

@Override
public void visitInsn(int opcode) {
    if (opcode == Opcodes.RETURN) {
        super.visitVarInsn(Opcodes.ALOAD, 0); // load this.
        super.visitMethodInsn(Opcodes.INVOKEVIRTUAL, className, SignatureHolder.DRILL_INIT_METHOD, "()V"); // execute drill init.
    }/*from   w w w  . j av  a  2 s  . c  o m*/
    super.visitInsn(opcode);
}

From source file:org.apache.maven.shared.dependency.analyzer.asm.DependencyVisitorTest.java

License:Apache License

public void testVisitMethodInsn() {
    visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "a/b/c", "x", "()V");

    assertClasses("a.b.c");
}

From source file:org.apache.maven.shared.dependency.analyzer.asm.DependencyVisitorTest.java

License:Apache License

public void testVisitMethodInsnWithPrimitiveArgument() {
    visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "a/b/c", "x", "(I)V");

    assertClasses("a.b.c");
}