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.template.soy.jbcsrc.restricted.MethodRef.java

License:Apache License

public static MethodRef createInstanceMethod(TypeInfo owner, Method method) {
    return new AutoValue_MethodRef(Opcodes.INVOKEVIRTUAL, owner, method, method.getReturnType(),
            ImmutableList.<Type>builder().add(owner.type()).add(method.getArgumentTypes()).build(),
            Features.of());//  w w w  .j a  va 2 s  .c o m
}

From source file:com.googlecode.ddom.weaver.ext.ModelExtensionFactoryImplementation.java

License:Apache License

public void accept(ClassVisitor classVisitor) {
    // Note: the name chosen here must match what is expected in ExtensionFactoryLocator
    String name = Util.classNameToInternalName(implementationInfo.getFactoryInterface().getName() + "$$Impl");
    String factoryInterfaceName = Util
            .classNameToInternalName(implementationInfo.getFactoryInterface().getName());
    classVisitor.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, name, null, "java/lang/Object",
            new String[] { factoryInterfaceName });
    {//from w  ww  .  ja  v a  2s  .  c o  m
        FieldVisitor fw = classVisitor.visitField(Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL | Opcodes.ACC_STATIC,
                "INSTANCE", "L" + factoryInterfaceName + ";", null, null);
        if (fw != null) {
            fw.visitEnd();
        }
    }
    {
        FieldVisitor fw = classVisitor.visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL, "delegates",
                "Ljava/util/Map;", null, null);
        if (fw != null) {
            fw.visitEnd();
        }
    }
    {
        MethodVisitor mv = classVisitor.visitMethod(Opcodes.ACC_PRIVATE, "<init>", "()V", null, null);
        if (mv != null) {
            mv.visitCode();
            Label l0 = new Label();
            mv.visitLabel(l0);
            // Call constructor from superclass
            mv.visitVarInsn(Opcodes.ALOAD, 0);
            mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
            // Create delegates map
            mv.visitVarInsn(Opcodes.ALOAD, 0);
            mv.visitTypeInsn(Opcodes.NEW, "java/util/HashMap");
            mv.visitInsn(Opcodes.DUP);
            mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/util/HashMap", "<init>", "()V");
            mv.visitFieldInsn(Opcodes.PUTFIELD, name, "delegates", "Ljava/util/Map;");
            // Populate delegates map
            for (ModelExtensionInfo modelExtensionInfo : implementationInfo.getModelExtensions()) {
                for (ModelExtensionInterfaceInfo extensionInterface : modelExtensionInfo
                        .getExtensionInterfaces()) {
                    if (!extensionInterface.isAbstract()) {
                        // TODO: this is stupid; we should not recreate the info object here
                        ModelExtensionClassInfo modelExtensionClassInfo = new ModelExtensionClassInfo(
                                implementationInfo.getImplementation(), modelExtensionInfo.getRootInterface(),
                                extensionInterface);
                        String factoryDelegateImplName = Util.classNameToInternalName(
                                modelExtensionClassInfo.getFactoryDelegateImplementationClassName());
                        mv.visitVarInsn(Opcodes.ALOAD, 0);
                        mv.visitFieldInsn(Opcodes.GETFIELD, name, "delegates", "Ljava/util/Map;");
                        mv.visitLdcInsn(Type.getObjectType(Util.classNameToInternalName(
                                modelExtensionClassInfo.getExtensionInterface().getClassInfo().getName())));
                        mv.visitTypeInsn(Opcodes.NEW, factoryDelegateImplName);
                        mv.visitInsn(Opcodes.DUP);
                        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, factoryDelegateImplName, "<init>", "()V");
                        mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Map", "put",
                                "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
                        mv.visitInsn(Opcodes.POP);
                    }
                }
            }
            mv.visitInsn(Opcodes.RETURN);
            Label l1 = new Label();
            mv.visitLabel(l1);
            mv.visitLocalVariable("this", "L" + name + ";", null, l0, l1, 0);
            mv.visitMaxs(4, 1);
            mv.visitEnd();
        }
    }
    String factoryDelegateInterfaceName = Util
            .classNameToInternalName(implementationInfo.getFactoryDelegateInterfaceName());
    String getDelegateDesc = "(Ljava/lang/Class;)L" + factoryDelegateInterfaceName + ";";
    {
        MethodVisitor mv = classVisitor.visitMethod(Opcodes.ACC_PRIVATE, "getDelegate", getDelegateDesc, null,
                null);
        if (mv != null) {
            mv.visitCode();
            Label l0 = new Label();
            mv.visitLabel(l0);
            mv.visitVarInsn(Opcodes.ALOAD, 0);
            mv.visitFieldInsn(Opcodes.GETFIELD, name, "delegates", "Ljava/util/Map;");
            mv.visitVarInsn(Opcodes.ALOAD, 1);
            mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Map", "get",
                    "(Ljava/lang/Object;)Ljava/lang/Object;");
            mv.visitTypeInsn(Opcodes.CHECKCAST, factoryDelegateInterfaceName);
            mv.visitInsn(Opcodes.ARETURN);
            Label l1 = new Label();
            mv.visitLabel(l1);
            mv.visitLocalVariable("this", "L" + name + ";", null, l0, l1, 0);
            mv.visitLocalVariable("extensionInterface", "Ljava/lang/Class;", null, l0, l1, 1);
            mv.visitMaxs(2, 2);
            mv.visitEnd();
        }
    }
    String implementationName = Util.classNameToInternalName(implementationInfo.getImplementation().getName());
    for (ConstructorInfo constructor : implementationInfo.getConstructors()) {
        Type[] constructorArgumentTypes = constructor.getArgumentTypes();
        Type[] argumentTypes = new Type[constructorArgumentTypes.length + 1];
        argumentTypes[0] = Type.getObjectType("java/lang/Class");
        System.arraycopy(constructorArgumentTypes, 0, argumentTypes, 1, constructorArgumentTypes.length);
        MethodVisitor mv = classVisitor.visitMethod(Opcodes.ACC_PUBLIC, "create",
                Type.getMethodDescriptor(Type.getObjectType(implementationName), argumentTypes), null, null);
        if (mv != null) {
            mv.visitCode();
            Label l0 = new Label();
            mv.visitLabel(l0);
            mv.visitVarInsn(Opcodes.ALOAD, 1);
            Label l1 = new Label();
            mv.visitJumpInsn(Opcodes.IFNONNULL, l1);
            mv.visitTypeInsn(Opcodes.NEW, implementationName);
            mv.visitInsn(Opcodes.DUP);
            for (int i = 0; i < constructorArgumentTypes.length; i++) {
                mv.visitVarInsn(constructorArgumentTypes[i].getOpcode(Opcodes.ILOAD), i + 2);
            }
            mv.visitMethodInsn(Opcodes.INVOKESPECIAL, implementationName, "<init>",
                    constructor.getDescriptor());
            mv.visitInsn(Opcodes.ARETURN);
            mv.visitLabel(l1);
            mv.visitVarInsn(Opcodes.ALOAD, 0);
            mv.visitVarInsn(Opcodes.ALOAD, 1);
            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, name, "getDelegate", getDelegateDesc);
            for (int i = 0; i < constructorArgumentTypes.length; i++) {
                mv.visitVarInsn(constructorArgumentTypes[i].getOpcode(Opcodes.ILOAD), i + 2);
            }
            mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, factoryDelegateInterfaceName, "create",
                    constructor.getFactoryDelegateMethodDescriptor());
            mv.visitInsn(Opcodes.ARETURN);
            Label l3 = new Label();
            mv.visitLabel(l3);
            mv.visitLocalVariable("this", "L" + name + ";", null, l0, l3, 0);
            mv.visitLocalVariable("extensionInterface", "Ljava/lang/Class;", null, l0, l3, 1);
            for (int i = 0; i < constructorArgumentTypes.length; i++) {
                mv.visitLocalVariable("arg" + i, constructorArgumentTypes[i].getDescriptor(), null, l0, l3,
                        i + 2);
            }
            mv.visitMaxs(argumentTypes.length + 1, argumentTypes.length + 1);
            mv.visitEnd();
        }
    }
    {
        MethodVisitor mv = classVisitor.visitMethod(Opcodes.ACC_STATIC, "<clinit>", "()V", null, null);
        if (mv != null) {
            mv.visitCode();
            mv.visitTypeInsn(Opcodes.NEW, name);
            mv.visitInsn(Opcodes.DUP);
            mv.visitMethodInsn(Opcodes.INVOKESPECIAL, name, "<init>", "()V");
            mv.visitFieldInsn(Opcodes.PUTSTATIC, name, "INSTANCE", "L" + factoryInterfaceName + ";");
            mv.visitInsn(Opcodes.RETURN);
            mv.visitMaxs(2, 0);
            mv.visitEnd();
        }
    }
    classVisitor.visitEnd();
}

