Example usage for org.objectweb.asm ClassVisitor ClassVisitor

List of usage examples for org.objectweb.asm ClassVisitor ClassVisitor

Introduction

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

Prototype

public ClassVisitor(final int api) 

Source Link

Document

Constructs a new ClassVisitor .

Usage

From source file:com.google.devtools.build.android.desugar.DefaultMethodClassFixerTest.java

License:Open Source License

private void checkClinitForDefaultInterfaceMethodWithStaticInitializerTestInterfaceSetOneC(
        byte[] classContent) {
    ClassReader reader = new ClassReader(classContent);
    reader.accept(new ClassVisitor(Opcodes.ASM5) {

        class ClinitMethod extends MethodNode {

            public ClinitMethod(int access, String name, String desc, String signature, String[] exceptions) {
                super(Opcodes.ASM5, access, name, desc, signature, exceptions);
            }//w  w  w .  j a  va  2 s . com
        }

        private ClinitMethod clinit;

        @Override
        public MethodVisitor visitMethod(int access, String name, String desc, String signature,
                String[] exceptions) {
            if ("<clinit>".equals(name)) {
                assertThat(clinit).isNull();
                clinit = new ClinitMethod(access, name, desc, signature, exceptions);
                return clinit;
            }
            return super.visitMethod(access, name, desc, signature, exceptions);
        }

        @Override
        public void visitEnd() {
            assertThat(clinit).isNotNull();
            assertThat(clinit.instructions.size()).isEqualTo(3);
            AbstractInsnNode instruction = clinit.instructions.getFirst();
            {
                assertThat(instruction).isInstanceOf(MethodInsnNode.class);
                MethodInsnNode field = (MethodInsnNode) instruction;
                assertThat(field.owner).isEqualTo("com/google/devtools/build/android/desugar/testdata/java8/"
                        + "DefaultInterfaceMethodWithStaticInitializer" + "$TestInterfaceSetOne$I1$$CC");
                assertThat(field.name)
                        .isEqualTo(InterfaceDesugaring.COMPANION_METHOD_TO_TRIGGER_INTERFACE_CLINIT_NAME);
                assertThat(field.desc)
                        .isEqualTo(InterfaceDesugaring.COMPANION_METHOD_TO_TRIGGER_INTERFACE_CLINIT_DESC);
            }
            {
                instruction = instruction.getNext();
                assertThat(instruction).isInstanceOf(MethodInsnNode.class);
                MethodInsnNode field = (MethodInsnNode) instruction;
                assertThat(field.owner).isEqualTo("com/google/devtools/build/android/desugar/testdata/java8/"
                        + "DefaultInterfaceMethodWithStaticInitializer" + "$TestInterfaceSetOne$I2$$CC");
                assertThat(field.name)
                        .isEqualTo(InterfaceDesugaring.COMPANION_METHOD_TO_TRIGGER_INTERFACE_CLINIT_NAME);
                assertThat(field.desc)
                        .isEqualTo(InterfaceDesugaring.COMPANION_METHOD_TO_TRIGGER_INTERFACE_CLINIT_DESC);
            }
            {
                instruction = instruction.getNext();
                assertThat(instruction).isInstanceOf(InsnNode.class);
                assertThat(instruction.getOpcode()).isEqualTo(Opcodes.RETURN);
            }
        }
    }, 0);
}

From source file:com.google.devtools.build.android.desugar.DesugarLongCompareTest.java

License:Open Source License

@Test
public void testClassCallingLongCompareHasNoReferenceToLong_compare() {
    try {/*from  w ww  . j  av  a  2 s  .c om*/
        ClassReader reader = new ClassReader(ClassCallingLongCompare.class.getName());

        AtomicInteger counter = new AtomicInteger(0);

        reader.accept(new ClassVisitor(Opcodes.ASM5) {
            @Override
            public MethodVisitor visitMethod(int access, String name, String desc, String signature,
                    String[] exceptions) {
                return new MethodVisitor(api) {
                    @Override
                    public void visitMethodInsn(int opcode, String owner, String name, String desc,
                            boolean itf) {
                        if (opcode == INVOKESTATIC && owner.equals("java/lang/Long") && name.equals("compare")
                                && desc.equals("(JJ)I")) {
                            counter.incrementAndGet();
                        }
                    }
                };
            }
        }, 0);
        assertThat(counter.get()).isEqualTo(0);
    } catch (IOException e) {
        fail();
    }
}

From source file:com.google.devtools.build.android.desugar.DesugarObjectsRequireNonNullTest.java

License:Open Source License

