Example usage for org.objectweb.asm Opcodes INVOKESTATIC

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

Introduction

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

Prototype

int INVOKESTATIC

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

Click Source Link

Usage

From source file:com.mebigfatguy.exagent.StackTraceMethodVisitor.java

License:Apache License

private void injectCallStackPopulation() {

    // ExAgent.METHOD_INFO.get();
    super.visitFieldInsn(Opcodes.GETSTATIC, EXASUPPORT_CLASS_NAME, "METHOD_INFO",
            signaturizeClass(THREADLOCAL_CLASS_NAME));
    super.visitMethodInsn(Opcodes.INVOKEVIRTUAL, THREADLOCAL_CLASS_NAME, "get", "()Ljava/lang/Object;", false);
    super.visitTypeInsn(Opcodes.CHECKCAST, LIST_CLASS_NAME);

    super.visitInsn(Opcodes.DUP);
    super.visitMethodInsn(Opcodes.INVOKEINTERFACE, LIST_CLASS_NAME, "size", "()I", true);
    super.visitVarInsn(Opcodes.ISTORE, depthLocalSlot);

    //new MethodInfo(cls, name, parmMap);
    super.visitTypeInsn(Opcodes.NEW, METHODINFO_CLASS_NAME);
    super.visitInsn(Opcodes.DUP);
    super.visitLdcInsn(clsName.replace('.', '/'));
    super.visitLdcInsn(methodName);

    if (parms.isEmpty()) {
        super.visitMethodInsn(Opcodes.INVOKESTATIC, COLLECTIONS_CLASS_NAME, "emptyList", "()Ljava/util/List;",
                false);/*from   www  .  ja  v  a 2  s.  c om*/
    } else {
        super.visitTypeInsn(Opcodes.NEW, ARRAYLIST_CLASS_NAME);
        super.visitInsn(Opcodes.DUP);
        super.visitIntInsn(Opcodes.BIPUSH, parms.size());
        super.visitMethodInsn(Opcodes.INVOKESPECIAL, ARRAYLIST_CLASS_NAME, CTOR_NAME, "(I)V", false);

        for (Parm parm : parms) {
            super.visitInsn(Opcodes.DUP);

            switch (parm.signature) {

            case "C":
                super.visitVarInsn(Opcodes.ILOAD, parm.register);
                super.visitMethodInsn(Opcodes.INVOKESTATIC, STRING_CLASS_NAME, "valueOf",
                        "(C)Ljava/lang/String;", false);
                break;

            case "Z":
                super.visitVarInsn(Opcodes.ILOAD, parm.register);
                super.visitMethodInsn(Opcodes.INVOKESTATIC, STRING_CLASS_NAME, "valueOf",
                        "(Z)Ljava/lang/String;", false);
                break;

            case "B":
            case "S":
            case "I":
                super.visitVarInsn(Opcodes.ILOAD, parm.register);
                super.visitMethodInsn(Opcodes.INVOKESTATIC, STRING_CLASS_NAME, "valueOf",
                        "(I)Ljava/lang/String;", false);
                break;

            case "J":
                super.visitVarInsn(Opcodes.LLOAD, parm.register);
                super.visitMethodInsn(Opcodes.INVOKESTATIC, STRING_CLASS_NAME, "valueOf",
                        "(J)Ljava/lang/String;", false);
                break;

            case "F":
                super.visitVarInsn(Opcodes.FLOAD, parm.register);
                super.visitMethodInsn(Opcodes.INVOKESTATIC, STRING_CLASS_NAME, "valueOf",
                        "(F)Ljava/lang/String;", false);
                break;

            case "D":
                super.visitVarInsn(Opcodes.DLOAD, parm.register);
                super.visitMethodInsn(Opcodes.INVOKESTATIC, STRING_CLASS_NAME, "valueOf",
                        "(D)Ljava/lang/String;", false);
                break;

            default:
                super.visitVarInsn(Opcodes.ALOAD, parm.register);
                if (parm.signature.startsWith("[")) {
                    char arrayElemTypeChar = parm.signature.charAt(1);
                    if ((arrayElemTypeChar == 'L') || (arrayElemTypeChar == '[')) {
                        super.visitMethodInsn(Opcodes.INVOKESTATIC, ARRAYS_CLASS_NAME, "toString",
                                "([Ljava/lang/Object;)Ljava/lang/String;", false);
                    } else {
                        super.visitMethodInsn(Opcodes.INVOKESTATIC, ARRAYS_CLASS_NAME, "toString",
                                "([" + arrayElemTypeChar + ")Ljava/lang/String;", false);
                    }
                } else {
                    super.visitMethodInsn(Opcodes.INVOKESTATIC, STRING_CLASS_NAME, "valueOf",
                            "(Ljava/lang/Object;)Ljava/lang/String;", false);
                }
                break;
            }

            if (maxParmSize > 0) {
                super.visitInsn(Opcodes.DUP);
                super.visitMethodInsn(Opcodes.INVOKEVIRTUAL, STRING_CLASS_NAME, "length", "()I", false);
                if (maxParmSize <= 127) {
                    super.visitIntInsn(Opcodes.BIPUSH, maxParmSize);
                } else {
                    super.visitLdcInsn(maxParmSize);
                }
                Label falseLabel = new Label();
                super.visitJumpInsn(Opcodes.IF_ICMPLE, falseLabel);
                super.visitIntInsn(Opcodes.BIPUSH, 0);
                super.visitLdcInsn(maxParmSize);
                super.visitMethodInsn(Opcodes.INVOKEVIRTUAL, STRING_CLASS_NAME, "substring",
                        "(II)Ljava/lang/String;", false);
                super.visitLabel(falseLabel);
            }

            super.visitMethodInsn(Opcodes.INVOKEINTERFACE, LIST_CLASS_NAME, "add", "(Ljava/lang/Object;)Z",
                    true);
            super.visitInsn(Opcodes.POP);
        }
    }

    super.visitMethodInsn(Opcodes.INVOKESPECIAL, METHODINFO_CLASS_NAME, CTOR_NAME,
            "(Ljava/lang/String;Ljava/lang/String;Ljava/util/List;)V", false);

    //add(methodInfo);
    super.visitMethodInsn(Opcodes.INVOKEINTERFACE, LIST_CLASS_NAME, "add", "(Ljava/lang/Object;)Z", true);
    super.visitInsn(Opcodes.POP);
}

