Class AnnotationUsageCache


  • @NotThreadSafe
    public class AnnotationUsageCache
    extends Object
    A simple cache for the usage of a certain annotation class at other classes.
    Note: cannot use Cache because it would need a Class<?> as a key and this would be a hard wired reference.
    Since 10.1.3 the class is no longer read-write-locked, as the performance overhead is too big for reading only, compared to the penalty of double annotation determination.
    Author:
    Philip Helger
    • Constructor Detail

      • AnnotationUsageCache

        public AnnotationUsageCache​(@Nonnull
                                    Class<? extends Annotation> aAnnotationClass)
        Constructor
        Parameters:
        aAnnotationClass - The annotation class to store the existence of. It must have the RetentionPolicy.RUNTIME to be usable within this class!
    • Method Detail

      • getAnnotationClass

        @Nonnull
        public final Class<? extends Annotation> getAnnotationClass()
        Returns:
        The annotation class passed in the constructor. Never null.
      • hasAnnotation

        public boolean hasAnnotation​(@Nonnull
                                     Object aObject)
        Check if the class of the passed object has the annotation provided in the constructor.
        Parameters:
        aObject - The object. To be checked. May not be null.
        Returns:
        true if the owning class has the annotation, false if not.
        See Also:
        hasAnnotation(Class)
      • hasAnnotation

        public boolean hasAnnotation​(@Nonnull
                                     Class<?> aClass)
        Check if the provided class has the annotation from the constructor or not. If the value is not yet in the cache, it will be determined.
        Parameters:
        aClass - The class to check. May not be null.
        Returns:
        true if the provided class has the annotation, false if not.
      • setAnnotation

        public void setAnnotation​(@Nonnull
                                  Class<?> aClass,
                                  boolean bHasAnnotation)
        In case the knowledge was gathered on the outside, remember the wisdom in this class.
        Parameters:
        aClass - The class that it is about.
        bHasAnnotation - true to indicate the presence, false to
      • clearCache

        public void clearCache()
        Remove all entries from the cache. That is mainly of interested for testing purposes, to provide a clean state.