Class ClassAnnotationUtil


  • public class ClassAnnotationUtil
    extends Object
    Author:
    Yohann Chastagnier
    • Constructor Detail

      • ClassAnnotationUtil

        public ClassAnnotationUtil()
    • Method Detail

      • searchParameterizedTypeFrom

        public static <T> Class<T> searchParameterizedTypeFrom​(Class<?> searchedParametrizedTypeClass,
                                                               Class<?> fromClass)
        This method is awesome. It permits to retrieve the class of a parametrized type, even if this type is parametrized into a super class or interface from a hierarchy of classes.

        Example (it is stupid, but it is for understanding):

           
           public interface Car<? extends Model, ? extends Engine> {...}
        
           public abstract SedanElectricCar extends Car<Sedan, Electric> {...}
        
           public class Wikispeed implements SedanElectricCar {...}
           
         
        When trying to get some parametrized type information from an instance of Wikispeed, perform this operation:
           
           Class<? extends Engine> engineClassOfCar =
             AnnotationUtil.searchParameterizedTypeFrom(Engine.class, Wikispeed.class)
           
         
        Some explanations:
        • Engine.class is the superclass of the searched class from parametrized type
        • Wikispeed.class is the instance from which the parametrized type is searched
        Type Parameters:
        T - the class type.
        Parameters:
        searchedParametrizedTypeClass - the superclass of the searched parametrized type.
        fromClass - the instance from which the parametrized type is searched.
        Returns:
        the class of the parametrized type if any, null otherwise.
      • searchClassThatDeclaresAnnotation

        public static <T> Class<T> searchClassThatDeclaresAnnotation​(Class<? extends Annotation> searchedAnnotationClass,
                                                                     Class<?> fromClass)
        This method is awesome. It permits to retrieve the class from a class hierarchy the one that declares the given annotation.
        Type Parameters:
        T - the class type.
        Parameters:
        searchedAnnotationClass - the class of the searched annotation.
        fromClass - the class from which the annotation is searched.
        Returns:
        the class that declares the given annotation, null otherwise.