use of org.apache.felix.scrplugin.annotations.FieldAnnotation 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.FieldAnnotation in project felix by apache.
the class SCRAnnotationProcessor method createProperties.
/**
* Create properties descriptions
*
* @throws SCRDescriptorException
* @throws SCRDescriptorFailureException
*/
private void createProperties(final List<? extends ScannedAnnotation> descs, final ClassDescription describedClass) throws SCRDescriptorFailureException, SCRDescriptorException {
for (final ScannedAnnotation ad : descs) {
final PropertyDescription prop = new PropertyDescription(ad);
// check for field annotation
final FieldAnnotation fieldAnnotation;
if (ad instanceof FieldAnnotation) {
fieldAnnotation = (FieldAnnotation) ad;
} else {
fieldAnnotation = null;
}
// Detect values from annotation
String type = null;
String[] values = null;
int index = 0;
while (type == null && index < PROPERTY_VALUE_PROCESSING.length) {
final String propType = PROPERTY_VALUE_PROCESSING[index];
final String propName = PROPERTY_VALUE_PROCESSING[index + 1];
final Object propValue = ad.getValue(propName);
if (propValue != null && propValue.getClass().isArray()) {
type = propType;
values = new String[Array.getLength(propValue)];
for (int i = 0; i < values.length; i++) {
values[i] = Array.get(propValue, i).toString();
}
}
index += 2;
}
String name = ad.getStringValue("name", null);
if (values != null) {
prop.setType(PropertyType.valueOf(type));
if (values.length == 1) {
prop.setValue(values[0]);
} else {
prop.setMultiValue(values);
}
if (name == null) {
final Object value = fieldAnnotation.getAnnotatedFieldValue();
if (value != null) {
name = value.toString();
}
}
} else if (fieldAnnotation != null) {
// Detect values from field
if (name != null) {
final Object value = fieldAnnotation.getAnnotatedFieldValue();
if (value != null) {
if (value.getClass().isArray()) {
final String[] newValues = new String[Array.getLength(value)];
for (int i = 0; i < newValues.length; i++) {
newValues[i] = Array.get(value, i).toString();
}
prop.setMultiValue(newValues);
prop.setType(PropertyType.from(fieldAnnotation.getAnnotatedField().getType().getComponentType()));
} else {
prop.setType(PropertyType.from(value.getClass()));
prop.setValue(value.toString());
}
}
} else {
if (Modifier.isStatic(fieldAnnotation.getAnnotatedField().getModifiers())) {
final Object value = fieldAnnotation.getAnnotatedFieldValue();
if (value != null) {
name = value.toString();
}
} else {
// non static, no name, no value (FELIX-4393)
name = fieldAnnotation.getAnnotatedField().getName();
final Object value = fieldAnnotation.getAnnotatedFieldValue();
if (value != null) {
if (value.getClass().isArray()) {
final String[] newValues = new String[Array.getLength(value)];
for (int i = 0; i < newValues.length; i++) {
newValues[i] = Array.get(value, i).toString();
}
prop.setMultiValue(newValues);
prop.setType(PropertyType.from(fieldAnnotation.getAnnotatedField().getType().getComponentType()));
} else {
prop.setType(PropertyType.from(value.getClass()));
prop.setValue(value.toString());
}
}
}
}
}
prop.setName(name);
prop.setLabel(ad.getStringValue("label", null));
prop.setDescription(ad.getStringValue("description", null));
// check type
if (prop.getType() == null) {
prop.setType(PropertyType.String);
}
// private
if (ad.getValue("propertyPrivate") != null) {
prop.setPrivate(ad.getBooleanValue("propertyPrivate", false));
}
// cardinality handling
final PropertyUnbounded pu = PropertyUnbounded.valueOf(ad.getEnumValue("unbounded", PropertyUnbounded.DEFAULT.name()));
prop.setUnbounded(pu);
if (pu == PropertyUnbounded.DEFAULT) {
prop.setCardinality(ad.getIntegerValue("cardinality", 0));
if (prop.getMultiValue() != null && prop.getCardinality() == 0) {
prop.setUnbounded(PropertyUnbounded.ARRAY);
}
} else {
prop.setCardinality(0);
}
if (prop.getValue() != null) {
if (prop.getUnbounded() == PropertyUnbounded.ARRAY || prop.getUnbounded() == PropertyUnbounded.VECTOR) {
prop.setMultiValue(new String[] { prop.getValue() });
} else if (prop.getCardinality() < -1 || prop.getCardinality() > 1) {
prop.setMultiValue(new String[] { prop.getValue() });
}
}
// options
final ScannedAnnotation[] options = (ScannedAnnotation[]) ad.getValue("options");
if (options != null) {
final List<String> propertyOptions = new ArrayList<String>();
for (final ScannedAnnotation po : options) {
propertyOptions.add(po.getStringValue("name", ""));
propertyOptions.add(po.getStringValue("value", ""));
}
prop.setOptions(propertyOptions.toArray(new String[propertyOptions.size()]));
}
describedClass.add(prop);
}
}
use of org.apache.felix.scrplugin.annotations.FieldAnnotation in project felix by apache.
the class SCRAnnotationProcessor method createReferences.
/**
* Create reference descriptions
*
* @param descs
* List of reference annotations.s
* @param describedClass
* The described class.
*/
private void createReferences(final List<? extends ScannedAnnotation> descs, final ClassDescription describedClass) {
for (final ScannedAnnotation ad : descs) {
final ReferenceDescription ref = new ReferenceDescription(ad);
// check for field annotation
final FieldAnnotation fieldAnnotation;
if (ad instanceof FieldAnnotation) {
fieldAnnotation = (FieldAnnotation) ad;
ref.setField(fieldAnnotation.getAnnotatedField());
} else {
fieldAnnotation = null;
}
ref.setName(ad.getStringValue("name", (fieldAnnotation != null ? fieldAnnotation.getAnnotatedField().getName() : null)));
String defaultInterfaceName = null;
if (fieldAnnotation != null) {
if (fieldAnnotation.getAnnotatedField().getType().isArray()) {
defaultInterfaceName = fieldAnnotation.getAnnotatedField().getType().getComponentType().getName();
} else {
defaultInterfaceName = fieldAnnotation.getAnnotatedField().getType().getName();
}
}
ref.setInterfaceName(ad.getStringValue("referenceInterface", defaultInterfaceName));
ref.setTarget(ad.getStringValue("target", null));
ref.setCardinality(ReferenceCardinality.valueOf(ad.getEnumValue("cardinality", ReferenceCardinality.MANDATORY_UNARY.name())));
ref.setPolicy(ReferencePolicy.valueOf(ad.getEnumValue("policy", ReferencePolicy.STATIC.name())));
ref.setPolicyOption(ReferencePolicyOption.valueOf(ad.getEnumValue("policyOption", ReferencePolicyOption.RELUCTANT.name())));
ref.setStrategy(ReferenceStrategy.valueOf(ad.getEnumValue("strategy", ReferenceStrategy.EVENT.name())));
ref.setBind(ad.getStringValue("bind", null));
ref.setUnbind(ad.getStringValue("unbind", null));
ref.setUpdated(ad.getStringValue("updated", null));
describedClass.add(ref);
}
}
use of org.apache.felix.scrplugin.annotations.FieldAnnotation 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