@Test
public void testClassCallingRequireNonNullHasNoReferenceToRequiresNonNull() {
    try {//from  w ww.ja v  a  2  s.co m
        ClassReader reader = new ClassReader(ClassCallingRequireNonNull.class.getName());

        AtomicInteger counterForSingleArgument = new AtomicInteger(0);
        AtomicInteger counterForString = new AtomicInteger(0);
        AtomicInteger counterForSupplier = new AtomicInteger(0);

        reader.accept(new ClassVisitor(Opcodes.ASM5) {
            @Override
            public MethodVisitor visitMethod(int access, String name, String desc, String signature,
                    String[] exceptions) {
                return new MethodVisitor(api) {
                    @Override
                    public void visitMethodInsn(int opcode, String owner, String name, String desc,
                            boolean itf) {
                        if (opcode == INVOKESTATIC && owner.equals("java/util/Objects")
                                && name.equals("requireNonNull")) {
                            switch (desc) {
                            case "(Ljava/lang/Object;)Ljava/lang/Object;":
                                counterForSingleArgument.incrementAndGet();
                                break;
                            case "(Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/Object;":
                                counterForString.incrementAndGet();
                                break;
                            case "(Ljava/lang/Object;Ljava/util/function/Supplier;)Ljava/lang/Object;":
                                counterForSupplier.incrementAndGet();
                                break;
                            default:
                                fail("Unknown overloaded requireNonNull is found: " + desc);
                            }
                        }
                    }
                };
            }
        }, 0);
        assertThat(counterForSingleArgument.get()).isEqualTo(0);
        // we do not desugar requireNonNull(Object, String) or requireNonNull(Object, Supplier)
        assertThat(counterForString.get()).isEqualTo(1);
        assertThat(counterForSupplier.get()).isEqualTo(1);
    } catch (IOException e) {
        fail();
    }
}

From source file:com.google.devtools.build.android.desugar.TryWithResourcesRewriterTest.java

License:Open Source License

/** Check whether java.lang.AutoCloseable is used as arguments of any method. */
private static boolean hasAutoCloseable(byte[] classContent) {
    ClassReader reader = new ClassReader(classContent);
    final AtomicInteger counter = new AtomicInteger();
    ClassVisitor visitor = new ClassVisitor(Opcodes.ASM5) {
        @Override/*from ww w  .ja  va  2  s.  c  o  m*/
        public MethodVisitor visitMethod(int access, String name, String desc, String signature,
                String[] exceptions) {
            for (Type argumentType : Type.getArgumentTypes(desc)) {
                if ("Ljava/lang/AutoCloseable;".equals(argumentType.getDescriptor())) {
                    counter.incrementAndGet();
                }
            }
            return null;
        }
    };
    reader.accept(visitor, 0);
    return counter.get() > 0;
}

From source file:com.google.devtools.j2objc.gen.TypeGenerator.java

License:Apache License

private boolean areParametersNonnullByDefault() {
    if (BindingUtil.hasAnnotation(typeBinding, ParametersAreNonnullByDefault.class)) {
        return true;
    }/*from   w w w.j a va 2  s. c om*/
    IPackageBinding pkg = typeBinding.getPackage();
    try {
        // See if a package-info source file has a ParametersAreNonnullByDefault annotation.
        InputFile file = FileUtil.findOnSourcePath(pkg.getName() + ".package-info");
        if (file != null) {
            String pkgInfo = FileUtil.readFile(file);
            if (pkgInfo.indexOf("@ParametersAreNonnullByDefault") >= 0) {
                return true;
            }
            if (pkgInfo.indexOf("@javax.annotation.ParametersAreNonnullByDefault") >= 0) {
                return true;
            }
        }

        // See if the package-info class file has it.
        final boolean[] result = new boolean[1];
        file = FileUtil.findOnClassPath(pkg.getName() + ".package-info");
        if (file != null) {
            ClassReader classReader = new ClassReader(file.getInputStream());
            classReader.accept(new ClassVisitor(Opcodes.ASM5) {
                @Override
                public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
                    if (desc.equals("Ljavax/annotation/ParametersAreNonnullByDefault;")) {
                        result[0] = true;
                    }
                    return null;
                }
            }, 0);
            return result[0];
        }
    } catch (IOException e) {
        // fall-through
    }
    return false;
}

From source file:com.google.devtools.j2objc.util.PackageInfoLookup.java

License:Apache License

