Utility methods for Types manipulation.
Type
allows to represent parameterized type at runtime which is not
possible with a regular Class
due to type erasure. This can be useful
especially when one needs to perform reflective operation at runtime.
A TypeBuilder
makes it easy to create types, for instance:
Type listOfStringType = Types.type(List.class).type(String.class).and().build();
- Since:
- 1.0
- Author:
- Jeremy Kuhn
- See Also:
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic ArrayTypeBuilder
Creates an array type builder.static Class
<?> Returns the boxed type corresponding to the specified primitive type.static boolean
isAssignable
(Type source, Type target) Determines whether a type can be assignable to another.static Class
<?> Determines the raw type of the specified type.static TypeBuilder
Creates a type builder with the specified raw type.static Type
typeAsMemberOf
(Type containing, AccessibleObject accessor) Returns the type of an accessible object when viewed as a member of, or otherwise directly contained by, a given type.static Type
typeAsMemberOf
(Type containing, Field field) Returns the type of a field when viewed as a member of, or otherwise directly contained by, a given type.static Type
typeAsMemberOf
(Type containing, Method method) Returns the type of a generic method return type when viewed as a member of, or otherwise directly contained by, a given type.static Class
<?> Returns the primitive type corresponding to the specified boxed type.
-
Constructor Details
-
Types
public Types()
-
-
Method Details
-
boxType
Returns the boxed type corresponding to the specified primitive type.
- Parameters:
type
- a primitive type- Returns:
- a boxed type
- Throws:
IllegalArgumentException
- if the specified type is not a primitive type
-
unboxType
Returns the primitive type corresponding to the specified boxed type.
- Parameters:
type
- a boxed type- Returns:
- a primitive type
- Throws:
IllegalArgumentException
- if the specified type is not a boxed type
-
type
Creates a type builder with the specified raw type.
- Parameters:
rawType
- an erased type- Returns:
- a type builder
-
arrayType
Creates an array type builder.
- Returns:
- an array type builder
-
toClass
Determines the raw type of the specified type.
- Parameters:
type
- a type- Returns:
- a raw type
- Throws:
IllegalArgumentException
- if it is not possible to determine a raw type from the specified type
-
typeAsMemberOf
public static Type typeAsMemberOf(Type containing, AccessibleObject accessor) throws IllegalArgumentException Returns the type of an accessible object when viewed as a member of, or otherwise directly contained by, a given type.
- Parameters:
containing
- the containing typeaccessor
- the accessible object- Returns:
- a type
- Throws:
IllegalArgumentException
- if the specified accessor is neither a field nor a method or if there are missing type arguments in the containing type
-
typeAsMemberOf
Returns the type of a generic method return type when viewed as a member of, or otherwise directly contained by, a given type.
- Parameters:
containing
- the containing typemethod
- the method- Returns:
- a type
- Throws:
IllegalArgumentException
- if there are missing type arguments in the containing type
-
typeAsMemberOf
Returns the type of a field when viewed as a member of, or otherwise directly contained by, a given type.
- Parameters:
containing
- the containing typefield
- the field- Returns:
- a type
- Throws:
IllegalArgumentException
- if there are missing type arguments in the containing type
-
isAssignable
Determines whether a type can be assignable to another.
When a type is assignable to another, it can be implicitly casted to the other one following the Java generics rules.
- Parameters:
source
- a source typetarget
- a target type- Returns:
- true if the source type is assignable to the target type
-