Class ReflectionUtil

java.lang.Object
dk.lockfuglsang.minecraft.reflection.ReflectionUtil

public class ReflectionUtil extends Object
Wrapper methods that allow accesss to reflection for backward compatible code.
Since:
1.8
  • Constructor Details

    • ReflectionUtil

      public ReflectionUtil()
  • Method Details

    • getCraftBukkitVersion

      public static String getCraftBukkitVersion()
      Returns the current version of the Bukkit implementation
      Returns:
      the current version of the Bukkit implementation
      Since:
      1.8
    • getNMSVersion

      public static String getNMSVersion(Object nmsObject)
      Returns the current version of the net.minecraft.server implementation
      Parameters:
      nmsObject - A native object from nms namespace
      Returns:
      the current version of the net.minecraft.server implementation
      Since:
      1.8
    • getNMSVersion

      public static String getNMSVersion()
      Returns the NMS version.
      Returns:
      the NMS version (i.e. "v1_10").
      Since:
      1.9
    • nms

      public static String nms()
      Returns the real packagename for the net.minecraft.server.
      Returns:
      the real packagename for the net.minecraft.server.
      Since:
      1.9
    • cb

      public static String cb()
      Returns the real packagename for the org.bukkit.craftbukkit package
      Returns:
      the real packagename for the org.bukkit.craftbukkit package
      Since:
      1.9
    • getPackageName

      public static String getPackageName(Object nmsObject)
      Returns the packagename of the given object.
      Parameters:
      nmsObject - An object
      Returns:
      the packagename of the given object.
      Since:
      1.8
    • getBukkitClass

      public static Class<?> getBukkitClass(Object craftObject)
      Returns the corresponding Bukkit class, given a CraftBukkit implementation object.
      Parameters:
      craftObject - A CraftBukkit implementation of a Bukkit class.
      Returns:
      the corresponding Bukkit class, given a CraftBukkit implementation object.
      Since:
      1.8
    • execStatic

      public static <T> T execStatic(Class<?> clazz, String methodName, Object... args)
      Uses reflection to execute the named method on the supplied class giving the arguments. Sinks all exceptions, but log an entry and returns null
      Parameters:
      clazz - The class on which to invoke the method
      methodName - The name of the method to invoke
      args - The arguments to supply to the method
      Returns:
      null or the return-object from the method.
      Since:
      1.8
    • exec

      public static <T> T exec(Object obj, String methodName, Class[] argTypes, Object... args)
      Uses reflection to execute the named method on the supplied class giving the arguments. Sinks all exceptions, but log an entry and returns null
      Parameters:
      obj - The object on which to invoke the method
      methodName - The name of the method to invoke
      argTypes - An array of argument-types (classes).
      args - The arguments to supply to the method
      Returns:
      null or the return-object from the method.
      Since:
      1.8
    • getMethod

      public static Method getMethod(Class<?> aClass, String methodName, Class... argTypes) throws NoSuchMethodException
      Throws:
      NoSuchMethodException
    • findMethod

      public static Method findMethod(Class<?> aClass, Class returnType, Class... argTypes) throws NoSuchMethodException
      Throws:
      NoSuchMethodException
    • findMethods

      public static List<Method> findMethods(Class<?> aClass, Class returnType, Class... argTypes) throws NoSuchMethodException
      Throws:
      NoSuchMethodException
    • exec

      public static <T> T exec(Object obj, String methodName, Object... args)
      Uses reflection to execute the named method on the supplied class giving the arguments. Sinks all exceptions, but log an entry and returns null
      Parameters:
      obj - The object on which to invoke the method
      methodName - The name of the method to invoke
      args - The arguments to supply to the method
      Returns:
      null or the return-object from the method.
      Since:
      1.8
    • getField

      public static <T> T getField(Object obj, String fieldName)
      Returns the value of a field on the object.
      Type Parameters:
      T - The type of field
      Parameters:
      obj - The object
      fieldName - The name of the field
      Returns:
      the value or null
      Since:
      1.9
    • setField

      public static <T> void setField(Object obj, String fieldName, T field)
      Sets the value of a field on the object.
      Type Parameters:
      T - The type of field
      Parameters:
      obj - The object
      fieldName - The name of the field
      field - The value to set
      Since:
      1.9
    • newInstance

      public static <T> T newInstance(String className, Class<?>[] argTypes, Object... args)
      Instantiates an object.
      Type Parameters:
      T - Return-type
      Parameters:
      className - The name of the class
      argTypes - An array of argument-types
      args - An array of arguments
      Returns:
      the object, or null.
      Since:
      1.9
    • newInstance

      public static <T> T newInstance(String className, Object... args)
      Instantiates an object.
      Type Parameters:
      T - Return-type
      Parameters:
      className - The name of the class
      args - An array of arguments
      Returns:
      the object, or null.
      Since:
      1.9
    • dumpMethods

      public static List<String> dumpMethods(Class aClass)