use of java.lang.reflect.ParameterizedType in project neo4j by neo4j.
the class PluginPointFactoryImpl method parameterExtractor.
private static ParameterExtractor parameterExtractor(Type type, Parameter parameter, Description description) {
if (type instanceof ParameterizedType) {
ParameterizedType paramType = (ParameterizedType) type;
Class<?> raw = (Class<?>) paramType.getRawType();
Type componentType = paramType.getActualTypeArguments()[0];
Class<?> component = null;
if (componentType instanceof Class<?>) {
component = (Class<?>) componentType;
}
if (Set.class == raw) {
TypeCaster caster = TYPES.get(component);
if (caster != null) {
return new ListParameterExtractor(caster, component, parameter, description) {
@Override
Object convert(Object[] result) {
return new HashSet<Object>(Arrays.asList(result));
}
};
}
} else if (List.class == raw || Collection.class == raw || Iterable.class == raw) {
TypeCaster caster = TYPES.get(component);
if (caster != null) {
return new ListParameterExtractor(caster, component, parameter, description) {
@Override
Object convert(Object[] result) {
return Arrays.asList(result);
}
};
}
}
} else if (type instanceof Class<?>) {
Class<?> raw = (Class<?>) type;
if (raw.isArray()) {
TypeCaster caster = TYPES.get(raw.getComponentType());
if (caster != null) {
return new ListParameterExtractor(caster, raw.getComponentType(), parameter, description) {
@Override
Object convert(Object[] result) {
return result;
}
};
}
} else {
TypeCaster caster = TYPES.get(raw);
if (caster != null) {
return new ParameterExtractor(caster, raw, parameter, description);
}
}
} else if (type instanceof GenericArrayType) {
GenericArrayType array = (GenericArrayType) type;
Type component = array.getGenericComponentType();
if (component instanceof Class<?>) {
TypeCaster caster = TYPES.get(component);
if (caster != null) {
return new ListParameterExtractor(caster, (Class<?>) component, parameter, description) {
@Override
Object convert(Object[] result) {
return result;
}
};
}
}
}
throw new IllegalStateException("Unsupported parameter type: " + type);
}
use of java.lang.reflect.ParameterizedType in project jodd by oblac.
the class ReflectUtil method typeToString.
private static void typeToString(StringBuilder sb, Type type, Set<Type> visited) {
if (type instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) type;
final Class<?> rawType = (Class<?>) parameterizedType.getRawType();
sb.append(rawType.getName());
boolean first = true;
for (Type typeArg : parameterizedType.getActualTypeArguments()) {
if (first) {
first = false;
} else {
sb.append(", ");
}
sb.append('<');
typeToString(sb, typeArg, visited);
sb.append('>');
}
} else if (type instanceof WildcardType) {
WildcardType wildcardType = (WildcardType) type;
sb.append('?');
// According to JLS(http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.5.1):
// - Lower and upper can't coexist: (for instance, this is not allowed: <? extends List<String> & super MyInterface>)
// - Multiple bounds are not supported (for instance, this is not allowed: <? extends List<String> & MyInterface>)
final Type bound;
if (wildcardType.getLowerBounds().length != 0) {
sb.append(" super ");
bound = wildcardType.getLowerBounds()[0];
} else {
sb.append(" extends ");
bound = wildcardType.getUpperBounds()[0];
}
typeToString(sb, bound, visited);
} else if (type instanceof TypeVariable<?>) {
TypeVariable<?> typeVariable = (TypeVariable<?>) type;
sb.append(typeVariable.getName());
if (!visited.contains(type)) {
visited.add(type);
sb.append(" extends ");
boolean first = true;
for (Type bound : typeVariable.getBounds()) {
if (first) {
first = false;
} else {
sb.append(" & ");
}
typeToString(sb, bound, visited);
}
visited.remove(type);
}
} else if (type instanceof GenericArrayType) {
GenericArrayType genericArrayType = (GenericArrayType) type;
typeToString(genericArrayType.getGenericComponentType());
sb.append(genericArrayType.getGenericComponentType());
sb.append("[]");
} else if (type instanceof Class) {
Class<?> typeClass = (Class<?>) type;
sb.append(typeClass.getName());
} else {
throw new IllegalArgumentException("Unsupported type: " + type);
}
}
use of java.lang.reflect.ParameterizedType in project jodd by oblac.
the class ReflectUtil method getRawType.
/**
* Returns raw class for given <code>type</code> when implementation class is known
* and it makes difference.
* @see #resolveVariable(java.lang.reflect.TypeVariable, Class)
*/
public static Class<?> getRawType(Type type, Class implClass) {
if (type instanceof Class) {
return (Class) type;
}
if (type instanceof ParameterizedType) {
ParameterizedType pType = (ParameterizedType) type;
return getRawType(pType.getRawType(), implClass);
}
if (type instanceof WildcardType) {
WildcardType wType = (WildcardType) type;
Type[] lowerTypes = wType.getLowerBounds();
if (lowerTypes.length > 0) {
return getRawType(lowerTypes[0], implClass);
}
Type[] upperTypes = wType.getUpperBounds();
if (upperTypes.length != 0) {
return getRawType(upperTypes[0], implClass);
}
return Object.class;
}
if (type instanceof GenericArrayType) {
Type genericComponentType = ((GenericArrayType) type).getGenericComponentType();
Class<?> rawType = getRawType(genericComponentType, implClass);
// this is sort of stupid, but there seems no other way (consider don't creating new instances each time)...
return Array.newInstance(rawType, 0).getClass();
}
if (type instanceof TypeVariable) {
TypeVariable<?> varType = (TypeVariable<?>) type;
if (implClass != null) {
Type resolvedType = resolveVariable(varType, implClass);
if (resolvedType != null) {
return getRawType(resolvedType, null);
}
}
Type[] boundsTypes = varType.getBounds();
if (boundsTypes.length == 0) {
return Object.class;
}
return getRawType(boundsTypes[0], implClass);
}
return null;
}
use of java.lang.reflect.ParameterizedType in project jodd by oblac.
the class ReflectUtil method getComponentTypes.
/**
* Returns all component types of the given type.
* For example the following types all have the
* component-type MyClass:
* <ul>
* <li>MyClass[]</li>
* <li>List<MyClass></li>
* <li>Foo<? extends MyClass></li>
* <li>Bar<? super MyClass></li>
* <li><T extends MyClass> T[]</li>
* </ul>
*/
public static Class[] getComponentTypes(Type type, Class implClass) {
if (type instanceof Class) {
Class clazz = (Class) type;
if (clazz.isArray()) {
return new Class[] { clazz.getComponentType() };
}
} else if (type instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType) type;
Type[] generics = pt.getActualTypeArguments();
if (generics.length == 0) {
return null;
}
Class[] types = new Class[generics.length];
for (int i = 0; i < generics.length; i++) {
types[i] = getRawType(generics[i], implClass);
}
return types;
} else if (type instanceof GenericArrayType) {
GenericArrayType gat = (GenericArrayType) type;
Class rawType = getRawType(gat.getGenericComponentType(), implClass);
if (rawType == null) {
return null;
}
return new Class[] { rawType };
}
return null;
}
use of java.lang.reflect.ParameterizedType in project orientdb by orientechnologies.
the class OObjectSerializerHelper method convertInObject.
public static Object convertInObject(final Object iPojo, final String iField, final Object iValue, final Class<?> parameterType) {
// New conversion method working with OLazyObjectList
if (!(iValue instanceof OObjectLazyList<?>))
return OType.convert(iValue, parameterType);
List<Object> aSubList = null;
try {
final Field aField = OObjectSerializerHelper.getField(iPojo, iField);
final Class<?> listClass = aField.getType();
final ParameterizedType aType = (ParameterizedType) aField.getGenericType();
final Class<?> objectClass = (Class<?>) aType.getActualTypeArguments()[0];
final OObjectLazyList<?> aList = (OObjectLazyList<?>) iValue;
// Instantiation of the list
if (listClass.isInterface()) {
aSubList = new ArrayList<Object>();
} else {
aSubList = (List<Object>) listClass.newInstance();
}
for (final Object value : aList) {
if (value instanceof ODocument) {
final ODocument aDocument = (ODocument) value;
aSubList.add(OObjectSerializerHelper.convertDocumentInType(aDocument, objectClass));
} else {
aSubList.add(value);
}
}
} catch (Exception e) {
OLogManager.instance().error(null, "Error on convertInObject()", e);
}
return aSubList;
}
Aggregations