From source file:com.googlecode.gwt.test.internal.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.
 *//*from   w  w  w.jav  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());
                mv.visitInsn(toCall.getReturnType().getOpcode(Opcodes.IRETURN));
                mv.visitMaxs(size, var);
                mv.visitEnd();
            }
        }
    }
}

From source file:com.hea3ven.hardmodetweaks.core.ClassTransformerHardModeTweaks.java

License:Open Source License

private void patchWorldServerTick(MethodNode method, boolean obfuscated) {
    Iterator<AbstractInsnNode> iter = method.instructions.iterator();

    // Replace/*from w  w  w.  ja  v a2  s .c  om*/
    // > this.worldInfo.setWorldTime(this.worldInfo.getWorldTime() + 1L);
    // To
    // > TimeTweaksManager.addTick(this.worldInfo);
    int index = 0;
    while (iter.hasNext()) {
        AbstractInsnNode currentNode = iter.next();

        if (currentNode.getOpcode() == Opcodes.INVOKEVIRTUAL) {
            MethodInsnNode methodInsnNode = (MethodInsnNode) currentNode;
            if (WORLD_INFO_GET_WORLD_TIME.matchesNode(methodInsnNode, "()J", obfuscated)
                    && currentNode.getNext().getOpcode() == Opcodes.LCONST_1) {
                // Found the call
                index = method.instructions.indexOf(currentNode) - 2;
            }
        }
    }
    if (index == 0)
        error("Could not find call in WorldServer.tick method");

    method.instructions.remove(method.instructions.get(index));
    method.instructions.remove(method.instructions.get(index));
    method.instructions.remove(method.instructions.get(index));
    method.instructions.remove(method.instructions.get(index));
    method.instructions.remove(method.instructions.get(index));
    method.instructions.remove(method.instructions.get(index));
    method.instructions.insertBefore(method.instructions.get(index),
            new MethodInsnNode(Opcodes.INVOKESTATIC, "com/hea3ven/hardmodetweaks/TimeTweaksManager", "addTick",
                    "(L" + WORLD_INFO.getPath(obfuscated) + ";)V"));
}

