Class Reflections


  • public class Reflections
    extends java.lang.Object
    A collection of reflection utilities.
    Since:
    6.0.0
    Author:
    simonpai
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static interface  Reflections.FieldRunner<A extends java.lang.annotation.Annotation>  
      static interface  Reflections.MethodRunner<A extends java.lang.annotation.Annotation>  
    • Constructor Summary

      Constructors 
      Constructor Description
      Reflections()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void addToCollectionField​(java.lang.Object owner, java.lang.reflect.Field field, java.lang.Object item)
      Add the item to a collection field of an object.
      static <A extends java.lang.annotation.Annotation>
      void
      forFields​(java.lang.Class<?> clazz, java.lang.Class<A> annotationClass, Reflections.FieldRunner<A> runner)
      Traverse through all fields with a certain annotation in a class and it super classes.
      static <A extends java.lang.annotation.Annotation>
      void
      forMethods​(java.lang.Class<?> clazz, java.lang.Class<A> annotationClass, Reflections.MethodRunner<A> runner)
      Traverse through all methods with a certain annotation in a class, including ones inherited from its super class.
      static java.lang.Class<?> getClass​(java.lang.reflect.Type type)
      Return the Class representing the given Type.
      static java.lang.Object getFieldValue​(java.lang.Object bean, java.lang.reflect.Field field)
      Get field value.
      static void invokeMethod​(java.lang.reflect.Method method, java.lang.Object bean, java.lang.Object... arguments)
      Invoke a method reflexively.
      static boolean isAppendableToCollection​(java.lang.reflect.Type type, java.lang.Object object)
      Return true if field is of Collection type, and an object of type clazz can be added into the Collection.
      static boolean isPassableToMethod​(java.lang.reflect.Method method, java.lang.Object... arguments)
      Return true if the given arguments can be passed as the arguments of the method.
      static void setFieldValue​(java.lang.Object bean, java.lang.reflect.Field field, java.lang.Object value)
      Set a value to a field of the object.
      • Methods inherited from class java.lang.Object

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

      • Reflections

        public Reflections()
    • Method Detail

      • forFields

        public static <A extends java.lang.annotation.Annotation> void forFields​(java.lang.Class<?> clazz,
                                                                                 java.lang.Class<A> annotationClass,
                                                                                 Reflections.FieldRunner<A> runner)
        Traverse through all fields with a certain annotation in a class and it super classes. Subclass first.

        All the fields with the same name will be scanned too. (since 6.5.1), before 6.5.1 release, only the subclass will be scanned.

      • forMethods

        public static <A extends java.lang.annotation.Annotation> void forMethods​(java.lang.Class<?> clazz,
                                                                                  java.lang.Class<A> annotationClass,
                                                                                  Reflections.MethodRunner<A> runner)
        Traverse through all methods with a certain annotation in a class, including ones inherited from its super class.
      • isAppendableToCollection

        public static boolean isAppendableToCollection​(java.lang.reflect.Type type,
                                                       java.lang.Object object)
        Return true if field is of Collection type, and an object of type clazz can be added into the Collection.
      • isPassableToMethod

        public static boolean isPassableToMethod​(java.lang.reflect.Method method,
                                                 java.lang.Object... arguments)
        Return true if the given arguments can be passed as the arguments of the method.
      • setFieldValue

        public static void setFieldValue​(java.lang.Object bean,
                                         java.lang.reflect.Field field,
                                         java.lang.Object value)
        Set a value to a field of the object.
      • getFieldValue

        public static java.lang.Object getFieldValue​(java.lang.Object bean,
                                                     java.lang.reflect.Field field)
        Get field value.
      • addToCollectionField

        public static void addToCollectionField​(java.lang.Object owner,
                                                java.lang.reflect.Field field,
                                                java.lang.Object item)
        Add the item to a collection field of an object.
      • invokeMethod

        public static void invokeMethod​(java.lang.reflect.Method method,
                                        java.lang.Object bean,
                                        java.lang.Object... arguments)
        Invoke a method reflexively.
      • getClass

        public static java.lang.Class<?> getClass​(java.lang.reflect.Type type)
        Return the Class representing the given Type.