Class AnnotationProvider.Builder
- Enclosing class:
- AnnotationProvider
AnnotationProvider instances.
Example:
AnnotationProvider
See Also:
-
Method Summary
Modifier and TypeMethodDescriptionaddRuntimeAnnotations(Annotation... annotations) Adds runtime annotations to be applied to classes, methods, fields, and constructors.addRuntimeAnnotations(List<Annotation> annotations) Adds runtime annotations to be applied to classes, methods, fields, and constructors.build()Builds a newAnnotationProviderinstance with the configured settings.Sets the caching mode for annotation lookups.Enables logging of cache statistics on JVM shutdown.logOnExit(boolean value) Conditionally enables logging of cache statistics on JVM shutdown.
-
Method Details
-
addRuntimeAnnotations
Adds runtime annotations to be applied to classes, methods, fields, and constructors.This is a convenience method that delegates to
addRuntimeAnnotations(List). See that method for detailed documentation on how runtime annotations work.// Example: Add multiple runtime annotations using varargs BeanbeanAnnotation = BeanAnnotation .create () .onClass(MyClass.class ) .typeName("MyType" ) .build(); SwapswapAnnotation = SwapAnnotation .create () .on("com.example.MyClass.getValue" ) .value(MySwap.class ) .build(); AnnotationProviderprovider = AnnotationProvider .create () .addRuntimeAnnotations(beanAnnotation ,swapAnnotation )// Varargs .build();- Parameters:
annotations- The runtime annotation objects to add (varargs).- Returns:
- This object for method chaining.
- Throws:
BeanRuntimeException- If any annotation is invalid.- See Also:
-
addRuntimeAnnotations
Adds runtime annotations to be applied to classes, methods, fields, and constructors.Runtime annotations are concrete Java objects that implement annotation interfaces (e.g.,
@Bean). They allow you to dynamically apply annotations to code elements at runtime without modifying source code.How It Works:
- Create annotation objects using builder classes (e.g.,
BeanAnnotation.create()) - Specify targets using
on()oronClass()methods - Set annotation properties (e.g.,
typeName(),properties()) - Build the annotation object
- Add to the provider via this method
Targeting Requirements:
- Annotations MUST define an
onClass()method returningClass[]for type-safe targeting - OR an
on()method returningString[]for string-based targeting - The
on()method accepts fully-qualified names:"com.example.MyClass"- targets a class"com.example.MyClass.myMethod"- targets a method"com.example.MyClass.myField"- targets a field
// Example 1: Target a specific class using type-safe targeting BeanbeanAnnotation = BeanAnnotation .create () .onClass(MyClass.class )// Targets MyClass .typeName("MyType" ) .properties("id,name" ) .build();// Example 2: Target multiple classes BeanmultiAnnotation = BeanAnnotation .create () .onClass(MyClass.class , OtherClass.class ) .sort(true ) .build();// Example 3: Target using string names (useful for dynamic/reflection scenarios) BeanstringAnnotation = BeanAnnotation .create () .on("com.example.MyClass" ) .findFluentSetters(true ) .build();// Example 4: Target a specific method SwapswapAnnotation = SwapAnnotation .create () .on("com.example.MyClass.getValue" ) .value(MySwap.class ) .build();// Add all runtime annotations to the provider AnnotationProviderprovider = AnnotationProvider .create () .addRuntimeAnnotations(beanAnnotation ,multiAnnotation ,stringAnnotation ,swapAnnotation ) .build();Priority: Runtime annotations always take precedence over declared annotations at the same level. They are evaluated first when searching for annotations.
- Parameters:
annotations- The list of runtime annotation objects to add.- Returns:
- This object for method chaining.
- Throws:
BeanRuntimeException- If any annotation is invalid (missingon()oronClass()methods, or if the methods return incorrect types).
- Create annotation objects using builder classes (e.g.,
-
build
Builds a newAnnotationProviderinstance with the configured settings.- Returns:
- A new immutable
AnnotationProviderinstance.
-
cacheMode
Sets the caching mode for annotation lookups.Available modes:
NONE - Disables all caching (always recompute)WEAK - Uses WeakHashMap (allows GC of cached entries)FULL - Uses ConcurrentHashMap (best performance)
- Parameters:
value- The caching mode.- Returns:
- This object for method chaining.
-
logOnExit
Enables logging of cache statistics on JVM shutdown.- Returns:
- This object for method chaining.
-
logOnExit
Conditionally enables logging of cache statistics on JVM shutdown.- Parameters:
value- Whether to log on exit.- Returns:
- This object for method chaining.
-