From source file:com.mebigfatguy.junitflood.jvm.OperandStack.java

License:Apache License

public void performMethodInsn(int opcode, String owner, String name, String desc) {
    switch (opcode) {
    case Opcodes.INVOKEVIRTUAL:
    case Opcodes.INVOKESPECIAL:
    case Opcodes.INVOKESTATIC:
    case Opcodes.INVOKEINTERFACE:
        String[] parmSigs = SignatureUtils.splitMethodParameterSignatures(desc);
        int numParms = parmSigs.length;
        while ((numParms--) > 0) {
            pop();//from  w w  w  . j a v  a2  s .com
        }
        if (opcode != Opcodes.INVOKESTATIC) {
            pop();
        }

        String returnSig = SignatureUtils.getReturnSignature(desc);
        if (!"V".equals(returnSig)) {
            Operand op = new Operand();
            op.setSignature(returnSig);
            push(op);
        }
        break;

    case Opcodes.INVOKEDYNAMIC:
        //no idea
        break;
    }
}

From source file:com.microsoft.applicationinsights.agent.internal.agent.redis.JedisMethodVisitor.java

License:Open Source License

protected void boxVariable(Type argumentType, int argumentIndex) {
    if (argumentType.equals(Type.BOOLEAN_TYPE)) {
        mv.visitVarInsn(Opcodes.ILOAD, argumentIndex);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Boolean", "valueOf", "(Z)Ljava/lang/Boolean;",
                false);//from   w  w w  . j a  v  a 2s  .c o  m
    } else if (argumentType.equals(Type.BYTE_TYPE)) {
        mv.visitVarInsn(Opcodes.ILOAD, argumentIndex);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Byte", "valueOf", "(B)Ljava/lang/Byte;", false);
    } else if (argumentType.equals(Type.CHAR_TYPE)) {
        mv.visitVarInsn(Opcodes.ILOAD, argumentIndex);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Character", "valueOf", "(C)Ljava/lang/Character;",
                false);
    } else if (argumentType.equals(Type.SHORT_TYPE)) {
        mv.visitVarInsn(Opcodes.ILOAD, argumentIndex);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Short", "valueOf", "(S)Ljava/lang/Short;", false);
    } else if (argumentType.equals(Type.INT_TYPE)) {
        mv.visitVarInsn(Opcodes.ILOAD, argumentIndex);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;",
                false);
    } else if (argumentType.equals(Type.LONG_TYPE)) {
        mv.visitVarInsn(Opcodes.LLOAD, argumentIndex);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Long", "valueOf", "(J)Ljava/lang/Long;", false);
    } else if (argumentType.equals(Type.FLOAT_TYPE)) {
        mv.visitVarInsn(Opcodes.FLOAD, argumentIndex);
        super.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Float", "valueOf", "(F)Ljava/lang/Long;",
                false);
    } else if (argumentType.equals(Type.DOUBLE_TYPE)) {
        mv.visitVarInsn(Opcodes.DLOAD, argumentIndex);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Double", "valueOf", "(D)Ljava/lang/Double;", false);
    } else {
        mv.visitVarInsn(Opcodes.ALOAD, argumentIndex);
    }
}

