Example usage for org.objectweb.asm Opcodes ASM5

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

Introduction

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

Prototype

int ASM5

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

Click Source Link

Usage

From source file:co.paralleluniverse.common.util.ExtendedStackTrace.java

License:Open Source License

protected /*Executable*/ Member getMethod(final ExtendedStackTraceElement este) {
    if (este.getDeclaringClass() == null)
        return null;
    Member[] ms = getMethods(este.getDeclaringClass());
    Member method = null;//  w  w  w.ja  v  a 2  s .  c o  m

    for (Member m : ms) {
        if (este.getMethodName().equals(m.getName())) {
            if (method == null)
                method = m;
            else {
                method = null; // more than one match
                break;
            }
        }
    }
    if (method == null && este.getLineNumber() >= 0) {
        try {
            final AtomicReference<String> descriptor = new AtomicReference<>();
            ASMUtil.accept(este.getDeclaringClass(), ClassReader.SKIP_FRAMES, new ClassVisitor(Opcodes.ASM5) {
                @Override
                public MethodVisitor visitMethod(int access, String name, final String desc, String signature,
                        String[] exceptions) {
                    MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
                    if (descriptor.get() == null && este.getMethodName().equals(name)) {
                        mv = new MethodVisitor(api, mv) {
                            int minLine = Integer.MAX_VALUE, maxLine = Integer.MIN_VALUE;

                            @Override
                            public void visitLineNumber(int line, Label start) {
                                if (line < minLine)
                                    minLine = line;
                                if (line > maxLine)
                                    maxLine = line;
                            }

                            @Override
                            public void visitEnd() {
                                if (minLine <= este.getLineNumber() && maxLine >= este.getLineNumber())
                                    descriptor.set(desc);
                                super.visitEnd();
                            }
                        };
                    }
                    return mv;
                }
            });

            if (descriptor.get() != null) {
                final String desc = descriptor.get();
                for (Member m : ms) {
                    if (este.getMethodName().equals(getName(m)) && desc.equals(getDescriptor(m))) {
                        method = m;
                        break;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    return method;
}

From source file:co.paralleluniverse.vtime.VirtualTimeClassTransformer.java

License:Open Source License

private ClassVisitor createVisitor(ClassVisitor next) {
    int api;//from   w w  w.ja  v  a2s.com
    if (System.getProperty("java.version").startsWith("1.8")) {
        api = Opcodes.ASM5;
    } else if (System.getProperty("java.version").startsWith("10")) {
        api = Opcodes.ASM6;
    } else {
        api = Opcodes.ASM7;
    }
    return new ClassVisitor(api, next) {
        @Override
        public MethodVisitor visitMethod(int access, String name, String desc, String signature,
                String[] exceptions) {
            return new MethodVisitor(api, super.visitMethod(access, name, desc, signature, exceptions)) {
                @Override
                public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) {
                    if (!captureTimeCall(owner, name, desc)) {
                        super.visitMethodInsn(opcode, owner, name, desc, itf);
                    }
                }

                private boolean captureTimeCall(String owner, String name, String desc) {
                    switch (owner) {
                    case "java/lang/Object":
                        if ("wait".equals(name)) {
                            return callClockMethod("Object_wait", instanceToStatic(owner, desc));
                        }
                        break;
                    case "java/lang/System":
                        switch (name) {
                        case "nanoTime":
                            return callClockMethod("System_nanoTime", desc);
                        case "currentTimeMillis":
                            return callClockMethod("System_currentTimeMillis", desc);
                        }
                        break;
                    case "java/lang/Thread":
                        if ("sleep".equals(name)) {
                            return callClockMethod("Thread_sleep", desc);
                        }
                        break;
                    case "sun/misc/Unsafe":
                        if ("park".equals(name)) {
                            return callClockMethod("Unsafe_park", instanceToStatic(owner, desc));
                        }
                        break;
                    case "java/lang/management/RuntimeMXBean":
                        if ("getStartTime".equals(name)) {
                            return callClockMethod("RuntimeMXBean_getStartTime", instanceToStatic(owner, desc));
                        }
                        break;
                    }
                    return false;
                }

                private boolean callClockMethod(String name, String desc) {
                    if (includedMethods == null || includedMethods.contains(name)) {
                        super.visitMethodInsn(Opcodes.INVOKESTATIC, CLOCK, name, desc, false);
                        return true;
                    } else {
                        return false;
                    }
                }

                private String instanceToStatic(String owner, String desc) {
                    return "(L" + owner + ";" + desc.substring(1);
                }
            };
        }
    };
}

From source file:com.android.build.gradle.integration.application.ExternalBuildPluginTest.java

License:Apache License

private static byte[] hotswapChange(byte[] inputClass) {
    ClassReader cr = new ClassReader(inputClass);
    ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_FRAMES);
    ClassVisitor cv = new ClassVisitor(Opcodes.ASM5, cw) {
        @Override//from   www .  j a va  2  s  .  com
        public MethodVisitor visitMethod(int access, String name, String desc, String signature,
                String[] exceptions) {
            MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
            return new MethodVisitor(Opcodes.ASM5, mv) {
                @Override
                public void visitCode() {
                    // add a useless logging to the method.
                    mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
                    mv.visitLdcInsn("test changed !");
                    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println",
                            "(Ljava/lang/String;)V", false);
                    super.visitCode();
                }
            };
        }
    };
    cr.accept(cv, ClassReader.EXPAND_FRAMES);
    return cw.toByteArray();
}

From source file:com.android.build.gradle.integration.common.utils.ZipHelper.java

License:Apache License

public static FieldNode checkClassFile(@NonNull File jarFile, @NonNull String entryName,
        @NonNull String fieldName) throws IOException {

    try (ZipFile zipFile = new ZipFile(jarFile)) {
        ZipEntry entry = zipFile.getEntry(entryName);
        assertThat(entry).named(entryName + " entry").isNotNull();
        ClassReader classReader = new ClassReader(zipFile.getInputStream(entry));
        ClassNode mainTestClassNode = new ClassNode(Opcodes.ASM5);
        classReader.accept(mainTestClassNode, 0);

        FieldNode fieldNode = null;//ww w  . j av  a2 s.  c  o m
        for (Object o : mainTestClassNode.fields) {
            FieldNode fn = (FieldNode) o;
            if (fn.name.equals(fieldName)) {
                fieldNode = fn;
                break;
            }
        }

        assert fieldNode != null;
        return fieldNode;
    }
}

From source file:com.android.build.gradle.internal.incremental.IncrementalSupportVisitorTest.java

License:Apache License

@Nullable
public static String findOwnerForMethodDispatch(@NonNull MethodNode methodNode,
        @NonNull String dispatchedMethod, @Nullable String dispatchedDesc) {
    AtomicReference<String> ownerRef = new AtomicReference<>(null);
    MethodVisitor methodVisitor = new MethodVisitor(Opcodes.ASM5) {
        @Override// w ww.j  av  a2 s  .c o m
        public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) {
            super.visitMethodInsn(opcode, owner, name, desc, itf);
            if (name.equals(dispatchedMethod) && (dispatchedDesc == null || dispatchedDesc.equals(desc))) {
                ownerRef.set(owner);
            }
        }
    };
    methodNode.accept(methodVisitor);
    return ownerRef.get();
}

From source file:com.android.build.gradle.internal.incremental.IncrementalVisitor.java

License:Apache License

public IncrementalVisitor(@NonNull ClassNode classNode, @NonNull List<ClassNode> parentNodes,
        @NonNull ClassVisitor classVisitor, @NonNull ILogger logger) {
    super(Opcodes.ASM5, classVisitor);
    this.classNode = classNode;
    this.parentNodes = parentNodes;
    this.logger = logger;
    this.logger.verbose("%s: Visiting %s", getClass().getSimpleName(), classNode.name);
}

From source file:com.android.build.gradle.internal2.incremental.IncrementalVisitor.java

License:Apache License

public IncrementalVisitor(@NonNull ClassNode classNode, @NonNull List<ClassNode> parentNodes,
        @NonNull ClassVisitor classVisitor) {
    super(Opcodes.ASM5, classVisitor);
    this.classNode = classNode;
    this.parentNodes = parentNodes;
    LOG.info("%s: Visiting %s", getClass().getSimpleName(), classNode.name);
}

From source file:com.android.build.gradle.shrinker.AbstractShrinkerTest.java

License:Apache License

@NonNull
private static ClassNode getClassNode(File classFile) throws IOException {
    ClassReader classReader = new ClassReader(Files.toByteArray(classFile));
    ClassNode classNode = new ClassNode(Opcodes.ASM5);
    classReader.accept(classNode, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
    return classNode;
}

From source file:com.android.build.gradle.shrinker.ClassStructureVisitor.java

License:Apache License

public ClassStructureVisitor(@NonNull ShrinkerGraph<T> graph, @Nullable File classFile,
        @Nullable ClassVisitor cv) {//  www. j av a2s.  c  om
    super(Opcodes.ASM5, cv);
    mClassFile = classFile;
    mGraph = graph;
}

From source file:com.android.build.gradle.shrinker.ClassStructureVisitor.java

License:Apache License

@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    final T method = mGraph.addMember(mClass, name, desc, access);

    MethodVisitor superVisitor = super.visitMethod(access, name, desc, signature, exceptions);
    return new MethodVisitor(Opcodes.ASM5, superVisitor) {
        @Override/*from  ww w  .ja va  2s.  c  o  m*/
        public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
            mGraph.addAnnotation(method, Type.getType(desc).getInternalName());
            return super.visitAnnotation(desc, visible);
        }
    };
}