use of org.apache.felix.scrplugin.annotations.MethodAnnotation in project felix by apache.
the class SCRAnnotationProcessor method process.
/**
* @throws SCRDescriptorException
* @throws SCRDescriptorFailureException
* @see org.apache.felix.scrplugin.annotations.AnnotationProcessor#process(org.apache.felix.scrplugin.annotations.ScannedClass, org.apache.felix.scrplugin.description.ClassDescription)
*/
@Override
public void process(final ScannedClass scannedClass, final ClassDescription describedClass) throws SCRDescriptorFailureException, SCRDescriptorException {
final List<ClassAnnotation> componentTags = scannedClass.getClassAnnotations(Component.class.getName());
scannedClass.processed(componentTags);
for (final ClassAnnotation cad : componentTags) {
describedClass.add(createComponent(cad, scannedClass));
}
// search for the component descriptions and use the first one
final List<ComponentDescription> componentDescs = describedClass.getDescriptions(ComponentDescription.class);
ComponentDescription found = null;
if (!componentDescs.isEmpty()) {
found = componentDescs.get(0);
}
if (found != null) {
final ComponentDescription cd = found;
// search for methods
final List<MethodAnnotation> methodTags = scannedClass.getMethodAnnotations(null);
for (final MethodAnnotation m : methodTags) {
if (m.getName().equals(Activate.class.getName())) {
cd.setActivate(m.getAnnotatedMethod().getName());
scannedClass.processed(m);
} else if (m.getName().equals(Deactivate.class.getName())) {
cd.setDeactivate(m.getAnnotatedMethod().getName());
scannedClass.processed(m);
} else if (m.getName().equals(Modified.class.getName())) {
cd.setModified(m.getAnnotatedMethod().getName());
scannedClass.processed(m);
}
}
}
// service tags
final List<ClassAnnotation> allServiceTags = new ArrayList<ClassAnnotation>();
final List<ClassAnnotation> serviceTags = scannedClass.getClassAnnotations(Service.class.getName());
if (serviceTags.size() > 0) {
scannedClass.processed(serviceTags);
allServiceTags.addAll(serviceTags);
}
// services tags - class level
final List<ClassAnnotation> servicesTags = scannedClass.getClassAnnotations(Services.class.getName());
if (servicesTags.size() > 0) {
scannedClass.processed(servicesTags);
for (final ClassAnnotation cad : servicesTags) {
final ClassAnnotation[] values = (ClassAnnotation[]) cad.getValue("value");
if (values != null) {
allServiceTags.addAll(Arrays.asList(values));
}
}
}
if (allServiceTags.size() > 0) {
describedClass.add(createService(allServiceTags, scannedClass));
}
// references - class level
final List<ClassAnnotation> referencesClassTags = scannedClass.getClassAnnotations(References.class.getName());
scannedClass.processed(referencesClassTags);
for (final ClassAnnotation cad : referencesClassTags) {
final ClassAnnotation[] values = (ClassAnnotation[]) cad.getValue("value");
if (values != null) {
createReferences(Arrays.asList(values), describedClass);
}
}
// reference - class level
final List<ClassAnnotation> refClassTags = scannedClass.getClassAnnotations(Reference.class.getName());
scannedClass.processed(refClassTags);
createReferences(refClassTags, describedClass);
// reference - field level
final List<FieldAnnotation> refFieldTags = scannedClass.getFieldAnnotations(Reference.class.getName());
scannedClass.processed(refFieldTags);
createReferences(refFieldTags, describedClass);
// properties - class level
final List<ClassAnnotation> propsClassTags = scannedClass.getClassAnnotations(Properties.class.getName());
scannedClass.processed(propsClassTags);
for (final ClassAnnotation cad : propsClassTags) {
final ClassAnnotation[] values = (ClassAnnotation[]) cad.getValue("value");
if (values != null) {
createProperties(Arrays.asList(values), describedClass);
}
}
// property - class level
final List<ClassAnnotation> propClassTags = scannedClass.getClassAnnotations(Property.class.getName());
scannedClass.processed(propClassTags);
createProperties(propClassTags, describedClass);
// property - field level
final List<FieldAnnotation> propFieldTags = scannedClass.getFieldAnnotations(Property.class.getName());
scannedClass.processed(propFieldTags);
createProperties(propFieldTags, describedClass);
}
use of org.apache.felix.scrplugin.annotations.MethodAnnotation in project felix by apache.
the class DSAnnotationProcessor method process.
/**
* @see org.apache.felix.scrplugin.annotations.AnnotationProcessor#process(org.apache.felix.scrplugin.annotations.ScannedClass, org.apache.felix.scrplugin.description.ClassDescription)
*/
@Override
public void process(final ScannedClass scannedClass, final ClassDescription describedClass) throws SCRDescriptorFailureException, SCRDescriptorException {
final List<ClassAnnotation> componentTags = scannedClass.getClassAnnotations(Component.class.getName());
scannedClass.processed(componentTags);
for (final ClassAnnotation cad : componentTags) {
this.createComponent(cad, describedClass, scannedClass);
}
// search for the component descriptions and use the first one
final List<ComponentDescription> componentDescs = describedClass.getDescriptions(ComponentDescription.class);
ComponentDescription found = null;
if (!componentDescs.isEmpty()) {
found = componentDescs.get(0);
}
if (found != null) {
final ComponentDescription cd = found;
// search for methods
final List<MethodAnnotation> methodTags = scannedClass.getMethodAnnotations(null);
for (final MethodAnnotation m : methodTags) {
if (m.getName().equals(Activate.class.getName())) {
cd.setActivate(m.getAnnotatedMethod().getName());
scannedClass.processed(m);
} else if (m.getName().equals(Deactivate.class.getName())) {
cd.setDeactivate(m.getAnnotatedMethod().getName());
scannedClass.processed(m);
} else if (m.getName().equals(Modified.class.getName())) {
cd.setModified(m.getAnnotatedMethod().getName());
scannedClass.processed(m);
} else if (m.getName().equals(Reference.class.getName())) {
this.processReference(describedClass, m);
scannedClass.processed(m);
}
}
}
}
use of org.apache.felix.scrplugin.annotations.MethodAnnotation 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);
}
Aggregations