From source file:com.microsoft.Malmo.OverclockingClassTransformer.java

License:Open Source License

private static void overclockRenderer(ClassNode node, boolean isObfuscated) {
    // We're attempting to turn this line from Minecraft.runGameLoop:
    //          this.updateDisplay();
    // into this:
    //          TimeHelper.updateDisplay();
    // TimeHelper's method then decides whether or not to pass the call on to Minecraft.updateDisplay().

    final String methodName = isObfuscated ? "as" : "runGameLoop";
    final String methodDescriptor = "()V"; // No params, returns void.

    System.out.println("MALMO: Found Minecraft, attempting to transform it");

    for (MethodNode method : node.methods) {
        if (method.name.equals(methodName) && method.desc.equals(methodDescriptor)) {
            System.out.println("MALMO: Found Minecraft.runGameLoop() method, attempting to transform it");
            for (AbstractInsnNode instruction : method.instructions.toArray()) {
                if (instruction.getOpcode() == Opcodes.INVOKEVIRTUAL) {
                    MethodInsnNode visitMethodNode = (MethodInsnNode) instruction;
                    if (visitMethodNode.name.equals(isObfuscated ? "h" : "updateDisplay")) {
                        visitMethodNode.owner = "com/microsoft/Malmo/Utils/TimeHelper";
                        if (isObfuscated) {
                            visitMethodNode.name = "updateDisplay";
                        }/*from w  w w  .  j  a v a  2  s  .  c o m*/
                        visitMethodNode.setOpcode(Opcodes.INVOKESTATIC);
                        method.instructions.remove(visitMethodNode.getPrevious()); // ALOAD 0 not needed for static invocation.
                        System.out.println("MALMO: Hooked into call to Minecraft.updateDisplay()");
                    }
                }
            }
        }
    }
}

From source file:com.mogujie.instantrun.IncrementalChangeVisitor.java

License:Apache License

/**
 * To each class, add the dispatch method called by the original code that acts as a trampoline to
 * invoke the changed methods./*  w  w  w .  ja va2 s  .c  o  m*/
 * <p/>
 * Pseudo code:
 * <code>
 * Object access$dispatch(String name, object[] args) {
 * if (name.equals(
 * "firstMethod.(L$type;Ljava/lang/String;Ljava/lang/Object;)Ljava/lang/Object;")) {
 * return firstMethod(($type)arg[0], (String)arg[1], arg[2]);
 * }
 * if (name.equals("secondMethod.(L$type;Ljava/lang/String;I;)V")) {
 * secondMethod(($type)arg[0], (String)arg[1], (int)arg[2]);
 * return;
 * }
 * ...
 * StringBuilder $local1 = new StringBuilder();
 * $local1.append("Method not found ");
 * $local1.append(name);
 * $local1.append(" in " + visitedClassName +
 * "$dispatch implementation, restart the application");
 * throw new $package/InstantReloadException($local1.toString());
 * }
 * </code>
 */
