Package org.atlanmod.commons.reflect
Class MoreReflection
- java.lang.Object
-
- org.atlanmod.commons.reflect.MoreReflection
-
@ParametersAreNonnullByDefault public final class MoreReflection extends Object
Static utility methods related to reflection.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Optional<Constructor>findConstructor(Class type, Class[] argumentTypes)Returns the constructor for creating a new instance oftype, whose parameters are assignable with theargumentTypes.static Optional<Method>findFactoryMethod(Class type, Class[] argumentTypes)Returns the static factory method for creating instances oftype, whose parameters are assignable with theargumentTypes.static Set<Class<?>>getAllInterfaces(Class<?> type)Retrieves all interfaces implemented by the given class and its superclasses.static <T> Function<Object[],T>getInstantiator(Class<T> type, Class[] argumentTypes)Returns aFunctionthat creates new instances oftype, using as arguments instances ofargumentTypes, i.e., using a constructor or factory method whose signature matchesargumentTypes.static booleanisAssignable(Class<?> leftOperand, Class<?> rightOperand)Checks whether a left operand type can be assigned from the right operand type If the operand is a primitive type, first converts it to its wrapper class.static booleanmatches(Executable executable, Class[] argumentTypes)Checks whether anExecutable(ConstructororMethod) is compatible with theargumentTypes, i.e., whether all parameters types are assignable from all types ofargumentTypes, respectively.static <T> TnewInstance(Class<T> type)Gets or creates a instance of the giventypeby using the static method namedname.
-
-
-
Method Detail
-
newInstance
@Nonnull public static <T> T newInstance(Class<T> type)
Gets or creates a instance of the giventypeby using the static method namedname.If the
typeis annotated bySingletonor byBuilder, then the static method identified by the value of the annotation is used to get the instance. Otherwise, the default constructor is used.- Type Parameters:
T- the type of the instance- Parameters:
type- the class to look for- Returns:
- the single instance if the
typeis a singleton, or a new instance - Throws:
ReflectionException- if an error occurs during the instantiation
-
getAllInterfaces
@Nonnull public static Set<Class<?>> getAllInterfaces(Class<?> type)
Retrieves all interfaces implemented by the given class and its superclasses.The order is determined by looking through each interface in turn as declared in the source file and following its hierarchy up. Then each superclass is considered in the same way. Later duplicates are ignored, so the order is maintained.
- Parameters:
type- the class to look up- Returns:
- a
Setof interfaces in order
-
findConstructor
public static Optional<Constructor> findConstructor(Class type, Class[] argumentTypes)
Returns the constructor for creating a new instance oftype, whose parameters are assignable with theargumentTypes.- Parameters:
type- the class to look forargumentTypes- a set of types allowing to find the suitable constructor.- Returns:
- an
Optionalcontaining the suitable constructor, orOptional.empty()if no constructor is found.
-
findFactoryMethod
public static Optional<Method> findFactoryMethod(Class type, Class[] argumentTypes)
Returns the static factory method for creating instances oftype, whose parameters are assignable with theargumentTypes.- Parameters:
type- the class to look for.argumentTypes- a set of types allowing to find the suitable method.- Returns:
- an
Optionalcontaining the suitable factory method, orOptional.empty()if no constructor is found.
-
getInstantiator
public static <T> Function<Object[],T> getInstantiator(Class<T> type, Class[] argumentTypes)
Returns aFunctionthat creates new instances oftype, using as arguments instances ofargumentTypes, i.e., using a constructor or factory method whose signature matchesargumentTypes.- Parameters:
type- the class to look for.argumentTypes- the signature of the suitable constructor or method- Returns:
- a
Functionthat creates new instances oftype, using either aConstructoror a static factoryMethod.
-
matches
public static boolean matches(Executable executable, Class[] argumentTypes)
Checks whether anExecutable(ConstructororMethod) is compatible with theargumentTypes, i.e., whether all parameters types are assignable from all types ofargumentTypes, respectively. TheargumentTypesparameter is an array ofClassobjects that are assignment-compatible with the executable's formal parameter types, in declared order.- Parameters:
executable- the instance ofExecutableto be considered.argumentTypes- an array of types to be compared with the executable parameters.- Returns:
- true, if each parameter of
executableis assignable from an element ofargumentTypes.
-
isAssignable
public static boolean isAssignable(Class<?> leftOperand, Class<?> rightOperand)
Checks whether a left operand type can be assigned from the right operand type If the operand is a primitive type, first converts it to its wrapper class. Contrary to theClass.isAssignableFrom(java.lang.Class)method, this method takes ont account the assignment between Wrappers Classes and Primitive Types.- Parameters:
leftOperand- The Class or Primitive we want to assign intorightOperand- The Class or Primitive we want to assign- Returns:
true, if assignment is possible
-
-