Skip to content

Commit 946abba

Browse files
committed
#9 - Add details about "index" and "element"/"component" types to MemberDetails
#21 - Work on generics / parameterized-types
1 parent aa3b441 commit 946abba

File tree

1 file changed

+143
-15
lines changed

1 file changed

+143
-15
lines changed

src/test/java/org/hibernate/models/generics/GenericsSmokeTest.java

Lines changed: 143 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,20 @@
77

88
package org.hibernate.models.generics;
99

10+
import java.util.Collection;
11+
import java.util.List;
12+
import java.util.Map;
13+
import java.util.Set;
14+
1015
import org.hibernate.models.SourceModelTestHelper;
16+
import org.hibernate.models.internal.ClassTypeDetailsImpl;
1117
import org.hibernate.models.internal.SourceModelBuildingContextImpl;
1218
import org.hibernate.models.spi.ClassDetails;
1319
import org.hibernate.models.spi.ClassTypeDetails;
1420
import org.hibernate.models.spi.FieldDetails;
21+
import org.hibernate.models.spi.ParameterizedTypeDetails;
1522
import org.hibernate.models.spi.TypeDetails;
23+
import org.hibernate.models.spi.TypeVariableDetails;
1624

1725
import org.junit.jupiter.api.Test;
1826

@@ -57,17 +65,17 @@ void testSimpleClass(Index jandexIndex) {
5765
}
5866

