public final class ReflectionUtils
extends java.lang.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
|
Modifier and Type | Method and Description |
---|---|
static java.lang.reflect.Constructor<?> |
getConstructor(java.lang.Class<?> clazz,
java.lang.Class<?>... parameterTypes)
Returns the constructor of a given class with the given parameter types
|
static java.lang.reflect.Constructor<?> |
getConstructor(java.lang.String className,
ReflectionUtils.PackageType packageType,
java.lang.Class<?>... parameterTypes)
Returns the constructor of a desired class with the given parameter types
|
static java.lang.reflect.Field |
getField(java.lang.Class<?> clazz,
boolean declared,
java.lang.String fieldName)
Returns a field of the target class with the given name
|
static java.lang.reflect.Field |
getField(java.lang.String className,
ReflectionUtils.PackageType packageType,
boolean declared,
java.lang.String fieldName)
Returns a field of a desired class with the given name
|
static java.lang.reflect.Method |
getMethod(java.lang.Class<?> clazz,
java.lang.String methodName,
java.lang.Class<?>... parameterTypes)
Returns a method of a class with the given parameter types
|
static java.lang.reflect.Method |
getMethod(java.lang.String className,
ReflectionUtils.PackageType packageType,
java.lang.String methodName,
java.lang.Class<?>... parameterTypes)
Returns a method of a desired class with the given parameter types
|
static java.lang.Object |
getValue(java.lang.Object instance,
boolean declared,
java.lang.String fieldName)
Returns the value of a field with the given name of an object
|
static java.lang.Object |
getValue(java.lang.Object instance,
java.lang.Class<?> clazz,
boolean declared,
java.lang.String fieldName)
Returns the value of a field of the given class of an object
|
static java.lang.Object |
getValue(java.lang.Object instance,
java.lang.String className,
ReflectionUtils.PackageType packageType,
boolean declared,
java.lang.String fieldName)
Returns the value of a field of a desired class of an object
|
static java.lang.Object |
instantiateObject(java.lang.Class<?> clazz,
java.lang.Object... arguments)
Returns an instance of a class with the given arguments
|
static java.lang.Object |
instantiateObject(java.lang.String className,
ReflectionUtils.PackageType packageType,
java.lang.Object... arguments)
Returns an instance of a desired class with the given arguments
|
static java.lang.Object |
invokeMethod(java.lang.Object instance,
java.lang.Class<?> clazz,
java.lang.String methodName,
java.lang.Object... arguments)
Invokes a method of the target class on an object with the given arguments
|
static java.lang.Object |
invokeMethod(java.lang.Object instance,
java.lang.String methodName,
java.lang.Object... arguments)
Invokes a method on an object with the given arguments
|
static java.lang.Object |
invokeMethod(java.lang.Object instance,
java.lang.String className,
ReflectionUtils.PackageType packageType,
java.lang.String methodName,
java.lang.Object... arguments)
Invokes a method of a desired class on an object with the given arguments
|
static void |
setValue(java.lang.Object instance,
boolean declared,
java.lang.String fieldName,
java.lang.Object value)
Sets the value of a field with the given name of an object
|
static void |
setValue(java.lang.Object instance,
java.lang.Class<?> clazz,
boolean declared,
java.lang.String fieldName,
java.lang.Object value)
Sets the value of a field of the given class of an object
|
static void |
setValue(java.lang.Object instance,
java.lang.String className,
ReflectionUtils.PackageType packageType,
boolean declared,
java.lang.String fieldName,
java.lang.Object value)
Sets the value of a field of a desired class of an object
|
public static java.lang.reflect.Constructor<?> getConstructor(java.lang.Class<?> clazz, java.lang.Class<?>... parameterTypes) throws java.lang.NoSuchMethodException
clazz
- Target classparameterTypes
- Parameter types of the desired constructorjava.lang.NoSuchMethodException
- If the desired constructor with the specified parameter types cannot be foundReflectionUtils.DataType
,
ReflectionUtils.DataType.getPrimitive(Class[])
,
ReflectionUtils.DataType.compare(Class[], Class[])
public static java.lang.reflect.Constructor<?> getConstructor(java.lang.String className, ReflectionUtils.PackageType packageType, java.lang.Class<?>... parameterTypes) throws java.lang.NoSuchMethodException, java.lang.ClassNotFoundException
className
- Name of the desired target classpackageType
- Package where the desired target class is locatedparameterTypes
- Parameter types of the desired constructorjava.lang.NoSuchMethodException
- If the desired constructor with the specified parameter types cannot be foundjava.lang.ClassNotFoundException
- ClassNotFoundException If the desired target class with the specified name and package cannot be found#getClass(String, PackageType)
,
getConstructor(Class, Class...)
public static java.lang.Object instantiateObject(java.lang.Class<?> clazz, java.lang.Object... arguments) throws java.lang.InstantiationException, java.lang.IllegalAccessException, java.lang.IllegalArgumentException, java.lang.reflect.InvocationTargetException, java.lang.NoSuchMethodException
clazz
- Target classarguments
- Arguments which are used to construct an object of the target classjava.lang.InstantiationException
- If you cannot create an instance of the target class due to certain circumstancesjava.lang.IllegalAccessException
- If the desired constructor cannot be accessed due to certain circumstancesjava.lang.IllegalArgumentException
- 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)java.lang.reflect.InvocationTargetException
- If the desired constructor cannot be invokedjava.lang.NoSuchMethodException
- If the desired constructor with the specified arguments cannot be foundpublic static java.lang.Object instantiateObject(java.lang.String className, ReflectionUtils.PackageType packageType, java.lang.Object... arguments) throws java.lang.InstantiationException, java.lang.IllegalAccessException, java.lang.IllegalArgumentException, java.lang.reflect.InvocationTargetException, java.lang.NoSuchMethodException, java.lang.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 classjava.lang.InstantiationException
- If you cannot create an instance of the desired target class due to certain circumstancesjava.lang.IllegalAccessException
- If the desired constructor cannot be accessed due to certain circumstancesjava.lang.IllegalArgumentException
- 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)java.lang.reflect.InvocationTargetException
- If the desired constructor cannot be invokedjava.lang.NoSuchMethodException
- If the desired constructor with the specified arguments cannot be foundjava.lang.ClassNotFoundException
- If the desired target class with the specified name and package cannot be found#getClass(String, PackageType)
,
instantiateObject(Class, Object...)
public static java.lang.reflect.Method getMethod(java.lang.Class<?> clazz, java.lang.String methodName, java.lang.Class<?>... parameterTypes) throws java.lang.NoSuchMethodException
clazz
- Target classmethodName
- Name of the desired methodparameterTypes
- Parameter types of the desired methodjava.lang.NoSuchMethodException
- 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 java.lang.reflect.Method getMethod(java.lang.String className, ReflectionUtils.PackageType packageType, java.lang.String methodName, java.lang.Class<?>... parameterTypes) throws java.lang.NoSuchMethodException, java.lang.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 methodjava.lang.NoSuchMethodException
- If the desired method of the desired target class with the specified name and parameter types cannot be foundjava.lang.ClassNotFoundException
- If the desired target class with the specified name and package cannot be found#getClass(String, PackageType)
,
getMethod(Class, String, Class...)
public static java.lang.Object invokeMethod(java.lang.Object instance, java.lang.String methodName, java.lang.Object... arguments) throws java.lang.IllegalAccessException, java.lang.IllegalArgumentException, java.lang.reflect.InvocationTargetException, java.lang.NoSuchMethodException
instance
- Target objectmethodName
- Name of the desired methodarguments
- Arguments which are used to invoke the desired methodjava.lang.IllegalAccessException
- If the desired method cannot be accessed due to certain circumstancesjava.lang.IllegalArgumentException
- 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)java.lang.reflect.InvocationTargetException
- If the desired method cannot be invoked on the target objectjava.lang.NoSuchMethodException
- 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 java.lang.Object invokeMethod(java.lang.Object instance, java.lang.Class<?> clazz, java.lang.String methodName, java.lang.Object... arguments) throws java.lang.IllegalAccessException, java.lang.IllegalArgumentException, java.lang.reflect.InvocationTargetException, java.lang.NoSuchMethodException
instance
- Target objectclazz
- Target classmethodName
- Name of the desired methodarguments
- Arguments which are used to invoke the desired methodjava.lang.IllegalAccessException
- If the desired method cannot be accessed due to certain circumstancesjava.lang.IllegalArgumentException
- 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)java.lang.reflect.InvocationTargetException
- If the desired method cannot be invoked on the target objectjava.lang.NoSuchMethodException
- 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 java.lang.Object invokeMethod(java.lang.Object instance, java.lang.String className, ReflectionUtils.PackageType packageType, java.lang.String methodName, java.lang.Object... arguments) throws java.lang.IllegalAccessException, java.lang.IllegalArgumentException, java.lang.reflect.InvocationTargetException, java.lang.NoSuchMethodException, java.lang.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 methodjava.lang.IllegalAccessException
- If the desired method cannot be accessed due to certain circumstancesjava.lang.IllegalArgumentException
- 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)java.lang.reflect.InvocationTargetException
- If the desired method cannot be invoked on the target objectjava.lang.NoSuchMethodException
- If the desired method of the desired target class with the specified name and arguments cannot be foundjava.lang.ClassNotFoundException
- If the desired target class with the specified name and package cannot be found#getClass(String, PackageType)
,
invokeMethod(Object, Class, String, Object...)
public static java.lang.reflect.Field getField(java.lang.Class<?> clazz, boolean declared, java.lang.String fieldName) throws java.lang.NoSuchFieldException, java.lang.SecurityException
clazz
- Target classdeclared
- Whether the desired field is declared or notfieldName
- Name of the desired fieldjava.lang.NoSuchFieldException
- If the desired field of the given class cannot be foundjava.lang.SecurityException
- If the desired field cannot be made accessiblepublic static java.lang.reflect.Field getField(java.lang.String className, ReflectionUtils.PackageType packageType, boolean declared, java.lang.String fieldName) throws java.lang.NoSuchFieldException, java.lang.SecurityException, java.lang.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 fieldjava.lang.NoSuchFieldException
- If the desired field of the desired class cannot be foundjava.lang.SecurityException
- If the desired field cannot be made accessiblejava.lang.ClassNotFoundException
- If the desired target class with the specified name and package cannot be foundgetField(Class, boolean, String)
public static java.lang.Object getValue(java.lang.Object instance, java.lang.Class<?> clazz, boolean declared, java.lang.String fieldName) throws java.lang.IllegalArgumentException, java.lang.IllegalAccessException, java.lang.NoSuchFieldException, java.lang.SecurityException
instance
- Target objectclazz
- Target classdeclared
- Whether the desired field is declared or notfieldName
- Name of the desired fieldjava.lang.IllegalArgumentException
- If the target object does not feature the desired fieldjava.lang.IllegalAccessException
- If the desired field cannot be accessedjava.lang.NoSuchFieldException
- If the desired field of the target class cannot be foundjava.lang.SecurityException
- If the desired field cannot be made accessiblegetField(Class, boolean, String)
public static java.lang.Object getValue(java.lang.Object instance, java.lang.String className, ReflectionUtils.PackageType packageType, boolean declared, java.lang.String fieldName) throws java.lang.IllegalArgumentException, java.lang.IllegalAccessException, java.lang.NoSuchFieldException, java.lang.SecurityException, java.lang.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 fieldjava.lang.IllegalArgumentException
- If the target object does not feature the desired fieldjava.lang.IllegalAccessException
- If the desired field cannot be accessedjava.lang.NoSuchFieldException
- If the desired field of the desired class cannot be foundjava.lang.SecurityException
- If the desired field cannot be made accessiblejava.lang.ClassNotFoundException
- If the desired target class with the specified name and package cannot be foundgetValue(Object, Class, boolean, String)
public static java.lang.Object getValue(java.lang.Object instance, boolean declared, java.lang.String fieldName) throws java.lang.IllegalArgumentException, java.lang.IllegalAccessException, java.lang.NoSuchFieldException, java.lang.SecurityException
instance
- Target objectdeclared
- Whether the desired field is declared or notfieldName
- Name of the desired fieldjava.lang.IllegalArgumentException
- 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)java.lang.IllegalAccessException
- If the desired field cannot be accessedjava.lang.NoSuchFieldException
- If the desired field of the target object cannot be foundjava.lang.SecurityException
- If the desired field cannot be made accessiblegetValue(Object, Class, boolean, String)
public static void setValue(java.lang.Object instance, java.lang.Class<?> clazz, boolean declared, java.lang.String fieldName, java.lang.Object value) throws java.lang.IllegalArgumentException, java.lang.IllegalAccessException, java.lang.NoSuchFieldException, java.lang.SecurityException
instance
- Target objectclazz
- Target classdeclared
- Whether the desired field is declared or notfieldName
- Name of the desired fieldvalue
- New valuejava.lang.IllegalArgumentException
- If the type of the value does not match the type of the desired fieldjava.lang.IllegalAccessException
- If the desired field cannot be accessedjava.lang.NoSuchFieldException
- If the desired field of the target class cannot be foundjava.lang.SecurityException
- If the desired field cannot be made accessiblegetField(Class, boolean, String)
public static void setValue(java.lang.Object instance, java.lang.String className, ReflectionUtils.PackageType packageType, boolean declared, java.lang.String fieldName, java.lang.Object value) throws java.lang.IllegalArgumentException, java.lang.IllegalAccessException, java.lang.NoSuchFieldException, java.lang.SecurityException, java.lang.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 valuejava.lang.IllegalArgumentException
- If the type of the value does not match the type of the desired fieldjava.lang.IllegalAccessException
- If the desired field cannot be accessedjava.lang.NoSuchFieldException
- If the desired field of the desired class cannot be foundjava.lang.SecurityException
- If the desired field cannot be made accessiblejava.lang.ClassNotFoundException
- If the desired target class with the specified name and package cannot be foundsetValue(Object, Class, boolean, String, Object)
public static void setValue(java.lang.Object instance, boolean declared, java.lang.String fieldName, java.lang.Object value) throws java.lang.IllegalArgumentException, java.lang.IllegalAccessException, java.lang.NoSuchFieldException, java.lang.SecurityException
instance
- Target objectdeclared
- Whether the desired field is declared or notfieldName
- Name of the desired fieldvalue
- New valuejava.lang.IllegalArgumentException
- If the type of the value does not match the type of the desired fieldjava.lang.IllegalAccessException
- If the desired field cannot be accessedjava.lang.NoSuchFieldException
- If the desired field of the target object cannot be foundjava.lang.SecurityException
- If the desired field cannot be made accessiblesetValue(Object, Class, boolean, String, Object)