Class ReflectionUtils
java.lang.Object
dev.esophose.playerparticles.particles.spawning.reflective.ReflectionUtils
public final class ReflectionUtils
extends java.lang.Object
ReflectionUtils
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:
- Don't claim this class as your own
- Don't remove this disclaimer
It would be nice if you provide credit to me if you use this class in a published project
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ReflectionUtils.DataType
Represents an enumeration of Java data types with corresponding classesstatic class
ReflectionUtils.PackageType
Represents an enumeration of dynamic packages of NMS and CraftBukkit -
Method Summary
Modifier and Type Method 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 typesstatic 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 typesstatic 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 namestatic 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 namestatic 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 typesstatic 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 typesstatic 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 objectstatic 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 objectstatic 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 objectstatic java.lang.Object
instantiateObject(java.lang.Class<?> clazz, java.lang.Object... arguments)
Returns an instance of a class with the given argumentsstatic 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 argumentsstatic 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 argumentsstatic 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 argumentsstatic 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 argumentsstatic 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 objectstatic 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 objectstatic 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
-
Method Details
-
getConstructor
public static java.lang.reflect.Constructor<?> getConstructor(java.lang.Class<?> clazz, java.lang.Class<?>... parameterTypes) throws java.lang.NoSuchMethodExceptionReturns the constructor of a given class with the given parameter types- Parameters:
clazz
- Target classparameterTypes
- Parameter types of the desired constructor- Returns:
- The constructor of the target class with the specified parameter types
- Throws:
java.lang.NoSuchMethodException
- If the desired constructor with the specified parameter types cannot be found- See Also:
ReflectionUtils.DataType
,ReflectionUtils.DataType.getPrimitive(Class[])
,ReflectionUtils.DataType.compare(Class[], Class[])
-
getConstructor
public static java.lang.reflect.Constructor<?> getConstructor(java.lang.String className, ReflectionUtils.PackageType packageType, java.lang.Class<?>... parameterTypes) throws java.lang.NoSuchMethodException, java.lang.ClassNotFoundExceptionReturns the constructor of a desired class with the given parameter types- Parameters:
className
- Name of the desired target classpackageType
- Package where the desired target class is locatedparameterTypes
- Parameter types of the desired constructor- Returns:
- The constructor of the desired target class with the specified parameter types
- Throws:
java.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
-
instantiateObject
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.NoSuchMethodExceptionReturns an instance of a class with the given arguments- Parameters:
clazz
- Target classarguments
- Arguments which are used to construct an object of the target class- Returns:
- The instance of the target class with the specified arguments
- Throws:
java.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 found
-
instantiateObject
public 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.ClassNotFoundExceptionReturns an instance of a desired class with the given arguments- Parameters:
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 class- Returns:
- The instance of the desired target class with the specified arguments
- Throws:
java.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
-
getMethod
public static java.lang.reflect.Method getMethod(java.lang.Class<?> clazz, java.lang.String methodName, java.lang.Class<?>... parameterTypes) throws java.lang.NoSuchMethodExceptionReturns a method of a class with the given parameter types- Parameters:
clazz
- Target classmethodName
- Name of the desired methodparameterTypes
- Parameter types of the desired method- Returns:
- The method of the target class with the specified name and parameter types
- Throws:
java.lang.NoSuchMethodException
- If the desired method of the target class with the specified name and parameter types cannot be found- See Also:
ReflectionUtils.DataType.getPrimitive(Class[])
,ReflectionUtils.DataType.compare(Class[], Class[])
-
getMethod
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.ClassNotFoundExceptionReturns a method of a desired class with the given parameter types- Parameters:
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 method- Returns:
- The method of the desired target class with the specified name and parameter types
- Throws:
java.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
-
invokeMethod
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.NoSuchMethodExceptionInvokes a method on an object with the given arguments- Parameters:
instance
- Target objectmethodName
- Name of the desired methodarguments
- Arguments which are used to invoke the desired method- Returns:
- The result of invoking the desired method on the target object
- Throws:
java.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 found
-
invokeMethod
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.NoSuchMethodExceptionInvokes a method of the target class on an object with the given arguments- Parameters:
instance
- Target objectclazz
- Target classmethodName
- Name of the desired methodarguments
- Arguments which are used to invoke the desired method- Returns:
- The result of invoking the desired method on the target object
- Throws:
java.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 found- See Also:
getMethod(Class, String, Class...)
,ReflectionUtils.DataType.getPrimitive(Object[])
-
invokeMethod
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.ClassNotFoundExceptionInvokes a method of a desired class on an object with the given arguments- Parameters:
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 method- Returns:
- The result of invoking the desired method on the target object
- Throws:
java.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
-
getField
public static java.lang.reflect.Field getField(java.lang.Class<?> clazz, boolean declared, java.lang.String fieldName) throws java.lang.NoSuchFieldException, java.lang.SecurityExceptionReturns a field of the target class with the given name- Parameters:
clazz
- Target classdeclared
- Whether the desired field is declared or notfieldName
- Name of the desired field- Returns:
- The field of the target class with the specified name
- Throws:
java.lang.NoSuchFieldException
- If the desired field of the given class cannot be foundjava.lang.SecurityException
- If the desired field cannot be made accessible
-
getField
public 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.ClassNotFoundExceptionReturns a field of a desired class with the given name- Parameters:
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 field- Returns:
- The field of the desired target class with the specified name
- Throws:
java.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 found- See Also:
getField(Class, boolean, String)
-
getValue
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.SecurityExceptionReturns the value of a field of the given class of an object- Parameters:
instance
- Target objectclazz
- Target classdeclared
- Whether the desired field is declared or notfieldName
- Name of the desired field- Returns:
- The value of field of the target object
- Throws:
java.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 accessible- See Also:
getField(Class, boolean, String)
-
getValue
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.ClassNotFoundExceptionReturns the value of a field of a desired class of an object- Parameters:
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 field- Returns:
- The value of field of the target object
- Throws:
java.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 found- See Also:
getValue(Object, Class, boolean, String)
-
getValue
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.SecurityExceptionReturns the value of a field with the given name of an object- Parameters:
instance
- Target objectdeclared
- Whether the desired field is declared or notfieldName
- Name of the desired field- Returns:
- The value of field of the target object
- Throws:
java.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 accessible- See Also:
getValue(Object, Class, boolean, String)
-
setValue
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.SecurityExceptionSets the value of a field of the given class of an object- Parameters:
instance
- Target objectclazz
- Target classdeclared
- Whether the desired field is declared or notfieldName
- Name of the desired fieldvalue
- New value- Throws:
java.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 accessible- See Also:
getField(Class, boolean, String)
-
setValue
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.ClassNotFoundExceptionSets the value of a field of a desired class of an object- Parameters:
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 value- Throws:
java.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 found- See Also:
setValue(Object, Class, boolean, String, Object)
-
setValue
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.SecurityExceptionSets the value of a field with the given name of an object- Parameters:
instance
- Target objectdeclared
- Whether the desired field is declared or notfieldName
- Name of the desired fieldvalue
- New value- Throws:
java.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 accessible- See Also:
setValue(Object, Class, boolean, String, Object)
-