5967
@Test
60-
void testSimple2ClassWithJandex() {
68+
void testParameterizedClassWithJandex() {
6169
final Index index = SourceModelTestHelper.buildJandexIndex( Simple2.class );
62-
testSimple2Class( index );
70+
testParameterizedClass( index );
6371
}
6472

6573
@Test
66-
void testSimple2ClassWithoutJandex() {
67-
testSimple2Class( null );
74+
void testParameterizedClassWithoutJandex() {
75+
testParameterizedClass( null );
6876
}
6977

70-
void testSimple2Class(Index index) {
78+
void testParameterizedClass(Index index) {
7179
final SourceModelBuildingContextImpl buildingContext = SourceModelTestHelper.createBuildingContext(
7280
index,
7381
Simple2.class
@@ -81,20 +89,23 @@ void testSimple2Class(Index index) {
8189
assertThat( idFieldType.isImplementor( Number.class ) ).isTrue();
8290
assertThat( idFieldType.isImplementor( Object.class ) ).isTrue();
8391
assertThat( idFieldType.isImplementor( String.class ) ).isFalse();
92+
93+
final ClassTypeDetailsImpl classTypeDetails = new ClassTypeDetailsImpl( classDetails, TypeDetails.Kind.CLASS );
94+
assertThat( classTypeDetails.getClassDetails() ).isSameAs( classDetails );
8495
}
8596

8697
@Test
87-
void testSimple3ClassWithJandex() {
98+
void testTypeVariableReferenceJandex() {
8899
final Index index = SourceModelTestHelper.buildJandexIndex( Simple3.class );
89-
testSimple3Class( index );
100+
testTypeVariableReference( index );
90101
}
91102

92103
@Test
93-
void testSimple3ClassWithoutJandex() {
94-
testSimple3Class( null );
104+
void testTypeVariableReferenceWithoutJandex() {
105+
testTypeVariableReference( null );
95106
}
96107

97-
void testSimple3Class(Index index) {
108+
void testTypeVariableReference(Index index) {
98109
final SourceModelBuildingContextImpl buildingContext = SourceModelTestHelper.createBuildingContext(
99110
index,
100111
Simple3.class
@@ -112,21 +123,21 @@ void testSimple3Class(Index index) {
112123
}
113124

114125
@Test
115-
void testParameterizedClassWithJandex() {
126+
void testParameterizedHierarchyWithJandex() {
116127
final Index index = SourceModelTestHelper.buildJandexIndex(
117128
Root.class,
118129
Base1.class,
119130
Base2.class
120131
);
121-
testParameterizedClass( index );
132+
testParameterizedHierarchy( index );
122133
}
123134

124135
@Test
125-
void testParameterizedClass() {
126-
testParameterizedClass( null );
136+
void testParameterizedHierarchyWithoutJandex() {
137+
testParameterizedHierarchy( null );
127138
}
128139

129-
void testParameterizedClass(Index index) {
140+
void testParameterizedHierarchy(Index index) {
130141
final SourceModelBuildingContextImpl buildingContext = SourceModelTestHelper.createBuildingContext(
131142
index,
132143
Root.class,
@@ -173,18 +184,122 @@ void testArrays(Index index) {
173184
assertThat( intArrayFieldType.isImplementor( String.class ) ).isFalse();
174185
}
175186

187+
@Test
188+
void testCollectionsWithJandex() {
189+
final Index index = SourceModelTestHelper.buildJandexIndex( ClassOfCollections.class );
190+
testCollections( index );
191+
}
192+
193+
@Test
194+
void testCollectionsWithoutJandex() {
195+
testCollections( null );
196+
}
197+
198+
void testCollections(Index index) {
199+
final SourceModelBuildingContextImpl buildingContext = SourceModelTestHelper.createBuildingContext(
200+
index,
201+
ClassOfCollections.class
202+
);
203+
204+
final ClassDetails classDetails = buildingContext.getClassDetailsRegistry().getClassDetails( ClassOfCollections.class.getName() );
205+
206+
{
207+
final FieldDetails listOfString = classDetails.findFieldByName( "listOfString" );
208+
209+
assertThat( listOfString.getType() ).isInstanceOf( ParameterizedTypeDetails.class );
210+
final ParameterizedTypeDetails type = (ParameterizedTypeDetails) listOfString.getType();
211+
assertThat( type.isImplementor( Collection.class ) ).isTrue();
212+
assertThat( type.isImplementor( List.class ) ).isTrue();
213+
assertThat( type.isImplementor( Set.class ) ).isFalse();
214+
assertThat( type.isImplementor( Map.class ) ).isFalse();
215+
216+
assertThat( type.getArguments() ).hasSize( 1 );
217+
assertThat( type.getArguments().get(0) ).isInstanceOf( ClassTypeDetails.class );
218+
final ClassTypeDetails elementType = (ClassTypeDetails) type.getArguments().get( 0);
219+
assertThat( elementType.isImplementor( String.class ) ).isTrue();
220+
}
221+
222+
{
223+
final FieldDetails listOfT = classDetails.findFieldByName( "listOfT" );
224+
225+
assertThat( listOfT.getType() ).isInstanceOf( ParameterizedTypeDetails.class );
226+
final ParameterizedTypeDetails type = (ParameterizedTypeDetails) listOfT.getType();
227+
assertThat( type.isImplementor( Collection.class ) ).isTrue();
228+
assertThat( type.isImplementor( List.class ) ).isTrue();
229+
assertThat( type.isImplementor( Set.class ) ).isFalse();
230+
assertThat( type.isImplementor( Map.class ) ).isFalse();
231+
232+
assertThat( type.getArguments() ).hasSize( 1 );
233+
assertThat( type.getArguments().get(0) ).isInstanceOf( TypeVariableDetails.class );
234+
final TypeVariableDetails elementType = (TypeVariableDetails) type.getArguments().get( 0);
235+
assertThat( elementType.isImplementor( String.class ) ).isFalse();
236+
assertThat( elementType.getIdentifier() ).isEqualTo( "T" );
237+
}
238+
239+
{
240+
final FieldDetails mapOfString = classDetails.findFieldByName( "mapOfString" );
241+
242+
assertThat( mapOfString.getType() ).isInstanceOf( ParameterizedTypeDetails.class );
243+
final ParameterizedTypeDetails type = (ParameterizedTypeDetails) mapOfString.getType();
244+
assertThat( type.isImplementor( Collection.class ) ).isFalse();
245+
assertThat( type.isImplementor( List.class ) ).isFalse();
246+
assertThat( type.isImplementor( Set.class ) ).isFalse();
247+
assertThat( type.isImplementor( Map.class ) ).isTrue();
248+
249+
assertThat( type.getArguments() ).hasSize( 2 );
250+
251+
// key
252+
assertThat( type.getArguments().get(0) ).isInstanceOf( ClassTypeDetails.class );
253+
final ClassTypeDetails keyType = (ClassTypeDetails) type.getArguments().get( 0);
254+
assertThat( keyType.isImplementor( String.class ) ).isTrue();
255+
256+
// value
257+
assertThat( type.getArguments().get(1) ).isInstanceOf( ClassTypeDetails.class );
258+
final ClassTypeDetails valueType = (ClassTypeDetails) type.getArguments().get( 0);
259+
assertThat( valueType.isImplementor( String.class ) ).isTrue();
260+
}
261+
262+
{
263+
final FieldDetails mapOfT = classDetails.findFieldByName( "mapOfT" );
264+
265+
assertThat( mapOfT.getType() ).isInstanceOf( ParameterizedTypeDetails.class );
266+
final ParameterizedTypeDetails type = (ParameterizedTypeDetails) mapOfT.getType();
267+
assertThat( type.isImplementor( Collection.class ) ).isFalse();
268+
assertThat( type.isImplementor( List.class ) ).isFalse();
269+
assertThat( type.isImplementor( Set.class ) ).isFalse();
270+
assertThat( type.isImplementor( Map.class ) ).isTrue();
271+
272+
assertThat( type.getArguments() ).hasSize( 2 );
273+
274+
// key
275+
assertThat( type.getArguments().get(0) ).isInstanceOf( ClassTypeDetails.class );
276+
final ClassTypeDetails keyType = (ClassTypeDetails) type.getArguments().get(0);
277+
assertThat( keyType.isImplementor( String.class ) ).isTrue();
278+
279+
// value
280+
assertThat( type.getArguments().get(1) ).isInstanceOf( TypeVariableDetails.class );
281+
final TypeVariableDetails valueType = (TypeVariableDetails) type.getArguments().get(1);
282+
assertThat( valueType.isImplementor( String.class ) ).isFalse();
283+
assertThat( valueType.getIdentifier() ).isEqualTo( "T" );
284+
}
285+
}
286+
287+
@SuppressWarnings("unused")
176288
static class Simple {
177289
Integer id;
178290
}
179291

292+
@SuppressWarnings("unused")
180293
static class Simple2<I extends Number> {
181294
I id;
182295
}
183296

297+
@SuppressWarnings("unused")
184298
static class Simple3<I extends Comparable<I>> {
185299
I id;
186300
}
187301

302+
@SuppressWarnings("unused")
188303
static class Root<I> {
189304
I id;
190305
}
@@ -195,10 +310,23 @@ static class Base1 extends Root<Integer> {
195310
static class Base2 extends Root<String> {
196311
}
197312

313+
@SuppressWarnings("unused")
198314
static class ClassOfArrays<T> {
199315
int[] intArray;
200316
int[][] int2Array;
201317

202318
T[] tArray;
203319
}
320+
321+
@SuppressWarnings("unused")
322+
static class ClassOfCollections<T> {
323+
List<String> listOfString;
324+
List<T> listOfT;
325+
326+
Set<String> setOfString;
327+
Set<T> setOfT;
328+
329+
Map<String,String> mapOfString;
330+
Map<String,T> mapOfT;
331+
}
204332
}

0 commit comments

Comments
 (0)