Package org.eclipse.yasson.internal
Class ReflectionUtils
- java.lang.Object
-
- org.eclipse.yasson.internal.ReflectionUtils
-
public class ReflectionUtils extends java.lang.ObjectUtility class for resolution of generics during unmarshalling.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> TcreateNoArgConstructorInstance(java.lang.reflect.Constructor<T> constructor)Create instance with constructor.static java.lang.reflect.ParameterizedTypefindParameterizedType(java.lang.Class<?> classToSearch, java.lang.Class<?> parameterizedInterface)For generic adapters like:static <T> java.lang.reflect.Constructor<T>getDefaultConstructor(java.lang.Class<T> clazz, boolean required)Get default no argument constructor of the class.static java.util.Optional<java.lang.Class<?>>getOptionalRawType(java.lang.reflect.Type type)Get raw type by type.static java.lang.Class<?>getRawType(java.lang.reflect.Type type)Get raw type by type.static booleanisResolvedType(java.lang.reflect.Type type)Check if type needs resolution.static java.util.Optional<java.lang.reflect.Type>resolveOptionalType(RuntimeTypeInfo info, java.lang.reflect.Type type)Resolves type by item information and wraps it withOptional.static java.lang.Class<?>resolveRawType(RuntimeTypeInfo item, java.lang.reflect.Type type)Get a raw type of any type.static java.lang.reflect.TyperesolveType(RuntimeTypeInfo item, java.lang.reflect.Type type)Resolve a type by item.static java.lang.reflect.TyperesolveTypeArguments(java.lang.reflect.ParameterizedType typeToResolve, java.lang.reflect.Type typeToSearch)ResolvesTypeVariablearguments of generic types.
-
-
-
Method Detail
-
getOptionalRawType
public static java.util.Optional<java.lang.Class<?>> getOptionalRawType(java.lang.reflect.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 java.lang.Class<?> getRawType(java.lang.reflect.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 java.lang.Class<?> resolveRawType(RuntimeTypeInfo item, java.lang.reflect.Type type)
Get a raw type of any type. If type is aTypeVariablerecursively searchAbstractItemfor resolution of typevar. If type is aWildcardTypefind 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 java.lang.reflect.Type resolveType(RuntimeTypeInfo item, java.lang.reflect.Type type)
Resolve a type by item. If type is aTypeVariablerecursively searchAbstractItemfor resolution of typevar. If type is aWildcardTypefind 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 java.util.Optional<java.lang.reflect.Type> resolveOptionalType(RuntimeTypeInfo info, java.lang.reflect.Type type)
Resolves type by item information and wraps it withOptional.- Parameters:
info- item informationtype- type- Returns:
- resolved type wrapped with Optional
-
resolveTypeArguments
public static java.lang.reflect.Type resolveTypeArguments(java.lang.reflect.ParameterizedType typeToResolve, java.lang.reflect.Type typeToSearch)ResolvesTypeVariablearguments of generic types.- Parameters:
typeToResolve- type to resolvetypeToSearch- type to search- Returns:
- resolved type
-
createNoArgConstructorInstance
public static <T> T createNoArgConstructorInstance(java.lang.reflect.Constructor<T> constructor)
Create instance with constructor.- Type Parameters:
T- type of instance- Parameters:
constructor- const not null- Returns:
- instance
-
getDefaultConstructor
public static <T> java.lang.reflect.Constructor<T> getDefaultConstructor(java.lang.Class<T> clazz, boolean required)Get default no argument constructor of the class.- Type Parameters:
T- Class generic type- Parameters:
clazz- Class to get constructor fromrequired- if true, throws an exception if the default constructor is missing. If false, returns null in that case- Returns:
- the constructor of the class, or null. Depending on required.
-
findParameterizedType
public static java.lang.reflect.ParameterizedType findParameterizedType(java.lang.Class<?> classToSearch, java.lang.Class<?> parameterizedInterface)For generic adapters like:
We need to find a JsonbAdapter class which will hold basic generic type arguments, and resolve them if they are TypeVariables from there.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(java.lang.reflect.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
-
-