Search in sources :

Example 1 with PropertyUnbounded

use of org.apache.felix.scrplugin.description.PropertyUnbounded 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);
    }
}
Also used : PropertyDescription(org.apache.felix.scrplugin.description.PropertyDescription) FieldAnnotation(org.apache.felix.scrplugin.annotations.FieldAnnotation) ArrayList(java.util.ArrayList) ScannedAnnotation(org.apache.felix.scrplugin.annotations.ScannedAnnotation) PropertyUnbounded(org.apache.felix.scrplugin.description.PropertyUnbounded)

Aggregations

ArrayList (java.util.ArrayList)1 FieldAnnotation (org.apache.felix.scrplugin.annotations.FieldAnnotation)1 ScannedAnnotation (org.apache.felix.scrplugin.annotations.ScannedAnnotation)1 PropertyDescription (org.apache.felix.scrplugin.description.PropertyDescription)1 PropertyUnbounded (org.apache.felix.scrplugin.description.PropertyUnbounded)1