15
15
import org .hibernate .models .internal .AnnotationProxy ;
16
16
import org .hibernate .models .spi .AnnotationDescriptor ;
17
17
import org .hibernate .models .spi .AnnotationTarget ;
18
+ import org .hibernate .models .spi .AttributeDescriptor ;
18
19
import org .hibernate .models .spi .MutableAnnotationUsage ;
19
20
20
21
/**
22
+ * AnnotationUsage built dynamically (for dynamic models, XML mappings, etc.)
23
+ *
21
24
* @author Steve Ebersole
22
25
*/
23
26
public class DynamicAnnotationUsage <A extends Annotation > implements MutableAnnotationUsage <A > {
@@ -33,6 +36,8 @@ public DynamicAnnotationUsage(AnnotationDescriptor<A> annotationDescriptor) {
33
36
public DynamicAnnotationUsage (AnnotationDescriptor <A > annotationDescriptor , AnnotationTarget target ) {
34
37
this .annotationDescriptor = annotationDescriptor ;
35
38
this .target = target ;
39
+
40
+ this .values = extractBaselineValues ( annotationDescriptor );
36
41
}
37
42
38
43
@ Override
@@ -60,11 +65,24 @@ public <V> V findAttributeValue(String name) {
60
65
return null ;
61
66
}
62
67
68
+ /**
69
+ * DynamicAnnotationUsage
70
+ */
71
+ @ Override
72
+ public <V > V getAttributeValue (String name ) {
73
+ final Object value = findAttributeValue ( name );
74
+ if ( value == null ) {
75
+ // null values are not supported as annotation attribute values; we honor that
76
+ // in hibernate-models. return the default.
77
+ //noinspection unchecked
78
+ return (V ) getAnnotationDescriptor ().getAttribute ( name ).getAttributeMethod ().getDefaultValue ();
79
+ }
80
+ //noinspection unchecked
81
+ return (V ) value ;
82
+ }
83
+
63
84
@ Override
64
85
public <V > V setAttributeValue (String name , V value ) {
65
- // for set, we need to check up front -
66
- // todo : do we want to add a distinction for a checked versus unchecked set?
67
- // - i.e. setAttributeValueSafely
68
86
if ( annotationDescriptor .getAttribute ( name ) == null ) {
69
87
throw new UnknownAnnotationAttributeException (
70
88
String .format (
@@ -83,4 +101,12 @@ public <V> V setAttributeValue(String name, V value) {
83
101
//noinspection unchecked
84
102
return (V ) values .put ( name , value );
85
103
}
104
+
105
+ private static <A extends Annotation > Map <String , Object > extractBaselineValues (AnnotationDescriptor <A > annotationDescriptor ) {
106
+ final HashMap <String , Object > values = new HashMap <>();
107
+ for ( AttributeDescriptor <?> attribute : annotationDescriptor .getAttributes () ) {
108
+ values .put ( attribute .getName (), attribute .getAttributeMethod ().getDefaultValue () );
109
+ }
110
+ return values ;
111
+ }
86
112
}
0 commit comments