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.google.monitoring.runtime.instrumentation.adapters.EscapeMethodAdapter.java

License:Apache License

private void invokeRecordAllocation(String typeName) {
    allocIndex++;/* ww w  . j  a va  2  s. co  m*/
    super.visitLdcInsn("methodNamePath:" + allocIndex);
    super.visitMethodInsn(Opcodes.INVOKESTATIC, CLASS_PATH, "stop", STOP_SIGNATURE, false);
}

From source file:com.google.singletondetector.visitors.SingletonUsageMethodVisitor.java

License:Apache License

@Override
public void visitMethodInsn(int opcode, String referencedClass, String methodName, String desc) {
    if (opcode == Opcodes.INVOKESTATIC) {
        sd.invokeStatic(referencedClass, desc);
    }//w ww  .j  a v  a 2s .  c o  m
}

From source file:com.google.template.soy.jbcsrc.MethodRef.java

License:Apache License

static MethodRef create(java.lang.reflect.Method method) {
    Class<?> clazz = method.getDeclaringClass();
    TypeInfo ownerType = TypeInfo.create(method.getDeclaringClass());
    boolean isStatic = Modifier.isStatic(method.getModifiers());
    ImmutableList<Type> argTypes;
    if (isStatic) {
        argTypes = ImmutableList.copyOf(Type.getArgumentTypes(method));
    } else {/*from w  w  w . java2s  .  c  o  m*/
        // for instance methods the first 'argument' is always an instance of the class.
        argTypes = ImmutableList.<Type>builder().add(ownerType.type()).add(Type.getArgumentTypes(method))
                .build();
    }
    return new AutoValue_MethodRef(
            clazz.isInterface() ? Opcodes.INVOKEINTERFACE
                    : isStatic ? Opcodes.INVOKESTATIC : Opcodes.INVOKEVIRTUAL,
            ownerType, org.objectweb.asm.commons.Method.getMethod(method), Type.getType(method.getReturnType()),
            argTypes, Features.of());
}

From source file:com.google.template.soy.jbcsrc.restricted.MethodRef.java

License:Apache License

public static MethodRef create(java.lang.reflect.Method method) {
    Class<?> clazz = method.getDeclaringClass();
    TypeInfo ownerType = TypeInfo.create(method.getDeclaringClass());
    boolean isStatic = Modifier.isStatic(method.getModifiers());
    ImmutableList<Type> argTypes;
    if (isStatic) {
        argTypes = ImmutableList.copyOf(Type.getArgumentTypes(method));
    } else {//from  w  w w. j  av  a 2  s.  co  m
        // for instance methods the first 'argument' is always an instance of the class.
        argTypes = ImmutableList.<Type>builder().add(ownerType.type()).add(Type.getArgumentTypes(method))
                .build();
    }
    return new AutoValue_MethodRef(
            clazz.isInterface() ? Opcodes.INVOKEINTERFACE
                    : isStatic ? Opcodes.INVOKESTATIC : Opcodes.INVOKEVIRTUAL,
            ownerType, Method.getMethod(method), Type.getType(method.getReturnType()), argTypes, Features.of());
}

From source file:com.google.template.soy.jbcsrc.restricted.MethodRef.java

License:Apache License

public static MethodRef createStaticMethod(TypeInfo owner, Method method) {
    return new AutoValue_MethodRef(Opcodes.INVOKESTATIC, owner, method, method.getReturnType(),
            ImmutableList.<Type>builder().add(method.getArgumentTypes()).build(), Features.of());
}

From source file:com.google.test.metric.asm.MethodVisitorBuilder.java

License:Apache License

public void visitMethodInsn(final int opcode, final String clazz, final String name, final String desc) {
    SignatureParser signature = parse(desc);
    final List<Type> params = signature.getParameters();
    final Type returnType = signature.getReturnType();
    recorder.add(new Runnable() {
        public void run() {
            String className = namer.nameClass(clazz);
            block.addOp(new Invoke(lineNumber, className, namer.nameMethod(className, name, desc), params,
                    opcode == Opcodes.INVOKESTATIC, returnType));
        }/*w  ww.  j  a  va  2  s .  c  om*/
    });
}

From source file:com.googlecode.ddom.weaver.inject.InjectionAdapter.java

License:Apache License

