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.dragome.callbackevictor.serverside.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/")) {
     System.out.println("SKIP:: " + owner + "." + name + desc);
     return false;//w  ww.  j  a v  a  2 s.c  o m
        }
    */

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

From source file:com.e2info.helloasm.HelloAsmApp.java

License:Open Source License

public static void main(String[] args) throws IOException {
    String name = "HelloAsm";
    int flag = ClassWriter.COMPUTE_MAXS;
    ClassWriter cw = new ClassWriter(flag);
    cw.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, name, null, "java/lang/Object", null);

    cw.visitSource(name + ".java", null);

    {/*w  w w.  j  a  v a  2  s .  c  om*/
        MethodVisitor mv;
        mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
        mv.visitInsn(Opcodes.RETURN);
        // we need this call to take effect ClassWriter.COMPUTE_MAXS flag.
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }

    {
        MethodVisitor mv;
        mv = cw.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, "main", "([Ljava/lang/String;)V", null,
                null);
        mv.visitCode();
        mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
        mv.visitLdcInsn("hello ASM");
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
        mv.visitInsn(Opcodes.RETURN);
        // we need this call to take effect ClassWriter.COMPUTE_MAXS flag.
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }

    cw.visitEnd();

    // build binary
    byte[] bin = cw.toByteArray();

    // save asm trace for human readable
    {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        new ClassReader(bin).accept(new TraceClassVisitor(pw), 0);
        File f = new File(name + ".txt");
        FileUtils.writeStringToFile(f, sw.toString());
    }

    // save as calss file
    {
        File f = new File(name + ".class");
        FileUtils.writeByteArrayToFile(f, bin);
    }

}

From source file:com.enea.jcarder.agent.instrument.StackAnalyzeMethodVisitor.java

License:GNU General Public License

public void visitMethodInsn(int opCode, String owner, String name, String desc) {
    mMethodVisitor.visitMethodInsn(opCode, owner, name, desc);
    switch (opCode) {
    case Opcodes.INVOKEVIRTUAL:
        // pass through to next case.
    case Opcodes.INVOKESPECIAL:
        // pass through to next case.
    case Opcodes.INVOKESTATIC:
        if ("forName".equals(name) && "java/lang/Class".equals(owner)
                && "(Ljava/lang/String;)Ljava/lang/Class;".equals(desc)) {
            Object stackObject = popObject();
            if (stackObject instanceof String) {
                String classDescription = ((String) stackObject) + ".class";
                pushTextualDescription(classDescription);
                break;
            }//from   w w w  . j a  v  a 2  s.c o  m
        }
        // pass through to next case.
    case Opcodes.INVOKEINTERFACE:
        clear();
        if (isNonVoidMethod(name, desc)) {
            pushTextualDescription(owner + "." + name + "()");
        }
        break;
    default:
        clear();
    }
}

From source file:com.foxelbox.spigotpatcher.patchers.HideShowPlayerPatcher.java

License:Open Source License

@Override
public MethodVisitor getVisitor(int api, MethodVisitor parent) {
    return new MethodPatcherVisitor(api, parent) {
        @Override// w  w w . j  a v  a 2s  .c o m
        protected boolean checkMethodInsn(int opcode, String clazz, String name, String desc) {
            if (opcode == Opcodes.INVOKEVIRTUAL && clazz.endsWith("/PlayerConnection")
                    && name.equals("sendPacket")) {
                visitInsn(Opcodes.POP);
                visitInsn(Opcodes.ACONST_NULL);
                System.out.println("HideShowPatcher: PATCHED (2 times = OK)");
            }
            return true;
        }
    };
}

From source file:com.foxelbox.spigotpatcher.patchers.OnPlayerJoinDisconnectPatcher.java

License:Open Source License

@Override
public MethodVisitor getVisitor(int api, MethodVisitor parent) {
    return new MethodVisitor(api, parent) {
        protected void checkMethodInsn(int opcode, String clazz, String name, String desc) {
            if (opcode == Opcodes.INVOKEVIRTUAL && clazz.endsWith("/CraftPlayer") && name.equals("canSee")) {
                visitInsn(Opcodes.POP);/*  w ww  . j av a2  s  . c o  m*/
                visitInsn(Opcodes.ICONST_1);
                System.out.println("OnJoinPatcher: PATCHED (3 times = OK)");
            }
        }

        @Override
        public void visitMethodInsn(int opcode, String clazz, String name, String desc, boolean isInterface) {
            super.visitMethodInsn(opcode, clazz, name, desc, isInterface);
            checkMethodInsn(opcode, clazz, name, desc);
        }

        @Override
        public void visitMethodInsn(int opcode, String clazz, String name, String desc) {
            super.visitMethodInsn(opcode, clazz, name, desc);
            checkMethodInsn(opcode, clazz, name, desc);
        }
    };
}

From source file:com.gargoylesoftware.js.nashorn.internal.codegen.CompilerConstants.java

License:Open Source License

/**
 * Create a call representing an invokevirtual to a given method. Don't
 * attempt to look this up at compile time
 *
 * @param clazz  the class//from   www .  j  av  a 2s.  c  o m
 * @param name   the method name
 * @param rtype  the return type
 * @param ptypes the parameter types
 *
 * @return Call representing specified invokevirtual call
 */
public static Call virtualCallNoLookup(final Class<?> clazz, final String name, final Class<?> rtype,
        final Class<?>... ptypes) {
    return new Call(null, className(clazz), name, methodDescriptor(rtype, ptypes)) {
        @Override
        MethodEmitter invoke(final MethodEmitter method) {
            return method.invokevirtual(className, name, descriptor);
        }

        @Override
        public void invoke(final MethodVisitor mv) {
            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, className, name, descriptor, false);
        }
    };
}

From source file:com.gargoylesoftware.js.nashorn.internal.codegen.CompilerConstants.java

License:Open Source License

/**
 * Create a virtual call, given an explicit lookup, looking up the method handle for it at the same time
 *
 * @param lookup the lookup/*from ww  w  .  j  a v  a  2s  .  c  om*/
 * @param clazz  the class
 * @param name   the name of the method
 * @param rtype  the return type
 * @param ptypes the parameter types
 *
 * @return the call object representing the virtual call
 */
public static Call virtualCall(final MethodHandles.Lookup lookup, final Class<?> clazz, final String name,
        final Class<?> rtype, final Class<?>... ptypes) {
    return new Call(MH.findVirtual(lookup, clazz, name, MH.type(rtype, ptypes)), className(clazz), name,
            methodDescriptor(rtype, ptypes)) {
        @Override
        MethodEmitter invoke(final MethodEmitter method) {
            return method.invokevirtual(className, name, descriptor);
        }

        @Override
        public void invoke(final MethodVisitor mv) {
            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, className, name, descriptor, false);
        }
    };
}

From source file:com.geeksaga.light.profiler.util.ASMUtil.java

License:Apache License

public static MethodInsnNode createINVOKEVIRTUAL(String className, String methodName, String methodDesc) {
    return createMethodInsn(Opcodes.INVOKEVIRTUAL, className, methodName, methodDesc);
}

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

License:Open Source License

public void invokevirtual(Type owner, String name, MethodTypeDescriptor desc, boolean itf) {
    methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, owner.internalName(), name, desc.descriptor(), itf);
    stack.invokevirtual(desc);//from www  .  j a  v  a 2  s. c  om
}

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);//
}