Class ReflectUtil

java.lang.Object
org.nuiton.util.ReflectUtil

public class ReflectUtil
extends java.lang.Object
Introspection sur une classe. Détermine si un attribut est constant, recherche de constantes d'un type donné, conversion d'une classe en Enum... Created: 30 décembre 2007
Author:
Tony Chemit - chemit@codelutin.com
  • Constructor Summary

    Constructors 
    Constructor Description
    ReflectUtil()  
  • Method Summary

    Modifier and Type Method Description
    static java.lang.Class<?> boxType​(java.lang.Class<?> type)
    Obtain the boxed type of any incoming type.
    static java.util.Set<java.lang.reflect.Field> getAllDeclaredFields​(java.lang.Class<?> objectClass)
    Obtain all the declared fields of the given objectClass with a deep scan inside super classes and interfaces.
    protected static void getAllDeclaredFields​(java.lang.Class<?> objectClass, java.util.Set<java.lang.Class<?>> visitedClasses, java.util.Set<java.lang.reflect.Field> result)  
    static java.util.Set<java.lang.reflect.Method> getAllDeclaredMethods​(java.lang.Class<?> objectClass)
    Obtain all the declared methods of the given objectClass with a deep scan inside super classes and interfaces.
    protected static void getAllDeclaredMethods​(java.lang.Class<?> objectClass, java.util.Set<java.lang.Class<?>> visitedClasses, java.util.Set<java.lang.reflect.Method> result)  
    static <T> T getConstant​(java.lang.Class<?> klass, java.lang.String fieldName)  
    static <T> java.util.List<T> getConstants​(java.lang.Class<?> klass, java.lang.Class<T> searchingClass)
    Recherche dans une classe donnée klazz, les constantes d'un certain type searchingClass et les retourne.
    static java.lang.reflect.Method getDeclaredMethod​(java.lang.Class<?> klass, java.lang.String methodName, boolean strict, java.lang.Object... arguments)
    Cherche une methode selon son nom et ses paramètres d'invocation.
    protected static java.lang.reflect.Method getDeclaredMethod​(java.lang.Class<?> klass, java.lang.String methodName, java.util.Set<java.lang.Class<?>> visitedClasses, java.lang.Object... arguments)  
    static <T extends java.lang.Enum<T>>
    java.lang.Class<T>
    getEnumClass​(java.lang.Class<?> type)
    Convertit une classe non typée, en une classe d'enum
    static <A extends java.lang.annotation.Annotation>
    java.util.Map<java.lang.reflect.Field,​A>
    getFieldAnnotation​(java.lang.Class<?> objectClass, java.lang.Class<A> annotationClass)
    Obtain all the fields with the given annotation type.
    static <A extends java.lang.annotation.Annotation>
    java.util.Map<java.lang.reflect.Field,​A>
    getFieldAnnotation​(java.lang.Class<?> objectClass, java.lang.Class<A> annotationClass, boolean deepVisit)
    Obtain all the fields with the given annotation type.
    protected static <A extends java.lang.annotation.Annotation>
    void
    getFieldAnnotation​(java.lang.Class<?> objectClass, java.lang.Class<A> annotationClass, boolean deepVisit, java.util.Set<java.lang.Class<?>> visitedClasses, java.util.Map<java.lang.reflect.Field,​A> result)  
    static <A extends java.lang.annotation.Annotation>
    java.util.Map<java.lang.reflect.Method,​A>
    getMethodAnnotation​(java.lang.Class<?> objectClass, java.lang.Class<A> annotationClass)
    Obtain all the methods with the given annotation type.
    static <A extends java.lang.annotation.Annotation>
    java.util.Map<java.lang.reflect.Method,​A>
    getMethodAnnotation​(java.lang.Class<?> objectClass, java.lang.Class<A> annotationClass, boolean deepVisit)
    Obtain all the methods with the given annotation type.
    protected static <A extends java.lang.annotation.Annotation>
    void
    getMethodAnnotation​(java.lang.Class<?> objectClass, java.lang.Class<A> annotationClass, boolean deepVisit, java.util.Set<java.lang.Class<?>> visitedClasses, java.util.Map<java.lang.reflect.Method,​A> result)  
    static boolean isConstantField​(java.lang.reflect.Field field)
    Pour déterminer si un champ d'une classe est une constante (modifiers sont static, final et public)
    static java.lang.Class<?> unboxType​(java.lang.Class<?> type)
    Obtain the unboxed type of any incoming type.

    Methods inherited from class java.lang.Object

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

  • Method Details

    • isConstantField

      public static boolean isConstantField​(java.lang.reflect.Field field)
      Pour déterminer si un champ d'une classe est une constante (modifiers sont static, final et public)
      Parameters:
      field - le champs à tester
      Returns:
      true si les modifiers sont final, static et public
    • getConstants

      public static <T> java.util.List<T> getConstants​(java.lang.Class<?> klass, java.lang.Class<T> searchingClass)
      Recherche dans une classe donnée klazz, les constantes d'un certain type searchingClass et les retourne. L'algorithme parcourt aussi les superclasses.
      Type Parameters:
      T - enumeration's type
      Parameters:
      klass - la classe contenant les constantes
      searchingClass - le type des champs constants à récupérer
      Returns:
      la liste des champs du type requis dans
      Throws:
      java.lang.RuntimeException - si problème lors de la récupération
    • getConstant

      public static <T> T getConstant​(java.lang.Class<?> klass, java.lang.String fieldName)
      Type Parameters:
      T - enumeration's type
      Parameters:
      klass - the required class
      fieldName - the required constant name
      Returns:
      the constant value
    • getEnumClass

      public static <T extends java.lang.Enum<T>> java.lang.Class<T> getEnumClass​(java.lang.Class<?> type) throws java.lang.IllegalArgumentException
      Convertit une classe non typée, en une classe d'enum
      Type Parameters:
      T - enumeration's type
      Parameters:
      type - la classe a typer
      Returns:
      la classe typee
      Throws:
      java.lang.IllegalArgumentException - si le type est null ou non une extension de la classe Enum.
    • getDeclaredMethod

      public static java.lang.reflect.Method getDeclaredMethod​(java.lang.Class<?> klass, java.lang.String methodName, boolean strict, java.lang.Object... arguments) throws java.lang.IllegalArgumentException
      Cherche une methode selon son nom et ses paramètres d'invocation.
      Parameters:
      klass - la classe dans laquelle rechercher la méthode
      methodName - le nom de la méthode recherchée
      strict - un drapeau pour déclancher une exception si la méthode n'est pas trouvée
      arguments - les arguments d'invocation de la méthode
      Returns:
      la méthode trouvée
      Throws:
      java.lang.IllegalArgumentException - si la méthode n'est pas trouvée et que le drapeau strict est à true
      Since:
      1.3.1
    • boxType

      public static java.lang.Class<?> boxType​(java.lang.Class<?> type)
      Obtain the boxed type of any incoming type. If incoming type is not a primitive type, then just returns himself.
      Parameters:
      type - the type to box
      Returns:
      the boxed type
      Since:
      1.3.1
      See Also:
      Class.isPrimitive()
    • unboxType

      public static java.lang.Class<?> unboxType​(java.lang.Class<?> type)
      Obtain the unboxed type of any incoming type. If incoming type is a primitive type, then just returns himself.
      Parameters:
      type - the type to unbox
      Returns:
      the unboxed type
      Since:
      1.3.1
      See Also:
      Class.isPrimitive()
    • getAllDeclaredFields

      public static java.util.Set<java.lang.reflect.Field> getAllDeclaredFields​(java.lang.Class<?> objectClass)
      Obtain all the declared fields of the given objectClass with a deep scan inside super classes and interfaces. Note: The type Object will not be scanned.
      Parameters:
      objectClass - the object class to scan
      Returns:
      the set of all declared fields found
      Since:
      2.0
    • getAllDeclaredMethods

      public static java.util.Set<java.lang.reflect.Method> getAllDeclaredMethods​(java.lang.Class<?> objectClass)
      Obtain all the declared methods of the given objectClass with a deep scan inside super classes and interfaces. Note: The type Object will not be scanned.
      Parameters:
      objectClass - the object class to scan
      Returns:
      the set of all declared methods found
      Since:
      2.0
    • getFieldAnnotation

      public static <A extends java.lang.annotation.Annotation> java.util.Map<java.lang.reflect.Field,​A> getFieldAnnotation​(java.lang.Class<?> objectClass, java.lang.Class<A> annotationClass)
      Obtain all the fields with the given annotation type. Note: This method will not scan deeply the given type (no scan of super-classes nor interfaces).
      Type Parameters:
      A - type of annotation to scan
      Parameters:
      objectClass - the type to scan
      annotationClass - the type of annotation to scan
      Returns:
      the dictionnary of fields with the given annotation
      Since:
      2.0
    • getFieldAnnotation

      public static <A extends java.lang.annotation.Annotation> java.util.Map<java.lang.reflect.Field,​A> getFieldAnnotation​(java.lang.Class<?> objectClass, java.lang.Class<A> annotationClass, boolean deepVisit)
      Obtain all the fields with the given annotation type. Note: This method will scan deeply the given type if parameter deepVisit is setted to true. Note: The type Object will not be scanned.
      Type Parameters:
      A - type of annotation to scan
      Parameters:
      objectClass - the type to scan
      annotationClass - the type of annotation to scan
      deepVisit - flag to visit deeply the class (if set to true, will also scan super classes and interfaces)
      Returns:
      the dictionnary of fields with the given annotation
      Since:
      2.0
    • getMethodAnnotation

      public static <A extends java.lang.annotation.Annotation> java.util.Map<java.lang.reflect.Method,​A> getMethodAnnotation​(java.lang.Class<?> objectClass, java.lang.Class<A> annotationClass)
      Obtain all the methods with the given annotation type. Note: This method will not scan deeply the given type (no scan of super-classes nor interfaces).
      Type Parameters:
      A - type of annotation to scan
      Parameters:
      objectClass - the type to scan
      annotationClass - the type of annotation to scan
      Returns:
      the dictionnary of methods with the given annotation
      Since:
      2.0
    • getMethodAnnotation

      public static <A extends java.lang.annotation.Annotation> java.util.Map<java.lang.reflect.Method,​A> getMethodAnnotation​(java.lang.Class<?> objectClass, java.lang.Class<A> annotationClass, boolean deepVisit)
      Obtain all the methods with the given annotation type. Note: This method will scan deeply the given type if parameter deepVisit is setted to true. Note: The type Object will not be scanned.
      Type Parameters:
      A - type of annotation to scan
      Parameters:
      objectClass - the type to scan
      annotationClass - the type of annotation to scan
      deepVisit - flag to visit deeply the class (if set to true, will also scan super classes and interfaces)
      Returns:
      the dictionnary of methods with the given annotation
      Since:
      2.0
    • getDeclaredMethod

      protected static java.lang.reflect.Method getDeclaredMethod​(java.lang.Class<?> klass, java.lang.String methodName, java.util.Set<java.lang.Class<?>> visitedClasses, java.lang.Object... arguments)
    • getAllDeclaredFields

      protected static void getAllDeclaredFields​(java.lang.Class<?> objectClass, java.util.Set<java.lang.Class<?>> visitedClasses, java.util.Set<java.lang.reflect.Field> result)
    • getAllDeclaredMethods

      protected static void getAllDeclaredMethods​(java.lang.Class<?> objectClass, java.util.Set<java.lang.Class<?>> visitedClasses, java.util.Set<java.lang.reflect.Method> result)
    • getFieldAnnotation

      protected static <A extends java.lang.annotation.Annotation> void getFieldAnnotation​(java.lang.Class<?> objectClass, java.lang.Class<A> annotationClass, boolean deepVisit, java.util.Set<java.lang.Class<?>> visitedClasses, java.util.Map<java.lang.reflect.Field,​A> result)
    • getMethodAnnotation

      protected static <A extends java.lang.annotation.Annotation> void getMethodAnnotation​(java.lang.Class<?> objectClass, java.lang.Class<A> annotationClass, boolean deepVisit, java.util.Set<java.lang.Class<?>> visitedClasses, java.util.Map<java.lang.reflect.Method,​A> result)