use of java.lang.reflect.GenericArrayType in project jersey by jersey.
the class ReflectionHelper method resolveTypeVariable.
private static ClassTypePair resolveTypeVariable(ParameterizedType pt, Class<?> c, final Class<?> dc, final TypeVariable tv, final Map<TypeVariable, Type> map) {
final Type[] typeArguments = pt.getActualTypeArguments();
final TypeVariable[] typeParameters = c.getTypeParameters();
final Map<TypeVariable, Type> subMap = new HashMap<TypeVariable, Type>();
for (int i = 0; i < typeArguments.length; i++) {
// Substitute a type variable with the Java class
final Type typeArgument = typeArguments[i];
if (typeArgument instanceof TypeVariable) {
final Type t = map.get(typeArgument);
subMap.put(typeParameters[i], t);
} else {
subMap.put(typeParameters[i], typeArgument);
}
}
if (c == dc) {
Type t = subMap.get(tv);
if (t instanceof Class) {
return ClassTypePair.of((Class) t);
} else if (t instanceof GenericArrayType) {
final GenericArrayType gat = (GenericArrayType) t;
t = gat.getGenericComponentType();
if (t instanceof Class) {
c = (Class<?>) t;
try {
return ClassTypePair.of(getArrayForComponentType(c));
} catch (final Exception ignored) {
// ignored
}
return null;
} else if (t instanceof ParameterizedType) {
final Type rt = ((ParameterizedType) t).getRawType();
if (rt instanceof Class) {
c = (Class<?>) rt;
} else {
return null;
}
try {
return ClassTypePair.of(getArrayForComponentType(c), gat);
} catch (final Exception e) {
return null;
}
} else {
return null;
}
} else if (t instanceof ParameterizedType) {
pt = (ParameterizedType) t;
if (pt.getRawType() instanceof Class) {
return ClassTypePair.of((Class<?>) pt.getRawType(), pt);
} else {
return null;
}
} else {
return null;
}
} else {
return resolveTypeVariable(c, dc, tv, subMap);
}
}
use of java.lang.reflect.GenericArrayType in project jersey by jersey.
the class ReflectionHelper method getRawClass.
/**
* Given the type parameter gets the raw type represented by the type, or null if this has no associated raw class.
*
* @param type the type to find the raw class on.
* @return the raw class associated with this type.
*/
public static Class<?> getRawClass(Type type) {
if (type == null)
return null;
if (type instanceof GenericArrayType) {
Type componentType = ((GenericArrayType) type).getGenericComponentType();
if (!(componentType instanceof ParameterizedType) && !(componentType instanceof Class)) {
// type variable is not supported
return null;
}
Class<?> rawComponentClass = getRawClass(componentType);
String forNameName = "[L" + rawComponentClass.getName() + ";";
try {
return Class.forName(forNameName);
} catch (Throwable th) {
// ignore, but return null
return null;
}
}
if (type instanceof Class) {
return (Class<?>) type;
}
if (type instanceof ParameterizedType) {
Type rawType = ((ParameterizedType) type).getRawType();
if (rawType instanceof Class) {
return (Class<?>) rawType;
}
}
return null;
}
use of java.lang.reflect.GenericArrayType in project jersey by jersey.
the class ReflectionHelper method resolveGenericType.
/**
* Resolve generic type parameter(s) of a raw class and it's generic type
* based on the class that declares the generic type parameter(s) to be resolved
* and a concrete implementation of the declaring class.
*
* @param concreteClass concrete implementation of the declaring class.
* @param declaringClass class declaring the generic type parameter(s) to be
* resolved.
* @param rawResolvedType raw class of the generic type to be resolved.
* @param genericResolvedType generic type information of th type to be resolved.
* @return a pair of class and the generic type values with the the resolved
* generic parameter types.
*/
public static ClassTypePair resolveGenericType(final Class concreteClass, final Class declaringClass, final Class rawResolvedType, final Type genericResolvedType) {
if (genericResolvedType instanceof TypeVariable) {
final ClassTypePair ct = resolveTypeVariable(concreteClass, declaringClass, (TypeVariable) genericResolvedType);
if (ct != null) {
return ct;
}
} else if (genericResolvedType instanceof ParameterizedType) {
final ParameterizedType pt = (ParameterizedType) genericResolvedType;
final Type[] ptts = pt.getActualTypeArguments();
boolean modified = false;
for (int i = 0; i < ptts.length; i++) {
final ClassTypePair ct = resolveGenericType(concreteClass, declaringClass, (Class) pt.getRawType(), ptts[i]);
if (ct.type() != ptts[i]) {
ptts[i] = ct.type();
modified = true;
}
}
if (modified) {
final ParameterizedType rpt = new ParameterizedType() {
@Override
public Type[] getActualTypeArguments() {
return ptts.clone();
}
@Override
public Type getRawType() {
return pt.getRawType();
}
@Override
public Type getOwnerType() {
return pt.getOwnerType();
}
};
return ClassTypePair.of((Class<?>) pt.getRawType(), rpt);
}
} else if (genericResolvedType instanceof GenericArrayType) {
final GenericArrayType gat = (GenericArrayType) genericResolvedType;
final ClassTypePair ct = resolveGenericType(concreteClass, declaringClass, null, gat.getGenericComponentType());
if (gat.getGenericComponentType() != ct.type()) {
try {
final Class ac = ReflectionHelper.getArrayForComponentType(ct.rawClass());
return ClassTypePair.of(ac);
} catch (final Exception e) {
LOGGER.log(Level.FINEST, "", e);
}
}
}
return ClassTypePair.of(rawResolvedType, genericResolvedType);
}
use of java.lang.reflect.GenericArrayType 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.GenericArrayType 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);
}
}
Aggregations