Package dev.array21.bukkitreflectionlib
Class ReflectionUtil
java.lang.Object
dev.array21.bukkitreflectionlib.ReflectionUtil
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Class<?>getBukkitClass(String className) Get a Class from the org.bukkit.craftbukkit.SERVER_VERSION.static Constructor<?>getConstructor(Class<?> clazz, Class<?>... args) Get the Constructor of a Classstatic Enum<?>Get an Enum from an Enum constantstatic Enum<?>Get an Enum constant by it's name and constantstatic FieldGet a Fieldstatic intGet the current major Minecraft version.static MethodGet a Methodstatic Class<?>getMinecraftClass(String className) 1.17 and newer only Get a Class from the net.minecraft packagestatic Class<?>getNmsClass(String className) Deprecated.static ObjectGet the value of a Field, where the Class in which the Field is defined is explicitly given.static ObjectDeprecated.static ObjectGet the value of a Field, where the Class in which the field is defined is derived from the provided Objectstatic ObjectinvokeConstructor(Class<?> clazz, Class<?>[] argTypes, Object[] args) Invoke a Class' Constructor, where the argument types are explicitly given (Helpful when working with primitives)static ObjectinvokeConstructor(Class<?> clazz, Object... args) Invoke a Class' constructor.static ObjectInvoke a Method where the Class where the Method is defined is explicitly given, and the argument types are explicitly givenstatic ObjectinvokeMethod(Class<?> clazz, Object obj, String methodName, Object... args) Invoke a Method where the Class where to find the method is explicitly given (Helpful if the method is located in a superclass).static ObjectinvokeMethod(Object obj, String methodName) Invoke a Method which takes no arguments.static ObjectinvokeMethod(Object obj, String methodName, Class<?>[] argTypes, Object[] args) Invoke a Method where the argument types are explicitly given (Helpful when working with primitives).static ObjectinvokeMethod(Object obj, String methodName, Object[] args) Invoke a Method where the argument types are derived from the provided arguments.static booleanCheck if the new way of packaging Spigot is used
For >=1.17 this will be true, for =<1.16 this will be false.
This dictates if you should usegetNmsClass(String)(<=1.16) orgetMinecraftClass(String)(>=1.17).static voidprintFieldsInClass(Class<?> clazz) Print all Fields in a Class.static voidprintMethodsInClass(Class<?> clazz) Print all Methods in a Class with their parameters.
-
Field Details
-
SERVER_VERSION
-
-
Constructor Details
-
ReflectionUtil
public ReflectionUtil()
-
-
Method Details
-
isUseNewSpigotPackaging
public static boolean isUseNewSpigotPackaging()Check if the new way of packaging Spigot is used
For >=1.17 this will be true, for =<1.16 this will be false.
This dictates if you should usegetNmsClass(String)(<=1.16) orgetMinecraftClass(String)(>=1.17).- Returns:
- Returns true if it is, false if it is now
-
getMajorVersion
public static int getMajorVersion()Get the current major Minecraft version. E.g for Minecraft 1.18 this is 18.- Returns:
- The current major Minecraft version
-
getBukkitClass
Get a Class from the org.bukkit.craftbukkit.SERVER_VERSION. package- Parameters:
className- The name of the class- Returns:
- Returns the Class
- Throws:
ClassNotFoundException- Thrown when the Class was not found
-
getNmsClass
Deprecated.1.16 and older only Get a Class from the net.minecraft.server.SERVER_VERSION. package- Parameters:
className- The name of the class- Returns:
- Returns the Class
- Throws:
ClassNotFoundException- Thrown when the Class was not found
-
getMinecraftClass
1.17 and newer only Get a Class from the net.minecraft package- Parameters:
className- The name of the class- Returns:
- Returns the class
- Throws:
ClassNotFoundException- Thrown when the Class was not found
-
getConstructor
public static Constructor<?> getConstructor(Class<?> clazz, Class<?>... args) throws NoSuchMethodException Get the Constructor of a Class- Parameters:
clazz- The Class in which the Constructor is definedargs- Arguments taken by the Constructor- Returns:
- Returns the Constructor
- Throws:
NoSuchMethodException- Thrown when no Constructor in the Class was found with the provided combination of arguments
-
getEnum
Get an Enum from an Enum constant- Parameters:
clazz- The Class in which the Enum is definedconstant- The name of the Enum Constant- Returns:
- Returns the Enum or null if the Enum does not have a member called constant
-
getEnum
public static Enum<?> getEnum(Class<?> clazz, String enumname, String constant) throws ClassNotFoundException Get an Enum constant by it's name and constant- Parameters:
clazz- The Class in which the Enum is definedenumname- The name of the Enumconstant- The name of the Constant- Returns:
- Returns the Enum or null if the Enum does not have a member called constant
- Throws:
ClassNotFoundException- If the Class does not have an Enum called enumname
-
getField
Get a Field- Parameters:
clazz- The Class in which the Field is definedfieldName- The name of the Field- Returns:
- Returns the Field
- Throws:
NoSuchFieldException- Thrown when the Field was not present in the Class
-
getMethod
public static Method getMethod(Class<?> clazz, String methodName, Class<?>... args) throws NoSuchMethodException Get a Method- Parameters:
clazz- The Class in which the Method is definedmethodName- The name of the methodargs- The argument types the method takes- Returns:
- Returns the Method
- Throws:
NoSuchMethodException
-
invokeMethod
public static Object invokeMethod(Object obj, String methodName) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException Invoke a Method which takes no arguments. The Class in which the Method is defined is derived from the provided Object- Parameters:
obj- The object to invoke the method onmethodName- The name of the Method- Returns:
- Returns the result of the method, can be null if the method returns void
- Throws:
NoSuchMethodExceptionIllegalAccessExceptionIllegalArgumentExceptionInvocationTargetException
-
invokeMethod
public static Object invokeMethod(Object obj, String methodName, Object[] args) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException Invoke a Method where the argument types are derived from the provided arguments. The Class in which the Method is defined is derived from the provided Object- Parameters:
obj- The object to invoke the method onmethodName- The name of the Methodargs- The arguments to pass to the Method- Returns:
- Returns the result of the method, can be null if the method returns void
- Throws:
NoSuchMethodExceptionIllegalAccessExceptionIllegalArgumentExceptionInvocationTargetException
-
invokeMethod
public static Object invokeMethod(Object obj, String methodName, Class<?>[] argTypes, Object[] args) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException Invoke a Method where the argument types are explicitly given (Helpful when working with primitives). The Class in which the Method is defined is derived from the provided Object.- Parameters:
obj- The Object to invoke the method onmethodName- The name of the MethodargTypes- The types of arguments as a Class arrayargs- The arguments as an object array- Returns:
- Returns the result of the method, can be null if the method returns void
- Throws:
NoSuchMethodExceptionIllegalAccessExceptionIllegalArgumentExceptionInvocationTargetException
-
invokeMethod
public static Object invokeMethod(Class<?> clazz, Object obj, String methodName, Object... args) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException Invoke a Method where the Class where to find the method is explicitly given (Helpful if the method is located in a superclass). The argument types are derived from the provided arguments- Parameters:
clazz- The Class where the method is locatedobj- The Object to invoke the method onmethodName- The name of the methodargs- The arguments to be passed to the method- Returns:
- Returns the result of the method, can be null if the method returns void
- Throws:
NoSuchMethodExceptionIllegalAccessExceptionIllegalArgumentExceptionInvocationTargetException
-
invokeMethod
public static Object invokeMethod(Class<?> clazz, Object obj, String methodName, Class<?>[] argTypes, Object[] args) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException Invoke a Method where the Class where the Method is defined is explicitly given, and the argument types are explicitly given- Parameters:
clazz- The Class in which the Method is locatedobj- The Object on which to invoke the MethodmethodName- The name of the MethodargTypes- Argument typesargs- Arguments to pass to the method- Returns:
- Returns the result of the method, can be null if the method returns void
- Throws:
NoSuchMethodExceptionIllegalAccessExceptionIllegalArgumentExceptionInvocationTargetException
-
getObject
public static Object getObject(Object obj, String name) throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException Get the value of a Field, where the Class in which the field is defined is derived from the provided Object- Parameters:
obj- The object in which the field is located, and from which to get the valuename- The name of the Field to get the value from- Returns:
- Returns the value of the Field
- Throws:
NoSuchFieldExceptionIllegalArgumentExceptionIllegalAccessException
-
getObject
@Deprecated public static Object getObject(Object obj, Class<?> clazz, String name) throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException Deprecated.UsegetObject(Class, Object, String)insteadGet the value of a Field, where the Class in which the Field is defined is explicitly given. (Helpful when the Field is in a superclass)- Parameters:
obj- The Object to get the value fromclazz- The Class in which the Field is definedname- The name of the Field- Returns:
- Returns the value of the Field
- Throws:
NoSuchFieldExceptionIllegalArgumentExceptionIllegalAccessException
-
getObject
public static Object getObject(Class<?> clazz, Object obj, String name) throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException Get the value of a Field, where the Class in which the Field is defined is explicitly given. (Helpful when the Field is in a superclass)- Parameters:
clazz- The Class in which the Field is definedobj- The Object to get the value fromname- The name of the Field- Returns:
- Returns the value of the Field
- Throws:
NoSuchFieldExceptionIllegalArgumentExceptionIllegalAccessException
-
invokeConstructor
public static Object invokeConstructor(Class<?> clazz, Object... args) throws NoSuchMethodException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException Invoke a Class' constructor. The argument types are derived from the provided arguments- Parameters:
clazz- The Class in which the Constructor is definedargs- The arguments to pass to the Constructor- Returns:
- Returns an instance of the provided Class in which the Constructor is located
- Throws:
NoSuchMethodExceptionInstantiationExceptionIllegalAccessExceptionIllegalArgumentExceptionInvocationTargetException
-
invokeConstructor
public static Object invokeConstructor(Class<?> clazz, Class<?>[] argTypes, Object[] args) throws NoSuchMethodException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException Invoke a Class' Constructor, where the argument types are explicitly given (Helpful when working with primitives)- Parameters:
clazz- The Class in which the Constructor is definedargTypes- The argument typesargs- The arguments to pass to the constructor- Returns:
- Returns an instance of the provided Class in which the Constructor is located
- Throws:
NoSuchMethodExceptionInstantiationExceptionIllegalAccessExceptionIllegalArgumentExceptionInvocationTargetException
-
printMethodsInClass
Print all Methods in a Class with their parameters. Will print the Method's modifiers, return type, name and arguments and their types- Parameters:
clazz- The class to get methods from
-
printFieldsInClass
Print all Fields in a Class. Will print the Field's modifiers, type and name- Parameters:
clazz- The class to get fields from
-
getObject(Class, Object, String)instead