use of java.lang.reflect.GenericArrayType in project cxf by apache.
the class Wrapper method getTypeString.
protected String getTypeString(Type t) {
String type = "Object";
if (t instanceof Class) {
Class<?> clz = (Class<?>) t;
if (clz.isArray()) {
if (isBuiltInTypes(clz.getComponentType())) {
type = clz.getComponentType().getSimpleName() + "[]";
} else {
type = clz.getComponentType().getName() + "[]";
}
} else {
type = clz.getName();
}
} else if (t instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType) t;
Class<?> c = (Class<?>) pt.getRawType();
if (Holder.class.isAssignableFrom(c) && pt.getActualTypeArguments().length == 1 && pt.getActualTypeArguments()[0] instanceof Class) {
type = getTypeString(pt.getActualTypeArguments()[0]);
} else {
type = t.toString();
}
} else if (t instanceof GenericArrayType) {
GenericArrayType gat = (GenericArrayType) t;
type = gat.toString();
}
type = type.replace('$', '.');
return type;
}
use of java.lang.reflect.GenericArrayType in project appsly-android-rest by 47deg.
the class Types method equals.
/**
* Returns true if {@code a} and {@code b} are equal.
*/
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;
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 {
// This isn't a type we support!
return false;
}
}
use of java.lang.reflect.GenericArrayType in project jdk8u_jdk by JetBrains.
the class TypeResolver method fixGenericArray.
/**
* Replaces a {@link GenericArrayType GenericArrayType}
* with plain array class where it is possible.
* Bug <a href="http://bugs.sun.com/view_bug.do?bug_id=5041784">5041784</a>
* is that arrays of non-generic type sometimes show up
* as {@link GenericArrayType GenericArrayType} when using reflection.
* For example, a {@code String[]} might show up
* as a {@link GenericArrayType GenericArrayType}
* where {@link GenericArrayType#getGenericComponentType getGenericComponentType}
* is {@code String.class}. This violates the specification,
* which says that {@link GenericArrayType GenericArrayType}
* is used when the component type is a type variable or parameterized type.
* We fit the specification here.
*
* @param type the type to fix
* @return a corresponding type for the generic array type,
* or the same type as {@code type}
*/
private static Type fixGenericArray(Type type) {
if (type instanceof GenericArrayType) {
Type comp = ((GenericArrayType) type).getGenericComponentType();
comp = fixGenericArray(comp);
if (comp instanceof Class) {
return Array.newInstance((Class<?>) comp, 0).getClass();
}
}
return type;
}
use of java.lang.reflect.GenericArrayType in project jdk8u_jdk by JetBrains.
the class TypeResolver method erase.
/**
* Converts the given {@code type} to the corresponding class.
* This method implements the concept of type erasure,
* that is described in section 4.6 of
* <cite>The Java™ Language Specification</cite>.
*
* @param type the array of types to convert
* @return a corresponding class
*/
public static Class<?> erase(Type type) {
if (type instanceof Class) {
return (Class<?>) type;
}
if (type instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType) type;
return (Class<?>) pt.getRawType();
}
if (type instanceof TypeVariable) {
TypeVariable tv = (TypeVariable) type;
Type[] bounds = tv.getBounds();
return (0 < bounds.length) ? erase(bounds[0]) : Object.class;
}
if (type instanceof WildcardType) {
WildcardType wt = (WildcardType) type;
Type[] bounds = wt.getUpperBounds();
return (0 < bounds.length) ? erase(bounds[0]) : Object.class;
}
if (type instanceof GenericArrayType) {
GenericArrayType gat = (GenericArrayType) type;
return Array.newInstance(erase(gat.getGenericComponentType()), 0).getClass();
}
throw new IllegalArgumentException("Unknown Type kind: " + type.getClass());
}
use of java.lang.reflect.GenericArrayType in project jdk8u_jdk by JetBrains.
the class TypeResolver method resolve.
/**
* Replaces type variables of the given {@code formal} type
* with the types they stand for in the given {@code actual} type.
*
* <p>A ParameterizedType is a class with type parameters, and the values
* of those parameters. For example, Map<K,V> is a generic class, and
* a corresponding ParameterizedType might look like
* Map<K=String,V=Integer>. Given such a ParameterizedType, this method
* will replace K with String, or List<K> with List<String;, or
* List<? super K> with List<? super String>.</p>
*
* <p>The {@code actual} argument to this method can also be a Class.
* In this case, either it is equivalent to a ParameterizedType with
* no parameters (for example, Integer.class), or it is equivalent to
* a "raw" ParameterizedType (for example, Map.class). In the latter
* case, every type parameter declared or inherited by the class is replaced
* by its "erasure". For a type parameter declared as <T>, the erasure
* is Object. For a type parameter declared as <T extends Number>,
* the erasure is Number.</p>
*
* <p>Although type parameters are not inherited by subclasses in the Java
* language, they <em>are</em> effectively inherited when using reflection.
* For example, if you declare an interface like this...</p>
*
* <pre>
* public interface StringToIntMap extends Map<String,Integer> {}
* </pre>
*
* <p>...then StringToIntMap.class.getMethods() will show that it has methods
* like put(K,V) even though StringToIntMap has no type parameters. The K
* and V variables are the ones declared by Map, so
* {@link TypeVariable#getGenericDeclaration()} will return {@link Map Map.class}.</p>
*
* <p>For this reason, this method replaces inherited type parameters too.
* Therefore if this method is called with {@code actual} being
* StringToIntMap.class and {@code formal} being the K from Map,
* it will return {@link String String.class}.</p>
*
* <p>In the case where {@code actual} is a "raw" ParameterizedType, the
* inherited type parameters will also be replaced by their erasures.
* The erasure of a Class is the Class itself, so a "raw" subinterface of
* StringToIntMap will still show the K from Map as String.class. But
* in a case like this...
*
* <pre>
* public interface StringToIntListMap extends Map<String,List<Integer>> {}
* public interface RawStringToIntListMap extends StringToIntListMap {}
* </pre>
*
* <p>...the V inherited from Map will show up as List<Integer> in
* StringToIntListMap, but as plain List in RawStringToIntListMap.</p>
*
* @param actual the type that supplies bindings for type variables
* @param formal the type where occurrences of the variables
* in {@code actual} will be replaced by the corresponding bound values
* @return a resolved type
*/
public static Type resolve(Type actual, Type formal) {
if (formal instanceof Class) {
return formal;
}
if (formal instanceof GenericArrayType) {
Type comp = ((GenericArrayType) formal).getGenericComponentType();
comp = resolve(actual, comp);
return (comp instanceof Class) ? Array.newInstance((Class<?>) comp, 0).getClass() : GenericArrayTypeImpl.make(comp);
}
if (formal instanceof ParameterizedType) {
ParameterizedType fpt = (ParameterizedType) formal;
Type[] actuals = resolve(actual, fpt.getActualTypeArguments());
return ParameterizedTypeImpl.make((Class<?>) fpt.getRawType(), actuals, fpt.getOwnerType());
}
if (formal instanceof WildcardType) {
WildcardType fwt = (WildcardType) formal;
Type[] upper = resolve(actual, fwt.getUpperBounds());
Type[] lower = resolve(actual, fwt.getLowerBounds());
return new WildcardTypeImpl(upper, lower);
}
if (formal instanceof TypeVariable) {
Map<Type, Type> map;
synchronized (CACHE) {
map = CACHE.get(actual);
if (map == null) {
map = new HashMap<>();
prepare(map, actual);
CACHE.put(actual, map);
}
}
Type result = map.get(formal);
if (result == null || result.equals(formal)) {
return formal;
}
result = fixGenericArray(result);
// So if we have to resolve T, we need the tail recursion here.
return resolve(actual, result);
}
throw new IllegalArgumentException("Bad Type kind: " + formal.getClass());
}
Aggregations