use of java.lang.reflect.GenericArrayType 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.GenericArrayType 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.GenericArrayType in project Genius-Android by qiujuer.
the class Reflector method getClass.
/**
* Get the underlying class for a type, or null if the type is a variable
* type.
*
* @param type the type
* @return the underlying class
*/
public static Class<?> getClass(Type type) {
if (type instanceof Class) {
return (Class) type;
} else if (type instanceof ParameterizedType) {
return getClass(((ParameterizedType) type).getRawType());
} else if (type instanceof GenericArrayType) {
Type componentType = ((GenericArrayType) type).getGenericComponentType();
Class<?> componentClass = getClass(componentType);
if (componentClass != null) {
return Array.newInstance(componentClass, 0).getClass();
} else {
return null;
}
} else {
return null;
}
}
use of java.lang.reflect.GenericArrayType in project roboguice by roboguice.
the class TypesTest method testEqualsAndHashcode.
public void testEqualsAndHashcode() {
ParameterizedType parameterizedType = Types.newParameterizedType(Map.class, String.class, Integer.class);
assertEqualsBothWays(mapStringInteger, parameterizedType);
assertEquals(mapStringInteger.toString(), parameterizedType.toString());
GenericArrayType genericArrayType = Types.arrayOf(Types.arrayOf(Types.newParameterizedType(Set.class, String.class)));
assertEqualsBothWays(setStringArray, genericArrayType);
assertEquals(setStringArray.toString(), genericArrayType.toString());
}
use of java.lang.reflect.GenericArrayType in project robovm by robovm.
the class Types method appendGenericType.
public static void appendGenericType(StringBuilder out, Type type) {
if (type instanceof TypeVariable) {
out.append(((TypeVariable) type).getName());
} else if (type instanceof ParameterizedType) {
out.append(type.toString());
} else if (type instanceof GenericArrayType) {
Type simplified = ((GenericArrayType) type).getGenericComponentType();
appendGenericType(out, simplified);
out.append("[]");
} else if (type instanceof Class) {
Class c = (Class<?>) type;
if (c.isArray()) {
String[] as = c.getName().split("\\[");
int len = as.length - 1;
if (as[len].length() > 1) {
out.append(as[len].substring(1, as[len].length() - 1));
} else {
char ch = as[len].charAt(0);
if (ch == 'I') {
out.append("int");
} else if (ch == 'B') {
out.append("byte");
} else if (ch == 'J') {
out.append("long");
} else if (ch == 'F') {
out.append("float");
} else if (ch == 'D') {
out.append("double");
} else if (ch == 'S') {
out.append("short");
} else if (ch == 'C') {
out.append("char");
} else if (ch == 'Z') {
out.append("boolean");
} else if (ch == 'V') {
out.append("void");
}
}
for (int i = 0; i < len; i++) {
out.append("[]");
}
} else {
out.append(c.getName());
}
}
}
Aggregations