From source file:com.hea3ven.hardmodetweaks.core.ClassTransformerHardModeTweaks.java

License:Open Source License

private void patchWorldClientTick(MethodNode method, boolean obfuscated) {
    Iterator<AbstractInsnNode> iter = method.instructions.iterator();

    // Replace/*from   w ww . j a  va2  s  . co m*/
    // > this.setWorldTime(this.getWorldTime() + 1L);
    // To
    // > TimeTweaksManager.addTick(this.provider);
    int index = 0;
    while (iter.hasNext()) {
        AbstractInsnNode currentNode = iter.next();

        if (currentNode.getOpcode() == Opcodes.INVOKEVIRTUAL) {
            MethodInsnNode methodInsnNode = (MethodInsnNode) currentNode;
            if (WORLD_CLIENT_GET_WORLD_TIME.matchesNode(methodInsnNode, "()J", obfuscated)
                    && currentNode.getNext().getOpcode() == Opcodes.LCONST_1) {
                index = method.instructions.indexOf(currentNode) - 1;
            }
        }
    }
    if (index == 0)
        error("Could not find call in WorldServer.tick method");

    method.instructions.remove(method.instructions.get(index));
    method.instructions.remove(method.instructions.get(index));
    method.instructions.remove(method.instructions.get(index));
    method.instructions.remove(method.instructions.get(index));
    method.instructions.remove(method.instructions.get(index));
    method.instructions.insertBefore(method.instructions.get(index),
            new FieldInsnNode(Opcodes.GETFIELD, WORLD.getPath(obfuscated), WORLD_PROVIDER.get(obfuscated),
                    "L" + WORLD_PROVIDER_CLASS.getPath(obfuscated) + ";"));
    method.instructions.insert(method.instructions.get(index),
            new MethodInsnNode(Opcodes.INVOKESTATIC, "com/hea3ven/hardmodetweaks/TimeTweaksManager", "addTick",
                    "(L" + WORLD_PROVIDER_CLASS.getPath(obfuscated) + ";)V"));
}

