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;
}
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;
}
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;
}
}
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;
}
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;
}
Aggregations