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,20 @@ 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 );
36+ public DynamicAnnotationUsage (
37+ AnnotationDescriptor <A > annotationDescriptor ,
38+ SourceModelBuildingContext context ) {
39+ this ( annotationDescriptor , null , context );
3440 }
3541
36- public DynamicAnnotationUsage (AnnotationDescriptor <A > annotationDescriptor , AnnotationTarget target ) {
42+ public DynamicAnnotationUsage (
43+ AnnotationDescriptor <A > annotationDescriptor ,
44+ AnnotationTarget target ,
45+ SourceModelBuildingContext context ) {
3746 this .annotationDescriptor = annotationDescriptor ;
3847 this .target = target ;
3948
40- this .values = extractBaselineValues ( annotationDescriptor );
49+ this .values = extractBaselineValues ( annotationDescriptor , target , context );
4150 }
4251
4352 @ Override
@@ -109,4 +118,46 @@ private static <A extends Annotation> Map<String, Object> extractBaselineValues(
109118 }
110119 return values ;
111120 }
121+
122+ private static <A extends Annotation > Map <String , Object > extractBaselineValues (
123+ AnnotationDescriptor <A > annotationDescriptor ,
124+ AnnotationTarget target ,
125+ SourceModelBuildingContext context ) {
126+ final HashMap <String , Object > values = new HashMap <>();
127+ for ( AttributeDescriptor <?> attribute : annotationDescriptor .getAttributes () ) {
128+ final Object defaultValue = attribute .getAttributeMethod ().getDefaultValue ();
129+ if ( defaultValue instanceof Annotation annotation ) {
130+ try {
131+ values .put ( attribute .getName (), extractDynamicAnnotationUsage ( annotation , target , context ) );
132+ }
133+ catch (InvocationTargetException | IllegalAccessException e ) {
134+ throw new AnnotationAccessException ( "Error accessing default annotation attribute value" , e );
135+ }
136+ }
137+ else if ( attribute .isMultiValued () ) {
138+ values .put ( attribute .getName (), Arrays .asList ( defaultValue ) );
139+ }
140+ else {
141+ values .put ( attribute .getName (), defaultValue );
142+ }
143+ }
144+ return values ;
145+ }
146+
147+ private static DynamicAnnotationUsage <?> extractDynamicAnnotationUsage (
148+ Annotation annotation ,
149+ AnnotationTarget target ,
150+ SourceModelBuildingContext context ) throws InvocationTargetException , IllegalAccessException {
151+ final Class <? extends Annotation > annotationType = annotation .annotationType ();
152+ final AnnotationDescriptor <?> descriptor = context .getAnnotationDescriptorRegistry ()
153+ .getDescriptor ( annotationType );
154+ final DynamicAnnotationUsage <?> annotationUsage = new DynamicAnnotationUsage <>( descriptor , target , context );
155+ for ( AttributeDescriptor <?> attribute : descriptor .getAttributes () ) {
156+ annotationUsage .setAttributeValue (
157+ attribute .getName (),
158+ attribute .getAttributeMethod ().invoke ( annotation )
159+ );
160+ }
161+ return annotationUsage ;
162+ }
112163}
0 commit comments