From source file:com.heliosdecompiler.appifier.Appifier.java

License:Apache License

private static byte[] transform(byte[] classBytes) {
    ClassReader reader = new ClassReader(classBytes);
    ClassWriter writer = new ClassWriter(Opcodes.ASM5);

    reader.accept(new ClassVisitor(Opcodes.ASM5, writer) {
        @Override//from ww  w .  ja va 2 s.  c  om
        public MethodVisitor visitMethod(int i, String s, String s1, String s2, String[] strings) {
            return new MethodVisitor(Opcodes.ASM5, writer.visitMethod(i, s, s1, s2, strings)) {
                @Override
                public void visitFieldInsn(int opcode, String owner, String name, String desc) {
                    if (opcode == Opcodes.GETSTATIC) {
                        if (owner.equals("java/lang/System")) {
                            if (desc.equals("Ljava/io/PrintStream;")) {
                                if (name.equals("out")) {
                                    super.visitFieldInsn(Opcodes.GETSTATIC,
                                            "com/heliosdecompiler/appifier/SystemHook", "out",
                                            "Ljava/lang/ThreadLocal;");
                                    super.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/ThreadLocal", "get",
                                            "()Ljava/lang/Object;", false);
                                    super.visitTypeInsn(Opcodes.CHECKCAST, "java/io/PrintStream");
                                    return;
                                } else if (name.equals("err")) {
                                    super.visitFieldInsn(Opcodes.GETSTATIC,
                                            "com/heliosdecompiler/appifier/SystemHook", "err",
                                            "Ljava/lang/ThreadLocal;");
                                    super.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/ThreadLocal", "get",
                                            "()Ljava/lang/Object;", false);
                                    super.visitTypeInsn(Opcodes.CHECKCAST, "java/io/PrintStream");
                                    return;
                                }
                            }
                        }
                    }
                    super.visitFieldInsn(opcode, owner, name, desc);
                }
            };
        }
    }, 0);

    return writer.toByteArray();
}

From source file:com.khubla.jvmbasic.jvmbasicc.compiler.RTLHelper.java

License:Open Source License

/**
 * push a value onto the Execution Context stack
 * <p>/*from  ww  w.  j  a  v  a2s .c o m*/
 * <code>
 * executionContext.push(db);
 * </code>
 * </p>
 */
