Class Reflector
The Reflector class encapsulates a target class and provides methods to retrieve
its constructors, fields, methods, and inner classes. It also offers helper functions to
invoke methods, access and modify fields, and create new instances dynamically.
This tool is particularly useful in plugin development where access to internal server classes (such as NMS or CraftBukkit classes) is needed. It abstracts away the boilerplate associated with reflection and helps in writing more readable and maintainable code.
Example usage:
// Create a Reflector for a target class by its fully-qualified name:
Reflector reflector = Reflector.of("net.minecraft.server.v1_16_R3.EntityPlayer");
// Retrieve a declared method and invoke it:
Method method = reflector.getMethod("someInternalMethod", String.class);
Object result = method.invoke(someEntityPlayerInstance, "argument");
// Alternatively, invoke a method using the fluent call:
Object resultFluent = reflector.call("someInternalMethod", "argument");
Additionally, the Reflector supports creating new instances, accessing private fields,
and converting return values into new Reflector objects for further chained operations.
- See Also:
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription<T> TInvokes a method on the target object (or an object provided by the initial supplier) with the given parameters.<T> TInvokes a method on the target object using the specified method name and parameters.callAsReflector(Object initial, String methodName, Object... objects) Invokes a method on the target object and wraps the result in a newReflectorfor further reflection.callAsReflector(String methodName, Object... objects) Invokes a method on the target object and wraps the result in a newReflectorfor further reflection.<T> TCreates a new instance of the target class using the constructor that matches the provided arguments.createAsReflector(Object... objects) Creates a new instance of the target class using the provided constructor arguments, and wraps the resulting object in aReflectorfor further reflection.static ReflectorCreates a newReflectorfrom a supplier that provides an object instance.<T> TRetrieves the value of a field (by matching type) from the target object.<T> TRetrieves the value of a field (by matching type) from the target object.<T> TRetrieves the value of a field from the target object (or an object provided by the initial supplier).<T> TRetrieves the value of a field with the given name from the target object.getAsReflector(Class<?> clazz) Retrieves a field value (by matching type) and wraps it in a newReflectorfor further reflective operations.getAsReflector(Object initial, Class<?> clazz) Retrieves a field value (by matching type) and wraps it in a newReflectorfor further reflective operations.getAsReflector(Object initial, String fieldName) Retrieves a field value and wraps it in a newReflectorfor further reflective operations.getAsReflector(String fieldName) Retrieves a field value by name and wraps it in a newReflectorfor further reflective operations.Retrieves a list of all declared inner classes of the target class.Constructor<?> getConstructor(Class<?>... parameters) Retrieves a declared constructor with the specified parameter types.List<Constructor<?>> Retrieves a list of all declared constructors of the target class.Retrieves a declared field from the specified class that is not of the same type as the class itself.Retrieves a declared field by name.Retrieves a list of all declared fields of the target class.Retrieves a declared method by name and parameter types.Retrieves a list of all declared methods of the target class.Class<?> getType()Returns the target class wrapped by this Reflector.static ReflectorCreates a newReflectorfor the specified target class.static ReflectorCreates a newReflectorfor the class with the specified fully-qualified name.static ReflectorofCraftBukkit(String className) Creates a newReflectorfor a CraftBukkit class with the given name.static ReflectorCreates a newReflectorfor an NMS (net.minecraft.server) class with the given name.voidSets the value of a field with the specified name on the target object (or an object provided by the initial supplier).voidSets the value of a field with the specified name on the target object.setInitial(Supplier<Object> supplier) Sets the supplier for the initial object instance.
-
Field Details
-
NMS_PACKAGE
The package path for NMS (net.minecraft.server) classes, dynamically constructed based on the API version. -
CRAFT_BUKKIT_PACKAGE
The package path for CraftBukkit classes, derived from the server's implementation.
-
-
Constructor Details
-
Reflector
public Reflector()
-
-
Method Details
-
setInitial
Sets the supplier for the initial object instance. This can be used to delay the instantiation or to provide dynamic values for non-static method invocations.- Parameters:
supplier- the supplier to set (must not be null)- Returns:
- this Reflector instance for chaining
-
getType
Returns the target class wrapped by this Reflector.- Returns:
- the target
Class<?>
-
getMethods
Retrieves a list of all declared methods of the target class.- Returns:
- a
ListofMethodobjects representing the declared methods
-
getFields
Retrieves a list of all declared fields of the target class.- Returns:
- a
ListofFieldobjects representing the declared fields
-
getConstructors
Retrieves a list of all declared constructors of the target class.- Returns:
- a
ListofConstructorobjects representing the declared constructors
-
getClasses
Retrieves a list of all declared inner classes of the target class.- Returns:
- a
ListofClassobjects representing the declared inner classes
-
getMethod
Retrieves a declared method by name and parameter types.If the method is not accessible, its accessibility is set to
true.- Parameters:
name- the name of the methodparameters- the parameter types of the method- Returns:
- the
Methodmatching the given signature - Throws:
NullPointerException- if the method is not found
-
getField
Retrieves a declared field by name.If the field is not accessible, its accessibility is set to
true.- Parameters:
name- the name of the field- Returns:
- the
Fieldwith the specified name - Throws:
NullPointerException- if the field is not found
-
getField
Retrieves a declared field from the specified class that is not of the same type as the class itself.This method is useful when trying to find a field that holds an instance of a different type.
- Parameters:
clazz- the class in which to search for the field- Returns:
- the first
Fieldthat does not have the same type as the provided class - Throws:
NullPointerException- if no such field is found
-
getConstructor
Retrieves a declared constructor with the specified parameter types.If the constructor is not accessible, its accessibility is set to
true.- Parameters:
parameters- the parameter types of the constructor- Returns:
- the
Constructormatching the given signature - Throws:
NullPointerException- if the constructor is not found
-
call
Invokes a method on the target object (or an object provided by the initial supplier) with the given parameters.The method is looked up by name and parameter types inferred from the provided arguments.
- Type Parameters:
T- the expected return type- Parameters:
initial- an optional initial object to invoke the method on; ifnull, the supplier's value is usedmethodName- the name of the method to invokeobjects- the arguments to pass to the method- Returns:
- the result of the method invocation, cast to type
T
-
call
Invokes a method on the target object using the specified method name and parameters.This is a convenience overload that assumes a
nullinitial object.- Type Parameters:
T- the expected return type- Parameters:
methodName- the name of the method to invokeobjects- the arguments to pass to the method- Returns:
- the result of the method invocation, cast to type
T
-
callAsReflector
Invokes a method on the target object and wraps the result in a newReflectorfor further reflection.- Parameters:
initial- an optional initial object to invoke the method onmethodName- the name of the method to invokeobjects- the arguments to pass to the method- Returns:
- a new
Reflectorwrapping the result of the method call
-
callAsReflector
Invokes a method on the target object and wraps the result in a newReflectorfor further reflection.- Parameters:
methodName- the name of the method to invokeobjects- the arguments to pass to the method- Returns:
- a new
Reflectorwrapping the result of the method call
-
get
Retrieves the value of a field from the target object (or an object provided by the initial supplier).- Type Parameters:
T- the expected type of the field value- Parameters:
initial- an optional initial object from which to retrieve the field value; ifnull, the supplier's value is usedfieldName- the name of the field- Returns:
- the value of the field, cast to type
T
-
get
Retrieves the value of a field with the given name from the target object.- Type Parameters:
T- the expected type of the field value- Parameters:
fieldName- the name of the field- Returns:
- the field value, cast to type
T
-
get
Retrieves the value of a field (by matching type) from the target object.- Type Parameters:
T- the expected type of the field value- Parameters:
initial- an optional initial object from which to retrieve the field value; ifnull, the supplier's value is usedclazz- the type of the field to retrieve- Returns:
- the field value, cast to type
T
-
get
Retrieves the value of a field (by matching type) from the target object.- Type Parameters:
T- the expected type of the field value- Parameters:
clazz- the type of the field to retrieve- Returns:
- the field value, cast to type
T
-
getAsReflector
Retrieves a field value and wraps it in a newReflectorfor further reflective operations.- Parameters:
initial- an optional initial object from which to retrieve the field valuefieldName- the name of the field- Returns:
- a new
Reflectorwrapping the retrieved field value
-
getAsReflector
Retrieves a field value by name and wraps it in a newReflectorfor further reflective operations.- Parameters:
fieldName- the name of the field- Returns:
- a new
Reflectorwrapping the retrieved field value
-
getAsReflector
Retrieves a field value (by matching type) and wraps it in a newReflectorfor further reflective operations.- Parameters:
initial- an optional initial object from which to retrieve the field valueclazz- the type of the field to retrieve- Returns:
- a new
Reflectorwrapping the retrieved field value
-
getAsReflector
Retrieves a field value (by matching type) and wraps it in a newReflectorfor further reflective operations.- Parameters:
clazz- the type of the field to retrieve- Returns:
- a new
Reflectorwrapping the retrieved field value
-
set
Sets the value of a field with the specified name on the target object (or an object provided by the initial supplier).- Parameters:
initial- an optional initial object on which to set the field value; ifnull, the supplier's value is usedfieldName- the name of the fieldvalue- the value to set in the field
-
set
Sets the value of a field with the specified name on the target object.- Parameters:
fieldName- the name of the fieldvalue- the value to set in the field
-
create
Creates a new instance of the target class using the constructor that matches the provided arguments.- Type Parameters:
T- the type of the created instance- Parameters:
objects- the arguments to pass to the constructor- Returns:
- a new instance of type
T
-
createAsReflector
Creates a new instance of the target class using the provided constructor arguments, and wraps the resulting object in aReflectorfor further reflection.- Parameters:
objects- the arguments to pass to the constructor- Returns:
- a new
Reflectorwrapping the created instance
-
of
Creates a newReflectorfor the specified target class.- Parameters:
clazz- the target class- Returns:
- a new
Reflectorwrapping the target class
-
of
Creates a newReflectorfor the class with the specified fully-qualified name.- Parameters:
path- the fully-qualified class name- Returns:
- a new
Reflectorwrapping the target class - Throws:
IllegalStateException- if the class cannot be found
-
ofNms
Creates a newReflectorfor an NMS (net.minecraft.server) class with the given name.- Parameters:
className- the name of the NMS class- Returns:
- a new
Reflectorwrapping the NMS class
-
ofCraftBukkit
Creates a newReflectorfor a CraftBukkit class with the given name.- Parameters:
className- the name of the CraftBukkit class- Returns:
- a new
Reflectorwrapping the CraftBukkit class
-
from
Creates a newReflectorfrom a supplier that provides an object instance.The supplier is stored as the initial supplier for this reflector, allowing future operations to work with the supplied instance.
- Parameters:
supplier- a supplier that provides an object instance- Returns:
- a new
Reflectorwrapping the class of the supplied object
-