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 SmartAndroidSource by jaychou2012.
the class $Gson$Types method equals.
/**
* Returns true if {@code a} and {@code b} are equal.
*/
@SuppressWarnings("unchecked")
public static boolean equals(Type a, Type b) {
if (a == b) {
// also handles (a == null && b == null)
return true;
} else if (a instanceof Class) {
// Class already specifies equals().
return a.equals(b);
} else if (a instanceof ParameterizedType) {
if (!(b instanceof ParameterizedType)) {
return false;
}
// TODO: save a .clone() call
ParameterizedType pa = (ParameterizedType) a;
ParameterizedType pb = (ParameterizedType) b;
return equal(pa.getOwnerType(), pb.getOwnerType()) && pa.getRawType().equals(pb.getRawType()) && Arrays.equals(pa.getActualTypeArguments(), pb.getActualTypeArguments());
} else if (a instanceof GenericArrayType) {
if (!(b instanceof GenericArrayType)) {
return false;
}
GenericArrayType ga = (GenericArrayType) a;
GenericArrayType gb = (GenericArrayType) b;
return equals(ga.getGenericComponentType(), gb.getGenericComponentType());
} else if (a instanceof WildcardType) {
if (!(b instanceof WildcardType)) {
return false;
}
WildcardType wa = (WildcardType) a;
WildcardType wb = (WildcardType) b;
return Arrays.equals(wa.getUpperBounds(), wb.getUpperBounds()) && Arrays.equals(wa.getLowerBounds(), wb.getLowerBounds());
} else if (a instanceof TypeVariable) {
if (!(b instanceof TypeVariable)) {
return false;
}
TypeVariable<?> va = (TypeVariable<?>) a;
TypeVariable<?> vb = (TypeVariable<?>) b;
return va.getGenericDeclaration() == vb.getGenericDeclaration() && va.getName().equals(vb.getName());
} else {
// wildcard type, etc.
return false;
}
}
use of java.lang.reflect.GenericArrayType in project SmartAndroidSource by jaychou2012.
the class $Gson$Types method resolve.
@SuppressWarnings("unchecked")
public static Type resolve(Type context, Class<?> contextRawType, Type toResolve) {
// to avoid object-creation
while (true) {
if (toResolve instanceof TypeVariable) {
TypeVariable<?> typeVariable = (TypeVariable<?>) toResolve;
toResolve = resolveTypeVariable(context, contextRawType, typeVariable);
if (toResolve == typeVariable) {
return toResolve;
}
} else if (toResolve instanceof Class && ((Class<?>) toResolve).isArray()) {
Class<?> original = (Class<?>) toResolve;
Type componentType = original.getComponentType();
Type newComponentType = resolve(context, contextRawType, componentType);
return componentType == newComponentType ? original : arrayOf(newComponentType);
} else if (toResolve instanceof GenericArrayType) {
GenericArrayType original = (GenericArrayType) toResolve;
Type componentType = original.getGenericComponentType();
Type newComponentType = resolve(context, contextRawType, componentType);
return componentType == newComponentType ? original : arrayOf(newComponentType);
} else if (toResolve instanceof ParameterizedType) {
ParameterizedType original = (ParameterizedType) toResolve;
Type ownerType = original.getOwnerType();
Type newOwnerType = resolve(context, contextRawType, ownerType);
boolean changed = newOwnerType != ownerType;
Type[] args = original.getActualTypeArguments();
for (int t = 0, length = args.length; t < length; t++) {
Type resolvedTypeArgument = resolve(context, contextRawType, args[t]);
if (resolvedTypeArgument != args[t]) {
if (!changed) {
args = args.clone();
changed = true;
}
args[t] = resolvedTypeArgument;
}
}
return changed ? newParameterizedTypeWithOwner(newOwnerType, original.getRawType(), args) : original;
} else if (toResolve instanceof WildcardType) {
WildcardType original = (WildcardType) toResolve;
Type[] originalLowerBound = original.getLowerBounds();
Type[] originalUpperBound = original.getUpperBounds();
if (originalLowerBound.length == 1) {
Type lowerBound = resolve(context, contextRawType, originalLowerBound[0]);
if (lowerBound != originalLowerBound[0]) {
return supertypeOf(lowerBound);
}
} else if (originalUpperBound.length == 1) {
Type upperBound = resolve(context, contextRawType, originalUpperBound[0]);
if (upperBound != originalUpperBound[0]) {
return subtypeOf(upperBound);
}
}
return original;
} else {
return toResolve;
}
}
}
Aggregations