Package org.eclipse.yasson.internal
Class ReflectionUtils
- java.lang.Object
-
- org.eclipse.yasson.internal.ReflectionUtils
-
public class ReflectionUtils extends Object
Utility class for resolution of generics during unmarshalling.- Author:
- Roman Grigoriadi
-
-
Constructor Summary
Constructors Constructor Description ReflectionUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> T
createNoArgConstructorInstance(Class<T> clazz)
Search for no argument constructor of a class and create instance.static ParameterizedType
findParameterizedType(Class<?> classToSearch, Class<?> parameterizedInterface)
For generic adapters like:static Optional<Class<?>>
getOptionalRawType(Type type)
Get raw type by type.static Class<?>
getRawType(Type type)
Get raw type by type.static boolean
isResolvedType(Type type)
Check if type needs resolution.static Type
resolveItemVariableType(RuntimeTypeInfo item, TypeVariable<?> typeVariable)
Resolve a bounded type variable type by its wrapper types.static Optional<Type>
resolveOptionalType(RuntimeTypeInfo info, Type type)
static Class<?>
resolveRawType(RuntimeTypeInfo item, Type type)
Get a raw type of any type.static Type
resolveType(RuntimeTypeInfo item, Type type)
Resolve a type by item.static Type
resolveTypeArguments(ParameterizedType typeToResolve, Type typeToSearch)
ResolvesTypeVariable
arguments of generic types.
-
-
-
Method Detail
-
getOptionalRawType
public static Optional<Class<?>> getOptionalRawType(Type type)
Get raw type by type. Only for ParametrizedTypes, GenericArrayTypes and Classes. Empty optional is returned if raw type cannot be resolved.- Parameters:
type
- Type to get class information from, not null.- Returns:
- Class of a type.
-
getRawType
public static Class<?> getRawType(Type type)
Get raw type by type. Resolves only ParametrizedTypes, GenericArrayTypes and Classes. Exception is thrown if raw type cannot be resolved.- Parameters:
type
- Type to get class information from, not null.- Returns:
- Class of a raw type.
-
resolveRawType
public static Class<?> resolveRawType(RuntimeTypeInfo item, Type type)
Get a raw type of any type. If type is aTypeVariable
recursively searchAbstractItem
for resolution of typevar. If type is aWildcardType
find most specific upper / lower bound, which can be used. If most specific bound is aTypeVariable
, perform typevar resolution.- Parameters:
item
- item containing wrapper class of a type field, not null.type
- type to resolve, typically field type or generic bound, not null.- Returns:
- resolved raw class
-
resolveType
public static Type resolveType(RuntimeTypeInfo item, Type type)
Resolve a type by item. If type is aTypeVariable
recursively searchAbstractItem
for resolution of typevar. If type is aWildcardType
find most specific upper / lower bound, which can be used. If most specific bound is aTypeVariable
, perform typevar resolution.- Parameters:
item
- item containing wrapper class of a type field, not null.type
- type to resolve, typically field type or generic bound, not null.- Returns:
- resolved type
-
resolveOptionalType
public static Optional<Type> resolveOptionalType(RuntimeTypeInfo info, Type type)
-
resolveItemVariableType
public static Type resolveItemVariableType(RuntimeTypeInfo item, TypeVariable<?> typeVariable)
Resolve a bounded type variable type by its wrapper types. Resolution could be done only if a compile time generic information is provided, either: by generic field or subclass of a generic class.- Parameters:
item
- item to search "runtime" generic type of a TypeVariable.typeVariable
- type to search in item for, not null.- Returns:
- Type of a generic "runtime" bound, not null.
-
resolveTypeArguments
public static Type resolveTypeArguments(ParameterizedType typeToResolve, Type typeToSearch)
ResolvesTypeVariable
arguments of generic types.- Parameters:
typeToResolve
- type to resolvetypeToSearch
- type to search- Returns:
- resolved type
-
createNoArgConstructorInstance
public static <T> T createNoArgConstructorInstance(Class<T> clazz)
Search for no argument constructor of a class and create instance.- Type Parameters:
T
- type of instance- Parameters:
clazz
- not null- Returns:
- instance
-
findParameterizedType
public static ParameterizedType findParameterizedType(Class<?> classToSearch, Class<?> parameterizedInterface)
For generic adapters like:interface ContainerAdapter<T> extends JsonbAdapter<Box<T>, Crate<T>>...; class IntegerBoxToCrateAdapter implements ContainerAdapter<Integer>...;
- Parameters:
classToSearch
- class to resolve parameterized interfaceparameterizedInterface
- interface to search- Returns:
- type of JsonbAdapter
-
isResolvedType
public static boolean isResolvedType(Type type)
Check if type needs resolution. If type is a class or a parametrized type with all type arguments as classes than it is considered resolved. If any of types is type variable or wildcard type is not resolved.- Parameters:
type
- Type to check.- Returns:
- True if resolved
-
-