use of java.lang.reflect.TypeVariable 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.TypeVariable 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.TypeVariable 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;
}
use of java.lang.reflect.TypeVariable 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.TypeVariable in project cxf by apache.
the class JAXBContextInitializer method shouldTypeBeAdded.
private boolean shouldTypeBeAdded(final Type t2, final ParameterizedType parameterizedType) {
if (!(t2 instanceof TypeVariable)) {
return true;
}
TypeVariable<?> typeVariable = (TypeVariable<?>) t2;
final Type[] bounds = typeVariable.getBounds();
for (Type bound : bounds) {
if (bound instanceof ParameterizedType && bound.equals(parameterizedType)) {
return false;
}
}
return true;
}
Aggregations