public final class TypeConstraint
extends java.lang.Object
| Constructor and Description |
|---|
TypeConstraint(java.lang.reflect.Type fullType)
Creates a new TypeConstraint from a
Type. |
| Modifier and Type | Method and Description |
|---|---|
java.lang.reflect.Type |
getFullType()
Returns a
Type that represents the full type constraint. |
java.util.Optional<java.lang.Class<?>> |
getSatisfyingRawType()
Returns a
Class that can satisfy the type constraint, if it can be found. |
java.util.Optional<TypeConstraint[]> |
resolveTypeArgumentsFor(java.lang.Class<?> classToFind)
Inspects this type constraint and attempts to resolve the type arguments that have been applied to the
given class, inside of the constraint.
|
java.lang.String |
toString() |
public TypeConstraint(java.lang.reflect.Type fullType)
Type.fullType - the type to matchpublic java.lang.reflect.Type getFullType()
Type that represents the full type constraint.
For instance, it can return a WildcardType with lower and/or upper bounds, like
? extends Bound,
or a parameterized type with generic type arguments like Map<String, ? extends Iterable<Object>>.
public java.util.Optional<java.lang.Class<?>> getSatisfyingRawType()
Class that can satisfy the type constraint, if it can be found.
If no such class can be found, an empty optional is returned.
An empty optional can be returned if the type constraint is a wildcard type or a type variable with
multiple bounds, such as ? super B1 extends B2 or T extends B1 & B2, in which case it's not
enough to select a bound, one must find a satisfying intersection.
It would be difficult to know every possible class, and there could be multiple solutions anyway.
public java.util.Optional<TypeConstraint[]> resolveTypeArgumentsFor(java.lang.Class<?> classToFind)
For instance, given a class like this:
class Cls extends MyCollection<String> {}
class MyCollection implements Collection<String> {}
You can expect the following result:
TypeConstraint t = new TypeConstraint(Cls.class);
Optional<TypeConstraint[]> args = t.resolveTypeArgumentsFor(Collection.class);
Type collectionValueType = args.get()[0].getFullType();
assertEquals(String.class, collectionValueType);
It can also resolve type variables used in field declarations.
class Cls<T extends Collection<String> & OtherBound> {
T field;
}
Field f = Cls.class.getDeclaredField("field");
TypeConstraint t = new TypeConstraint(f.getGenericType());
Optional<TypeConstraint[]> args = t.resolveTypeArgumentsFor(Collection.class);
Type collectionValueType = args.get()[0].getFullType();
assertEquals(String.class, collectionValueType);
classToFind - the class to findpublic java.lang.String toString()
toString in class java.lang.Object