Class ReflectionUtils
java.lang.Object
io.github.bakedlibs.dough.reflection.ReflectionUtils
This class provides some useful static methods to perform reflection.
- Author:
- TheBusyBiscuit
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Constructor<T>getConstructor(Class<T> c, Class<?>... paramTypes) Returns the Constructor of a Class with the specified Parametersstatic <T extends Enum<T>>
TgetEnumConstant(Class<T> c, String name) Returns a specific Enum Constant in an EnumgetEnumConstants(Class<T> c) Returns all Enum Constants in an Enumstatic FieldReturns the Field of a Classstatic <T> TgetFieldValue(Object object, Class<T> fieldType, String field) Returns the Value of a Field in an Objectstatic Class<?>getInnerNMSClass(String name, String subname) Returns an NMS Class inside a Classstatic Class<?>getInnerOBCClass(String name, String subname) Returns an OBC Class inside a Classstatic MethodReturns a certain Method in the specified Classstatic MethodReturns the Method with certain Parametersstatic MethodgetMethodOrAlternative(Class<?> c, String method, String alternative) Returns a certain Method in the specified Classstatic Class<?>getNetMinecraftClass(String name) Returns a `net.minecraft` class via Reflection.static Class<?>getNMSClass(String name) Returns an NMS Class via Reflectionstatic Class<?>getOBCClass(String name) Returns an OBC Class via Reflectionstatic <T> voidsetFieldValue(T object, Class<?> c, String field, Object value) Modifies a Field in an Objectstatic <T> voidsetFieldValue(T object, String field, Object value) Modifies a Field in an Objectstatic Class<?>[]toPrimitiveTypeArray(Class<?>[] classes) Converts the Classes to a Primitive Type Array in order to be used as paramaters
-
Method Details
-
getMethod
Returns a certain Method in the specified Class- Parameters:
c- The Class in which the Method is inmethod- The Method you are looking for- Returns:
- The found Method
-
getMethodOrAlternative
@Nullable public static Method getMethodOrAlternative(@Nonnull Class<?> c, @Nonnull String method, @Nonnull String alternative) Returns a certain Method in the specified Class- Parameters:
c- The Class in which the Method is inmethod- The Method you are looking foralternative- The alternative Method you are looking for- Returns:
- The found Method
-
getMethod
@Nullable public static Method getMethod(@Nonnull Class<?> c, @Nonnull String method, Class<?>... paramTypes) Returns the Method with certain Parameters- Parameters:
c- The Class in which the Method is inmethod- The Method you are looking forparamTypes- The Types of the Parameters- Returns:
- The found Method
-
getField
@Nonnull public static Field getField(@Nonnull Class<?> c, @Nonnull String field) throws NoSuchFieldException Returns the Field of a Class- Parameters:
c- The Class conating this Fieldfield- The name of the Field you are looking for- Returns:
- The found Field
- Throws:
NoSuchFieldException- If the field could not be found.
-
setFieldValue
public static <T> void setFieldValue(@Nonnull T object, @Nonnull Class<?> c, @Nonnull String field, @Nullable Object value) throws NoSuchFieldException, IllegalAccessException Modifies a Field in an Object- Type Parameters:
T- The type of the specified field- Parameters:
object- The Object containing the Fieldc- The Class in which we are looking for this fieldfield- The Name of that Fieldvalue- The Value for that Field- Throws:
NoSuchFieldException- If the field could not be found.IllegalAccessException- If the field could not be modified.
-
setFieldValue
public static <T> void setFieldValue(@Nonnull T object, @Nonnull String field, @Nullable Object value) throws NoSuchFieldException, IllegalAccessException Modifies a Field in an Object- Type Parameters:
T- The type of the specified field- Parameters:
object- The Object containing the Fieldfield- The Name of that Fieldvalue- The Value for that Field- Throws:
NoSuchFieldException- If the field could not be found.IllegalAccessException- If the field could not be modified.
-
getFieldValue
@ParametersAreNonnullByDefault @Nullable public static <T> T getFieldValue(Object object, Class<T> fieldType, String field) throws NoSuchFieldException, IllegalAccessException Returns the Value of a Field in an Object- Parameters:
object- The Object containing the Fieldfield- The Name of that Field- Returns:
- The Value of a Field
- Throws:
NoSuchFieldException- If the field could not be found.IllegalAccessException- If the field could not be queried.
-
toPrimitiveTypeArray
Converts the Classes to a Primitive Type Array in order to be used as paramaters- Parameters:
classes- The Types you want to convert- Returns:
- An Array of primitive Types
-
getConstructor
@Nullable public static <T> Constructor<T> getConstructor(@Nonnull Class<T> c, Class<?>... paramTypes) Returns the Constructor of a Class with the specified Parameters- Type Parameters:
T- The Type argument for the class of this constructor- Parameters:
c- The Class containing the ConstructorparamTypes- The Parameters for that Constructor- Returns:
- The Constructor for that Class
-
getInnerNMSClass
@Nonnull public static Class<?> getInnerNMSClass(@Nonnull String name, @Nonnull String subname) throws ClassNotFoundException, UnknownServerVersionException Returns an NMS Class inside a Class- Parameters:
name- The Name of the Class your Inner class is located insubname- The Name of the inner Class you are looking for- Returns:
- The Class in your specified Class
- Throws:
ClassNotFoundException- If the class could not be found.UnknownServerVersionException- If theMinecraftVersionwas unable to be determined
-
getNetMinecraftClass
@Nonnull public static Class<?> getNetMinecraftClass(@Nonnull String name) throws ClassNotFoundException, UnknownServerVersionException Returns a `net.minecraft` class via Reflection.- Parameters:
name- The class name of which to fetch- Returns:
- The `net.minecraft` class.
- Throws:
ClassNotFoundException- If the class does not existUnknownServerVersionException- If theMinecraftVersionwas unable to be determined
-
getNMSClass
@Nonnull public static Class<?> getNMSClass(@Nonnull String name) throws ClassNotFoundException, UnknownServerVersionException Returns an NMS Class via Reflection- Parameters:
name- The Name of the Class you are looking for- Returns:
- The Class in that Package
- Throws:
ClassNotFoundException- If the class could not be found.UnknownServerVersionException- If theMinecraftVersionwas not able to be determined.
-
getInnerOBCClass
@ParametersAreNonnullByDefault @Nonnull public static Class<?> getInnerOBCClass(String name, String subname) throws ClassNotFoundException Returns an OBC Class inside a Class- Parameters:
name- The Name of the Class your Inner class is located insubname- The Name of the inner Class you are looking for- Returns:
- The Class in your specified Class
- Throws:
ClassNotFoundException- If the class could not be found.
-
getOBCClass
Returns an OBC Class via Reflection- Parameters:
name- The Name of the Class you are looking for- Returns:
- The Class in that Package
- Throws:
ClassNotFoundException- If the class could not be found.
-
getEnumConstants
Returns all Enum Constants in an Enum- Type Parameters:
T- The Type argument of the enum we are querying- Parameters:
c- The Enum you are targeting- Returns:
- An ArrayList of all Enum Constants in that Enum
-
getEnumConstant
@ParametersAreNonnullByDefault @Nullable public static <T extends Enum<T>> T getEnumConstant(Class<T> c, String name) Returns a specific Enum Constant in an Enum- Type Parameters:
T- The Type argument of the enum we are querying- Parameters:
c- The Enum you are targetingname- The Name of the Constant you are targeting- Returns:
- The found Enum Constant
-