Skip to content

Commit fa0fff9

Browse files
committed
#101 - Add a method to find an annotation from a target or "up"
1 parent 8bed92f commit fa0fff9

File tree

13 files changed

+438
-24
lines changed

13 files changed

+438
-24
lines changed

hibernate-models-jandex/src/test/java/org/hibernate/models/annotations/target/AnnotationTargetTests.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@
22
* SPDX-License-Identifier: Apache-2.0
33
* Copyright: Red Hat Inc. and Hibernate Authors
44
*/
5-
6-
/*
7-
* SPDX-License-Identifier: Apache-2.0
8-
* Copyright: Red Hat Inc. and Hibernate Authors
9-
*/
10-
115
package org.hibernate.models.annotations.target;
126

137
import org.hibernate.models.annotations.target.sub.SubNoGeneratorEntity;

hibernate-models-jandex/src/test/java/org/hibernate/models/annotations/target/ClassGeneratorEntity.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
* SPDX-License-Identifier: Apache-2.0
33
* Copyright: Red Hat Inc. and Hibernate Authors
44
*/
5-
65
package org.hibernate.models.annotations.target;
76

87
/**
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright: Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.models.annotations.target;
6+
7+
import org.hibernate.models.annotations.target.sub.SubNoGeneratorEntity;
8+
import org.hibernate.models.spi.ClassDetails;
9+
import org.hibernate.models.spi.ClassDetailsRegistry;
10+
import org.hibernate.models.spi.FieldDetails;
11+
import org.hibernate.models.spi.SourceModelBuildingContext;
12+
13+
import org.junit.jupiter.api.Test;
14+
15+
import org.jboss.jandex.Index;
16+
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
import static org.hibernate.models.SourceModelTestHelper.buildJandexIndex;
20+
import static org.hibernate.models.SourceModelTestHelper.createBuildingContext;
21+
22+
/**
23+
* @author Steve Ebersole
24+
*/
25+
public class FromContainersTests {
26+
@Test
27+
void testNoGeneratorWithJandex() {
28+
testNoGenerator( buildJandexIndex( NoGeneratorEntity.class ) );
29+
}
30+
31+
@Test
32+
void testNoGeneratorWithoutJandex() {
33+
testNoGenerator( null );
34+
}
35+
36+
private void testNoGenerator(Index jandexIndex) {
37+
final SourceModelBuildingContext buildingContext = createBuildingContext( jandexIndex, NoGeneratorEntity.class );
38+
final ClassDetailsRegistry classDetailsRegistry = buildingContext.getClassDetailsRegistry();
39+
40+
final ClassDetails entityClass = classDetailsRegistry.getClassDetails( NoGeneratorEntity.class.getName() );
41+
{
42+
final GeneratorAnnotation found = entityClass.fromSelfAndContainers(
43+
false,
44+
buildingContext,
45+
(classDetails) -> classDetails.getDirectAnnotationUsage( GeneratorAnnotation.class )
46+
);
47+
assertThat( found ).isNotNull();
48+
assertThat( found.value() ).isEqualTo( GeneratorAnnotation.Source.PACKAGE );
49+
}
50+
51+
{
52+
final GeneratorAnnotation found = entityClass.findFieldByName( "id" ).fromSelfAndContainers(
53+
false,
54+
buildingContext,
55+
(classDetails) -> classDetails.getDirectAnnotationUsage( GeneratorAnnotation.class )
56+
);
57+
assertThat( found ).isNotNull();
58+
assertThat( found.value() ).isEqualTo( GeneratorAnnotation.Source.PACKAGE );
59+
}
60+
}
61+
62+
@Test
63+
void testClassDefinedWithJandex() {
64+
testClassDefined( buildJandexIndex( ClassGeneratorEntity.class ) );
65+
}
66+
67+
@Test
68+
void testClassDefinedWithoutJandex() {
69+
testClassDefined( null );
70+
}
71+
72+
private void testClassDefined(Index jandexIndex) {
73+
final SourceModelBuildingContext buildingContext = createBuildingContext( jandexIndex, ClassGeneratorEntity.class );
74+
final ClassDetailsRegistry classDetailsRegistry = buildingContext.getClassDetailsRegistry();
75+
76+
final ClassDetails entityClass = classDetailsRegistry.getClassDetails( ClassGeneratorEntity.class.getName() );
77+
final FieldDetails idMember = entityClass.findFieldByName( "id" );
78+
79+
final GeneratorAnnotation fromClass = entityClass.fromSelfAndContainers(
80+
false,
81+
buildingContext,
82+
(classDetails) -> classDetails.getDirectAnnotationUsage( GeneratorAnnotation.class )
83+
);
84+
assertThat( fromClass ).isNotNull();
85+
assertThat( fromClass.value() ).isEqualTo( GeneratorAnnotation.Source.TYPE );
86+
87+
final GeneratorAnnotation fromMember = idMember.fromSelfAndContainers(
88+
false,
89+
buildingContext,
90+
(classDetails) -> classDetails.getDirectAnnotationUsage( GeneratorAnnotation.class )
91+
);
92+
assertThat( fromMember ).isNotNull();
93+
assertThat( fromMember.value() ).isEqualTo( GeneratorAnnotation.Source.TYPE );
94+
}
95+
96+
@Test
97+
void testMemberDefinedWithJandex() {
98+
testMemberDefined( buildJandexIndex( MemberGeneratorEntity.class ) );
99+
}
100+
101+
@Test
102+
void testMemberDefinedWithoutJandex() {
103+
testMemberDefined( null );
104+
}
105+
106+
private void testMemberDefined(Index jandexIndex) {
107+
final SourceModelBuildingContext buildingContext = createBuildingContext( jandexIndex, MemberGeneratorEntity.class );
108+
final ClassDetailsRegistry classDetailsRegistry = buildingContext.getClassDetailsRegistry();
109+
110+
final ClassDetails entityClass = classDetailsRegistry.getClassDetails( MemberGeneratorEntity.class.getName() );
111+
final FieldDetails idMember = entityClass.findFieldByName( "id" );
112+
113+
final GeneratorAnnotation fromClass = entityClass.fromSelfAndContainers(
114+
false,
115+
buildingContext,
116+
(classDetails) -> classDetails.getDirectAnnotationUsage( GeneratorAnnotation.class )
117+
);
118+
assertThat( fromClass ).isNotNull();
119+
assertThat( fromClass.value() ).isEqualTo( GeneratorAnnotation.Source.PACKAGE );
120+
121+
final GeneratorAnnotation fromMember = idMember.fromSelfAndContainers(
122+
false,
123+
buildingContext,
124+
(classDetails) -> classDetails.getDirectAnnotationUsage( GeneratorAnnotation.class )
125+
);
126+
assertThat( fromMember ).isNotNull();
127+
assertThat( fromMember.value() ).isEqualTo( GeneratorAnnotation.Source.MEMBER );
128+
}
129+
130+
@Test
131+
void testUpPackageDefinedWithJandex() {
132+
testUpPackageDefined( buildJandexIndex( SubNoGeneratorEntity.class ) );
133+
}
134+
135+
@Test
136+
void testUpPackageDefinedWithoutJandex() {
137+
testUpPackageDefined( null );
138+
}
139+
140+
private void testUpPackageDefined(Index jandexIndex) {
141+
final SourceModelBuildingContext buildingContext = createBuildingContext( jandexIndex, SubNoGeneratorEntity.class );
142+
final ClassDetailsRegistry classDetailsRegistry = buildingContext.getClassDetailsRegistry();
143+
144+
final ClassDetails entityClass = classDetailsRegistry.getClassDetails( SubNoGeneratorEntity.class.getName() );
145+
final FieldDetails idMember = entityClass.findFieldByName( "id" );
146+
147+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
148+
// cross package boundary - false
149+
150+
final GeneratorAnnotation fromClassScoped = entityClass.fromSelfAndContainers(
151+
false,
152+
buildingContext,
153+
(classDetails) -> classDetails.getDirectAnnotationUsage( GeneratorAnnotation.class )
154+
);
155+
assertThat( fromClassScoped ).isNull();
156+
157+
final GeneratorAnnotation fromMemberScoped = idMember.fromSelfAndContainers(
158+
false,
159+
buildingContext,
160+
(classDetails) -> classDetails.getDirectAnnotationUsage( GeneratorAnnotation.class )
161+
);
162+
assertThat( fromMemberScoped ).isNull();
163+
164+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
165+
// cross package boundary - true
166+
167+
final GeneratorAnnotation fromClassUnScoped = entityClass.fromSelfAndContainers(
168+
true,
169+
buildingContext,
170+
(classDetails) -> classDetails.getDirectAnnotationUsage( GeneratorAnnotation.class )
171+
);
172+
assertThat( fromClassUnScoped ).isNotNull();
173+
assertThat( fromClassUnScoped.value() ).isEqualTo( GeneratorAnnotation.Source.PACKAGE );
174+
175+
final GeneratorAnnotation fromMemberUnScoped = idMember.fromSelfAndContainers(
176+
true,
177+
buildingContext,
178+
(classDetails) -> classDetails.getDirectAnnotationUsage( GeneratorAnnotation.class )
179+
);
180+
assertThat( fromMemberUnScoped ).isNotNull();
181+
assertThat( fromMemberUnScoped.value() ).isEqualTo( GeneratorAnnotation.Source.PACKAGE );
182+
}
183+
}

