7
7
8
8
package org .hibernate .models .generics ;
9
9
10
+ import java .util .Collection ;
11
+ import java .util .List ;
12
+ import java .util .Map ;
13
+ import java .util .Set ;
14
+
10
15
import org .hibernate .models .SourceModelTestHelper ;
16
+ import org .hibernate .models .internal .ClassTypeDetailsImpl ;
11
17
import org .hibernate .models .internal .SourceModelBuildingContextImpl ;
12
18
import org .hibernate .models .spi .ClassDetails ;
13
19
import org .hibernate .models .spi .ClassTypeDetails ;
14
20
import org .hibernate .models .spi .FieldDetails ;
21
+ import org .hibernate .models .spi .ParameterizedTypeDetails ;
15
22
import org .hibernate .models .spi .TypeDetails ;
23
+ import org .hibernate .models .spi .TypeVariableDetails ;
16
24
17
25
import org .junit .jupiter .api .Test ;
18
26
@@ -57,17 +65,17 @@ void testSimpleClass(Index jandexIndex) {
57
65
}
58
66
59
67
@ Test
60
- void testSimple2ClassWithJandex () {
68
+ void testParameterizedClassWithJandex () {
61
69
final Index index = SourceModelTestHelper .buildJandexIndex ( Simple2 .class );
62
- testSimple2Class ( index );
70
+ testParameterizedClass ( index );
63
71
}
64
72
65
73
@ Test
66
- void testSimple2ClassWithoutJandex () {
67
- testSimple2Class ( null );
74
+ void testParameterizedClassWithoutJandex () {
75
+ testParameterizedClass ( null );
68
76
}
69
77
70
- void testSimple2Class (Index index ) {
78
+ void testParameterizedClass (Index index ) {
71
79
final SourceModelBuildingContextImpl buildingContext = SourceModelTestHelper .createBuildingContext (
72
80
index ,
73
81
Simple2 .class
@@ -81,20 +89,23 @@ void testSimple2Class(Index index) {
81
89
assertThat ( idFieldType .isImplementor ( Number .class ) ).isTrue ();
82
90
assertThat ( idFieldType .isImplementor ( Object .class ) ).isTrue ();
83
91
assertThat ( idFieldType .isImplementor ( String .class ) ).isFalse ();
92
+
93
+ final ClassTypeDetailsImpl classTypeDetails = new ClassTypeDetailsImpl ( classDetails , TypeDetails .Kind .CLASS );
94
+ assertThat ( classTypeDetails .getClassDetails () ).isSameAs ( classDetails );
84
95
}
85
96
86
97
@ Test
87
- void testSimple3ClassWithJandex () {
98
+ void testTypeVariableReferenceJandex () {
88
99
final Index index = SourceModelTestHelper .buildJandexIndex ( Simple3 .class );
89
- testSimple3Class ( index );
100
+ testTypeVariableReference ( index );
90
101
}
91
102
92
103
@ Test
93
- void testSimple3ClassWithoutJandex () {
94
- testSimple3Class ( null );
104
+ void testTypeVariableReferenceWithoutJandex () {
105
+ testTypeVariableReference ( null );
95
106
}
96
107
97
- void testSimple3Class (Index index ) {
108
+ void testTypeVariableReference (Index index ) {
98
109
final SourceModelBuildingContextImpl buildingContext = SourceModelTestHelper .createBuildingContext (
99
110
index ,
100
111
Simple3 .class
@@ -112,21 +123,21 @@ void testSimple3Class(Index index) {
112
123
}
113
124
114
125
@ Test
115
- void testParameterizedClassWithJandex () {
126
+ void testParameterizedHierarchyWithJandex () {
116
127
final Index index = SourceModelTestHelper .buildJandexIndex (
117
128
Root .class ,
118
129
Base1 .class ,
119
130
Base2 .class
120
131
);
121
- testParameterizedClass ( index );
132
+ testParameterizedHierarchy ( index );
122
133
}
123
134
124
135
@ Test
125
- void testParameterizedClass () {
126
- testParameterizedClass ( null );
136
+ void testParameterizedHierarchyWithoutJandex () {
137
+ testParameterizedHierarchy ( null );
127
138
}
128
139
129
- void testParameterizedClass (Index index ) {
140
+ void testParameterizedHierarchy (Index index ) {
130
141
final SourceModelBuildingContextImpl buildingContext = SourceModelTestHelper .createBuildingContext (
131
142
index ,
132
143
Root .class ,
@@ -173,18 +184,122 @@ void testArrays(Index index) {
173
184
assertThat ( intArrayFieldType .isImplementor ( String .class ) ).isFalse ();
174
185
}
175
186
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" )
176
288
static class Simple {
177
289
Integer id ;
178
290
}
179
291
292
+ @ SuppressWarnings ("unused" )
180
293
static class Simple2 <I extends Number > {
181
294
I id ;
182
295
}
183
296
297
+ @ SuppressWarnings ("unused" )
184
298
static class Simple3 <I extends Comparable <I >> {
185
299
I id ;
186
300
}
187
301
302
+ @ SuppressWarnings ("unused" )
188
303
static class Root <I > {
189
304
I id ;
190
305
}
@@ -195,10 +310,23 @@ static class Base1 extends Root<Integer> {
195
310
static class Base2 extends Root <String > {
196
311
}
197
312
313
+ @ SuppressWarnings ("unused" )
198
314
static class ClassOfArrays <T > {
199
315
int [] intArray ;
200
316
int [][] int2Array ;
201
317
202
318
T [] tArray ;
203
319
}
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
+ }
204
332
}
0 commit comments