Search in sources :

Example 31 with AnnotationVisitor

use of org.objectweb.asm.AnnotationVisitor in project cdap by caskdata.

the class ArtifactInspector method isPlugin.

/**
   * Detects if a class is annotated with {@link Plugin} without loading the class.
   *
   * @param className name of the class
   * @param classLoader ClassLoader for loading the class file of the given class
   * @return true if the given class is annotated with {@link Plugin}
   */
private boolean isPlugin(String className, ClassLoader classLoader) {
    try (InputStream is = classLoader.getResourceAsStream(className.replace('.', '/') + ".class")) {
        if (is == null) {
            return false;
        }
        // Use ASM to inspect the class bytecode to see if it is annotated with @Plugin
        final boolean[] isPlugin = new boolean[1];
        ClassReader cr = new ClassReader(is);
        cr.accept(new ClassVisitor(Opcodes.ASM5) {

            @Override
            public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
                if (Plugin.class.getName().equals(Type.getType(desc).getClassName()) && visible) {
                    isPlugin[0] = true;
                }
                return super.visitAnnotation(desc, visible);
            }
        }, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
        return isPlugin[0];
    } catch (IOException e) {
        // If failed to open the class file, then it cannot be a plugin
        LOG.warn("Failed to open class file for {}", className, e);
        return false;
    }
}
Also used : InputStream(java.io.InputStream) AnnotationVisitor(org.objectweb.asm.AnnotationVisitor) ClassReader(org.objectweb.asm.ClassReader) ClassVisitor(org.objectweb.asm.ClassVisitor) IOException(java.io.IOException) Plugin(co.cask.cdap.api.annotation.Plugin)

Example 32 with AnnotationVisitor

use of org.objectweb.asm.AnnotationVisitor in project j2objc by google.

the class PackageInfoLookup method parseDataFromClassFile.

private PackageData parseDataFromClassFile(InputFile file) throws IOException {
    PackageDataBuilder builder = new PackageDataBuilder();
    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 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();
}
Also used : AnnotationVisitor(org.objectweb.asm.AnnotationVisitor) ClassReader(org.objectweb.asm.ClassReader) ClassVisitor(org.objectweb.asm.ClassVisitor)

Example 33 with AnnotationVisitor

use of org.objectweb.asm.AnnotationVisitor in project groovy-core by groovy.

the class AsmClassGenerator method visitParameterAnnotations.

private void visitParameterAnnotations(Parameter parameter, int paramNumber, MethodVisitor mv) {
    for (AnnotationNode an : parameter.getAnnotations()) {
        // skip built-in properties
        if (an.isBuiltIn())
            continue;
        if (an.hasSourceRetention())
            continue;
        final String annotationDescriptor = BytecodeHelper.getTypeDescription(an.getClassNode());
        AnnotationVisitor av = mv.visitParameterAnnotation(paramNumber, annotationDescriptor, an.hasRuntimeRetention());
        visitAnnotationAttributes(an, av);
        av.visitEnd();
    }
}
Also used : AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) AnnotationVisitor(org.objectweb.asm.AnnotationVisitor)

Example 34 with AnnotationVisitor

use of org.objectweb.asm.AnnotationVisitor in project groovy-core by groovy.

the class AsmClassGenerator method visitAnnotationArrayElement.

private void visitAnnotationArrayElement(Expression expr, int arrayElementType, AnnotationVisitor av) {
    switch(arrayElementType) {
        case 1:
            AnnotationNode atAttr = (AnnotationNode) ((AnnotationConstantExpression) expr).getValue();
            AnnotationVisitor av2 = av.visitAnnotation(null, BytecodeHelper.getTypeDescription(atAttr.getClassNode()));
            visitAnnotationAttributes(atAttr, av2);
            av2.visitEnd();
            break;
        case 2:
            av.visit(null, ((ConstantExpression) expr).getValue());
            break;
        case 3:
            av.visit(null, Type.getType(BytecodeHelper.getTypeDescription(expr.getType())));
            break;
        case 4:
            PropertyExpression propExpr = (PropertyExpression) expr;
            av.visitEnum(null, BytecodeHelper.getTypeDescription(propExpr.getObjectExpression().getType()), String.valueOf(((ConstantExpression) propExpr.getProperty()).getValue()));
            break;
    }
}
Also used : AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) AnnotationVisitor(org.objectweb.asm.AnnotationVisitor)

Example 35 with AnnotationVisitor

use of org.objectweb.asm.AnnotationVisitor in project groovy-core by groovy.

the class AsmClassGenerator method visitAnnotations.

private void visitAnnotations(AnnotatedNode targetNode, Object visitor) {
    for (AnnotationNode an : targetNode.getAnnotations()) {
        // skip built-in properties
        if (an.isBuiltIn())
            continue;
        if (an.hasSourceRetention())
            continue;
        AnnotationVisitor av = getAnnotationVisitor(targetNode, an, visitor);
        visitAnnotationAttributes(an, av);
        av.visitEnd();
    }
}
Also used : AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) AnnotationVisitor(org.objectweb.asm.AnnotationVisitor)

Aggregations

AnnotationVisitor (org.objectweb.asm.AnnotationVisitor)64 AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)12 ArrayList (java.util.ArrayList)9 Type (org.objectweb.asm.Type)9 MethodVisitor (org.objectweb.asm.MethodVisitor)7 LayoutlibDelegate (com.android.tools.layoutlib.annotations.LayoutlibDelegate)6 ClassNode (org.codehaus.groovy.ast.ClassNode)6 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)6 InterfaceHelperClassNode (org.codehaus.groovy.ast.InterfaceHelperClassNode)6 ClassVisitor (org.objectweb.asm.ClassVisitor)6 ClassReader (org.objectweb.asm.ClassReader)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 FieldVisitor (org.objectweb.asm.FieldVisitor)3 Label (org.objectweb.asm.Label)3 GroovyRuntimeException (groovy.lang.GroovyRuntimeException)2 InputStream (java.io.InputStream)2 List (java.util.List)2 GroovyBugError (org.codehaus.groovy.GroovyBugError)2 GenericsType (org.codehaus.groovy.ast.GenericsType)2