use of java.lang.reflect.GenericArrayType in project cxf by apache.
the class JAXBContextInitializer method addType.
private void addType(Type cls, boolean allowArray) {
if (cls instanceof Class) {
if (globalAdapters.contains(cls)) {
return;
}
if (((Class<?>) cls).isArray() && !allowArray) {
addClass(((Class<?>) cls).getComponentType());
} else {
addClass((Class<?>) cls);
}
} else if (cls instanceof ParameterizedType) {
final ParameterizedType parameterizedType = (ParameterizedType) cls;
addType(parameterizedType.getRawType());
if (!parameterizedType.getRawType().equals(Enum.class)) {
for (Type t2 : parameterizedType.getActualTypeArguments()) {
if (shouldTypeBeAdded(t2, parameterizedType)) {
addType(t2);
}
}
}
} else if (cls instanceof GenericArrayType) {
Class<?> ct;
GenericArrayType gt = (GenericArrayType) cls;
Type componentType = gt.getGenericComponentType();
if (componentType instanceof Class) {
ct = (Class<?>) componentType;
} else if (componentType instanceof ParameterizedType) {
final ParameterizedType parameterizedType = (ParameterizedType) componentType;
final Type rawType = parameterizedType.getRawType();
if (rawType instanceof Class) {
ct = (Class<?>) rawType;
} else {
throw new IllegalArgumentException("Unable to determine type for " + rawType);
}
if (!parameterizedType.getRawType().equals(Enum.class)) {
for (Type t2 : parameterizedType.getActualTypeArguments()) {
if (shouldTypeBeAdded(t2, parameterizedType)) {
addType(t2);
}
}
}
} else {
TypeVariable<?> tv = (TypeVariable<?>) componentType;
Type[] bounds = tv.getBounds();
if (bounds != null && bounds.length == 1) {
if (bounds[0] instanceof Class) {
ct = (Class<?>) bounds[0];
} else {
throw new IllegalArgumentException("Unable to determine type for: " + tv);
}
} else {
throw new IllegalArgumentException("Unable to determine type for: " + tv);
}
}
ct = Array.newInstance(ct, 0).getClass();
addClass(ct);
} else if (cls instanceof WildcardType) {
for (Type t : ((WildcardType) cls).getUpperBounds()) {
addType(t);
}
for (Type t : ((WildcardType) cls).getLowerBounds()) {
addType(t);
}
} else if (cls instanceof TypeVariable) {
for (Type t : ((TypeVariable<?>) cls).getBounds()) {
addType(t);
}
}
}
use of java.lang.reflect.GenericArrayType in project cxf by apache.
the class JAXBSchemaInitializer method getBeanInfo.
public JAXBBeanInfo getBeanInfo(Type cls) {
if (cls instanceof Class) {
if (((Class<?>) cls).isArray()) {
return getBeanInfo(((Class<?>) cls).getComponentType());
}
return getBeanInfo((Class<?>) cls);
} else if (cls instanceof ParameterizedType) {
for (Type t2 : ((ParameterizedType) cls).getActualTypeArguments()) {
return getBeanInfo(t2);
}
} else if (cls instanceof GenericArrayType) {
GenericArrayType gt = (GenericArrayType) cls;
Class<?> ct = (Class<?>) gt.getGenericComponentType();
ct = Array.newInstance(ct, 0).getClass();
return getBeanInfo(ct);
}
return null;
}
use of java.lang.reflect.GenericArrayType in project cxf by apache.
the class JAXBSchemaInitializer method getArrayComponentType.
static Class<?> getArrayComponentType(Type cls) {
if (cls instanceof Class) {
if (((Class<?>) cls).isArray()) {
return ((Class<?>) cls).getComponentType();
}
return (Class<?>) cls;
} else if (cls instanceof ParameterizedType) {
for (Type t2 : ((ParameterizedType) cls).getActualTypeArguments()) {
return getArrayComponentType(t2);
}
} else if (cls instanceof GenericArrayType) {
GenericArrayType gt = (GenericArrayType) cls;
Class<?> ct = (Class<?>) gt.getGenericComponentType();
return Array.newInstance(ct, 0).getClass();
}
return null;
}
use of java.lang.reflect.GenericArrayType in project cxf by apache.
the class TypeUtil method getTypeRelatedClass.
/**
* If a Type is a class, return it as a class.
* If it is a ParameterizedType, return the raw type as a class.
* Otherwise return null.
* @param type
* @return
*/
public static Class<?> getTypeRelatedClass(Type type) {
Class<?> directClass = getTypeClass(type, false);
if (directClass != null) {
return directClass;
}
if (type instanceof ParameterizedType) {
ParameterizedType pType = (ParameterizedType) type;
return getTypeRelatedClass(pType.getRawType());
}
if (type instanceof GenericArrayType) {
GenericArrayType gat = (GenericArrayType) type;
Type compType = gat.getGenericComponentType();
Class<?> arrayBaseType = getTypeRelatedClass(compType);
// believe it or not, this seems to be the only way to get the
// Class object for an array of primitive type.
Object instance = Array.newInstance(arrayBaseType, 0);
return instance.getClass();
}
return null;
}
use of java.lang.reflect.GenericArrayType in project cxf by apache.
the class InjectionUtils method getActualType.
public static Class<?> getActualType(Type genericType, int pos) {
if (genericType == null) {
return null;
}
if (genericType == Object.class) {
return (Class<?>) genericType;
}
if (!ParameterizedType.class.isAssignableFrom(genericType.getClass())) {
if (genericType instanceof TypeVariable) {
genericType = getType(((TypeVariable<?>) genericType).getBounds(), pos);
} else if (genericType instanceof WildcardType) {
WildcardType wildcardType = (WildcardType) genericType;
Type[] bounds = wildcardType.getLowerBounds();
if (bounds.length == 0) {
bounds = wildcardType.getUpperBounds();
}
genericType = getType(bounds, pos);
} else if (genericType instanceof GenericArrayType) {
genericType = ((GenericArrayType) genericType).getGenericComponentType();
}
Class<?> cls = null;
if (!(genericType instanceof ParameterizedType)) {
cls = (Class<?>) genericType;
} else {
cls = (Class<?>) ((ParameterizedType) genericType).getRawType();
}
return cls.isArray() ? cls.getComponentType() : cls;
}
ParameterizedType paramType = (ParameterizedType) genericType;
Type t = getType(paramType.getActualTypeArguments(), pos);
return t instanceof Class ? (Class<?>) t : getActualType(t, 0);
}
Aggregations