Class ReflectionUtils

java.lang.Object
org.geysermc.floodgate.core.util.ReflectionUtils

public final class ReflectionUtils extends Object
  • Constructor Details

    • ReflectionUtils

      public ReflectionUtils()
  • Method Details

    • getPrefixedClass

      @Nullable public static Class<?> getPrefixedClass(String className)
      Get a class that is prefixed with the prefix provided in setPrefix(String). Calling this method is equal to calling getClass(String) with prefix.classname as class name.
      Parameters:
      className - the prefix class to find
      Returns:
      the class if found, otherwise null
    • getPrefixedClassSilently

      @Nullable public static Class<?> getPrefixedClassSilently(String className)
    • getClass

      @Nullable public static Class<?> getClass(String className)
      Get the class from a class name. Calling this method is equal to calling Class.forName(String) where String is the class name.
      This method will return null when the class isn't found instead of throwing the exception, but the exception will be printed to the console.
      Parameters:
      className - the name of the class to find
      Returns:
      the class or null if the class wasn't found.
    • getCastedClass

      @Nullable public static <T> Class<T> getCastedClass(String className)
    • getClassSilently

      public static Class<?> getClassSilently(String className)
    • getClassOrThrow

      public static Class<?> getClassOrThrow(String className)
    • getClassOrFallbackPrefixed

      public static Class<?> getClassOrFallbackPrefixed(String className, String fallbackClassName)
    • getClassOrFallback

      public static Class<?> getClassOrFallback(String className, String fallbackClassName)
    • getConstructor

      @Nullable public static <T> Constructor<T> getConstructor(Class<T> clazz, boolean declared, Class<?>... parameters)
    • newInstance

      @Nullable public static <T> T newInstance(Constructor<T> constructor, Object... parameters)
    • getField

      @Nullable public static Field getField(Class<?> clazz, String fieldName, boolean declared)
      Get a field of a class. Calling this method is equal to calling Class.getField(String) where String is the fieldName when isPublic is true and calling this method is equal to calling Class.getDeclaredField(String) where String is the fieldName when isPublic is false.
      Please note that this method will return null instead of throwing the exception.
      Parameters:
      clazz - the class name to get the field from
      fieldName - the name of the field
      declared - if the field is declared.
      Returns:
      the field if found, otherwise null
    • getField

      @Nullable public static Field getField(Class<?> clazz, String fieldName)
      Get a field from a class, it doesn't matter if the field is public or not. This method will first try to get a declared field and if that failed it'll try to get a public field.
      Parameters:
      clazz - the class to get the field from
      fieldName - the name of the field
      Returns:
      the field if found, otherwise null
    • getFieldOfType

      @Nullable public static Field getFieldOfType(Class<?> clazz, Class<?> fieldType, boolean declared)
      Get a field from a class without having to provide a field name.
      Parameters:
      clazz - the class to search the field from
      fieldType - the type of the field
      declared - if the field is declared
      Returns:
      the field if it has been found, otherwise null
    • getFieldOfType

      @Nullable public static Field getFieldOfType(Class<?> clazz, Class<?> fieldType)
      Get a declared field from a class without having to provide a field name.
      Calling this method is equal to calling getFieldOfType(Class, Class, boolean) with declared = true.
      Parameters:
      clazz - the class to search the field from
      fieldType - the type of the declared field
      Returns:
      the field if it has been found, otherwise null
    • getValue

      @Nullable public static Object getValue(Object instance, Field field)
      Get the value of a field. This method first makes the field accessible and then gets the value.
      This method will return null instead of throwing an exception, but it'll log the stacktrace to the console.
      Parameters:
      instance - the instance to get the value from
      field - the field to get the value from
      Returns:
      the value when succeeded, otherwise null
    • getBooleanValue

      public static boolean getBooleanValue(Object instance, Field field)
      Get the value of a boolean field. This method first makes the field accessible and then gets the value.
      This method will return false instead of throwing an exception, but it'll log the stacktrace to the console.
      Parameters:
      instance - the instance to get the value from
      field - the field to get the value from
      Returns:
      the value when succeeded, otherwise null
    • getValue

      @Nullable public static Object getValue(Object instance, String fieldName)
      Get the value of the given field by finding the field and then get the value of it.
      Parameters:
      instance - the instance of the object
      fieldName - the name of the field to get the value from
      Returns:
      the value of the field when succeeded, otheriwse null
    • getCastedValue

      @Nullable public static <T> T getCastedValue(Object instance, Field field)
      Get the value of a field and cast it to T.
      Type Parameters:
      T - the type to cast the value to
      Parameters:
      instance - the instance to get the value from
      field - the field to get the value from
      Returns:
      the casted value when succeeded, otherwise null
    • getCastedValue

      @Nullable public static <T> T getCastedValue(Object instance, String fieldName)
      Get the value of a field and cast it to T.
      Type Parameters:
      T - the type to cast the value to
      Parameters:
      instance - the instance to get the value from
      fieldName - the field to get the value from
      Returns:
      the casted value when succeeded, otherwise null
    • castedStaticValue

      @Nullable public static <T> T castedStaticValue(Field field)
    • castedStaticBooleanValue

      public static boolean castedStaticBooleanValue(Field field)
    • setValue

      public static void setValue(Object instance, Field field, Object value)
      Set the value of a field. This method make the field accessible and then sets the value.
      This method doesn't throw an exception when failed, but it'll log the error to the console.
      Parameters:
      instance - the instance to set the value to
      field - the field to set the value to
      value - the value to set
    • setValue

      public static boolean setValue(Object instance, String fieldName, Object value)
      Set the value of a field. This method finds the field, and then calls setValue(Object, Field, Object).
      Parameters:
      instance - the instance to set the value to
      fieldName - the field to set the value to
      value - the value to set
      Returns:
      true if the field was found
    • getMethod

      @Nullable public static Method getMethod(Class<?> clazz, String method, boolean declared, Class<?>... arguments)
      Get a method from a class, it doesn't matter if the field is public or not. This method will first try to get a declared field and if that failed it'll try to get a public field.
      Instead of throwing an exception when the method wasn't found, it will return null, but the exception will be printed in the console.
      Parameters:
      clazz - the class to get the method from
      method - the name of the method to find
      declared - if the the method is declared
      arguments - the classes of the method arguments
      Returns:
      the requested method if it has been found, otherwise null
    • getMethod

      @Nullable public static Method getMethod(Class<?> clazz, String methodName, Class<?>... arguments)
      Get a method from a class, it doesn't matter if the method is public or not. This method will first try to get a declared method and if that fails it'll try to get a public method.
      Parameters:
      clazz - the class to get the method from
      methodName - the name of the method to find
      arguments - the classes of the method arguments
      Returns:
      the requested method if it has been found, otherwise null
    • getMethod

      @Nullable public static Method getMethod(Object instance, String methodName, Class<?>... arguments)
      Get a method from a class, it doesn't matter if the method is public or not. This method will first try to get a declared method and if that fails it'll try to get a public method.
      Parameters:
      instance - the class to get the method from
      methodName - the name of the method to find
      arguments - the classes of the method arguments
      Returns:
      the requested method if it has been found, otherwise null
    • getMethodThatReturns

      @Nullable public static Method getMethodThatReturns(Class<?> clazz, Class<?> returnType, boolean declared, Class<?>... parameterTypes)
    • getMethodByName

      @Nullable public static Method getMethodByName(Class<?> clazz, String methodName, boolean declared)
      Get a method from a class by using the name of the method.
      Parameters:
      clazz - the class to search the method in
      methodName - the name of the method
      declared - if the method is declared
      Returns:
      the method if it has been found, otherwise null
    • getMethodFromParam

      @Nullable public static Method getMethodFromParam(Class<?> clazz, Class<?> paramType, boolean declared)
      Get a method from a class without having to provide a method name.
      Parameters:
      clazz - the class to search the method in
      paramType - the type of one of the method parameters
      declared - if the method is declared
      Returns:
      the method if it has been found, otherwise null
    • invoke

      @Nullable public static Object invoke(Object instance, Method method, Object... arguments)
      Invoke the given method of the given instance with the given arguments.
      Parameters:
      instance - the instance to get the value from
      method - the method to invoke
      arguments - the arguments of the method
      Returns:
      the value got from invoking the method, or null when failed to invoke
    • cast

      @Nullable public static <T> T cast(Object instance, Class<T> castTo)
    • castedInvoke

      @Nullable public static <T> T castedInvoke(Object instance, Method method, Object... arguments)
      Invoke the given method of the given instance with the given arguments and cast the value.
      Parameters:
      instance - the instance to get the value from
      method - the method to invoke
      arguments - the arguments of the method
      Returns:
      the casted value got from invoking the method, or null when failed to invoke
    • castedInvoke

      @Nullable public static <T> T castedInvoke(Object instance, String method)
      Invoke the given method of the given instance and cast the value.
      Parameters:
      instance - the instance to get the value from
      method - the method to invoke
      Returns:
      the casted value got from invoking the method, or null when failed to invoke
    • invokeStatic

      @Nullable public static Object invokeStatic(Class<?> clazz, String method)
      Invoke the given static method.
      Parameters:
      clazz - the class to get the method from
      method - the name of the method to invoke
      Returns:
      the value got from invoking the status method, or null when failed to invoke
    • makeAccessible

      public static <T extends AccessibleObject> T makeAccessible(T accessibleObject)
      Make the object accessible if it isn't accessible yet
      Type Parameters:
      T - accessible object type
      Parameters:
      accessibleObject - the object to make accessible
      Returns:
      the accessibleObject
    • getPrefix

      public static String getPrefix()
      The package name that is shared between all the getPrefixedClass(String) calls so that the className will be a lot shorter. Example net.minecraft.server.v1_8R3.PacketHandshakingInSetProtocol will become PacketHandshakingInSetProtocol if the prefix is set to net.minecraft.server.v1_8R3
    • setPrefix

      public static void setPrefix(String prefix)
      The package name that is shared between all the getPrefixedClass(String) calls so that the className will be a lot shorter. Example net.minecraft.server.v1_8R3.PacketHandshakingInSetProtocol will become PacketHandshakingInSetProtocol if the prefix is set to net.minecraft.server.v1_8R3