77package org .hibernate .models .internal .dynamic ;
88
99import java .lang .annotation .Annotation ;
10+ import java .lang .reflect .InvocationTargetException ;
11+ import java .util .Arrays ;
1012import java .util .HashMap ;
1113import java .util .Locale ;
1214import java .util .Map ;
1315
16+ import org .hibernate .models .AnnotationAccessException ;
1417import org .hibernate .models .UnknownAnnotationAttributeException ;
1518import org .hibernate .models .internal .AnnotationProxy ;
1619import org .hibernate .models .spi .AnnotationDescriptor ;
1720import org .hibernate .models .spi .AnnotationTarget ;
1821import org .hibernate .models .spi .AttributeDescriptor ;
1922import org .hibernate .models .spi .MutableAnnotationUsage ;
23+ import org .hibernate .models .spi .SourceModelBuildingContext ;
2024
2125/**
2226 * AnnotationUsage built dynamically (for dynamic models, XML mappings, etc.)
@@ -29,15 +33,14 @@ public class DynamicAnnotationUsage<A extends Annotation> implements MutableAnno
2933
3034 private Map <String ,Object > values ;
3135
32- public DynamicAnnotationUsage (AnnotationDescriptor <A > annotationDescriptor ) {
33- this ( annotationDescriptor , null );
34- }
35-
36- public DynamicAnnotationUsage (AnnotationDescriptor <A > annotationDescriptor , AnnotationTarget target ) {
36+ public DynamicAnnotationUsage (
37+ AnnotationDescriptor <A > annotationDescriptor ,
38+ AnnotationTarget target ,
39+ SourceModelBuildingContext context ) {
3740 this .annotationDescriptor = annotationDescriptor ;
3841 this .target = target ;
3942
40- this .values = extractBaselineValues ( annotationDescriptor );
43+ this .values = extractBaselineValues ( annotationDescriptor , target , context );
4144 }
4245
4346 @ Override
@@ -109,4 +112,46 @@ private static <A extends Annotation> Map<String, Object> extractBaselineValues(
109112 }
110113 return values ;
111114 }
115+
116+ private static <A extends Annotation > Map <String , Object > extractBaselineValues (
117+ AnnotationDescriptor <A > annotationDescriptor ,
118+ AnnotationTarget target ,
119+ SourceModelBuildingContext context ) {
120+ final HashMap <String , Object > values = new HashMap <>();
121+ for ( AttributeDescriptor <?> attribute : annotationDescriptor .getAttributes () ) {
122+ final Object defaultValue = attribute .getAttributeMethod ().getDefaultValue ();
123+ if ( defaultValue instanceof Annotation annotation ) {
124+ try {
125+ values .put ( attribute .getName (), extractDynamicAnnotationUsage ( annotation , target , context ) );
126+ }
127+ catch (InvocationTargetException | IllegalAccessException e ) {
128+ throw new AnnotationAccessException ( "Error accessing default annotation attribute value" , e );
129+ }
130+ }
131+ else if ( attribute .isMultiValued () ) {
132+ values .put ( attribute .getName (), Arrays .asList ( defaultValue ) );
133+ }
134+ else {
135+ values .put ( attribute .getName (), defaultValue );
136+ }
137+ }
138+ return values ;
139+ }
140+
141+ private static DynamicAnnotationUsage <?> extractDynamicAnnotationUsage (
142+ Annotation annotation ,
143+ AnnotationTarget target ,
144+ SourceModelBuildingContext context ) throws InvocationTargetException , IllegalAccessException {
145+ final Class <? extends Annotation > annotationType = annotation .annotationType ();
146+ final AnnotationDescriptor <?> descriptor = context .getAnnotationDescriptorRegistry ()
147+ .getDescriptor ( annotationType );
148+ final DynamicAnnotationUsage <?> annotationUsage = new DynamicAnnotationUsage <>( descriptor , target , context );
149+ for ( AttributeDescriptor <?> attribute : descriptor .getAttributes () ) {
150+ annotationUsage .setAttributeValue (
151+ attribute .getName (),
152+ attribute .getAttributeMethod ().invoke ( annotation )
153+ );
154+ }
155+ return annotationUsage ;
156+ }
112157}
0 commit comments