use of java.lang.reflect.WildcardType in project commons-lang by apache.
the class AClass method testUnboundedWildcardType.
@Test
public void testUnboundedWildcardType() {
final WildcardType unbounded = TypeUtils.wildcardType().withLowerBounds((Type) null).withUpperBounds().build();
assertTrue(TypeUtils.equals(TypeUtils.WILDCARD_ALL, unbounded));
assertArrayEquals(new Type[] { Object.class }, TypeUtils.getImplicitUpperBounds(unbounded));
assertArrayEquals(new Type[] { null }, TypeUtils.getImplicitLowerBounds(unbounded));
assertEquals("?", TypeUtils.toString(unbounded));
assertEquals("?", unbounded.toString());
}
use of java.lang.reflect.WildcardType in project commons-lang by apache.
the class AClass method testWildcardType.
@Test
public void testWildcardType() throws Exception {
final WildcardType simpleWildcard = TypeUtils.wildcardType().withUpperBounds(String.class).build();
final Field cClass = AClass.class.getField("cClass");
assertTrue(TypeUtils.equals(((ParameterizedType) cClass.getGenericType()).getActualTypeArguments()[0], simpleWildcard));
assertEquals(String.format("? extends %s", String.class.getName()), TypeUtils.toString(simpleWildcard));
assertEquals(String.format("? extends %s", String.class.getName()), simpleWildcard.toString());
}
use of java.lang.reflect.WildcardType in project google-gin by gwtplus.
the class ReflectUtil method getSourceName.
/**
* Returns a string representation of the passed type's name while ensuring
* that all type names (base and parameters) are converted to source type
* names.
*
* @param type type for which string will be returned
* @return String representation of type
* @throws NoSourceNameException if source name is not available for type
*/
public static String getSourceName(Type type) throws NoSourceNameException {
if (type instanceof Class<?>) {
Class<?> clazz = (Class<?>) type;
if (clazz.isPrimitive()) {
// Returns "int" for integer etc.
return clazz.getName();
}
String name = clazz.getCanonicalName();
// have source names.
if (name == null) {
throw new NoSourceNameException(type);
}
return name;
}
if (type instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) type;
Type[] arguments = parameterizedType.getActualTypeArguments();
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(getSourceName(parameterizedType.getRawType()));
if (arguments.length == 0) {
return stringBuilder.toString();
}
stringBuilder.append("<").append(getSourceName(arguments[0]));
for (int i = 1; i < arguments.length; i++) {
stringBuilder.append(", ").append(getSourceName(arguments[i]));
}
return stringBuilder.append(">").toString();
}
if (type instanceof GenericArrayType) {
return getSourceName(((GenericArrayType) type).getGenericComponentType()) + "[]";
}
if (type instanceof WildcardType) {
Type[] lowerBounds = ((WildcardType) type).getLowerBounds();
Type[] upperBounds = ((WildcardType) type).getUpperBounds();
if (lowerBounds.length > 0 && upperBounds.length > 0 && lowerBounds.length != 1 && lowerBounds[0] != Object.class) {
throw new NoSourceNameException(type);
}
if (lowerBounds.length > 0) {
return getBoundedSourceName("?", lowerBounds, "super");
} else if (upperBounds.length == 1 && upperBounds[0] == Object.class) {
return "?";
} else {
return getBoundedSourceName("?", upperBounds, "extends");
}
}
// definition thereof. For the latter see #getTypeVariableDefinition(..).
if (type instanceof TypeVariable) {
return ((TypeVariable) type).getName();
}
throw new NoSourceNameException(type);
}
use of java.lang.reflect.WildcardType in project guice by google.
the class MoreTypes 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 Objects.equal(pa.getOwnerType(), pb.getOwnerType()) && pa.getRawType().equals(pb.getRawType()) && Arrays.equals(getSharedTypeArguments(pa), getSharedTypeArguments(pb));
} 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().equals(vb.getGenericDeclaration()) && va.getName().equals(vb.getName());
} else {
// This isn't a type we support. Could be a generic array type, wildcard type, etc.
return false;
}
}
use of java.lang.reflect.WildcardType in project guice by google.
the class MoreTypesTest method testGetRawType_wildcard.
public <T> void testGetRawType_wildcard() throws Exception {
WildcardType wildcard = (WildcardType) ((ParameterizedType) new TypeLiteral<List<?>>() {
}.getType()).getActualTypeArguments()[0];
assertEquals(Object.class, MoreTypes.getRawType(wildcard));
}
Aggregations