Skip to content

Commit a67400a

Browse files
committed
#24 - Add TypeDetails#determineRawClass
1 parent 146b022 commit a67400a

File tree

3 files changed

+34
-9
lines changed

3 files changed

+34
-9
lines changed

src/main/java/org/hibernate/models/spi/TypeDetails.java

+10
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,16 @@ default TypeDetails determineRelativeType(ClassDetails container) {
135135
return TypeDetailsHelper.resolveRelativeType( this, container );
136136
}
137137

138+
/**
139+
* Determine the raw {@linkplain ClassDetails class} for the given type. Never returns {@code null}, opting
140+
* to return {@linkplain ClassDetails#OBJECT_CLASS_DETAILS Object} instead if the raw class is not known
141+
*
142+
* @return The raw class details, or {@linkplain ClassDetails#OBJECT_CLASS_DETAILS Object} if "not known".
143+
*/
144+
default ClassDetails determineRawClass() {
145+
return TypeDetailsHelper.resolveRawClass( this );
146+
}
147+
138148

139149
enum Kind {
140150

src/main/java/org/hibernate/models/spi/TypeDetailsHelper.java

+18-8
Original file line numberDiff line numberDiff line change
@@ -215,30 +215,40 @@ else if ( typeDetails.getTypeKind() == TypeDetails.Kind.TYPE_VARIABLE ) {
215215

216216
/**
217217
* Given a type, resolve the underlying ClassDetails
218+
*
219+
* @see TypeDetails#determineRawClass()
218220
*/
219-
public static ClassDetails resolveRawClass(
220-
TypeDetails typeDetails,
221-
@SuppressWarnings("unused") SourceModelBuildingContext buildingContext) {
221+
public static ClassDetails resolveRawClass(TypeDetails typeDetails) {
222222
switch ( typeDetails.getTypeKind() ) {
223223
case CLASS, PRIMITIVE, VOID, ARRAY -> {
224224
return ( (ClassBasedTypeDetails) typeDetails ).getClassDetails();
225225
}
226226
case TYPE_VARIABLE -> {
227227
final TypeVariableDetails resolvedTypeVariable = typeDetails.asTypeVariable();
228228
if ( CollectionHelper.size( resolvedTypeVariable.getBounds() ) == 1 ) {
229-
// and assume the bound is a class
230-
return resolvedTypeVariable.getBounds().get( 0 ).asClassType().getClassDetails();
229+
return resolvedTypeVariable.getBounds().get( 0 ).determineRawClass();
231230
}
232231
return ClassDetails.OBJECT_CLASS_DETAILS;
233232
}
234233
case PARAMETERIZED_TYPE -> {
235234
final ParameterizedTypeDetails parameterizedType = typeDetails.asParameterizedType();
236235
if ( CollectionHelper.size( parameterizedType.getArguments() ) == 1 ) {
237-
// and assume the bound is a class
238-
return parameterizedType.getArguments().get( 0 ).asClassType().getClassDetails();
236+
return parameterizedType.getArguments().get( 0 ).determineRawClass();
239237
}
240238
return ClassDetails.OBJECT_CLASS_DETAILS;
241239
}
240+
case WILDCARD_TYPE -> {
241+
final WildcardTypeDetails wildcardType = typeDetails.asWildcardType();
242+
if ( wildcardType.getBound() != null ) {
243+
return wildcardType.getBound().determineRawClass();
244+
}
245+
return ClassDetails.OBJECT_CLASS_DETAILS;
246+
}
247+
case TYPE_VARIABLE_REFERENCE -> {
248+
final String identifier = typeDetails.asTypeVariableReference().getIdentifier();
249+
final TypeDetails identifiedTypeDetails = typeDetails.resolveTypeVariable( identifier );
250+
return identifiedTypeDetails.determineRawClass();
251+
}
242252
}
243253
return ClassDetails.OBJECT_CLASS_DETAILS;
244254
}
@@ -256,7 +266,7 @@ public static ArrayTypeDetails arrayOf(TypeDetails constituentType, SourceModelB
256266
.resolveClassDetails( "[" + primitiveKind.getJavaTypeChar() );
257267
}
258268
else {
259-
final ClassDetails rawComponentType = TypeDetailsHelper.resolveRawClass( constituentType, buildingContext );
269+
final ClassDetails rawComponentType = constituentType.determineRawClass();
260270
final String arrayClassName = "[L" + rawComponentType.getName().replace( '.', '/' ) + ";";
261271
arrayClassDetails = buildingContext
262272
.getClassDetailsRegistry()

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import org.hibernate.models.spi.FieldDetails;
2121
import org.hibernate.models.spi.ParameterizedTypeDetails;
2222
import org.hibernate.models.spi.TypeDetails;
23-
import org.hibernate.models.spi.TypeDetailsHelper;
2423
import org.hibernate.models.spi.TypeVariableDetails;
2524
import org.hibernate.models.spi.WildcardTypeDetails;
2625

@@ -225,6 +224,7 @@ void testWildcard(Index index) {
225224
assertThat( extendsStuffFieldWildcardType.getBound().asClassType().getClassDetails().getName() ).endsWith( "Stuff" );
226225
assertThat( extendsStuffField.getElementType() ).isSameAs( extendsStuffFieldWildcardType );
227226
assertThat( extendsStuffFieldType.isResolved() ).isTrue();
227+
assertThat( extendsStuffFieldWildcardType.determineRawClass() ).isNotNull();
228228

229229
final FieldDetails superStuffField = classDetails.findFieldByName( "superStuff" );
230230
final TypeDetails superStuffFieldType = superStuffField.getType();
@@ -244,6 +244,7 @@ void testWildcard(Index index) {
244244
assertThat( superStuffFieldWildcardType.getBound().asClassType().getClassDetails().getName() ).endsWith( "Stuff" );
245245
assertThat( superStuffField.getElementType() ).isSameAs( superStuffFieldWildcardType );
246246
assertThat( superStuffFieldWildcardType.isResolved() ).isTrue();
247+
assertThat( superStuffFieldWildcardType.determineRawClass() ).isNotNull();
247248

248249
final FieldDetails namedStuffField = classDetails.findFieldByName( "namedStuff" );
249250
final TypeDetails namedStuffFieldType = namedStuffField.getType();
@@ -267,6 +268,7 @@ void testWildcard(Index index) {
267268
assertThat( namedStuffField.getMapKeyType().getTypeKind() ).isEqualTo( TypeDetails.Kind.CLASS );
268269
assertThat( namedStuffField.getMapKeyType().asClassType().getClassDetails().toJavaClass() ).isEqualTo( String.class );
269270
assertThat( namedStuffFieldType.isResolved() ).isTrue();
271+
assertThat( namedStuffFieldValueWildcardType.determineRawClass() ).isNotNull();
270272
}
271273

272274
@SuppressWarnings("unused")
@@ -295,12 +297,15 @@ static class Item {
295297
static class Stuff extends Item {
296298
}
297299

300+
@SuppressWarnings("unused")
298301
static class Coat extends Stuff {
299302
}
300303

304+
@SuppressWarnings("unused")
301305
static class Hat extends Stuff {
302306
}
303307

308+
@SuppressWarnings("unused")
304309
static class Things {
305310
List<? extends Stuff> extendsStuff;
306311
List<? super Stuff> superStuff;

0 commit comments

Comments
 (0)