Class Reflection


  • public final class Reflection
    extends java.lang.Object
    An utility class that simplifies reflection in Bukkit plugins.

    Modified Reflection utility from LagMonitor plugin. https://github.com/games647/LagMonitor/blob/master/src/main/java/com/github/games647/lagmonitor/traffic/Reflection.java

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static interface  Reflection.FieldAccessor<T>
      An interface for retrieving the field content.
      static interface  Reflection.MethodInvoker
      An interface for invoking a specific method.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.Class<?> getCraftBukkitClass​(java.lang.String name)
      Retrieve a class in the org.bukkit.craftbukkit.VERSION.* package.
      static <T> Reflection.FieldAccessor<T> getField​(java.lang.Class<?> target, java.lang.String name, java.lang.Class<T> fieldType)
      Retrieve a field accessor for a specific field type and name.
      static Reflection.MethodInvoker getMethod​(java.lang.Class<?> clazz, java.lang.String methodName, java.lang.Class<?>... params)
      Search for the first publicly and privately defined method of the given name and parameter count.
      static java.lang.Class<?> getMinecraftClass​(java.lang.String name)
      Retrieve a class in the net.minecraft.server.VERSION.* package.
      static Reflection.MethodInvoker getTypedMethod​(java.lang.Class<?> clazz, java.lang.String methodName, java.lang.Class<?> returnType, java.lang.Class<?>... params)
      Search for the first publicly and privately defined method of the given name and parameter count.
      • Methods inherited from class java.lang.Object

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

      • getField

        public static <T> Reflection.FieldAccessor<T> getField​(java.lang.Class<?> target,
                                                               java.lang.String name,
                                                               java.lang.Class<T> fieldType)
        Retrieve a field accessor for a specific field type and name.
        Type Parameters:
        T - Type of the field.
        Parameters:
        target - - the target type.
        name - - the name of the field, or NULL to ignore.
        fieldType - - a compatible field type.
        Returns:
        The field accessor.
      • getMethod

        public static Reflection.MethodInvoker getMethod​(java.lang.Class<?> clazz,
                                                         java.lang.String methodName,
                                                         java.lang.Class<?>... params)
        Search for the first publicly and privately defined method of the given name and parameter count.
        Parameters:
        clazz - - a class to start with.
        methodName - - the method name, or NULL to skip.
        params - - the expected parameters.
        Returns:
        An object that invokes this specific method.
        Throws:
        java.lang.IllegalStateException - If we cannot find this method.
      • getTypedMethod

        public static Reflection.MethodInvoker getTypedMethod​(java.lang.Class<?> clazz,
                                                              java.lang.String methodName,
                                                              java.lang.Class<?> returnType,
                                                              java.lang.Class<?>... params)
        Search for the first publicly and privately defined method of the given name and parameter count.
        Parameters:
        clazz - - a class to start with.
        methodName - - the method name, or NULL to skip.
        returnType - - the expected return type, or NULL to ignore.
        params - - the expected parameters.
        Returns:
        An object that invokes this specific method.
        Throws:
        java.lang.IllegalStateException - If we cannot find this method.
      • getMinecraftClass

        public static java.lang.Class<?> getMinecraftClass​(java.lang.String name)
        Retrieve a class in the net.minecraft.server.VERSION.* package.
        Parameters:
        name - - the name of the class, excluding the package.
        Returns:
        The found class.
        Throws:
        java.lang.IllegalArgumentException - If the class doesn't exist.
      • getCraftBukkitClass

        public static java.lang.Class<?> getCraftBukkitClass​(java.lang.String name)
        Retrieve a class in the org.bukkit.craftbukkit.VERSION.* package.
        Parameters:
        name - - the name of the class, excluding the package.
        Returns:
        The found class
        Throws:
        java.lang.IllegalArgumentException - If the class doesn't exist.