Search in sources :

Example 6 with ReifiedType

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

the class AggregateConverter method getExactDirectSuperTypes.

public static ReifiedType[] getExactDirectSuperTypes(ReifiedType type) {
    Class<?> clazz = type.getRawClass();
    Type[] superInterfaces = clazz.getGenericInterfaces();
    Type superClass = clazz.getGenericSuperclass();
    // the only supertype of an interface without superinterfaces is Object
    if (superClass == null && superInterfaces.length == 0 && clazz.isInterface()) {
        return new ReifiedType[] { new GenericType(Object.class) };
    }
    ReifiedType[] result;
    int resultIndex;
    if (superClass == null) {
        result = new ReifiedType[superInterfaces.length];
        resultIndex = 0;
    } else {
        result = new ReifiedType[superInterfaces.length + 1];
        resultIndex = 1;
        result[0] = mapTypeParameters(superClass, type);
    }
    for (Type superInterface : superInterfaces) {
        result[resultIndex++] = mapTypeParameters(superInterface, type);
    }
    return result;
}
Also used : ReifiedType(org.osgi.service.blueprint.container.ReifiedType) BoundType(org.apache.aries.blueprint.container.GenericType.BoundType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) ReifiedType(org.osgi.service.blueprint.container.ReifiedType)

Example 7 with ReifiedType

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

the class AggregateConverter method convertToArray.

private Object convertToArray(Object obj, ReifiedType type) throws Exception {
    if (obj instanceof Collection) {
        obj = ((Collection) obj).toArray();
    }
    if (!obj.getClass().isArray()) {
        throw new Exception("Unable to convert from " + obj + " to " + type);
    }
    ReifiedType componentType;
    if (type.size() > 0) {
        componentType = type.getActualTypeArgument(0);
    } else {
        componentType = new GenericType(type.getRawClass().getComponentType());
    }
    Object array = Array.newInstance(toClass(componentType), Array.getLength(obj));
    boolean converted = array.getClass() != obj.getClass();
    for (int i = 0; i < Array.getLength(obj); i++) {
        try {
            Object ov = Array.get(obj, i);
            Object nv = convert(ov, componentType);
            converted |= nv != ov;
            Array.set(array, i, nv);
        } catch (Exception t) {
            throw new Exception("Unable to convert from " + obj + " to " + type + "(error converting array element)", t);
        }
    }
    return converted ? array : obj;
}
Also used : ReifiedType(org.osgi.service.blueprint.container.ReifiedType) Collection(java.util.Collection)

Example 8 with ReifiedType

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

the class AggregateConverter method convertToMap.

private Object convertToMap(Object obj, ReifiedType type) throws Exception {
    ReifiedType keyType = type.getActualTypeArgument(0);
    ReifiedType valueType = type.getActualTypeArgument(1);
    Map newMap = (Map) ReflectionUtils.newInstance(blueprintContainer.getAccessControlContext(), MapRecipe.getMap(toClass(type)));
    if (obj instanceof Dictionary) {
        Dictionary dic = (Dictionary) obj;
        for (Enumeration keyEnum = dic.keys(); keyEnum.hasMoreElements(); ) {
            Object key = keyEnum.nextElement();
            try {
                newMap.put(convert(key, keyType), convert(dic.get(key), valueType));
            } catch (Exception t) {
                throw new Exception("Unable to convert from " + obj + " to " + type + "(error converting map entry)", t);
            }
        }
        return newMap;
    } else {
        boolean converted = false;
        for (Map.Entry e : ((Map<Object, Object>) obj).entrySet()) {
            try {
                Object nk = convert(e.getKey(), keyType);
                Object nv = convert(e.getValue(), valueType);
                converted |= nk != e.getKey() || nv != e.getValue();
                newMap.put(nk, nv);
            } catch (Exception t) {
                throw new Exception("Unable to convert from " + obj + " to " + type + "(error converting map entry)", t);
            }
        }
        return converted ? newMap : obj;
    }
}
Also used : Dictionary(java.util.Dictionary) Enumeration(java.util.Enumeration) ReifiedType(org.osgi.service.blueprint.container.ReifiedType) HashMap(java.util.HashMap) Map(java.util.Map)

Example 9 with ReifiedType

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

the class AggregateConverter method isTypeAssignable.

public static boolean isTypeAssignable(ReifiedType from, ReifiedType to) {
    if (from.equals(to)) {
        return true;
    }
    if (from.getRawClass() == to.getRawClass()) {
        if (to.size() == 0) {
            return true;
        }
        if (from.size() == to.size()) {
            boolean ok = true;
            for (int i = 0; i < from.size(); i++) {
                ReifiedType tf = from.getActualTypeArgument(i);
                ReifiedType tt = to.getActualTypeArgument(i);
                if (!isWildcardCompatible(tf, tt)) {
                    ok = false;
                    break;
                }
            }
            if (ok) {
                return true;
            }
        }
    } else {
        ReifiedType t = getExactSuperType(from, to.getRawClass());
        if (t != null) {
            return isTypeAssignable(t, to);
        }
    }
    return false;
}
Also used : ReifiedType(org.osgi.service.blueprint.container.ReifiedType)

Example 10 with ReifiedType

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

the class BeanRecipe method loadClass.

@Override
protected Class loadClass(String className) {
    ClassLoader loader = type instanceof Class ? ((Class) type).getClassLoader() : null;
    ReifiedType t = loadType(className, loader);
    return t != null ? t.getRawClass() : null;
}
Also used : ReifiedType(org.osgi.service.blueprint.container.ReifiedType)

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