private PackageData parseDataFromClassFile(InputFile file) throws IOException {
    PackageDataBuilder builder = new PackageDataBuilder();
    ClassReader classReader = new ClassReader(file.getInputStream());
    classReader.accept(new ClassVisitor(Opcodes.ASM5) {
        @Override/*from w  w w  . j a  v a2s  .co  m*/
        public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
            if (desc.equals("Lcom/google/j2objc/annotations/ObjectiveCName;")) {
                return new AnnotationVisitor(Opcodes.ASM5) {
                    @Override
                    public void visit(String name, Object value) {
                        if (name.equals("value")) {
                            builder.setObjectiveCName(value.toString());
                        }
                    }
                };
            } else if (desc.equals("Ljavax/annotation/ParametersAreNonnullByDefault;")) {
                builder.setParametersAreNonnullByDefault();
            } else if (desc.equals("Lcom/google/j2objc/annotations/ReflectionSupport;")) {
                return new AnnotationVisitor(Opcodes.ASM5) {
                    @Override
                    public void visitEnum(String name, String desc, String value) {
                        if (desc.equals("Lcom/google/j2objc/annotations/ReflectionSupport$Level;")
                                && name.equals("value")) {
                            builder.setReflectionSupportLevel(ReflectionSupport.Level.valueOf(value));
                        }
                    }
                };
            }
            return null;
        }
    }, 0);
    return builder.build();
}

From source file:com.google.devtools.j2objc.util.PackagePrefixes.java

License:Apache License

/**
 * Check if there is a package-info class with a prefix annotation.
 *///from ww  w  .ja  v a  2  s. c om
private String getPrefixFromPackageInfoClass(String packageName) {
    final String[] result = new String[1];
    try {
        InputFile file = FileUtil.findOnClassPath(packageName + ".package-info");
        if (file != null) {
            ClassReader classReader = new ClassReader(file.getInputStream());
            classReader.accept(new ClassVisitor(Opcodes.ASM5) {
                @Override
                public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
                    if (!desc.equals("Lcom/google/j2objc/annotations/ObjectiveCName;")) {
                        return null;
                    }
                    return new AnnotationVisitor(Opcodes.ASM5) {
                        @Override
                        public void visit(String name, Object value) {
                            if (name.equals("value")) {
                                result[0] = value.toString();
                            }
                        }
                    };
                }
            }, 0);
        }
    } catch (IOException e) {
        // Continue, as there's no package-info to check.
    }
    return result[0];
}

From source file:com.intellij.AbstractNotNullInstrumenterTask.java

License:Apache License

private static int getClassFileVersion(@NotNull final ClassReader reader) {
    final int[] classFileVersion = new int[1];
    reader.accept(new ClassVisitor(Opcodes.ASM5) {
        public void visit(int version, int access, String name, String signature, String superName,
                String[] interfaces) {
            classFileVersion[0] = version;
        }/*w  w w .ja  va 2s. c o  m*/
    }, 0);
    return classFileVersion[0];
}

From source file:com.intellij.NotNullInstrumenter.java

License:Apache License

private static int getClassFileVersion(@NotNull final ClassReader reader) {
    final int[] classFileVersion = new int[1];
    reader.accept(new ClassVisitor(AsmUtils.ASM_OPCODES_VERSION) {
        public void visit(final int version, final int access, final String name, final String signature,
                final String superName, final String[] interfaces) {
            classFileVersion[0] = version;
        }/*from w w  w .j ava 2 s  .  c o m*/
    }, NO_FLAGS);
    return classFileVersion[0];
}

From source file:com.netflix.hystrix.contrib.javanica.utils.MethodProvider.java

License:Apache License

/**
 * Finds generic method for the given bridge method.
 *
 * @param bridgeMethod the bridge method
 * @param aClass       the type where the bridge method is declared
 * @return generic method//from   ww w  . j  a v  a2s. c  om
 * @throws IOException
 * @throws NoSuchMethodException
 * @throws ClassNotFoundException
 */
public Method unbride(final Method bridgeMethod, Class<?> aClass)
        throws IOException, NoSuchMethodException, ClassNotFoundException {
    if (bridgeMethod.isBridge() && bridgeMethod.isSynthetic()) {
        if (cache.containsKey(bridgeMethod)) {
            return cache.get(bridgeMethod);
        }

        ClassReader classReader = new ClassReader(aClass.getName());
        final MethodSignature methodSignature = new MethodSignature();
        classReader.accept(new ClassVisitor(ASM5) {
            @Override
            public MethodVisitor visitMethod(int access, String name, String desc, String signature,
                    String[] exceptions) {
                boolean bridge = (access & ACC_BRIDGE) != 0 && (access & ACC_SYNTHETIC) != 0;
                if (bridge && bridgeMethod.getName().equals(name)
                        && getParameterCount(desc) == bridgeMethod.getParameterTypes().length) {
                    return new MethodFinder(methodSignature);
                }
                return super.visitMethod(access, name, desc, signature, exceptions);
            }
        }, 0);
        Method method = aClass.getDeclaredMethod(methodSignature.name, methodSignature.getParameterTypes());
        cache.put(bridgeMethod, method);
        return method;

    } else {
        return bridgeMethod;
    }
}