public final class ReflectionUtils extends Object
This class provides useful methods which makes dealing with reflection much easier, especially when working with Bukkit
You are welcome to use it, modify it and redistribute it under the following conditions:
It would be nice if you provide credit to me if you use this class in a published project
Modifier and Type | Class and Description |
---|---|
static class |
ReflectionUtils.DataType
Represents an enumeration of Java data types with corresponding classes
|
static class |
ReflectionUtils.PackageType
Represents an enumeration of dynamic packages of NMS and CraftBukkit
|
Constructor and Description |
---|
ReflectionUtils() |
Modifier and Type | Method and Description |
---|---|
static Constructor<?> |
getAccessibleConstructor(Class<?> clazz,
Class<?>... parameterTypes)
Returns the accessible constructor of a desired class with the given parameter types
|
static Constructor<?> |
getAccessibleConstructor(String className,
ReflectionUtils.PackageType packageType,
Class<?>... parameterTypes)
Returns the accessible constructor of a desired class with the given parameter types
|
static Field |
getAccessibleField(Class<?> clazz,
boolean declared,
String fieldName)
Returns an accessible field of the target class with the given name
|
static Field |
getAccessibleField(String className,
ReflectionUtils.PackageType packageType,
boolean declared,
String fieldName)
Returns an accessible field of a desired class with the given name
|
static Method |
getAccessibleMethod(Class<?> clazz,
String methodName,
Class<?>... parameterTypes)
Returns an accessible method of a class with the given parameter types
|
static Method |
getAccessibleMethod(String className,
ReflectionUtils.PackageType packageType,
String methodName,
Class<?>... parameterTypes)
Returns an accessible method of a desired class with the given parameter types
|
static Constructor<?> |
getConstructor(Class<?> clazz,
Class<?>... parameterTypes)
Returns the constructor of a given class with the given parameter types
|
static Constructor<?> |
getConstructor(String className,
ReflectionUtils.PackageType packageType,
Class<?>... parameterTypes)
Returns the constructor of a desired class with the given parameter types
|
static Field |
getField(Class<?> clazz,
boolean declared,
String fieldName)
Returns a field of the target class with the given name
|
static Field |
getField(String className,
ReflectionUtils.PackageType packageType,
boolean declared,
String fieldName)
Returns a field of a desired class with the given name
|
static Method |
getMethod(Class<?> clazz,
String methodName,
Class<?>... parameterTypes)
Returns a method of a class with the given parameter types
|
static Method |
getMethod(String className,
ReflectionUtils.PackageType packageType,
String methodName,
Class<?>... parameterTypes)
Returns a method of a desired class with the given parameter types
|
static Object |
getValue(Object instance,
boolean declared,
String fieldName)
Returns the value of a field with the given name of an object
|
static Object |
getValue(Object instance,
Class<?> clazz,
boolean declared,
String fieldName)
Returns the value of a field of the given class of an object
|
static Object |
getValue(Object instance,
String className,
ReflectionUtils.PackageType packageType,
boolean declared,
String fieldName)
Returns the value of a field of a desired class of an object
|
static Object |
instantiateObject(Class<?> clazz,
Object... arguments)
Returns an instance of a class with the given arguments
|
static Object |
instantiateObject(String className,
ReflectionUtils.PackageType packageType,
Object... arguments)
Returns an instance of a desired class with the given arguments
|
static Object |
invokeMethod(Object instance,
Class<?> clazz,
String methodName,
Object... arguments)
Invokes a method of the target class on an object with the given arguments
|
static Object |
invokeMethod(Object instance,
String methodName,
Object... arguments)
Invokes a method on an object with the given arguments
|
static Object |
invokeMethod(Object instance,
String className,
ReflectionUtils.PackageType packageType,
String methodName,
Object... arguments)
Invokes a method of a desired class on an object with the given arguments
|
static void |
setValue(Object instance,
boolean declared,
String fieldName,
Object value)
Sets the value of a field with the given name of an object
|
static void |
setValue(Object instance,
Class<?> clazz,
boolean declared,
String fieldName,
Object value)
Sets the value of a field of the given class of an object
|
static void |
setValue(Object instance,
String className,
ReflectionUtils.PackageType packageType,
boolean declared,
String fieldName,
Object value)
Sets the value of a field of a desired class of an object
|
public static Constructor<?> getConstructor(Class<?> clazz, Class<?>... parameterTypes) throws NoSuchMethodException
clazz
- Target classparameterTypes
- Parameter types of the desired constructorNoSuchMethodException
- If the desired constructor with the specified parameter types cannot be foundReflectionUtils.DataType
,
ReflectionUtils.DataType.getPrimitive(Class[])
,
ReflectionUtils.DataType.compare(Class[], Class[])
public static Constructor<?> getConstructor(String className, ReflectionUtils.PackageType packageType, Class<?>... parameterTypes) throws NoSuchMethodException, ClassNotFoundException
className
- Name of the desired target classpackageType
- Package where the desired target class is locatedparameterTypes
- Parameter types of the desired constructorNoSuchMethodException
- If the desired constructor with the specified parameter types cannot be foundClassNotFoundException
- If the desired target class with the specified name and package cannot be foundgetConstructor(Class, Class...)
public static Constructor<?> getAccessibleConstructor(Class<?> clazz, Class<?>... parameterTypes) throws NoSuchMethodException
clazz
- Target classparameterTypes
- Parameter types of the desired constructorNoSuchMethodException
- If the desired constructor with the specified parameter types cannot be foundReflectionUtils.DataType
,
ReflectionUtils.DataType.getPrimitive(Class[])
,
ReflectionUtils.DataType.compare(Class[], Class[])
public static Constructor<?> getAccessibleConstructor(String className, ReflectionUtils.PackageType packageType, Class<?>... parameterTypes) throws NoSuchMethodException, ClassNotFoundException
className
- Name of the desired target classpackageType
- Package where the desired target class is locatedparameterTypes
- Parameter types of the desired constructorNoSuchMethodException
- If the desired constructor with the specified parameter types cannot be foundClassNotFoundException
- If the desired target class with the specified name and package cannot be foundgetAccessibleConstructor(Class, Class...)
public static Object instantiateObject(Class<?> clazz, Object... arguments) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException
clazz
- Target classarguments
- Arguments which are used to construct an object of the target classInstantiationException
- If you cannot create an instance of the target class due to certain circumstancesIllegalAccessException
- If the desired constructor cannot be accessed due to certain circumstancesIllegalArgumentException
- If the types of the arguments do not match the parameter types of the constructor (this should not occur since it searches for a constructor with the types of the arguments)InvocationTargetException
- If the desired constructor cannot be invokedNoSuchMethodException
- If the desired constructor with the specified arguments cannot be foundpublic static Object instantiateObject(String className, ReflectionUtils.PackageType packageType, Object... arguments) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, ClassNotFoundException
className
- Name of the desired target classpackageType
- Package where the desired target class is locatedarguments
- Arguments which are used to construct an object of the desired target classInstantiationException
- If you cannot create an instance of the desired target class due to certain circumstancesIllegalAccessException
- If the desired constructor cannot be accessed due to certain circumstancesIllegalArgumentException
- If the types of the arguments do not match the parameter types of the constructor (this should not occur since it searches for a constructor with the types of the arguments)InvocationTargetException
- If the desired constructor cannot be invokedNoSuchMethodException
- If the desired constructor with the specified arguments cannot be foundClassNotFoundException
- If the desired target class with the specified name and package cannot be foundinstantiateObject(Class, Object...)
public static Method getMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes) throws NoSuchMethodException
clazz
- Target classmethodName
- Name of the desired methodparameterTypes
- Parameter types of the desired methodNoSuchMethodException
- If the desired method of the target class with the specified name and parameter types cannot be foundReflectionUtils.DataType.getPrimitive(Class[])
,
ReflectionUtils.DataType.compare(Class[], Class[])
public static Method getMethod(String className, ReflectionUtils.PackageType packageType, String methodName, Class<?>... parameterTypes) throws NoSuchMethodException, ClassNotFoundException
className
- Name of the desired target classpackageType
- Package where the desired target class is locatedmethodName
- Name of the desired methodparameterTypes
- Parameter types of the desired methodNoSuchMethodException
- If the desired method of the desired target class with the specified name and parameter types cannot be foundClassNotFoundException
- If the desired target class with the specified name and package cannot be foundgetMethod(Class, String, Class...)
public static Method getAccessibleMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes) throws NoSuchMethodException
clazz
- Target classmethodName
- Name of the desired methodparameterTypes
- Parameter types of the desired methodNoSuchMethodException
- If the desired method of the target class with the specified name and parameter types cannot be foundReflectionUtils.DataType.getPrimitive(Class[])
,
ReflectionUtils.DataType.compare(Class[], Class[])
public static Method getAccessibleMethod(String className, ReflectionUtils.PackageType packageType, String methodName, Class<?>... parameterTypes) throws NoSuchMethodException, ClassNotFoundException
className
- Name of the desired target classpackageType
- Package where the desired target class is locatedmethodName
- Name of the desired methodparameterTypes
- Parameter types of the desired methodNoSuchMethodException
- If the desired method of the desired target class with the specified name and parameter types cannot be foundClassNotFoundException
- If the desired target class with the specified name and package cannot be foundgetAccessibleMethod(Class, String, Class...)
public static Object invokeMethod(Object instance, String methodName, Object... arguments) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException
instance
- Target objectmethodName
- Name of the desired methodarguments
- Arguments which are used to invoke the desired methodIllegalAccessException
- If the desired method cannot be accessed due to certain circumstancesIllegalArgumentException
- If the types of the arguments do not match the parameter types of the method (this should not occur since it searches for a method with the types of the arguments)InvocationTargetException
- If the desired method cannot be invoked on the target objectNoSuchMethodException
- If the desired method of the class of the target object with the specified name and arguments cannot be foundgetMethod(Class, String, Class...)
,
ReflectionUtils.DataType.getPrimitive(Object[])
public static Object invokeMethod(Object instance, Class<?> clazz, String methodName, Object... arguments) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException
instance
- Target objectclazz
- Target classmethodName
- Name of the desired methodarguments
- Arguments which are used to invoke the desired methodIllegalAccessException
- If the desired method cannot be accessed due to certain circumstancesIllegalArgumentException
- If the types of the arguments do not match the parameter types of the method (this should not occur since it searches for a method with the types of the arguments)InvocationTargetException
- If the desired method cannot be invoked on the target objectNoSuchMethodException
- If the desired method of the target class with the specified name and arguments cannot be foundgetMethod(Class, String, Class...)
,
ReflectionUtils.DataType.getPrimitive(Object[])
public static Object invokeMethod(Object instance, String className, ReflectionUtils.PackageType packageType, String methodName, Object... arguments) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, ClassNotFoundException
instance
- Target objectclassName
- Name of the desired target classpackageType
- Package where the desired target class is locatedmethodName
- Name of the desired methodarguments
- Arguments which are used to invoke the desired methodIllegalAccessException
- If the desired method cannot be accessed due to certain circumstancesIllegalArgumentException
- If the types of the arguments do not match the parameter types of the method (this should not occur since it searches for a method with the types of the arguments)InvocationTargetException
- If the desired method cannot be invoked on the target objectNoSuchMethodException
- If the desired method of the desired target class with the specified name and arguments cannot be foundClassNotFoundException
- If the desired target class with the specified name and package cannot be foundinvokeMethod(Object, Class, String, Object...)
public static Field getField(Class<?> clazz, boolean declared, String fieldName) throws NoSuchFieldException, SecurityException
clazz
- Target classdeclared
- Whether the desired field is declared or notfieldName
- Name of the desired fieldNoSuchFieldException
- If the desired field of the given class cannot be foundSecurityException
- If the desired field cannot be made accessiblepublic static Field getField(String className, ReflectionUtils.PackageType packageType, boolean declared, String fieldName) throws NoSuchFieldException, SecurityException, ClassNotFoundException
className
- Name of the desired target classpackageType
- Package where the desired target class is locateddeclared
- Whether the desired field is declared or notfieldName
- Name of the desired fieldNoSuchFieldException
- If the desired field of the desired class cannot be foundSecurityException
- If the desired field cannot be made accessibleClassNotFoundException
- If the desired target class with the specified name and package cannot be foundgetField(Class, boolean, String)
public static Field getAccessibleField(Class<?> clazz, boolean declared, String fieldName) throws NoSuchFieldException, SecurityException
clazz
- Target classdeclared
- Whether the desired field is declared or notfieldName
- Name of the desired fieldNoSuchFieldException
- If the desired field of the given class cannot be foundSecurityException
- If the desired field cannot be made accessiblepublic static Field getAccessibleField(String className, ReflectionUtils.PackageType packageType, boolean declared, String fieldName) throws NoSuchFieldException, SecurityException, ClassNotFoundException
className
- Name of the desired target classpackageType
- Package where the desired target class is locateddeclared
- Whether the desired field is declared or notfieldName
- Name of the desired fieldNoSuchFieldException
- If the desired field of the desired class cannot be foundSecurityException
- If the desired field cannot be made accessibleClassNotFoundException
- If the desired target class with the specified name and package cannot be foundgetAccessibleField(Class, boolean, String)
public static Object getValue(Object instance, Class<?> clazz, boolean declared, String fieldName) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException
instance
- Target objectclazz
- Target classdeclared
- Whether the desired field is declared or notfieldName
- Name of the desired fieldIllegalArgumentException
- If the target object does not feature the desired fieldIllegalAccessException
- If the desired field cannot be accessedNoSuchFieldException
- If the desired field of the target class cannot be foundSecurityException
- If the desired field cannot be made accessiblegetField(Class, boolean, String)
public static Object getValue(Object instance, String className, ReflectionUtils.PackageType packageType, boolean declared, String fieldName) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException, ClassNotFoundException
instance
- Target objectclassName
- Name of the desired target classpackageType
- Package where the desired target class is locateddeclared
- Whether the desired field is declared or notfieldName
- Name of the desired fieldIllegalArgumentException
- If the target object does not feature the desired fieldIllegalAccessException
- If the desired field cannot be accessedNoSuchFieldException
- If the desired field of the desired class cannot be foundSecurityException
- If the desired field cannot be made accessibleClassNotFoundException
- If the desired target class with the specified name and package cannot be foundgetValue(Object, Class, boolean, String)
public static Object getValue(Object instance, boolean declared, String fieldName) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException
instance
- Target objectdeclared
- Whether the desired field is declared or notfieldName
- Name of the desired fieldIllegalArgumentException
- If the target object does not feature the desired field (should not occur since it searches for a field with the given name in the class of the object)IllegalAccessException
- If the desired field cannot be accessedNoSuchFieldException
- If the desired field of the target object cannot be foundSecurityException
- If the desired field cannot be made accessiblegetValue(Object, Class, boolean, String)
public static void setValue(Object instance, Class<?> clazz, boolean declared, String fieldName, Object value) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException
instance
- Target objectclazz
- Target classdeclared
- Whether the desired field is declared or notfieldName
- Name of the desired fieldvalue
- New valueIllegalArgumentException
- If the type of the value does not match the type of the desired fieldIllegalAccessException
- If the desired field cannot be accessedNoSuchFieldException
- If the desired field of the target class cannot be foundSecurityException
- If the desired field cannot be made accessiblegetField(Class, boolean, String)
public static void setValue(Object instance, String className, ReflectionUtils.PackageType packageType, boolean declared, String fieldName, Object value) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException, ClassNotFoundException
instance
- Target objectclassName
- Name of the desired target classpackageType
- Package where the desired target class is locateddeclared
- Whether the desired field is declared or notfieldName
- Name of the desired fieldvalue
- New valueIllegalArgumentException
- If the type of the value does not match the type of the desired fieldIllegalAccessException
- If the desired field cannot be accessedNoSuchFieldException
- If the desired field of the desired class cannot be foundSecurityException
- If the desired field cannot be made accessibleClassNotFoundException
- If the desired target class with the specified name and package cannot be foundsetValue(Object, Class, boolean, String, Object)
public static void setValue(Object instance, boolean declared, String fieldName, Object value) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException
instance
- Target objectdeclared
- Whether the desired field is declared or notfieldName
- Name of the desired fieldvalue
- New valueIllegalArgumentException
- If the type of the value does not match the type of the desired fieldIllegalAccessException
- If the desired field cannot be accessedNoSuchFieldException
- If the desired field of the target object cannot be foundSecurityException
- If the desired field cannot be made accessiblesetValue(Object, Class, boolean, String, Object)
Copyright © 2023. All rights reserved.