private void addDispatchMethod() {
    int access = Opcodes.ACC_PUBLIC | Opcodes.ACC_VARARGS;
    Method m = new Method("access$dispatch", "(I[Ljava/lang/Object;)Ljava/lang/Object;");
    MethodVisitor visitor = super.visitMethod(access, m.getName(), m.getDescriptor(), null, null);

    final GeneratorAdapter mv = new GeneratorAdapter(access, m, visitor);

    if (TRACING_ENABLED) {
        mv.push("Redirecting ");
        mv.loadArg(0);
        trace(mv, 2);
    }

    List<MethodNode> allMethods = new ArrayList();

    // if we are disabled, do not generate any dispatch, the method will throw an exception
    // if invoked which should never happen.
    if (!instantRunDisabled) {
        //noinspection unchecked
        allMethods.addAll(classNode.methods);
        allMethods.addAll(addedMethods);
    }

    final Map<String, MethodNode> methods = new HashMap();
    for (MethodNode methodNode : allMethods) {
        if (methodNode.name.equals("<clinit>") || methodNode.name.equals("<init>")) {
            continue;
        }
        if (!isAccessCompatibleWithInstantRun(methodNode.access)) {
            continue;
        }
        methods.put(methodNode.name + "." + methodNode.desc, methodNode);
    }

    new IntSwitch() {
        @Override
        void visitString() {
            mv.visitVarInsn(Opcodes.ALOAD, 1);
        }

        @Override
        void visitInt() {
            mv.visitVarInsn(Opcodes.ILOAD, 1);
        }

        @Override
        void visitCase(String methodName) {
            MethodNode methodNode = methods.get(methodName);
            String name = methodNode.name;
            boolean isStatic = (methodNode.access & Opcodes.ACC_STATIC) != 0;
            String newDesc = computeOverrideMethodDesc(methodNode.desc, isStatic);

            if (TRACING_ENABLED) {
                trace(mv, "M: " + name + " P:" + newDesc);
            }
            Type[] args = Type.getArgumentTypes(newDesc);
            int argc = 0;
            for (Type t : args) {
                mv.visitVarInsn(Opcodes.ALOAD, 2);
                mv.push(argc);
                mv.visitInsn(Opcodes.AALOAD);
                ByteCodeUtils.unbox(mv, t);
                argc++;
            }
            mv.visitMethodInsn(Opcodes.INVOKESTATIC, visitedClassName + "$override",
                    isStatic ? computeOverrideMethodName(name, methodNode.desc) : name, newDesc, false);
            Type ret = Type.getReturnType(methodNode.desc);
            if (ret.getSort() == Type.VOID) {
                mv.visitInsn(Opcodes.ACONST_NULL);
            } else {
                mv.box(ret);
            }
            mv.visitInsn(Opcodes.ARETURN);
        }

        @Override
        void visitDefault() {
            writeMissingMessageWithHash(mv, visitedClassName);
        }
    }.visit(mv, methods.keySet(), visitedClassName);

    mv.visitMaxs(0, 0);
    mv.visitEnd();

}

From source file:com.mogujie.instantrun.IntSwitch.java

License:Apache License

void writeMissingMessageWithHash(GeneratorAdapter mv, String visitedClassName) {
    mv.newInstance(INSTANT_RELOAD_EXCEPTION_TYPE);
    mv.dup();/*from w ww.  jav  a 2  s.c  o m*/
    mv.push("int switch could not find %d in %s");
    mv.push(3);
    mv.newArray(OBJECT_TYPE);
    mv.dup();
    mv.push(0);
    visitInt();
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;", false);
    mv.arrayStore(OBJECT_TYPE);
    mv.dup();
    mv.push(2);
    mv.push(visitedClassName);
    mv.arrayStore(OBJECT_TYPE);
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/String", "format",
            "(Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;", false);
    mv.invokeConstructor(INSTANT_RELOAD_EXCEPTION_TYPE, Method.getMethod("void <init> (String)"));
    mv.throwException();
}

From source file:com.navercorp.pinpoint.profiler.instrument.ASMMethodNodeAdapter.java

License:Apache License

public void addDelegator(final String superClassInternalName) {
    if (superClassInternalName == null) {
        throw new IllegalArgumentException("super class internal name must not be null.");
    }/*from   w  w  w  . j  a va2  s  .c  om*/

    final InsnList instructions = this.methodNode.instructions;
    if (isStatic()) {
        // load parameters
        this.methodVariables.loadArgs(instructions);
        // invoke static
        instructions.add(new MethodInsnNode(Opcodes.INVOKESTATIC, superClassInternalName, this.methodNode.name,
                this.methodNode.desc, false));
    } else {
        // load this
        this.methodVariables.loadVar(instructions, 0);
        // load parameters
        this.methodVariables.loadArgs(instructions);
        // invoke special
        instructions.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, superClassInternalName, this.methodNode.name,
                this.methodNode.desc, false));
    }
    // return
    this.methodVariables.returnValue(instructions);
}

From source file:com.navercorp.pinpoint.profiler.instrument.ASMMethodVariables.java

License:Apache License

private void initInterceptorVar(final InsnList instructions, final int interceptorId) {
    assertInitializedInterceptorLocalVariables();
    this.interceptorVarIndex = addLocalVariable("_$PINPOINT$_interceptor",
            "Lcom/navercorp/pinpoint/bootstrap/interceptor/Interceptor;");
    push(instructions, interceptorId);/*www. ja  va2  s  .c om*/
    instructions.add(new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(InterceptorRegistry.class),
            "getInterceptor", "(I)" + Type.getDescriptor(Interceptor.class), false));
    storeVar(instructions, this.interceptorVarIndex);
    this.resultVarIndex = addLocalVariable("_$PINPOINT$_result", "Ljava/lang/Object;");
    loadNull(instructions);
    storeVar(instructions, this.resultVarIndex);
    this.throwableVarIndex = addLocalVariable("_$PINPOINT$_throwable", "Ljava/lang/Throwable;");
    loadNull(instructions);
    storeVar(instructions, this.throwableVarIndex);
}

