Skip to content

Commit ddc9377

Browse files
committed
#39 - DynamicAnnotationUsage#getAttributeValue should return default value if not set
1 parent 9595811 commit ddc9377

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

src/main/java/org/hibernate/models/internal/dynamic/DynamicAnnotationUsage.java

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@
1515
import org.hibernate.models.internal.AnnotationProxy;
1616
import org.hibernate.models.spi.AnnotationDescriptor;
1717
import org.hibernate.models.spi.AnnotationTarget;
18+
import org.hibernate.models.spi.AttributeDescriptor;
1819
import org.hibernate.models.spi.MutableAnnotationUsage;
1920

2021
/**
22+
* AnnotationUsage built dynamically (for dynamic models, XML mappings, etc.)
23+
*
2124
* @author Steve Ebersole
2225
*/
2326
public class DynamicAnnotationUsage<A extends Annotation> implements MutableAnnotationUsage<A> {
@@ -33,6 +36,8 @@ public DynamicAnnotationUsage(AnnotationDescriptor<A> annotationDescriptor) {
3336
public DynamicAnnotationUsage(AnnotationDescriptor<A> annotationDescriptor, AnnotationTarget target) {
3437
this.annotationDescriptor = annotationDescriptor;
3538
this.target = target;
39+
40+
this.values = extractBaselineValues( annotationDescriptor );
3641
}
3742

3843
@Override
@@ -60,11 +65,24 @@ public <V> V findAttributeValue(String name) {
6065
return null;
6166
}
6267

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+
6384
@Override
6485
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
6886
if ( annotationDescriptor.getAttribute( name ) == null ) {
6987
throw new UnknownAnnotationAttributeException(
7088
String.format(
@@ -83,4 +101,12 @@ public <V> V setAttributeValue(String name, V value) {
83101
//noinspection unchecked
84102
return (V) values.put( name, value );
85103
}
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+
}
86112
}

0 commit comments

Comments
 (0)