Class Reflexion
Consider the following example: you need a class which can only access via it's name and need to get the value of a field in it. This can look like the following code when doing it using the following plain java.lang reflection:
public static String getHelloWorldFieldValue() {
try {
Class<?> class = Class.forName("test.HelloWorld");
Field field = class.getDeclaredField("helloWorld");
field.setAccessible(true);
return field.get(null);
} catch (ClassNotFoundException | IllegalAccessException ex) {
ex.printStackTrace();
return null;
}
}
With reflexion the same code looks like this:
public static String getHelloWorldFieldValue() {
return Reflexion.find("test.HelloWorld")
.flatMap(reflexion -> reflexion.findField("helloWorld"))
.map(accessor -> accessor.<String>getValue().getOrElse(null))
.orElse(null);
}
Or consider this example: you need to find a method in a class which has 2 parameters and the name of the method starts with "hello" and returns a string. With normal java.lang reflection this could look like this:
public static String getHelloWorldMethodValue() {
try {
for (Method method : HelloWorld.class.getDeclaredMethods()) {
if (method.getParameterCount() == 2
&& method.getReturnType().equals(String.class)
&& method.getName().matches("hello.*")) {
return method.invoke(null);
}
}
} catch (IllegalAccessException | InvocationTargetException ex) {
ex.printStackTrace();
return null;
}
}
With reflexion the same code looks like this:
public static String getHelloWorldMethodValue() {
String valueMethod = Reflexion.on(HelloWorld.class).findMethod(MethodMatcher.newMatcher()
.parameterCount(2)
.hasName("helloWorld)
.exactType(Method::getReturnType, String.class))
.map(accessor -> accessor.<String>invoke().getOrElse(null))
.orElse(null);
}
As probably seen by the methods shown above using reflexion over normal java.lang reflection is much more convenient. Furthermore, reflexion does everything for you as safe as possible while also providing the best performance to access class members. To archive that you should cache your obtained reflexion instances, as each of them will maintain a non-shared cache over all members in a class. A good practice is something like:
public static final Reflexion HELLO_WORLD_REFLEXION = Reflexion.on(HelloWorld.class);
Note that the member caches are only populated when they are actually needed, meaning that if you only query fields
from a reflexion instance no methods or constructors from the class will get fetched and vise-versa.- Since:
- 1.0
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final AccessorFactoryThe current used accessor factory to wrap fields, methods and constructors.private final AccessorFactoryprivate final @Nullable Objectprivate Set<Constructor<?>> private final Class<?> -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivateReflexion(@NotNull Class<?> wrappedClass, @Nullable Object binding, @NotNull AccessorFactory factory) Constructs a new reflexion instance. -
Method Summary
Modifier and TypeMethodDescription@NotNull ReflexionBinds the current reflexion instance to the given binding even when this instance is already bound to an instance.Tries to find and wrap a class with the given name in a reflexion instance.find(@NotNull String name, @Nullable ClassLoader loader) Tries to find and wrap a class with the given name in a reflexion instance.Tries to find and wrap a class with one of the given names.findAny(@Nullable ClassLoader loader, @NotNull String @NotNull ... names) Tries to find and wrap a class with one of the given names.@NotNull Optional<MethodAccessor<Constructor<?>>> findConstructor(@NotNull ConstructorMatcher matcher) Finds the first constructor matching the given matcher in the wrapped class and creates a method accessor wrapper for it.@NotNull Optional<MethodAccessor<Constructor<?>>> findConstructor(@NotNull Class<?>... paramTypes) Finds a constructor with the given parameter types in the wrapped class and creates a method accessor wrapper for it.@NotNull Collection<MethodAccessor<Constructor<?>>> findConstructors(@NotNull ConstructorMatcher matcher) Finds all constructors matching the given matcher in the wrapped class and creates a method accessor wrapper for it.@NotNull Optional<FieldAccessor> findField(@NotNull FieldMatcher matcher) Finds the first field which matches the given matcher.@NotNull Optional<FieldAccessor> Finds a field with the given name in the wrapped class and creates a field accessor wrapper for it.@NotNull Collection<FieldAccessor> findFields(@NotNull FieldMatcher matcher) Finds all fields which are matching the given matcher.@NotNull Optional<MethodAccessor<Method>> findMethod(@NotNull MethodMatcher matcher) Finds the first method which matches the given matcher in the wrapped class and creates a method accessor wrapper for it.@NotNull Optional<MethodAccessor<Method>> findMethod(@NotNull String name, @NotNull Class<?>... paramTypes) Finds a method with the given name and parameter types in the wrapped class and creates a method accessor wrapper for it.@NotNull Collection<MethodAccessor<Method>> findMethods(@NotNull MethodMatcher matcher) Finds all methods which are matching the given matcher in the wrapped class and creates a method accessor wrapper for each of them.static @NotNull ReflexionTries to find and wrap a class with the given name in a reflexion instance.static @NotNull Reflexionget(@NotNull String name, @Nullable ClassLoader loader) Tries to find and wrap a class with the given name in a reflexion instance.@NotNull AccessorFactoryGet the accessor factory which is associated with this reflexion instance.static @NotNull ReflexionTries to find and wrap a class with one of the given names.static @NotNull ReflexiongetAny(@Nullable ClassLoader loader, @NotNull String @NotNull ... names) Tries to find and wrap a class with one of the given names.@Nullable ObjectGets the object instance to which this reflexion instance is bound.private @NotNull Set<Constructor<?>> Internal: gets the constructor cache for the wrapped class.Internal: gets the field cache for the wrapped class.Internal: gets the method cache for the wrapped class.@NotNull Class<?> Gets the class which is wrapped by this reflexion instance.static @NotNull ReflexionConstructs a new reflexion instance wrapping and targeting the given class.static @NotNull ReflexionConstructs a new reflexion instance wrapping the given class and binds it to the given object instance, if any is provided.static @NotNull Reflexionon(@NotNull Class<?> clazz, @Nullable Object binding, @NotNull AccessorFactory factory) Constructs a new reflexion instance wrapping the given class and binds it to the given object instance, if any is provided.static @NotNull ReflexionConstructs a new reflexion instance wrapping the class of the given object instance.static @NotNull ReflexionConstructs a new reflexion instance wrapping the class of the given object instance and binds it to the given instance.@NotNull MethodAccessor<Constructor<?>> unreflect(@NotNull Constructor<?> constructor) Unreflects the given constructor and wraps it in a method accessor, while preserving the functionality of bindings and better abstraction.@NotNull FieldAccessorUnreflects the given field and wraps it in a field accessor, while preserving the functionality of bindings and better abstraction.@NotNull MethodAccessor<Method> Unreflects the given method and wraps it in a method accessor, while preserving the functionality of bindings and better abstraction.static @NotNull MethodAccessor<Constructor<?>> unreflectConstructor(@NotNull Constructor<?> constructor) Unreflects the given constructor and wraps it in a method accessor, while preserving the functionality of bindings and better abstraction.static @NotNull FieldAccessorunreflectField(@NotNull Field field) Unreflects the given field and wraps it in a field accessor, while preserving the functionality of bindings and better abstraction.static @NotNull MethodAccessor<Method> unreflectMethod(@NotNull Method method) Unreflects the given method and wraps it in a method accessor, while preserving the functionality of bindings and better abstraction.
-
Field Details
-
ACCESSOR_FACTORY
The current used accessor factory to wrap fields, methods and constructors. Initialized once when the class is first used. -
wrappedClass
-
accFactory
-
binding
-
fields
-
methods
-
constructors
-
-
Constructor Details
-
Reflexion
private Reflexion(@NotNull @NotNull Class<?> wrappedClass, @Nullable @Nullable Object binding, @NotNull @NotNull AccessorFactory factory) Constructs a new reflexion instance. Do not use this constructor directly refer to the static factory methods in this class (see the top-level documentation comment for more details).- Parameters:
wrappedClass- the class wrapped by the constructed reflexion instance.binding- the instance to which the reflection instance is bound, null for no binding.factory- the accessor factory to use when wrapping reflection objects.- Throws:
NullPointerException- if the given wrapped class is null.
-
-
Method Details
-
on
Constructs a new reflexion instance wrapping and targeting the given class. The method call produces the same result as callingReflexion.on(clazz, null).- Parameters:
clazz- the class to wrap in the reflexion object.- Returns:
- a new reflexion object wrapping the given class.
- Throws:
NullPointerException- if the given class to wrap is null.
-
on
Constructs a new reflexion instance wrapping the class of the given object instance. The method call will not bind the reflexion object to the given instance as is equivalent toReflexion.on(instance.getClass(), null).If you want a reflexion object which targets the class of the given instance as binds the reflexion instance directly to it use
Reflexion.onBound(instance)instead.- Parameters:
instance- the instance of the object to wrap the class of.- Returns:
- a new reflexion object wrapping the class of the given object instance.
- Throws:
NullPointerException- if the given instance is null.
-
onBound
Constructs a new reflexion instance wrapping the class of the given object instance and binds it to the given instance. This method call is equivalent toReflexion.on(instance.getClass(), instance).- Parameters:
instance- the instance of the object to wrap the class of and bind the reflexion instance to it.- Returns:
- a new reflexion object bound to the given instance and wrapping the class of it.
- Throws:
NullPointerException- if the given instance is null.
-
on
@NotNull public static @NotNull Reflexion on(@NotNull @NotNull Class<?> clazz, @Nullable @Nullable Object binding) Constructs a new reflexion instance wrapping the given class and binds it to the given object instance, if any is provided.- Parameters:
clazz- the class to wrap in the new reflexion object.binding- the instance of the object to bind the reflexion instance to, can be null for no binding.- Returns:
- a new reflexion instance wrapping the given class and optionally bound to the given instance.
- Throws:
NullPointerException- if the given class to wrap is null.
-
on
@NotNull public static @NotNull Reflexion on(@NotNull @NotNull Class<?> clazz, @Nullable @Nullable Object binding, @NotNull @NotNull AccessorFactory factory) Constructs a new reflexion instance wrapping the given class and binds it to the given object instance, if any is provided.- Parameters:
clazz- the class to wrap in the new reflexion object.binding- the instance of the object to bind the reflexion instance to, can be null for no binding.factory- the accessor factory to use when constructing accessor in the reflexion object.- Returns:
- a new reflexion instance wrapping the given class and optionally bound to the given instance.
- Throws:
NullPointerException- if the given class to wrap is null.
-
find
Tries to find and wrap a class with the given name in a reflexion instance. This method will make use of the first available class loader in the context, identical toReflexion.find(name, null)in the following order:- the thread context class loader.
- the reflexion class loader.
- the system class loader.
This method will not initialize the class when wrapping it.
- Parameters:
name- the name of the class to find.- Returns:
- an optional reflexion class object wrapping a class with the given name.
- Throws:
NullPointerException- if the given class name is null.
-
find
@NotNull public static @NotNull Optional<Reflexion> find(@NotNull @NotNull String name, @Nullable @Nullable ClassLoader loader) Tries to find and wrap a class with the given name in a reflexion instance. This method will make use of the first available class loader in the context if no explicit loader is given in the following order:- the thread context class loader.
- the reflexion class loader.
- the system class loader.
This method will not initialize the class when wrapping it.
- Parameters:
name- the name of the class to find.loader- the loader to search the class in, null to use the first available contextual loader.- Returns:
- an optional reflexion class object wrapping a class with the given name.
- Throws:
NullPointerException- if the given class name is null.
-
findAny
@NotNull public static @NotNull Optional<Reflexion> findAny(@NotNull @NotNull String @NotNull ... names) Tries to find and wrap a class with one of the given names. The order of the given name array will be the same as the search order. This method will make use of the first available class loader in the context in the following order:- the thread context class loader.
- the reflexion class loader.
- the system class loader.
This method will not initialize the class when wrapping it.
- Parameters:
names- the names of the class to search for.- Returns:
- an optional reflexion instance wrapping the first class which can be resolved from the given names.
- Throws:
NullPointerException- if the given name array or an element of it is null.
-
findAny
@NotNull public static @NotNull Optional<Reflexion> findAny(@Nullable @Nullable ClassLoader loader, @NotNull @NotNull String @NotNull ... names) Tries to find and wrap a class with one of the given names. The order of the given name array will be the same as the search order. This method will make use of the first available class loader in the context if no explicit loader is given in the following order:- the thread context class loader.
- the reflexion class loader.
- the system class loader.
This method will not initialize the class when wrapping it.
- Parameters:
loader- the loader to search the class in, null to use the first available contextual loader.names- the names of the class to search for.- Returns:
- an optional reflexion instance wrapping the first class which can be resolved from the given names.
- Throws:
NullPointerException- if the given name array or an element of it is null.
-
get
Tries to find and wrap a class with the given name in a reflexion instance. If no class with the given name is found an illegal argument exception is raised. If you want a method to find a class which does not raise an exception, consider usingfind(String, ClassLoader)instead. This method will make use of the first available class loader in the context in the following order:- the thread context class loader.
- the reflexion class loader.
- the system class loader.
This method will not initialize the class when wrapping it. If a specific class loader should be used to resolve the class, use
get(String, ClassLoader)instead.- Parameters:
name- the name of the class to find.- Returns:
- a reflexion class object wrapping a class with the given name.
- Throws:
NullPointerException- if the given class name is null.ReflexionException- if no class with the given name was found.- Since:
- 1.9.0
-
get
@NotNull public static @NotNull Reflexion get(@NotNull @NotNull String name, @Nullable @Nullable ClassLoader loader) Tries to find and wrap a class with the given name in a reflexion instance. If no class with the given name is found an illegal argument exception is raised. If you want a method to find a class which does not raise an exception, consider usingfind(String, ClassLoader)instead. This method will make use of the first available class loader in the context if no explicit loader is given in the following order:- the thread context class loader.
- the reflexion class loader.
- the system class loader.
This method will not initialize the class when wrapping it.
- Parameters:
name- the name of the class to find.loader- the loader to search the class in, null to use the first available contextual loader.- Returns:
- a reflexion class object wrapping a class with the given name.
- Throws:
NullPointerException- if the given class name is null.ReflexionException- if no class with the given name was found.
-
getAny
Tries to find and wrap a class with one of the given names. If no class with the given name is found an illegal argument exception is raised. If you want a method to find a class which does not raise an exception, consider usingfindAny(ClassLoader, String...)instead. The order of the given name array will be the same as the search order. This method will make use of the first available class loader in the context in the following order:- the thread context class loader.
- the reflexion class loader.
- the system class loader.
This method will not initialize the class when wrapping it. If a specific class loader should be used to resolve the class, use
getAny(ClassLoader, String...)instead.- Parameters:
names- the names of the class to search for.- Returns:
- a reflexion instance wrapping the first class which can be resolved from the given names.
- Throws:
NullPointerException- if the given name array or an element of it is null.ReflexionException- if no class with one of the given names could be found.- Since:
- 1.9.0
-
getAny
@NotNull public static @NotNull Reflexion getAny(@Nullable @Nullable ClassLoader loader, @NotNull @NotNull String @NotNull ... names) Tries to find and wrap a class with one of the given names. If no class with the given name is found an illegal argument exception is raised. If you want a method to find a class which does not raise an exception, consider usingfindAny(ClassLoader, String...)instead. The order of the given name array will be the same as the search order. This method will make use of the first available class loader in the context if no explicit loader is given in the following order:- the thread context class loader.
- the reflexion class loader.
- the system class loader.
This method will not initialize the class when wrapping it.
- Parameters:
loader- the loader to search the class in, null to use the first available contextual loader.names- the names of the class to search for.- Returns:
- a reflexion instance wrapping the first class which can be resolved from the given names.
- Throws:
NullPointerException- if the given name array or an element of it is null.ReflexionException- if no class with one of the given names could be found.
-
unreflectField
Unreflects the given field and wraps it in a field accessor, while preserving the functionality of bindings and better abstraction. This method is specifically useful when an iteration for internal reasons is made, or a java field instance is already known.This method creates a new reflexion instance which is bound to the declaring class of the field and then unreflects it. If a specific binding is required use
Reflexion.onBound(instance).unreflect(field)instead.- Parameters:
field- the field to wrap in a field accessor.- Returns:
- a field accessor for the given field, bound to this reflexion instance.
- Throws:
NullPointerException- if the given field is null.- Since:
- 1.7.0
-
unreflectMethod
@NotNull public static @NotNull MethodAccessor<Method> unreflectMethod(@NotNull @NotNull Method method) Unreflects the given method and wraps it in a method accessor, while preserving the functionality of bindings and better abstraction. This method is specifically useful when an iteration for internal reasons is made, or a java method instance is already known.This method creates a new reflexion instance which is bound to the declaring class of the method and then unreflects it. If a specific binding is required use
Reflexion.onBound(instance).unreflect(method)instead.- Parameters:
method- the method to wrap in a method accessor.- Returns:
- a method accessor for the given method, bound to this reflexion instance.
- Throws:
NullPointerException- if the given method is null.- Since:
- 1.7.0
-
unreflectConstructor
@NotNull public static @NotNull MethodAccessor<Constructor<?>> unreflectConstructor(@NotNull @NotNull Constructor<?> constructor) Unreflects the given constructor and wraps it in a method accessor, while preserving the functionality of bindings and better abstraction. This method is specifically useful when an iteration for internal reasons is made, or a java constructor instance is already known.This method creates a new reflexion instance which is bound to the declaring class of the constructor and then unreflects it. If a specific binding is required use
Reflexion.onBound(instance).unreflect(constructor)instead.- Parameters:
constructor- the constructor to wrap in a method accessor.- Returns:
- a method accessor for the given constructor, bound to this reflexion instance.
- Throws:
NullPointerException- if the given constructor is null.- Since:
- 1.7.0
-
bind
@Contract(value="_ -> new", pure=true) @NotNull public @NotNull Reflexion bind(@Nullable @Nullable Object binding) Binds the current reflexion instance to the given binding even when this instance is already bound to an instance. Binding a reflexion instance has no direct effect on how the library performs, however all accessor methods which would normally require an instance to be passed in can then be executed without passing in an instance, using the instance to which the reflexion object was bound. For example:public static String getHelloWorldFieldValue() { HelloWorld obj = new HelloWorld("Hello, World!"); // without binding var unboundAcc = Reflexion.on(HelloWorld.class).findField("message").get(); System.out.println("Value: " + unboundAcc.getValue(obj)); // prints "Hello, World!" // with binding var boundAcc = Reflexion.on(HelloWorld.class).bind(obj).findField("message").get(); System.out.println("Value: " + boundAcc.getValue()); // prints "Hello, World!" // but we can still change the object to get the field value of HelloWorld world = new HelloWorld("Hello, Moon!"); System.out.println("Value: " + boundAcc.getValue(world)); // prints "Hello, Moon!" }Note: this method always returns a new reflexion instance and not the instance the method was called on. Re-using the instance from before the method call is still possible without any changes to the bindings. Changes to the returned reflexion instance will not reflect into this instance as vice-versa.
- Parameters:
binding- the object instance to bind the reflexion instance to, null to release the binding.- Returns:
- a new reflexion instance targeting the same class as the current one but with the given binding set.
-
getWrappedClass
Gets the class which is wrapped by this reflexion instance.- Returns:
- the wrapped class.
-
getBinding
Gets the object instance to which this reflexion instance is bound.- Returns:
- the bound object instance, null if not bound to any instance.
-
getAccessorFactory
Get the accessor factory which is associated with this reflexion instance.- Returns:
- the associated accessor factory with this reflexion instance.
-
unreflect
Unreflects the given field and wraps it in a field accessor, while preserving the functionality of bindings and better abstraction. This method is specifically useful when an iteration for internal reasons is made, or a java field instance is already known.- Parameters:
field- the field to wrap in a field accessor.- Returns:
- a field accessor for the given field, bound to this reflexion instance.
- Throws:
NullPointerException- if the given field is null.- Since:
- 1.7.0
-
unreflect
Unreflects the given method and wraps it in a method accessor, while preserving the functionality of bindings and better abstraction. This method is specifically useful when an iteration for internal reasons is made, or a java method instance is already known.- Parameters:
method- the method to wrap in a method accessor.- Returns:
- a method accessor for the given method, bound to this reflexion instance.
- Throws:
NullPointerException- if the given method is null.- Since:
- 1.7.0
-
unreflect
@NotNull public @NotNull MethodAccessor<Constructor<?>> unreflect(@NotNull @NotNull Constructor<?> constructor) Unreflects the given constructor and wraps it in a method accessor, while preserving the functionality of bindings and better abstraction. This method is specifically useful when an iteration for internal reasons is made, or a java constructor instance is already known.- Parameters:
constructor- the constructor to wrap in a method accessor.- Returns:
- a method accessor for the given constructor, bound to this reflexion instance.
- Throws:
NullPointerException- if the given constructor is null.- Since:
- 1.7.0
-
findField
Finds a field with the given name in the wrapped class and creates a field accessor wrapper for it. Internally this method uses a cache for all fields in the class, meaning that each call to the method will not result in duplicate lookups in the wrapped class.Example usage:
public static String getHelloWorldFieldValue(HelloWorld obj) { return Reflexion.on(HelloWorld.class) .findField("helloWorld") .flatMap(acc -> acc.getValue(obj).asOptional()) .orElse("Unable to resolve field value :/"); }- Parameters:
name- the name of the field to get.- Returns:
- an optional accessor for the field with the given name.
- Throws:
NullPointerException- if the given field name is null.
-
findField
Finds the first field which matches the given matcher. Internally this method uses a cache for all fields in the class, meaning that each call to the method will not result in duplicate lookups in the wrapped class.Example usage:
public static String getHelloWorldFieldValue(HelloWorld obj) { return Reflexion.on(HelloWorld.class) .findField(FieldMatcher.newMatcher().hasName("world")) .flatMap(acc -> acc.getValue(obj).asOptional()) .orElse("Unable to resolve field value :/"); }- Parameters:
matcher- the matcher for the field to find.- Returns:
- an optional accessor for the first field matching the given matcher.
- Throws:
NullPointerException- if the given matcher is null.
-
findFields
@NotNull public @NotNull Collection<FieldAccessor> findFields(@NotNull @NotNull FieldMatcher matcher) Finds all fields which are matching the given matcher. Internally this method uses a cache for all fields in the class, meaning that each call to the method will not result in duplicate lookups in the wrapped class.Example usage:
public static Collection<FieldAccessor> getHelloWorldFields() { return Reflexion.on(HelloWorld.class) .findFields(FieldMatcher.newMatcher().hasName("world")); }- Parameters:
matcher- the matcher to match the fields.- Returns:
- a collection of field accessors for all fields which are matching the given matcher.
- Throws:
NullPointerException- if the given matcher is null.
-
findMethod
@NotNull public @NotNull Optional<MethodAccessor<Method>> findMethod(@NotNull @NotNull String name, @NotNull @NotNull Class<?>... paramTypes) Finds a method with the given name and parameter types in the wrapped class and creates a method accessor wrapper for it. The given parameter types must match exactly and aren't derived types. Internally this method uses a cache for all methods in the class, meaning that each call to the method will not result in duplicate lookups in the wrapped class.Example usage:
public static String getHelloWorldMethodValue(HelloWorld obj) { return Reflexion.on(HelloWorld.class) .findMethod("helloWorld") .flatMap(acc -> acc.invoke(obj).asOptional()) .orElse("Unable to resolve method value :/"); }- Parameters:
name- the name of the method to get.paramTypes- the type of all parameters of the method.- Returns:
- an optional accessor for the method with the given name and parameters.
- Throws:
NullPointerException- if the given method name or parameter array is null.
-
findMethod
@NotNull public @NotNull Optional<MethodAccessor<Method>> findMethod(@NotNull @NotNull MethodMatcher matcher) Finds the first method which matches the given matcher in the wrapped class and creates a method accessor wrapper for it. Internally this method uses a cache for all methods in the class, meaning that each call to the method will not result in duplicate lookups in the wrapped class.Example usage:
public static String getHelloWorldMethodValue(HelloWorld obj) { return Reflexion.on(HelloWorld.class) .findMethod(MethodMatcher.newMatcher().hasName("helloWorld")) .flatMap(acc -> acc.invoke(obj).asOptional()) .orElse("Unable to resolve method value :/"); }- Parameters:
matcher- the matcher for the method to find.- Returns:
- an optional accessor for the first method matching the given matcher.
- Throws:
NullPointerException- if the given matcher is null.
-
findMethods
@NotNull public @NotNull Collection<MethodAccessor<Method>> findMethods(@NotNull @NotNull MethodMatcher matcher) Finds all methods which are matching the given matcher in the wrapped class and creates a method accessor wrapper for each of them. Internally this method uses a cache for all methods in the class, meaning that each call to the method will not result in duplicate lookups in the wrapped class.Example usage:
public static Collection<MethodAccessor<Method>> getHelloWorldMethods() { return Reflexion.on(HelloWorld.class) .findMethods(MethodMatcher.newMatcher().hasName("world")); }- Parameters:
matcher- the matcher for the method to find.- Returns:
- a collection of method accessors for all methods which are matching the given matcher.
- Throws:
NullPointerException- if the given matcher is null.
-
findConstructor
@NotNull public @NotNull Optional<MethodAccessor<Constructor<?>>> findConstructor(@NotNull @NotNull Class<?>... paramTypes) Finds a constructor with the given parameter types in the wrapped class and creates a method accessor wrapper for it. The given parameter types must match exactly and aren't derived types. Internally this method uses a cache for all constructors in the class, meaning that each call to the method will not result in duplicate lookups in the wrapped class.Example usage:
public static HelloWorld newHelloWorldInstance(String msg) { return Reflexion.on(HelloWorld.class) .findConstructor(String.class) .flatMap(acc -> acc.invoke(msg).asOptional()) .orElse(null); }- Parameters:
paramTypes- the type of all parameters of the constructor.- Returns:
- an optional accessor for the constructor with the given parameters.
- Throws:
NullPointerException- if the given parameter array is null.
-
findConstructor
@NotNull public @NotNull Optional<MethodAccessor<Constructor<?>>> findConstructor(@NotNull @NotNull ConstructorMatcher matcher) Finds the first constructor matching the given matcher in the wrapped class and creates a method accessor wrapper for it. Internally this method uses a cache for all constructors in the class, meaning that each call to the method will not result in duplicate lookups in the wrapped class.Example usage:
public static HelloWorld newHelloWorldInstance(String msg) { return Reflexion.on(HelloWorld.class) .findConstructor(ConstructorMatcher.newMatcher() .exactTypeAt(Constructor::getParameterTypes, String.class, 0)) .flatMap(acc -> acc.invoke(msg).asOptional()) .orElse(null); }- Parameters:
matcher- the matcher for the constructor to find.- Returns:
- an optional accessor for the constructor with the given parameters.
- Throws:
NullPointerException- if the given matcher is null.
-
findConstructors
@NotNull public @NotNull Collection<MethodAccessor<Constructor<?>>> findConstructors(@NotNull @NotNull ConstructorMatcher matcher) Finds all constructors matching the given matcher in the wrapped class and creates a method accessor wrapper for it. Internally this method uses a cache for all constructors in the class, meaning that each call to the method will not result in duplicate lookups in the wrapped class.Example usage:
public static Collection<MethodAccessor<Constructor<?>>> findHelloWorldConstructors() { return Reflexion.on(HelloWorld.class) .findConstructors(ConstructorMatcher.newMatcher() .exactTypeAt(Constructor::getParameterTypes, String.class, 0)); }- Parameters:
matcher- the matcher for the constructor to find.- Returns:
- a collection of method accessors for all constructors which are matching the given matcher.
- Throws:
NullPointerException- if the given matcher is null.
-
getFieldCache
-
getMethodCache
-
getConstructorCache
Internal: gets the constructor cache for the wrapped class. This method populates the cache before returning it if the cache isn't initialized yet.- Returns:
- the constructor cache for the wrapped class.
-