public static void push(GenerationContext generationContext, double db) {
    generationContext.getMethodVisitor().visitVarInsn(Opcodes.ALOAD, 0);
    generationContext.getMethodVisitor().visitFieldInsn(Opcodes.GETFIELD, generationContext.getClassName(),
            EXECUTIONCONTEXT_NAME, JASIC_RUNTIME_EXECUTIONCONTEXT_TYPE);
    generationContext.getMethodVisitor().visitLdcInsn(new Double(db));
    generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Double", "valueOf",
            "(D)Ljava/lang/Double;");
    generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL, JASIC_RUNTIME_EXECUTIONCONTEXT,
            "push", "(Ljava/lang/Double;)V");
}

From source file:com.khubla.jvmbasic.jvmbasicc.compiler.RTLHelper.java

License:Open Source License

/**
 * push a value onto the Execution Context stack
 * <p>/*from ww  w  .j  a v a2s.c  o  m*/
 * <code>
 * executionContext.push(i);
 * </code>
 * </p>
 */
public static void push(GenerationContext generationContext, int i) {
    generationContext.getMethodVisitor().visitVarInsn(Opcodes.ALOAD, 0);
    generationContext.getMethodVisitor().visitFieldInsn(Opcodes.GETFIELD, generationContext.getClassName(),
            EXECUTIONCONTEXT_NAME, JASIC_RUNTIME_EXECUTIONCONTEXT_TYPE);
    generationContext.getMethodVisitor().visitIntInsn(Opcodes.BIPUSH, i);
    generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Integer", "valueOf",
            "(I)Ljava/lang/Integer;");
    generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL, JASIC_RUNTIME_EXECUTIONCONTEXT,
            "push", "(Ljava/lang/Integer;)V");
}

From source file:com.khubla.jvmbasic.jvmbasicc.compiler.RTLHelper.java

License:Open Source License

/**
 * push a value onto the Execution Context stack
 * <p>//from w  w  w.j a  v a2 s  .c o  m
 * <code>
 * executionContext.push(str);
 * </code>
 * </p>
 */
public static void push(GenerationContext generationContext, String str) {
    generationContext.getMethodVisitor().visitVarInsn(Opcodes.ALOAD, 0);
    generationContext.getMethodVisitor().visitFieldInsn(Opcodes.GETFIELD, generationContext.getClassName(),
            EXECUTIONCONTEXT_NAME, JASIC_RUNTIME_EXECUTIONCONTEXT_TYPE);
    generationContext.getMethodVisitor().visitLdcInsn(str);
    generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL, JASIC_RUNTIME_EXECUTIONCONTEXT,
            "push", "(Ljava/lang/String;)V");
}

From source file:com.khubla.jvmbasic.jvmbasicc.function.impl.rule.absfuncRule.java

License:Open Source License

@Override
public boolean execute(GenerationContext generationContext) throws Exception {
    try {//from   w  ww  .  j  a  v a 2  s. c  o  m
        /*
         * get the argument
         */
        Dispatcher.dispatchChildren(generationContext);
        /*
         * pop the argument and apply abs
         */
        generationContext.getMethodVisitor().visitVarInsn(Opcodes.ALOAD, 0);
        generationContext.getMethodVisitor().visitFieldInsn(Opcodes.GETFIELD, generationContext.getClassName(),
                RTLHelper.EXECUTIONCONTEXT_NAME, RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT_TYPE);
        generationContext.getMethodVisitor().visitVarInsn(Opcodes.ALOAD, 0);
        generationContext.getMethodVisitor().visitFieldInsn(Opcodes.GETFIELD, generationContext.getClassName(),
                RTLHelper.EXECUTIONCONTEXT_NAME, RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT_TYPE);
        generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL,
                RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT, "pop", "()Lcom/khubla/jvmbasic/jvmbasicrt/Value;");
        generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKESTATIC, RTLHelper.JASIC_RUNTIME_MATH,
                "ABS", "(Lcom/khubla/jvmbasic/jvmbasicrt/Value;)Ljava/lang/Double;");
        generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL,
                RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT, "push", "(Ljava/lang/Double;)V");
        return true;
    } catch (final Exception e) {
        throw new Exception("Exception in execute", e);
    }
}