Skip to content

Commit aba8500

Browse files
committed
#50 - Better account for annotation attributes without default values
1 parent a8c7d3c commit aba8500

File tree

2 files changed

+96
-1
lines changed

2 files changed

+96
-1
lines changed

src/main/java/org/hibernate/models/internal/AbstractTypeDescriptor.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,15 @@ public V createValue(
3333
AttributeDescriptor<?> attributeDescriptor,
3434
AnnotationTarget target,
3535
SourceModelBuildingContext context) {
36+
final Object defaultValue = attributeDescriptor.getAttributeMethod().getDefaultValue();
37+
if ( defaultValue == null ) {
38+
// a non-defaulted attribute, just return null for the baseline
39+
return null;
40+
}
41+
3642
//noinspection unchecked
3743
final ValueWrapper<V, Object> valueWrapper = (ValueWrapper<V, Object>) createJdkWrapper( context );
38-
return valueWrapper.wrap( attributeDescriptor.getAttributeMethod().getDefaultValue(), target, context );
44+
return valueWrapper.wrap( defaultValue, target, context );
3945
}
4046

4147
@Override
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
* Copyright: Red Hat Inc. and Hibernate Authors
6+
*/
7+
8+
package org.hibernate.models.annotations;
9+
10+
import java.lang.annotation.ElementType;
11+
import java.lang.annotation.Retention;
12+
import java.lang.annotation.RetentionPolicy;
13+
import java.lang.annotation.Target;
14+
import java.util.List;
15+
import java.util.stream.Collectors;
16+
17+
import org.hibernate.models.internal.SourceModelBuildingContextImpl;
18+
import org.hibernate.models.spi.AnnotationDescriptor;
19+
import org.hibernate.models.spi.AnnotationDescriptorRegistry;
20+
import org.hibernate.models.spi.AnnotationUsage;
21+
import org.hibernate.models.spi.AttributeDescriptor;
22+
import org.hibernate.models.spi.ClassDetails;
23+
import org.hibernate.models.spi.ClassDetailsRegistry;
24+
import org.hibernate.models.spi.MutableAnnotationUsage;
25+
26+
import org.junit.jupiter.api.Test;
27+
28+
import org.jboss.jandex.Index;
29+
30+
import jakarta.persistence.Entity;
31+
import jakarta.persistence.Id;
32+
import jakarta.persistence.Table;
33+
34+
import static org.assertj.core.api.Assertions.assertThat;
35+
import static org.hibernate.models.SourceModelTestHelper.buildJandexIndex;
36+
import static org.hibernate.models.SourceModelTestHelper.createBuildingContext;
37+
38+
/**
39+
* @author Steve Ebersole
40+
*/
41+
public class DefaultValueTests {
42+
@Test
43+
void testWithJandex() {
44+
doTest( buildJandexIndex( SimpleEntity.class ) );
45+
}
46+
47+
@Test
48+
void testWithoutJandex() {
49+
doTest( null );
50+
}
51+
52+
private void doTest(Index index) {
53+
final SourceModelBuildingContextImpl buildingContext = createBuildingContext( index, TestEntity.class );
54+
final AnnotationDescriptorRegistry descriptorRegistry = buildingContext.getAnnotationDescriptorRegistry();
55+
56+
final AnnotationDescriptor<CustomAnnotation> descriptor = descriptorRegistry.getDescriptor( CustomAnnotation.class );
57+
final List<String> attributeNames = descriptor
58+
.getAttributes()
59+
.stream()
60+
.map( AttributeDescriptor::getName )
61+
.toList();
62+
assertThat( attributeNames ).contains( "name", "someClassValue" );
63+
assertThat( descriptor.findAttribute( "someClassValue" ) ).isNotNull();
64+
65+
final ClassDetailsRegistry classDetailsRegistry = buildingContext.getClassDetailsRegistry();
66+
final ClassDetails entityClassDetails = classDetailsRegistry.getClassDetails( TestEntity.class.getName() );
67+
final AnnotationUsage<CustomAnnotation> annotationUsage = entityClassDetails.getAnnotationUsage( CustomAnnotation.class );
68+
assertThat( annotationUsage.getClassDetails( "someClassValue" ) ).isNotNull();
69+
assertThat( annotationUsage.getClassDetails( "someClassValue" ).toJavaClass() ).isEqualTo( Entity.class );
70+
71+
final MutableAnnotationUsage<CustomAnnotation> created = descriptor.createUsage( null, buildingContext );
72+
}
73+
74+
@Target( ElementType.TYPE )
75+
@Retention( RetentionPolicy.RUNTIME )
76+
public @interface CustomAnnotation {
77+
String name();
78+
Class someClassValue();
79+
}
80+
81+
@Entity(name="TestEntity")
82+
@Table(name="TestEntity")
83+
@CustomAnnotation( name="tester", someClassValue = Entity.class )
84+
public static class TestEntity {
85+
@Id
86+
private Integer id;
87+
private String name;
88+
}
89+
}

0 commit comments

Comments
 (0)