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 classes
    static 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 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 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 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 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

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • getConstructor

      public static java.lang.reflect.Constructor<?> getConstructor​(java.lang.Class<?> clazz, java.lang.Class<?>... parameterTypes) throws java.lang.NoSuchMethodException
      Returns the constructor of a given class with the given parameter types
      Parameters:
      clazz - Target class
      parameterTypes - 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.ClassNotFoundException
      Returns the constructor of a desired class with the given parameter types
      Parameters:
      className - Name of the desired target class
      packageType - Package where the desired target class is located
      parameterTypes - 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 found
      java.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.NoSuchMethodException
      Returns an instance of a class with the given arguments
      Parameters:
      clazz - Target class
      arguments - 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 circumstances
      java.lang.IllegalAccessException - If the desired constructor cannot be accessed due to certain circumstances
      java.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 invoked
      java.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.ClassNotFoundException
      Returns an instance of a desired class with the given arguments
      Parameters:
      className - Name of the desired target class
      packageType - Package where the desired target class is located
      arguments - 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 circumstances
      java.lang.IllegalAccessException - If the desired constructor cannot be accessed due to certain circumstances
      java.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 invoked
      java.lang.NoSuchMethodException - If the desired constructor with the specified arguments cannot be found
      java.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.NoSuchMethodException
      Returns a method of a class with the given parameter types
      Parameters:
      clazz - Target class
      methodName - Name of the desired method
      parameterTypes - 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.ClassNotFoundException
      Returns a method of a desired class with the given parameter types
      Parameters:
      className - Name of the desired target class
      packageType - Package where the desired target class is located
      methodName - Name of the desired method
      parameterTypes - 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 found
      java.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.NoSuchMethodException
      Invokes a method on an object with the given arguments
      Parameters:
      instance - Target object
      methodName - Name of the desired method
      arguments - 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 circumstances
      java.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 object
      java.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.NoSuchMethodException
      Invokes a method of the target class on an object with the given arguments
      Parameters:
      instance - Target object
      clazz - Target class
      methodName - Name of the desired method
      arguments - 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 circumstances
      java.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 object
      java.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.ClassNotFoundException
      Invokes a method of a desired class on an object with the given arguments
      Parameters:
      instance - Target object
      className - Name of the desired target class
      packageType - Package where the desired target class is located
      methodName - Name of the desired method
      arguments - 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 circumstances
      java.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 object
      java.lang.NoSuchMethodException - If the desired method of the desired target class with the specified name and arguments cannot be found
      java.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.SecurityException
      Returns a field of the target class with the given name
      Parameters:
      clazz - Target class
      declared - Whether the desired field is declared or not
      fieldName - 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 found
      java.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.ClassNotFoundException
      Returns a field of a desired class with the given name
      Parameters:
      className - Name of the desired target class
      packageType - Package where the desired target class is located
      declared - Whether the desired field is declared or not
      fieldName - 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 found
      java.lang.SecurityException - If the desired field cannot be made accessible
      java.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.SecurityException
      Returns the value of a field of the given class of an object
      Parameters:
      instance - Target object
      clazz - Target class
      declared - Whether the desired field is declared or not
      fieldName - 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
      java.lang.IllegalAccessException - If the desired field cannot be accessed
      java.lang.NoSuchFieldException - If the desired field of the target class cannot be found
      java.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.ClassNotFoundException
      Returns the value of a field of a desired class of an object
      Parameters:
      instance - Target object
      className - Name of the desired target class
      packageType - Package where the desired target class is located
      declared - Whether the desired field is declared or not
      fieldName - 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
      java.lang.IllegalAccessException - If the desired field cannot be accessed
      java.lang.NoSuchFieldException - If the desired field of the desired class cannot be found
      java.lang.SecurityException - If the desired field cannot be made accessible
      java.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.SecurityException
      Returns the value of a field with the given name of an object
      Parameters:
      instance - Target object
      declared - Whether the desired field is declared or not
      fieldName - 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 accessed
      java.lang.NoSuchFieldException - If the desired field of the target object cannot be found
      java.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.SecurityException
      Sets the value of a field of the given class of an object
      Parameters:
      instance - Target object
      clazz - Target class
      declared - Whether the desired field is declared or not
      fieldName - Name of the desired field
      value - New value
      Throws:
      java.lang.IllegalArgumentException - If the type of the value does not match the type of the desired field
      java.lang.IllegalAccessException - If the desired field cannot be accessed
      java.lang.NoSuchFieldException - If the desired field of the target class cannot be found
      java.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.ClassNotFoundException
      Sets the value of a field of a desired class of an object
      Parameters:
      instance - Target object
      className - Name of the desired target class
      packageType - Package where the desired target class is located
      declared - Whether the desired field is declared or not
      fieldName - Name of the desired field
      value - New value
      Throws:
      java.lang.IllegalArgumentException - If the type of the value does not match the type of the desired field
      java.lang.IllegalAccessException - If the desired field cannot be accessed
      java.lang.NoSuchFieldException - If the desired field of the desired class cannot be found
      java.lang.SecurityException - If the desired field cannot be made accessible
      java.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.SecurityException
      Sets the value of a field with the given name of an object
      Parameters:
      instance - Target object
      declared - Whether the desired field is declared or not
      fieldName - Name of the desired field
      value - New value
      Throws:
      java.lang.IllegalArgumentException - If the type of the value does not match the type of the desired field
      java.lang.IllegalAccessException - If the desired field cannot be accessed
      java.lang.NoSuchFieldException - If the desired field of the target object cannot be found
      java.lang.SecurityException - If the desired field cannot be made accessible
      See Also:
      setValue(Object, Class, boolean, String, Object)