Class AnnotationObject
- All Implemented Interfaces:
Annotation
- Direct Known Subclasses:
AppliedAnnotationObject
This class provides a base for creating annotation instances without requiring them to be declared on program elements at compile-time. It allows annotations to be constructed using a builder pattern and follows all standard Java annotation semantics for equality, hashcode, and string representation.
Overview:
Java annotations are typically declared statically on classes, methods, fields, etc. at compile-time:
This class allows you to create those same annotations programmatically:
Bean
Equality and Hashcode:
Follows the standard Java conventions for annotation equality and hashcode calculation as defined in
Annotation.equals(Object) and Annotation.hashCode(). This ensures that programmatically-created
annotations are equivalent to compile-time declared annotations if they have the same type and properties.
Hashcode Caching:
For performance reasons, the hashcode is calculated once and cached on first access.
The hash is computed lazily when hashCode() is first called and then stored for subsequent calls.
Builder Pattern:
Subclasses should provide a nested AnnotationObject.Builder class that extends AnnotationObject.Builder
to construct instances using a fluent builder pattern. The builder should:
- Provide setter methods for each annotation property
- Return
this (or the builder type) from each setter for method chaining - Provide a
build()method that constructs the final annotation object
Example Implementation:
See Also:
AppliedAnnotationObject- For annotations with dynamic targeting support- Overview > juneau-commons > Annotations
-
Nested Class Summary
Nested Classes -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionClass<? extends Annotation>Implements theAnnotation.annotationType()method for child classes.booleaninthashCode()Returns this annotation as a map of key/value pairs.toString()
-
Constructor Details
-
AnnotationObject
Constructor.- Parameters:
b- The builder used to instantiate the fields of this class.
-
-
Method Details
-
annotationType
Implements theAnnotation.annotationType()method for child classes.- Specified by:
annotationTypein interfaceAnnotation- Returns:
- This class.
-
equals
- Specified by:
equalsin interfaceAnnotation- Overrides:
equalsin classObject
-
hashCode
- Specified by:
hashCodein interfaceAnnotation- Overrides:
hashCodein classObject
-
propertyMap
Returns this annotation as a map of key/value pairs.Useful for debugging.
- Returns:
- This annotation as a map of key/value pairs.
-
toString
- Specified by:
toStringin interfaceAnnotation- Overrides:
toStringin classObject
-