@Override
public void visitEnd() {
    for (InjectableFieldInfo fieldInfo : injectionInfo.getInjectableFields()) {
        MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC,
                fieldInfo.getFactoryMethodName(), fieldInfo.getFactoryMethodDesc(), null, new String[0]);
        if (mv != null) {
            fieldInfo.getInjector().generateFactoryMethodCode(mv);
        }/*from   w  ww.  jav  a2 s . c o m*/
    }
    if (injectionInfo.hasInjectableInstanceFields()) {
        MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PRIVATE, "inject$$instance", "()V", null, new String[0]);
        if (mv != null) {
            mv.visitCode();
            Label l0 = new Label();
            mv.visitLabel(l0);
            for (InjectableFieldInfo fieldInfo : injectionInfo.getInjectableFields()) {
                if (!fieldInfo.isStatic()) {
                    mv.visitVarInsn(Opcodes.ALOAD, 0);
                    mv.visitMethodInsn(Opcodes.INVOKESTATIC, className, fieldInfo.getFactoryMethodName(),
                            fieldInfo.getFactoryMethodDesc());
                    mv.visitFieldInsn(Opcodes.PUTFIELD, className, fieldInfo.getFieldName(),
                            fieldInfo.getFieldDesc());
                }
            }
            mv.visitInsn(Opcodes.RETURN);
            Label l1 = new Label();
            mv.visitLabel(l1);
            mv.visitLocalVariable("this", "L" + className + ";", null, l0, l1, 0);
            mv.visitMaxs(2, 1);
            mv.visitEnd();
        }
    }
    if (injectionInfo.hasInjectableClassFields()) {
        MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC,
                hasClassInitializer ? "inject$$class" : "<clinit>", "()V", null, new String[0]);
        if (mv != null) {
            mv.visitCode();
            for (InjectableFieldInfo fieldInfo : injectionInfo.getInjectableFields()) {
                if (fieldInfo.isStatic()) {
                    mv.visitMethodInsn(Opcodes.INVOKESTATIC, className, fieldInfo.getFactoryMethodName(),
                            fieldInfo.getFactoryMethodDesc());
                    mv.visitFieldInsn(Opcodes.PUTSTATIC, className, fieldInfo.getFieldName(),
                            fieldInfo.getFieldDesc());
                }
            }
            mv.visitInsn(Opcodes.RETURN);
            mv.visitMaxs(1, 0);
            mv.visitEnd();
        }
    }
    super.visitEnd();
}

From source file:com.googlecode.dex2jar.tools.DecryptStringCmd.java

License:Apache License

@Override
protected void doCommandLine() throws Exception {
    if (remainingArgs.length != 1) {
        usage();/*w  ww  .j  av  a2 s  .  c om*/
        return;
    }

    final Path jar = new File(remainingArgs[0]).toPath();
    if (!Files.exists(jar)) {
        System.err.println(jar + " is not exists");
        return;
    }
    if (methodName == null || methodOwner == null) {
        System.err.println("Please set --decrypt-method-owner and --decrypt-method-name");
        return;
    }

    if (output == null) {
        if (Files.isDirectory(jar)) {
            output = new File(jar.getFileName() + "-decrypted.jar").toPath();
        } else {
            output = new File(getBaseName(jar.getFileName().toString()) + "-decrypted.jar").toPath();
        }
    }

    if (Files.exists(output) && !forceOverwrite) {
        System.err.println(output + " exists, use --force to overwrite");
        return;
    }

    System.err.println(jar + " -> " + output);

    List<String> list = new ArrayList<String>();
    if (classpath != null) {
        list.addAll(Arrays.asList(classpath.split(";|:")));
    }
    list.add(jar.toAbsolutePath().toString());
    URL[] urls = new URL[list.size()];
    for (int i = 0; i < list.size(); i++) {
        urls[i] = new File(list.get(i)).toURI().toURL();
    }
    final Method jmethod;
    try {
        URLClassLoader cl = new URLClassLoader(urls);
        jmethod = cl.loadClass(methodOwner).getMethod(methodName, String.class);
        jmethod.setAccessible(true);
    } catch (Exception ex) {
        System.err.println("can't load method: String " + methodOwner + "." + methodName + "(String), message:"
                + ex.getMessage());
        return;
    }
    final String methodOwnerInternalType = this.methodOwner.replace('.', '/');
    try (FileSystem outputFileSystem = createZip(output)) {
        final Path outputBase = outputFileSystem.getPath("/");
        walkJarOrDir(jar, new FileVisitorX() {
            @Override
            public void visitFile(Path file, Path relative) throws IOException {
                if (file.getFileName().toString().endsWith(".class")) {

                    ClassReader cr = new ClassReader(Files.readAllBytes(file));
                    ClassNode cn = new ClassNode();
                    cr.accept(cn, 0);

                    for (Object m0 : cn.methods) {
                        MethodNode m = (MethodNode) m0;
                        if (m.instructions == null) {
                            continue;
                        }
                        AbstractInsnNode p = m.instructions.getFirst();
                        while (p != null) {
                            if (p.getOpcode() == Opcodes.LDC) {
                                LdcInsnNode ldc = (LdcInsnNode) p;
                                if (ldc.cst instanceof String) {
                                    String v = (String) ldc.cst;
                                    AbstractInsnNode q = p.getNext();
                                    if (q.getOpcode() == Opcodes.INVOKESTATIC) {
                                        MethodInsnNode mn = (MethodInsnNode) q;
                                        if (mn.name.equals(methodName)
                                                && mn.desc.equals("(Ljava/lang/String;)Ljava/lang/String;")
                                                && mn.owner.equals(methodOwnerInternalType)) {
                                            try {
                                                Object newValue = jmethod.invoke(null, v);
                                                ldc.cst = newValue;
                                            } catch (Exception e) {
                                                // ignore
                                            }
                                            m.instructions.remove(q);
                                        }
                                    }
                                }
                            }
                            p = p.getNext();
                        }
                    }

                    ClassWriter cw = new ClassWriter(0);
                    cn.accept(cw);
                    Files.write(outputBase.resolve(relative), cw.toByteArray());
                } else {
                    Files.copy(file, outputBase.resolve(relative));
                }
            }
        });
    }
}

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.  j a v  a2 s .co  m*/
    // > 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  w w  . ja v  a2  s .c  o 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"));
}