Search in sources :

Example 16 with ReifiedType

use of org.osgi.service.blueprint.container.ReifiedType in project aries by apache.

the class CollectionRecipe method internalCreate.

protected Object internalCreate() throws ComponentDefinitionException {
    Class type = getCollection(collectionTypeClass);
    if (!ReflectionUtils.hasDefaultConstructor(type)) {
        throw new ComponentDefinitionException("Type does not have a default constructor " + type.getName());
    }
    // create collection instance
    Object o;
    try {
        o = type.newInstance();
    } catch (Exception e) {
        throw new ComponentDefinitionException("Error while creating collection instance: " + type.getName());
    }
    if (!(o instanceof Collection)) {
        throw new ComponentDefinitionException("Specified collection type does not implement the Collection interface: " + type.getName());
    }
    Collection instance = (Collection) o;
    ReifiedType defaultConversionType = loadType(defaultValueType);
    Type conversionType = null;
    for (Recipe recipe : list) {
        Object value;
        if (recipe != null) {
            try {
                conversionType = defaultConversionType.getRawClass();
                if (recipe instanceof ValueRecipe) {
                    conversionType = ((ValueRecipe) recipe).getValueType();
                }
                value = convert(recipe.create(), conversionType);
            } catch (Exception e) {
                throw new ComponentDefinitionException("Unable to convert value " + recipe + " to type " + conversionType, e);
            }
        } else {
            value = null;
        }
        instance.add(value);
    }
    return instance;
}
Also used : ReifiedType(org.osgi.service.blueprint.container.ReifiedType) Type(java.lang.reflect.Type) ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) ReifiedType(org.osgi.service.blueprint.container.ReifiedType) Collection(java.util.Collection) ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException)

Aggregations

ReifiedType (org.osgi.service.blueprint.container.ReifiedType)16 HashMap (java.util.HashMap)6 ComponentDefinitionException (org.osgi.service.blueprint.container.ComponentDefinitionException)6 ArrayList (java.util.ArrayList)5 Collection (java.util.Collection)5 List (java.util.List)5 Map (java.util.Map)5 LinkedHashMap (java.util.LinkedHashMap)4 Method (java.lang.reflect.Method)3 Dictionary (java.util.Dictionary)3 Enumeration (java.util.Enumeration)3 Type (java.lang.reflect.Type)2 ExecutionContext (org.apache.aries.blueprint.di.ExecutionContext)2 Recipe (org.apache.aries.blueprint.di.Recipe)2 Constructor (java.lang.reflect.Constructor)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 AbstractMap (java.util.AbstractMap)1 Hashtable (java.util.Hashtable)1 LinkedHashSet (java.util.LinkedHashSet)1