Search in sources :

Example 6 with ClassAnnotation

use of org.apache.felix.scrplugin.annotations.ClassAnnotation in project felix by apache.

the class ClassScanner method parseAnnotation.

/**
 * Parse annotation and create a description.
 */
private void parseAnnotation(final List<ScannedAnnotation> descriptions, final AnnotationNode annotation, final Object annotatedObject) {
    // desc has the format 'L' + className.replace('.', '/') + ';'
    final String name = annotation.desc.substring(1, annotation.desc.length() - 1).replace('/', '.');
    Map<String, Object> values = null;
    if (annotation.values != null) {
        values = new HashMap<String, Object>();
        final Iterator<?> i = annotation.values.iterator();
        while (i.hasNext()) {
            final Object vName = i.next();
            Object value = i.next();
            // convert type to class name string
            if (value instanceof Type) {
                value = ((Type) value).getClassName();
            } else if (value instanceof List<?>) {
                final List<?> objects = (List<?>) value;
                if (objects.size() > 0) {
                    if (objects.get(0) instanceof Type) {
                        final String[] classNames = new String[objects.size()];
                        int index = 0;
                        for (final Object v : objects) {
                            classNames[index] = ((Type) v).getClassName();
                            index++;
                        }
                        value = classNames;
                    } else if (objects.get(0) instanceof AnnotationNode) {
                        final List<ScannedAnnotation> innerDesc = new ArrayList<ScannedAnnotation>();
                        for (final Object v : objects) {
                            parseAnnotation(innerDesc, (AnnotationNode) v, annotatedObject);
                        }
                        if (annotatedObject instanceof Method) {
                            value = innerDesc.toArray(new MethodAnnotation[innerDesc.size()]);
                        } else if (annotatedObject instanceof Field) {
                            value = innerDesc.toArray(new FieldAnnotation[innerDesc.size()]);
                        } else {
                            value = innerDesc.toArray(new ClassAnnotation[innerDesc.size()]);
                        }
                    } else {
                        value = convertToArray(objects, objects.get(0).getClass());
                    }
                } else {
                    value = null;
                }
            }
            values.put(vName.toString(), value);
        }
    }
    final ScannedAnnotation a;
    if (annotatedObject instanceof Method) {
        a = new MethodAnnotation(name, values, (Method) annotatedObject);
        ((Method) annotatedObject).setAccessible(true);
    } else if (annotatedObject instanceof Field) {
        a = new FieldAnnotation(name, values, (Field) annotatedObject);
        ((Field) annotatedObject).setAccessible(true);
    } else {
        a = new ClassAnnotation(name, values);
    }
    descriptions.add(a);
}
Also used : ArrayList(java.util.ArrayList) ClassAnnotation(org.apache.felix.scrplugin.annotations.ClassAnnotation) Method(java.lang.reflect.Method) Field(java.lang.reflect.Field) Type(org.objectweb.asm.Type) AnnotationNode(org.objectweb.asm.tree.AnnotationNode) MethodAnnotation(org.apache.felix.scrplugin.annotations.MethodAnnotation) FieldAnnotation(org.apache.felix.scrplugin.annotations.FieldAnnotation) ScannedAnnotation(org.apache.felix.scrplugin.annotations.ScannedAnnotation) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

ClassAnnotation (org.apache.felix.scrplugin.annotations.ClassAnnotation)6 ArrayList (java.util.ArrayList)3 MethodAnnotation (org.apache.felix.scrplugin.annotations.MethodAnnotation)3 FieldAnnotation (org.apache.felix.scrplugin.annotations.FieldAnnotation)2 ComponentDescription (org.apache.felix.scrplugin.description.ComponentDescription)2 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 List (java.util.List)1 Activate (org.apache.felix.scr.annotations.Activate)1 AutoDetect (org.apache.felix.scr.annotations.AutoDetect)1 Component (org.apache.felix.scr.annotations.Component)1 Modified (org.apache.felix.scr.annotations.Modified)1 Properties (org.apache.felix.scr.annotations.Properties)1 Property (org.apache.felix.scr.annotations.Property)1 Reference (org.apache.felix.scr.annotations.Reference)1 References (org.apache.felix.scr.annotations.References)1 Service (org.apache.felix.scr.annotations.Service)1 Services (org.apache.felix.scr.annotations.Services)1 SlingFilter (org.apache.felix.scr.annotations.sling.SlingFilter)1 SlingServlet (org.apache.felix.scr.annotations.sling.SlingServlet)1