Package com.tngtech.archunit.core.domain
Class JavaAnnotation
- java.lang.Object
-
- com.tngtech.archunit.core.domain.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
this annotation will be imported storing the association@MyAnnotation(name = "some name", anAttribute = 7) class MyClass {}
Properties will be made available vianame --> "some name" anAttribute --> 7
get(String)
, e.g.
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 proxymyAnnotation.get("anAttribute")
----------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
Accesses to 'type' will be different for the proxied version:@SomeAnnotation(type = String.class) class MyClass {}
someAnnotation.get("type"); // --> returns JavaClass{String} someAnnotation.as(SomeAnnotation.class).type(); // --> returns String.class
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface com.tngtech.archunit.core.domain.properties.HasType
HasType.Functions
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <A extends java.lang.annotation.Annotation>
Aas(java.lang.Class<A> annotationType)
Optional<java.lang.Object>
get(java.lang.String property)
Returns the value of the property with the given name, i.e.java.util.Map<java.lang.String,java.lang.Object>
getProperties()
JavaClass
getType()
-
-
-
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 representedAnnotation
. E.g. for
the results will be@SomeAnnotation(value = "someString", types = {SomeType.class, AnotherType.class}) class SomeAnnotatedClass {}
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)
-
-