Class JavaAnnotation

  • All Implemented Interfaces:
    HasType

    public final class JavaAnnotation
    extends java.lang.Object
    implements HasType
    Represents an imported annotation on an annotated object like a class or a method. To be independent of the classpath, all properties of this annotation are just stored as a simple key value pairs. I.e. if you consider
    
      @MyAnnotation(name = "some name", anAttribute = 7)
      class MyClass {}
     
    this annotation will be imported storing the association
    
       name --> "some name"
       anAttribute --> 7
     
    Properties will be made available via get(String), e.g.
    
       myAnnotation.get("anAttribute")
     
    will return the value 7. Since this behavior is inconvenient (loss of type safety), there is another approach to retrieve these values, if the annotation can be resolved on the classpath. It's then possible to access a simple proxy
    
       MyAnnotation moreConvenient = myAnnotation.as(MyAnnotation.class);
       moreConvenient.anAttribute(); // --> returns 7
     
    ----------
    NOTE
    ----------
    ArchUnit holds the annotation in a classpath independent representation, i.e. some types will be mapped, when the access is proxied. Consider
    
      @SomeAnnotation(type = String.class)
      class MyClass {}
     
    Accesses to 'type' will be different for the proxied version:
    
       someAnnotation.get("type"); // --> returns JavaClass{String}
       someAnnotation.as(SomeAnnotation.class).type(); // --> returns String.class
     
    • Method Detail

      • get

        @PublicAPI(usage=ACCESS)
        public Optional<java.lang.Object> get​(java.lang.String property)
        Returns the value of the property with the given name, i.e. the result of the method with the property name of the represented Annotation. E.g. for
              @SomeAnnotation(value = "someString", types = {SomeType.class, AnotherType.class})
         class SomeAnnotatedClass {}
         
        the results will be
               someAnnotation.get("value") --> "someString"
         someAnnotation.get("types") --> [JavaClass{SomeType}, JavaClass{AnotherType}]
         
        Parameters:
        property - The name of the annotation property, i.e. the declared method name
        Returns:
        the value of the given property, where the result type is more precisely
        • Class<?> --> TypeDetails{clazz}
        • Class<?>[] --> [TypeDetails{clazz},...]
        • Enum --> JavaEnumConstant
        • Enum[] --> [JavaEnumConstant,...]
        • anyOtherType --> anyOtherType
      • getProperties

        @PublicAPI(usage=ACCESS)
        public java.util.Map<java.lang.String,​java.lang.Object> getProperties()
        Returns:
        a map containing all [property --> value], where each value correlates to get(String property)
      • as

        @PublicAPI(usage=ACCESS)
        public <A extends java.lang.annotation.Annotation> A as​(java.lang.Class<A> annotationType)