hibernate-models-jandex/src/test/java/org/hibernate/models/annotations/target/GeneratorAnnotation.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
* SPDX-License-Identifier: Apache-2.0
33
* Copyright: Red Hat Inc. and Hibernate Authors
44
*/
5-
65
package org.hibernate.models.annotations.target;
76

87
import java.lang.annotation.ElementType;

hibernate-models-jandex/src/test/java/org/hibernate/models/annotations/target/MemberGeneratorEntity.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
* SPDX-License-Identifier: Apache-2.0
33
* Copyright: Red Hat Inc. and Hibernate Authors
44
*/
5-
65
package org.hibernate.models.annotations.target;
76

87
/**

hibernate-models-jandex/src/test/java/org/hibernate/models/annotations/target/NoGeneratorEntity.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
* SPDX-License-Identifier: Apache-2.0
33
* Copyright: Red Hat Inc. and Hibernate Authors
44
*/
5-
65
package org.hibernate.models.annotations.target;
76

87
/**
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright: Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.models.annotations.target;
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
10+
import org.hibernate.models.spi.AnnotationTarget;
11+
import org.hibernate.models.spi.ClassDetails;
12+
import org.hibernate.models.spi.ClassDetailsRegistry;
13+
import org.hibernate.models.spi.FieldDetails;
14+
import org.hibernate.models.spi.SourceModelBuildingContext;
15+
16+
import org.junit.jupiter.api.Test;
17+
18+
import org.jboss.jandex.Index;
19+
20+
import static org.assertj.core.api.Assertions.assertThat;
21+
import static org.hibernate.models.SourceModelTestHelper.buildJandexIndex;
22+
import static org.hibernate.models.SourceModelTestHelper.createBuildingContext;
23+
24+
/**
25+
* NOTE : the actual placement of annotations is irrelevant for these test - we are simply walking containers
26+
*
27+
* @author Steve Ebersole
28+
*/
29+
public class WalkContainerTests {
30+
@Test
31+
void testNoPackageCrossingWithJandex() {
32+
testNoPackageCrossing( buildJandexIndex( NoGeneratorEntity.class ) );
33+
}
34+
35+
@Test
36+
void testNoPackageCrossingWithoutJandex() {
37+
testNoPackageCrossing( null );
38+
}
39+
40+
private void testNoPackageCrossing(Index jandexIndex) {
41+
final SourceModelBuildingContext buildingContext = createBuildingContext( jandexIndex, NoGeneratorEntity.class );
42+
final ClassDetailsRegistry classDetailsRegistry = buildingContext.getClassDetailsRegistry();
43+
44+
final List<AnnotationTarget> collected = new ArrayList<>();
45+
46+
// starting from the class, we should get 2 - the class and the package
47+
final ClassDetails entityClass = classDetailsRegistry.getClassDetails( NoGeneratorEntity.class.getName() );
48+
entityClass.walkSelfAndContainers( false, buildingContext, collected::add );
49+
assertThat( collected ).hasSize( 2 );
50+
51+
collected.clear();
52+
53+
// starting from the member, we should get 3 containers - this member, the class and the package
54+
final FieldDetails idMember = entityClass.findFieldByName( "id" );
55+
idMember.walkSelfAndContainers( false, buildingContext, collected::add );
56+
assertThat( collected ).hasSize( 3 );
57+
}
58+
@Test
59+
void testPackageCrossingWithJandex() {
60+
testPackageCrossing( buildJandexIndex( NoGeneratorEntity.class ) );
61+
}
62+
63+
@Test
64+
void testPackageCrossingWithoutJandex() {
65+
testPackageCrossing( null );
66+
}
67+
68+
private void testPackageCrossing(Index jandexIndex) {
69+
final SourceModelBuildingContext buildingContext = createBuildingContext( jandexIndex, NoGeneratorEntity.class );
70+
final ClassDetailsRegistry classDetailsRegistry = buildingContext.getClassDetailsRegistry();
71+
72+
final List<AnnotationTarget> collected = new ArrayList<>();
73+
74+
// starting from the class, we should get 6 containers -
75+
// * org.hibernate.models.annotations.target.NoGeneratorEntity
76+
// * org.hibernate.models.annotations.target
77+
// * org.hibernate.models.annotations
78+
// * org.hibernate.models
79+
// * org.hibernate
80+
// * org
81+
final ClassDetails entityClass = classDetailsRegistry.getClassDetails( NoGeneratorEntity.class.getName() );
82+
entityClass.walkSelfAndContainers( true, buildingContext, collected::add );
83+
assertThat( collected ).hasSize( 6 );
84+
85+
collected.clear();
86+
87+
// starting from the member, we should get 7 containers -
88+
// * org.hibernate.models.annotations.target.NoGeneratorEntity#id
89+
// * org.hibernate.models.annotations.target.NoGeneratorEntity
90+
// * org.hibernate.models.annotations.target
91+
// * org.hibernate.models.annotations
92+
// * org.hibernate.models
93+
// * org.hibernate
94+
// * org
95+
final FieldDetails idMember = entityClass.findFieldByName( "id" );
96+
idMember.walkSelfAndContainers( true, buildingContext, collected::add );
97+
assertThat( collected ).hasSize( 7 );
98+
}
99+
}

hibernate-models-jandex/src/test/java/org/hibernate/models/annotations/target/sub/SubNoGeneratorEntity.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
* SPDX-License-Identifier: Apache-2.0
33
* Copyright: Red Hat Inc. and Hibernate Authors
44
*/
5-
65
package org.hibernate.models.annotations.target.sub;
76

87
/**

0 commit comments

Comments
 (0)