Class ReflectHelper


  • public class ReflectHelper
    extends java.lang.Object
    Utility class used to help in doing various reflection actions.
    • Constructor Summary

      Constructors 
      Constructor Description
      ReflectHelper()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static @Nullable java.lang.Class<?> getClass​(java.lang.String classPath)
      Try to get the Class based on its classpath.
      static <C> @Nullable java.lang.reflect.Field getField​(C classInstance, java.lang.String fieldName)
      Try to get a Field from a given class.
      static <C> @Nullable java.lang.reflect.Field getField​(java.lang.Class<C> clazz, java.lang.String fieldName)
      Try to get a Field from a given class.
      static <C,​V>
      V
      getFieldValue​(C classInstance, java.lang.reflect.Field field)
      Gets the value of an Field from an instance of the class responsible.
      static <C> @Nullable java.lang.reflect.Method getMethod​(C classInstance, java.lang.String methodName, java.lang.Class<?>... parameterTypes)
      Try to get a Method from a given class.
      static <C> @Nullable java.lang.reflect.Method getMethod​(java.lang.Class<C> clazz, java.lang.String methodName, java.lang.Class<?>... parameterTypes)
      Try to get a Method from a given class.
      static boolean hasClass​(java.lang.String classPath)
      Check if the Class for a give classpath is present/valid.
      static <C,​R>
      R
      invokeMethod​(C classInstance, java.lang.reflect.Method method, java.lang.Object... parameters)
      Calls a Method.
      • Methods inherited from class java.lang.Object

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

      • ReflectHelper

        public ReflectHelper()
    • Method Detail

      • getClass

        @Nullable
        public static @Nullable java.lang.Class<?> getClass​(java.lang.String classPath)
        Try to get the Class based on its classpath.
        Parameters:
        classPath - The target classpath.
        Returns:
        A Class if found, else null.
      • hasClass

        public static boolean hasClass​(java.lang.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.
      • getMethod

        @Nullable
        public static <C> @Nullable java.lang.reflect.Method getMethod​(java.lang.Class<C> clazz,
                                                                       java.lang.String methodName,
                                                                       java.lang.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 Method if found, else null.
      • getMethod

        @Nullable
        public static <C> @Nullable java.lang.reflect.Method getMethod​(C classInstance,
                                                                       java.lang.String methodName,
                                                                       java.lang.Class<?>... parameterTypes)
        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

        @Nullable
        public static <C,​R> R invokeMethod​(C classInstance,
                                                 java.lang.reflect.Method method,
                                                 java.lang.Object... parameters)
        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

        @Nullable
        public static <C> @Nullable java.lang.reflect.Field getField​(java.lang.Class<C> clazz,
                                                                     java.lang.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 Field if found, else null.
      • getField

        @Nullable
        public static <C> @Nullable java.lang.reflect.Field getField​(C classInstance,
                                                                     java.lang.String fieldName)
        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

        @Nullable
        public static <C,​V> V getFieldValue​(C classInstance,
                                                  java.lang.reflect.Field field)
        Gets the value of an 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.
        Returns:
        The field value if any, else null.