use of java.lang.reflect.GenericArrayType in project bnd by bndtools.
the class ClassParserTest method print.
public static void print(Appendable sb, Type t) throws Exception {
if (t instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType) t;
Class<?> c = (Class<?>) pt.getRawType();
sb.append("L");
sb.append(c.getCanonicalName().replace('.', '/'));
sb.append("<");
for (Type arg : pt.getActualTypeArguments()) {
print(sb, arg);
}
sb.append(">");
sb.append(";");
return;
} else if (t instanceof WildcardType) {
sb.append("yyyy");
} else if (t instanceof GenericArrayType) {
sb.append("[" + ((GenericArrayType) t).getGenericComponentType());
} else if (t instanceof TypeVariable) {
TypeVariable<?> tv = (TypeVariable<?>) t;
sb.append("T");
sb.append(tv.getName());
for (Type upper : tv.getBounds()) {
if (upper != Object.class) {
sb.append(":");
print(sb, upper);
}
}
sb.append(";");
} else {
Class<?> c = (Class<?>) t;
sb.append("L");
sb.append(c.getCanonicalName().replace('.', '/'));
if (c instanceof GenericDeclaration) {
GenericDeclaration gd = c;
if (gd.getTypeParameters().length != 0) {
sb.append("<");
for (Type arg : gd.getTypeParameters()) {
print(sb, arg);
}
sb.append(">");
}
}
sb.append(";");
}
}
use of java.lang.reflect.GenericArrayType in project dubbo by alibaba.
the class ClassUtils method getGenericClass.
public static Class<?> getGenericClass(Class<?> cls, int i) {
try {
ParameterizedType parameterizedType = ((ParameterizedType) cls.getGenericInterfaces()[0]);
Object genericClass = parameterizedType.getActualTypeArguments()[i];
if (genericClass instanceof ParameterizedType) {
return (Class<?>) ((ParameterizedType) genericClass).getRawType();
} else if (genericClass instanceof GenericArrayType) {
return (Class<?>) ((GenericArrayType) genericClass).getGenericComponentType();
} else if (genericClass != null) {
return (Class<?>) genericClass;
}
} catch (Throwable e) {
}
if (cls.getSuperclass() != null) {
return getGenericClass(cls.getSuperclass(), i);
} else {
throw new IllegalArgumentException(cls.getName() + " generic type undefined!");
}
}
use of java.lang.reflect.GenericArrayType in project dubbo by alibaba.
the class ReflectUtils method getGenericClass.
public static Class<?> getGenericClass(Class<?> cls, int i) {
try {
ParameterizedType parameterizedType = ((ParameterizedType) cls.getGenericInterfaces()[0]);
Object genericClass = parameterizedType.getActualTypeArguments()[i];
if (genericClass instanceof ParameterizedType) {
// handle nested generic type
return (Class<?>) ((ParameterizedType) genericClass).getRawType();
} else if (genericClass instanceof GenericArrayType) {
// handle array generic type
return (Class<?>) ((GenericArrayType) genericClass).getGenericComponentType();
} else if (((Class) genericClass).isArray()) {
// Requires JDK 7 or higher, Foo<int[]> is no longer GenericArrayType
return ((Class) genericClass).getComponentType();
} else {
return (Class<?>) genericClass;
}
} catch (Throwable e) {
throw new IllegalArgumentException(cls.getName() + " generic type undefined!", e);
}
}
use of java.lang.reflect.GenericArrayType in project gradle by gradle.
the class AsmBackedClassGeneratorTest method includesGenericTypeInformationForOverriddenConstructor.
@Test
public void includesGenericTypeInformationForOverriddenConstructor() throws Exception {
Class<?> generatedClass = generator.generate(BeanWithComplexConstructor.class);
Constructor<?> constructor = generatedClass.getDeclaredConstructors()[0];
assertThat(constructor.getTypeParameters().length, equalTo(3));
assertThat(constructor.getGenericParameterTypes().length, equalTo(12));
// Callable
Type paramType = constructor.getGenericParameterTypes()[0];
assertThat(paramType, equalTo((Type) Callable.class));
// Callable<String>
paramType = constructor.getGenericParameterTypes()[1];
assertThat(paramType, instanceOf(ParameterizedType.class));
ParameterizedType parameterizedType = (ParameterizedType) paramType;
assertThat(parameterizedType.getRawType(), equalTo((Type) Callable.class));
assertThat(parameterizedType.getActualTypeArguments()[0], equalTo((Type) String.class));
// Callable<? extends String>
paramType = constructor.getGenericParameterTypes()[2];
assertThat(paramType, instanceOf(ParameterizedType.class));
parameterizedType = (ParameterizedType) paramType;
assertThat(parameterizedType.getRawType(), equalTo((Type) Callable.class));
assertThat(parameterizedType.getActualTypeArguments()[0], instanceOf(WildcardType.class));
WildcardType wildcard = (WildcardType) parameterizedType.getActualTypeArguments()[0];
assertThat(wildcard.getUpperBounds().length, equalTo(1));
assertThat(wildcard.getUpperBounds()[0], equalTo((Type) String.class));
assertThat(wildcard.getLowerBounds().length, equalTo(0));
// Callable<? super String>
paramType = constructor.getGenericParameterTypes()[3];
assertThat(paramType, instanceOf(ParameterizedType.class));
parameterizedType = (ParameterizedType) paramType;
assertThat(parameterizedType.getRawType(), equalTo((Type) Callable.class));
assertThat(parameterizedType.getActualTypeArguments()[0], instanceOf(WildcardType.class));
wildcard = (WildcardType) parameterizedType.getActualTypeArguments()[0];
assertThat(wildcard.getUpperBounds().length, equalTo(1));
assertThat(wildcard.getUpperBounds()[0], equalTo((Type) Object.class));
assertThat(wildcard.getLowerBounds().length, equalTo(1));
assertThat(wildcard.getLowerBounds()[0], equalTo((Type) String.class));
// Callable<?>
paramType = constructor.getGenericParameterTypes()[4];
assertThat(paramType, instanceOf(ParameterizedType.class));
parameterizedType = (ParameterizedType) paramType;
assertThat(parameterizedType.getRawType(), equalTo((Type) Callable.class));
assertThat(parameterizedType.getActualTypeArguments()[0], instanceOf(WildcardType.class));
wildcard = (WildcardType) parameterizedType.getActualTypeArguments()[0];
assertThat(wildcard.getUpperBounds().length, equalTo(1));
assertThat(wildcard.getUpperBounds()[0], equalTo((Type) Object.class));
assertThat(wildcard.getLowerBounds().length, equalTo(0));
// Callable<? extends Callable<?>>
paramType = constructor.getGenericParameterTypes()[5];
assertThat(paramType, instanceOf(ParameterizedType.class));
parameterizedType = (ParameterizedType) paramType;
assertThat(parameterizedType.getRawType(), equalTo((Type) Callable.class));
assertThat(parameterizedType.getActualTypeArguments()[0], instanceOf(WildcardType.class));
wildcard = (WildcardType) parameterizedType.getActualTypeArguments()[0];
assertThat(wildcard.getUpperBounds().length, equalTo(1));
assertThat(wildcard.getLowerBounds().length, equalTo(0));
assertThat(wildcard.getUpperBounds()[0], instanceOf(ParameterizedType.class));
parameterizedType = (ParameterizedType) wildcard.getUpperBounds()[0];
assertThat(parameterizedType.getRawType(), equalTo((Type) Callable.class));
assertThat(parameterizedType.getActualTypeArguments()[0], instanceOf(WildcardType.class));
wildcard = (WildcardType) parameterizedType.getActualTypeArguments()[0];
assertThat(wildcard.getUpperBounds().length, equalTo(1));
assertThat(wildcard.getUpperBounds()[0], equalTo((Type) Object.class));
assertThat(wildcard.getLowerBounds().length, equalTo(0));
// Callable<S>
paramType = constructor.getGenericParameterTypes()[6];
assertThat(paramType, instanceOf(ParameterizedType.class));
parameterizedType = (ParameterizedType) paramType;
assertThat(parameterizedType.getRawType(), equalTo((Type) Callable.class));
assertThat(parameterizedType.getActualTypeArguments()[0], instanceOf(TypeVariable.class));
TypeVariable typeVariable = (TypeVariable) parameterizedType.getActualTypeArguments()[0];
assertThat(typeVariable.getName(), equalTo("S"));
assertThat(typeVariable.getBounds()[0], instanceOf(ParameterizedType.class));
// Callable<? extends T>
paramType = constructor.getGenericParameterTypes()[7];
assertThat(paramType, instanceOf(ParameterizedType.class));
parameterizedType = (ParameterizedType) paramType;
assertThat(parameterizedType.getRawType(), equalTo((Type) Callable.class));
assertThat(parameterizedType.getActualTypeArguments()[0], instanceOf(WildcardType.class));
wildcard = (WildcardType) parameterizedType.getActualTypeArguments()[0];
assertThat(wildcard.getUpperBounds().length, equalTo(1));
assertThat(wildcard.getLowerBounds().length, equalTo(0));
assertThat(wildcard.getUpperBounds()[0], instanceOf(TypeVariable.class));
typeVariable = (TypeVariable) wildcard.getUpperBounds()[0];
assertThat(typeVariable.getName(), equalTo("T"));
assertThat(typeVariable.getBounds()[0], equalTo((Type) IOException.class));
// V
paramType = constructor.getGenericParameterTypes()[8];
assertThat(paramType, instanceOf(TypeVariable.class));
typeVariable = (TypeVariable) paramType;
assertThat(typeVariable.getName(), equalTo("V"));
assertThat(typeVariable.getBounds()[0], equalTo((Type) Object.class));
GenericArrayType arrayType;
// String[]
paramType = constructor.getGenericParameterTypes()[9];
assertThat(paramType, equalTo((Type) String[].class));
assertThat(((Class<?>) paramType).getComponentType(), equalTo((Type) String.class));
// List<? extends String>[]
paramType = constructor.getGenericParameterTypes()[10];
assertThat(paramType, instanceOf(GenericArrayType.class));
arrayType = (GenericArrayType) paramType;
assertThat(arrayType.getGenericComponentType(), instanceOf(ParameterizedType.class));
parameterizedType = (ParameterizedType) arrayType.getGenericComponentType();
assertThat(parameterizedType.getRawType(), equalTo((Type) List.class));
assertThat(parameterizedType.getActualTypeArguments().length, equalTo(1));
assertThat(parameterizedType.getActualTypeArguments()[0], instanceOf(WildcardType.class));
// boolean
paramType = constructor.getGenericParameterTypes()[11];
assertThat(paramType, equalTo((Type) Boolean.TYPE));
assertThat(constructor.getGenericExceptionTypes().length, equalTo(2));
// throws Exception
Type exceptionType = constructor.getGenericExceptionTypes()[0];
assertThat(exceptionType, equalTo((Type) Exception.class));
// throws T
exceptionType = constructor.getGenericExceptionTypes()[1];
assertThat(exceptionType, instanceOf(TypeVariable.class));
typeVariable = (TypeVariable) exceptionType;
assertThat(typeVariable.getName(), equalTo("T"));
}
use of java.lang.reflect.GenericArrayType in project cxf by apache.
the class ASMHelper method getClassCode.
public static String getClassCode(java.lang.reflect.Type type) {
if (type instanceof Class) {
return getClassCode((Class<?>) type);
} else if (type instanceof GenericArrayType) {
GenericArrayType at = (GenericArrayType) type;
return "[" + getClassCode(at.getGenericComponentType());
} else if (type instanceof TypeVariable) {
TypeVariable<?> tv = (TypeVariable<?>) type;
java.lang.reflect.Type[] bounds = tv.getBounds();
if (bounds != null && bounds.length == 1) {
return getClassCode(bounds[0]);
}
throw new IllegalArgumentException("Unable to determine type for: " + tv);
} else if (type instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType) type;
StringBuilder a = new StringBuilder(getClassCode(pt.getRawType()));
if (!pt.getRawType().equals(Enum.class)) {
a.setLength(a.length() - 1);
a.append('<');
for (java.lang.reflect.Type t : pt.getActualTypeArguments()) {
a.append(getClassCode(t));
}
a.append(">;");
}
return a.toString();
} else if (type instanceof WildcardType) {
WildcardType wt = (WildcardType) type;
StringBuilder a = new StringBuilder();
java.lang.reflect.Type[] lowBounds = wt.getLowerBounds();
java.lang.reflect.Type[] upBounds = wt.getUpperBounds();
for (java.lang.reflect.Type t : upBounds) {
a.append("+");
a.append(getClassCode(t));
}
for (java.lang.reflect.Type t : lowBounds) {
a.append("-");
a.append(getClassCode(t));
}
return a.toString();
}
return null;
}
Aggregations