public final class ReflectionUtil
extends java.lang.Object
This class delegates methods to the underlying Java Reflection API,
but wraps checked exceptions in WrappedReflectiveOperationException.
You can get the original exception by calling Throwable.getCause().
This class handles the remapping of class names, field names, and method names
when running on Paper 1.20.5+. Because of the remapping, these methods will be
even slower than the normal Java Reflection API. This means that you should be
saving the results of these methods, typically in a static final
field, to avoid the performance penalty of reflection.
| Modifier and Type | Field and Description |
|---|---|
static @NotNull java.util.function.Predicate<java.lang.reflect.Member> |
IS_FINAL |
static @NotNull java.util.function.Predicate<java.lang.reflect.Member> |
IS_NOT_FINAL |
static @NotNull java.util.function.Predicate<java.lang.reflect.Member> |
IS_NOT_PRIVATE |
static @NotNull java.util.function.Predicate<java.lang.reflect.Member> |
IS_NOT_PUBLIC |
static @NotNull java.util.function.Predicate<java.lang.reflect.Member> |
IS_NOT_STATIC |
static @NotNull java.util.function.Predicate<java.lang.reflect.Member> |
IS_PRIVATE |
static @NotNull java.util.function.Predicate<java.lang.reflect.Member> |
IS_PUBLIC |
static @NotNull java.util.function.Predicate<java.lang.reflect.Member> |
IS_STATIC |
| Modifier and Type | Method and Description |
|---|---|
static <T> @NotNull java.lang.Class<T> |
getClass(@NotNull java.lang.String className)
Returns the
Class object with the specified name. |
static <T> @NotNull ConstructorInvoker<T> |
getConstructor(@NotNull java.lang.Class<T> clazz,
java.lang.Class<?>... parameterTypes)
Returns the
ConstructorInvoker for the specified constructor. |
static <T> @NotNull java.lang.Class<T> |
getCraftBukkitClass(@NotNull java.lang.String classPath)
Returns the
Class object from the org.bukkit.craftbukkit package with the specified
relative path. |
static @NotNull FieldAccessor |
getField(@NotNull java.lang.Class<?> clazz,
@NotNull java.lang.Class<?> fieldType)
Returns the
FieldAccessor for the specified field. |
static @NotNull FieldAccessor |
getField(@NotNull java.lang.Class<?> clazz,
@NotNull java.lang.Class<?> fieldType,
int index)
Returns the
FieldAccessor for the specified field. |
static @NotNull FieldAccessor |
getField(@NotNull java.lang.Class<?> clazz,
@NotNull java.lang.Class<?> fieldType,
int index,
@Nullable java.util.function.Predicate<? super java.lang.reflect.Field> predicate)
Returns the
FieldAccessor for the specified field. |
static @NotNull FieldAccessor |
getField(@NotNull java.lang.Class<?> clazz,
@NotNull java.lang.String fieldName)
Returns the
FieldAccessor for the specified field. |
static @NotNull MethodInvoker |
getMethod(@NotNull java.lang.Class<?> clazz,
@NotNull java.lang.Class<?> returnType,
java.lang.Class<?>... parameterTypes)
Returns the
MethodInvoker for the specified method. |
static @NotNull MethodInvoker |
getMethod(@NotNull java.lang.Class<?> clazz,
@NotNull java.lang.Class<?> returnType,
int index,
java.lang.Class<?>... parameterTypes)
Returns the
MethodInvoker for the specified method. |
static @NotNull MethodInvoker |
getMethod(@NotNull java.lang.Class<?> clazz,
@NotNull java.lang.Class<?> returnType,
int index,
@Nullable java.util.function.Predicate<? super java.lang.reflect.Method> predicate,
java.lang.Class<?>... parameterTypes)
Returns the
MethodInvoker for the specified method. |
static @NotNull MethodInvoker |
getMethod(@NotNull java.lang.Class<?> clazz,
@NotNull java.lang.String methodName,
java.lang.Class<?>... parameterTypes)
Returns the
MethodInvoker for the specified method. |
static <T> @NotNull java.lang.Class<T> |
getMinecraftClass(@NotNull java.lang.String packageName,
@NotNull java.lang.String className)
Returns the net.minecraft.server
Class object with the specified name. |
@NotNull public static final @NotNull java.util.function.Predicate<java.lang.reflect.Member> IS_PUBLIC
@NotNull public static final @NotNull java.util.function.Predicate<java.lang.reflect.Member> IS_NOT_PUBLIC
@NotNull public static final @NotNull java.util.function.Predicate<java.lang.reflect.Member> IS_PRIVATE
@NotNull public static final @NotNull java.util.function.Predicate<java.lang.reflect.Member> IS_NOT_PRIVATE
@NotNull public static final @NotNull java.util.function.Predicate<java.lang.reflect.Member> IS_STATIC
@NotNull public static final @NotNull java.util.function.Predicate<java.lang.reflect.Member> IS_NOT_STATIC
@NotNull public static final @NotNull java.util.function.Predicate<java.lang.reflect.Member> IS_FINAL
@NotNull public static final @NotNull java.util.function.Predicate<java.lang.reflect.Member> IS_NOT_FINAL
@NotNull
public static <T> @NotNull java.lang.Class<T> getClass(@NotNull
@NotNull java.lang.String className)
Class object with the specified name.
This method is a wrapper around Class.forName(String) that
catches the checked exception and rethrows it as a WrappedReflectiveOperationException.
When running on Paper 1.20.5+, this method will remap the class name using the mappings built into the Paper jar.
T - the type of the classclassName - the fully qualified name of the desired classClass object for the class with the specified nameWrappedReflectiveOperationException - if the class cannot be found@NotNull
public static <T> @NotNull java.lang.Class<T> getMinecraftClass(@NotNull
@NotNull java.lang.String packageName,
@NotNull
@NotNull java.lang.String className)
Class object with the specified name.
This method calls getClass(String) with the full class name constructed
from the package name and class name.
We recommend using a mappings website to find the correct package and class name. Always use the Spigot mapped package and class name.
T - the type of the classpackageName - the name of the Spigot mapped mojang package, without the
"net.minecraft." prefix (e.g. "world.entity.player")className - the mojang mapped class name (e.g. "EntityPlayer")Class object for the class with the specified nameWrappedReflectiveOperationException - if the class cannot be found@NotNull
public static <T> @NotNull java.lang.Class<T> getCraftBukkitClass(@NotNull
@NotNull java.lang.String classPath)
Class object from the org.bukkit.craftbukkit package with the specified
relative path. This method calls getClass(String).
For example, to get the class org.bukkit.craftbukkit.v1_17_R1.entity.CraftPlayer,
you would call getCraftBukkitClass("entity.CraftPlayer").
T - the type of the classclassPath - the relative path of the class, without the "org.bukkit.craftbukkit." prefix.Class object for the class with the specified name@NotNull public static @NotNull FieldAccessor getField(@NotNull @NotNull java.lang.Class<?> clazz, @NotNull @NotNull java.lang.String fieldName)
FieldAccessor for the specified field.clazz - The class that owns the field.fieldName - The name of the field.FieldAccessor for the field.@NotNull public static @NotNull FieldAccessor getField(@NotNull @NotNull java.lang.Class<?> clazz, @NotNull @NotNull java.lang.Class<?> fieldType)
FieldAccessor for the specified field. If no such field exists, an
IllegalArgumentException is thrown.clazz - The class that owns the field.fieldType - The type of the field.FieldAccessor for the field.getField(Class, Class, int, Predicate)@NotNull public static @NotNull FieldAccessor getField(@NotNull @NotNull java.lang.Class<?> clazz, @NotNull @NotNull java.lang.Class<?> fieldType, int index)
FieldAccessor for the specified field. If no such field exists, an
IllegalArgumentException is thrown.clazz - The class that owns the field.fieldType - The type of the field.index - The index of the field to get (in case there are multiple fields of the same type).FieldAccessor for the field.getField(Class, Class, int, Predicate)@NotNull public static @NotNull FieldAccessor getField(@NotNull @NotNull java.lang.Class<?> clazz, @NotNull @NotNull java.lang.Class<?> fieldType, int index, @Nullable @Nullable java.util.function.Predicate<? super java.lang.reflect.Field> predicate)
FieldAccessor for the specified field. If no such field exists, an
IllegalArgumentException is thrown.
This method can be used to get the nth field of a certain type in a class. For example,
to get the 2nd non-static field of type int in a class, you would call
getField(clazz, int.class, 2, IS_FIELD_NON_STATIC).
clazz - The class that owns the field.fieldType - The type of the field.index - The index of the field to get (in case there are multiple fields of the same type).predicate - A predicate to filter the fields. If the predicate returns false, the field is skipped.FieldAccessor for the field.@NotNull public static @NotNull MethodInvoker getMethod(@NotNull @NotNull java.lang.Class<?> clazz, @NotNull @NotNull java.lang.String methodName, java.lang.Class<?>... parameterTypes)
MethodInvoker for the specified method.clazz - The class that owns the method.methodName - The name of the method.parameterTypes - The parameter types of the method.MethodInvoker for the method.@NotNull public static @NotNull MethodInvoker getMethod(@NotNull @NotNull java.lang.Class<?> clazz, @NotNull @NotNull java.lang.Class<?> returnType, java.lang.Class<?>... parameterTypes)
MethodInvoker for the specified method. If no such method exists, an
IllegalArgumentException is thrown.clazz - The class that owns the method.returnType - The return type of the method.parameterTypes - The parameter types of the method.MethodInvoker for the method.getMethod(Class, Class, int, Predicate, Class...)@NotNull public static @NotNull MethodInvoker getMethod(@NotNull @NotNull java.lang.Class<?> clazz, @NotNull @NotNull java.lang.Class<?> returnType, int index, java.lang.Class<?>... parameterTypes)
MethodInvoker for the specified method. If no such method exists, an
IllegalArgumentException is thrown.clazz - The class that owns the method.returnType - The return type of the method.index - The index of the method to get (in case there are multiple methods with the same return type).parameterTypes - The parameter types of the method.MethodInvoker for the method.getMethod(Class, Class, int, Predicate, Class...)@NotNull public static @NotNull MethodInvoker getMethod(@NotNull @NotNull java.lang.Class<?> clazz, @NotNull @NotNull java.lang.Class<?> returnType, int index, @Nullable @Nullable java.util.function.Predicate<? super java.lang.reflect.Method> predicate, java.lang.Class<?>... parameterTypes)
MethodInvoker for the specified method. If no such method exists, an
IllegalArgumentException is thrown.
This method can be used to get the nth method of a certain return type in a
class. For example, to get the 2nd method with return type void in a class, you would call
getMethod(clazz, void.class, 2, null, parameterTypes).
clazz - The class that owns the method.returnType - The return type of the method.index - The index of the method to get (in case there are multiple methods with the same return type).predicate - A predicate to filter the methods. If the predicate returns false, the method is skipped.parameterTypes - The parameter types of the method.MethodInvoker for the method.@NotNull public static <T> @NotNull ConstructorInvoker<T> getConstructor(@NotNull @NotNull java.lang.Class<T> clazz, java.lang.Class<?>... parameterTypes)
ConstructorInvoker for the specified constructor.T - The type of the class.clazz - The class that owns the constructor.parameterTypes - The parameter types of the constructor.ConstructorInvoker for the constructor.