@ParametersAreNonnullByDefault public final class MoreReflection extends Object
| Modifier and Type | Method and Description |
|---|---|
static Optional<Constructor> |
findConstructor(Class type,
Class[] argumentTypes)
Returns the constructor for creating a new instance of
type, whose parameters are assignable
with the argumentTypes. |
static Optional<Method> |
findFactoryMethod(Class type,
Class[] argumentTypes)
Returns the static factory method for creating instances of
type, whose parameters are
assignable with the argumentTypes. |
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 a
Function that creates new instances of type, using as arguments instances of
argumentTypes, i.e., using a constructor or factory method whose signature matches argumentTypes. |
static boolean |
matches(Executable executable,
Class[] argumentTypes)
Checks whether an
Executable (Constructor or Method) is compatible with
the argumentTypes, i.e., whether all parameters types are assignable from all types of
argumentTypes, respectively. |
static <T> T |
newInstance(Class<T> type)
Gets or creates a instance of the given
type by using the static method named name. |
@Nonnull public static <T> T newInstance(Class<T> type)
type by using the static method named name.
If the type is annotated by Singleton or by Builder, then the static method identified by
the value of the annotation is used to get the instance. Otherwise, the default constructor is used.
T - the type of the instancetype - the class to look fortype is a singleton, or a new instanceReflectionException - if an error occurs during the instantiation@Nonnull public static Set<Class<?>> getAllInterfaces(Class<?> type)
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.
type - the class to look upSet of interfaces in orderpublic static Optional<Constructor> findConstructor(Class type, Class[] argumentTypes)
type, whose parameters are assignable
with the argumentTypes.type - the class to look forargumentTypes - a set of types allowing to find the suitable constructor.Optional containing the suitable constructor, or Optional.empty() if no constructor
is found.public static Optional<Method> findFactoryMethod(Class type, Class[] argumentTypes)
type, whose parameters are
assignable with the argumentTypes.type - the class to look for.argumentTypes - a set of types allowing to find the suitable method.Optional containing the suitable factory method, or Optional.empty()
if no constructor is found.public static <T> Function<Object[],T> getInstantiator(Class<T> type, Class[] argumentTypes)
Function that creates new instances of type, using as arguments instances of
argumentTypes, i.e., using a constructor or factory method whose signature matches argumentTypes.type - the class to look for.argumentTypes - the signature of the suitable constructor or methodFunction that creates new instances of type, using either a Constructor or
a static factory Method.public static boolean matches(Executable executable, Class[] argumentTypes)
Executable (Constructor or Method) is compatible with
the argumentTypes, i.e., whether all parameters types are assignable from all types of
argumentTypes, respectively.
The argumentTypes parameter is an array of Class objects that are assignment-compatible
with the executable's formal parameter types, in declared order.executable - the instance of Executable to be considered.argumentTypes - an array of types to be compared with the executable parameters.executable is assignable from an element of argumentTypes.Copyright © 2017–2020 Atlanmod. All rights reserved.