From source file:com.navercorp.pinpoint.profiler.instrument.lambda.MethodInstReplacer.java

License:Apache License

@Override
public MethodVisitor visitMethod(int access, String name, String descriptor, String signature,
        String[] exceptions) {/*from  w ww  .ja v  a2  s.c  om*/
    if (methodName.equals(name)) {
        logger.info("visitMethod {} desc:{} {}", name, descriptor);

        final MethodVisitor superMethodVisitor = super.visitMethod(access, name, descriptor, signature,
                exceptions);
        return new MethodVisitor(ASMVersion.VERSION, superMethodVisitor) {
            @Override
            public void visitMethodInsn(int opcode, String owner, String name, String descriptor,
                    boolean isInterface) {
                if (targetClassName.equals(owner) && targetMethodName.equals(name)) {
                    if (logger.isInfoEnabled()) {
                        logger.info("replace MethodInsn {}.{}() -> {}.{}()", owner, name, delegateClassName,
                                delegateMethodName);
                    }
                    transformCount++;
                    super.visitMethodInsn(Opcodes.INVOKESTATIC, delegateClassName, delegateMethodName,
                            descriptor, isInterface);
                } else {
                    super.visitMethodInsn(opcode, owner, name, descriptor, isInterface);
                }
            }
        };
    }
    return super.visitMethod(access, name, descriptor, signature, exceptions);
}

From source file:com.nginious.http.serialize.JsonSerializerCreator.java

License:Apache License

/**
 * Creates bytecode for serializing a bean property which returns a collection of beans that are serializable. Bean serializability
 * is determined as described in the class description.
 * /*from  www  .j a  v  a 2  s  .c  o m*/
 * @param visitor method visitor used for creating bytecode
 * @param intBeanClazzName binary class name of bean
 * @param methodName binary name of get method in bean returning collection
 * @param returnType return type of get method in bean
 * @param collectionBeanType class of serializable bean found in collection
 */
private void createBeanCollectionSerializationCode(MethodVisitor visitor, String intBeanClazzName,
        String methodName, String propertyName, Class<?> returnType, Class<?> collectionBeanType) {
    visitor.visitVarInsn(Opcodes.ALOAD, 0);
    visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "com/nginious/http/serialize/JsonSerializer",
            "getSerializerFactory", "()Lcom/nginious/http/serialize/SerializerFactoryImpl;");
    visitor.visitLdcInsn(collectionBeanType.getName());
    visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Class", "forName",
            "(Ljava/lang/String;)Ljava/lang/Class;");
    visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "com/nginious/http/serialize/SerializerFactoryImpl",
            "createJsonSerializer", "(Ljava/lang/Class;)Lcom/nginious/http/serialize/JsonSerializer;");
    visitor.visitVarInsn(Opcodes.ASTORE, 4);

    visitor.visitTypeInsn(Opcodes.NEW, "com/nginious/http/serialize/JsonBeanCollectionSerializer");
    visitor.visitInsn(Opcodes.DUP);
    visitor.visitVarInsn(Opcodes.ALOAD, 4);
    visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, "com/nginious/http/serialize/JsonBeanCollectionSerializer",
            "<init>", "(Lcom/nginious/http/serialize/JsonSerializer;)V");
    visitor.visitVarInsn(Opcodes.ASTORE, 5);

    visitor.visitVarInsn(Opcodes.ALOAD, 5);
    String intReturnClazzName = returnType.getName().replace('.', '/');
    visitor.visitVarInsn(Opcodes.ALOAD, 3);
    visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, intBeanClazzName, methodName,
            "()L" + intReturnClazzName + ";");
    visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "com/nginious/http/serialize/JsonBeanCollectionSerializer",
            "serialize", "(Ljava/util/Collection;)Lorg/json/JSONArray;");
    visitor.visitVarInsn(Opcodes.ASTORE, 5);

    visitor.visitVarInsn(Opcodes.ALOAD, 1);
    visitor.visitLdcInsn(propertyName);
    visitor.visitVarInsn(Opcodes.ALOAD, 5);
    visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "org/json/JSONObject", "put",
            "(Ljava/lang/String;Ljava/lang/Object;)Lorg/json/JSONObject;");
}