Class ReflectHelper

java.lang.Object
org.mvplugins.multiverse.core.utils.ReflectHelper

public final class ReflectHelper extends Object
Utility class used to help in doing various reflection actions.
  • Constructor Details

    • ReflectHelper

      public ReflectHelper()
  • Method Details

    • tryGetClass

      @AvailableSince("5.7") @NotNull public static @NotNull io.vavr.control.Try<Class<?>> tryGetClass(@NotNull @NotNull String classPath)
      Try to get the Class based on its classpath.
      Parameters:
      classPath - The target classpath.
      Returns:
      A Try containing the Class if found, else a failure.
      Since:
      5.7
    • hasClass

      public static boolean hasClass(String classPath)
      Check if the Class for a give classpath is present/valid.
      Parameters:
      classPath - Target classpath.
      Returns:
      True if class path is a valid class, else false.
    • tryGetMethod

      @AvailableSince("5.7") @NotNull public static <C> @NotNull io.vavr.control.Try<Method> tryGetMethod(@NotNull @NotNull Class<C> clazz, @NotNull @NotNull String methodName, Class<?>... parameterTypes)
      Try to get a Method from a given class.
      Type Parameters:
      C - The class type.
      Parameters:
      clazz - The class to search the method on.
      methodName - Name of the method to get.
      parameterTypes - Parameters present for that method.
      Returns:
      A Try containing the Method if found, else a failure.
      Since:
      5.7
    • hasMethod

      @AvailableSince("5.7") public static boolean hasMethod(@NotNull @NotNull Class<?> clazz, @NotNull @NotNull String methodName, Class<?>... parameterTypes)
      Check if a Method exists on a given class.
      Parameters:
      clazz - The class to search the method on.
      methodName - Name of the method to check.
      parameterTypes - Parameters present for that method.
      Returns:
      True if method exists, else false.
      Since:
      5.7
    • tryInvokeMethod

      @AvailableSince("5.7") @NotNull public static <C, R> @NotNull io.vavr.control.Try<R> tryInvokeMethod(@NotNull C classInstance, @NotNull @NotNull Method method, Object... parameters)
      Try to invoke a Method on a class instance.
      Type Parameters:
      C - The class type.
      R - The return type of the method.
      Parameters:
      classInstance - Instance of the class responsible for the method.
      method - The method to invoke.
      parameters - Parameters needed when invoking the method.
      Returns:
      A Try containing the return value of the method if found, else a failure.
      Since:
      5.7
    • tryInvokeStaticMethod

      @AvailableSince("5.7") @NotNull public static <R> @NotNull io.vavr.control.Try<R> tryInvokeStaticMethod(@NotNull @NotNull Method method, Object... parameters)
      Try to invoke a static Method.
      Type Parameters:
      R - The return type of the method.
      Parameters:
      method - The static method to invoke.
      parameters - Parameters needed when invoking the method.
      Returns:
      A Try containing the return value of the method if found, else a failure.
      Since:
      5.7
    • tryGetField

      @AvailableSince("5.7") @NotNull public static <C> @NotNull io.vavr.control.Try<Field> tryGetField(@NotNull @NotNull Class<C> clazz, @NotNull @NotNull String fieldName)
      Try to get a Field from a given class.
      Type Parameters:
      C - The class type.
      Parameters:
      clazz - The class to search the field on.
      fieldName - Name of the field to get.
      Returns:
      A Try containing the Field if found, else a failure.
      Since:
      5.7
    • hasField

      @AvailableSince("5.7") public static boolean hasField(@NotNull @NotNull Class<?> clazz, @NotNull @NotNull String fieldName)
      Check if a Field exists on a given class.
      Parameters:
      clazz - The class to search the field on.
      fieldName - Name of the field to check.
      Returns:
      True if field exists, else false.
      Since:
      5.7
    • tryGetFieldValue

      @AvailableSince("5.7") @NotNull public static <C, V> @NotNull io.vavr.control.Try<V> tryGetFieldValue(@NotNull C classInstance, @NotNull @NotNull Field field, @NotNull @NotNull Class<V> fieldType)
      Try to get the value of a Field from an instance of the class responsible.
      Type Parameters:
      C - The class type.
      V - The field value type.
      Parameters:
      classInstance - Instance of the class to get the field value from.
      field - The field to get the value from.
      fieldType - Type of the field.
      Returns:
      A Try containing the field value if found, else a failure.
      Since:
      5.7
    • tryGetStaticFieldValue

      @AvailableSince("5.7") @NotNull public static <V> @NotNull io.vavr.control.Try<V> tryGetStaticFieldValue(@NotNull @NotNull Field field, @NotNull @NotNull Class<V> fieldType)
      Try to get the value of a static Field.
      Type Parameters:
      V - The field value type.
      Parameters:
      field - The static field to get the value from.
      fieldType - Type of the field.
      Returns:
      A Try containing the field value if found, else a failure.
      Since:
      5.7
    • getClass

      @Deprecated(forRemoval=true, since="5.7") @Nullable public static @Nullable Class<?> getClass(String classPath)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Use tryGetClass(String) instead, which returns a Try that can be used to handle the failure case more explicitly.
      Try to get the Class based on its classpath.
      Parameters:
      classPath - The target classpath.
      Returns:
      A Class if found, else null.
    • getMethod

      @Deprecated(forRemoval=true, since="5.7") @Nullable public static <C> @Nullable Method getMethod(Class<C> clazz, String methodName, Class<?>... parameterTypes)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Use tryGetMethod(Class, String, Class[]) instead, which returns a Try that can be used to handle the failure case more explicitly.
      Try to get a Method from a given class.
      Type Parameters:
      C - The class type.
      Parameters:
      clazz - The class to search the method on.
      methodName - Name of the method to get.
      parameterTypes - Parameters present for that method.
      Returns:
      A Method if found, else null.
    • getMethod

      @Deprecated(forRemoval=true, since="5.7") @Nullable public static <C> @Nullable Method getMethod(C classInstance, String methodName, Class<?>... parameterTypes)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Use tryGetMethod(Class, String, Class[]) instead, which returns a Try that can be used to handle the failure case more explicitly.
      Try to get a Method from a given class.
      Type Parameters:
      C - The class type.
      Parameters:
      classInstance - Instance of the class to search the method on.
      methodName - Name of the method to get.
      parameterTypes - Parameters present for that method.
      Returns:
      A Method if found, else null.
    • invokeMethod

      @Deprecated(forRemoval=true, since="5.7") @Nullable public static <C, R> R invokeMethod(C classInstance, Method method, Object... parameters)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Use tryInvokeMethod(Object, Method, Object...) instead, which returns a Try that can be used to handle the failure case more explicitly.
      Calls a Method.
      Type Parameters:
      C - The class type.
      R - The return type.
      Parameters:
      classInstance - Instance of the class responsible for the method.
      method - The method to call.
      parameters - Parameters needed when calling the method.
      Returns:
      Return value of the method call if any, else null.
    • getField

      @Deprecated(forRemoval=true, since="5.7") @Nullable public static <C> @Nullable Field getField(Class<C> clazz, String fieldName)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Use tryGetField(Class, String) instead, which returns a Try that can be used to handle the failure case more explicitly.
      Try to get a Field from a given class.
      Type Parameters:
      C - The class type.
      Parameters:
      clazz - The class to search the field on.
      fieldName - Name of the field to get.
      Returns:
      A Field if found, else null.
    • getField

      @Deprecated(forRemoval=true, since="5.7") @Nullable public static <C> @Nullable Field getField(C classInstance, String fieldName)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Use tryGetField(Class, String) instead, which returns a Try that can be used to handle the failure case more explicitly.
      Try to get a Field from a given class.
      Type Parameters:
      C - The class type.
      Parameters:
      classInstance - Instance of the class to search the field on.
      fieldName - Name of the field to get.
      Returns:
      A Field if found, else null.
    • getFieldValue

      @Deprecated(forRemoval=true, since="5.7") @Nullable public static <C, V> V getFieldValue(C classInstance, @Nullable @Nullable Field field, @NotNull @NotNull Class<V> fieldType)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Use tryGetFieldValue(Object, Field, Class) instead, which returns a Try that can be used to handle the failure case more explicitly.
      Gets the value of a Field from an instance of the class responsible.
      Type Parameters:
      C - The class type.
      V - The field value type.
      Parameters:
      classInstance - Instance of the class to get the field value from.
      field - The field to get value from.
      fieldType - Type of the field.
      Returns:
      The field value if any, else null.
    • getFieldValue

      @Deprecated(forRemoval=true, since="5.7") @Nullable public static <C, V> V getFieldValue(C classInstance, @Nullable @Nullable String fieldName, @NotNull @NotNull Class<V> fieldType)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Use tryGetField(Class, String) then map to tryGetFieldValue(Object, Field, Class) instead, which returns a Try that can be used to handle the failure case more explicitly.
      Gets the value of a field name from an instance of the class responsible.
      Type Parameters:
      C - The class type.
      V - The field value type.
      Parameters:
      classInstance - Instance of the class to get the field value from.
      fieldName - Name of the field to get value from.
      fieldType - Type of the field.
      Returns:
      The field value if any, else null.