Skip to content

Commit 090bd5b

Browse files
mbelladesebersole
authored andcommitted
Improvements to relative generic type resolution
* Introduce the concept of `declaringType` for type variables * Better handling of subtypes re-declaring a type variable with the same identifier * Correctly handle nested recursive type resolution
1 parent df2f29f commit 090bd5b

24 files changed

+441
-146
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.hibernate.models.spi.ArrayTypeDetails;
1414
import org.hibernate.models.spi.ClassDetails;
1515
import org.hibernate.models.spi.TypeDetails;
16+
import org.hibernate.models.spi.TypeVariableDetails;
1617

1718
/**
1819
* @author Steve Ebersole
@@ -84,9 +85,9 @@ public boolean isImplementor(Class<?> checkType) {
8485
}
8586

8687
@Override
87-
public TypeDetails resolveTypeVariable(String identifier) {
88+
public TypeDetails resolveTypeVariable(TypeVariableDetails typeVariable) {
8889
if ( constituentType.getTypeKind() == Kind.PARAMETERIZED_TYPE ) {
89-
return constituentType.asParameterizedType().resolveTypeVariable( identifier );
90+
return constituentType.asParameterizedType().resolveTypeVariable( typeVariable );
9091
}
9192
return null;
9293
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.hibernate.models.spi.ClassDetails;
1313
import org.hibernate.models.spi.ClassTypeDetails;
1414
import org.hibernate.models.spi.TypeDetails;
15+
import org.hibernate.models.spi.TypeVariableDetails;
1516

1617
/**
1718
* @author Steve Ebersole
@@ -42,8 +43,8 @@ public Kind getTypeKind() {
4243
}
4344

4445
@Override
45-
public TypeDetails resolveTypeVariable(String identifier) {
46-
return getClassDetails().resolveTypeVariable( identifier );
46+
public TypeDetails resolveTypeVariable(TypeVariableDetails typeVariable) {
47+
return getClassDetails().resolveTypeVariable( typeVariable );
4748
}
4849

4950
@Override

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,15 @@ public TypeDetails caseWildcardType(WildcardTypeDetails wildcardType) {
8686
@Override
8787
public TypeDetails caseTypeVariable(TypeVariableDetails typeVariable) {
8888
if ( typeVariable.isImplementor( Collection.class ) ) {
89-
return memberTypeDetails.resolveTypeVariable( typeVariable.getIdentifier() );
89+
return memberTypeDetails.resolveTypeVariable( typeVariable );
9090
}
9191
return null;
9292
}
9393

9494
@Override
9595
public TypeDetails caseTypeVariableReference(TypeVariableReferenceDetails typeVariableReference) {
9696
if ( typeVariableReference.isImplementor( Collection.class ) ) {
97-
return memberTypeDetails.resolveTypeVariable( typeVariableReference.getIdentifier() );
97+
return memberTypeDetails.resolveTypeVariable( typeVariableReference.getTarget() );
9898
}
9999
return null;
100100
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,15 @@ public TypeDetails caseWildcardType(WildcardTypeDetails wildcardType) {
8686
@Override
8787
public TypeDetails caseTypeVariable(TypeVariableDetails typeVariable) {
8888
if ( typeVariable.isImplementor( Map.class ) ) {
89-
return memberTypeDetails.resolveTypeVariable( typeVariable.getIdentifier() );
89+
return memberTypeDetails.resolveTypeVariable( typeVariable );
9090
}
9191
return null;
9292
}
9393

9494
@Override
9595
public TypeDetails caseTypeVariableReference(TypeVariableReferenceDetails typeVariableReference) {
9696
if ( typeVariableReference.isImplementor( Map.class ) ) {
97-
return memberTypeDetails.resolveTypeVariable( typeVariableReference.getIdentifier() );
97+
return memberTypeDetails.resolveTypeVariable( typeVariableReference.getTarget() );
9898
}
9999
return null;
100100
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,15 @@ public TypeDetails caseWildcardType(WildcardTypeDetails wildcardType) {
8686
@Override
8787
public TypeDetails caseTypeVariable(TypeVariableDetails typeVariable) {
8888
if ( typeVariable.isImplementor( Map.class ) ) {
89-
return memberTypeDetails.resolveTypeVariable( typeVariable.getIdentifier() );
89+
return memberTypeDetails.resolveTypeVariable( typeVariable );
9090
}
9191
return null;
9292
}
9393

9494
@Override
9595
public TypeDetails caseTypeVariableReference(TypeVariableReferenceDetails typeVariableReference) {
9696
if ( typeVariableReference.isImplementor( Map.class ) ) {
97-
return memberTypeDetails.resolveTypeVariable( typeVariableReference.getIdentifier() );
97+
return memberTypeDetails.resolveTypeVariable( typeVariableReference.getTarget() );
9898
}
9999
return null;
100100
}

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

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import org.hibernate.models.spi.TypeVariableDetails;
1616
import org.hibernate.models.spi.TypeVariableScope;
1717

18+
import static org.hibernate.models.spi.TypeDetailsHelper.resolveTypeVariableFromParameterizedType;
19+
1820
/**
1921
* @author Steve Ebersole
2022
*/
@@ -45,16 +47,7 @@ public TypeVariableScope getOwner() {
4547
}
4648

4749
@Override
48-
public TypeDetails resolveTypeVariable(String identifier) {
49-
final List<TypeVariableDetails> typeParameters = genericClassDetails.getTypeParameters();
50-
assert typeParameters.size() == arguments.size();
51-
52-
for ( int i = 0; i < typeParameters.size(); i++ ) {
53-
if ( typeParameters.get( i ).getIdentifier().equals( identifier ) ) {
54-
return arguments.get( i );
55-
}
56-
}
57-
58-
return null;
50+
public TypeDetails resolveTypeVariable(TypeVariableDetails typeVariable) {
51+
return resolveTypeVariableFromParameterizedType( this, typeVariable );
5952
}
6053
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.hibernate.models.spi.ClassDetails;
1111
import org.hibernate.models.spi.PrimitiveTypeDetails;
1212
import org.hibernate.models.spi.TypeDetails;
13+
import org.hibernate.models.spi.TypeVariableDetails;
1314

1415
/**
1516
* @author Steve Ebersole
@@ -31,7 +32,7 @@ public PrimitiveTypeDetails asPrimitiveType() {
3132
}
3233

3334
@Override
34-
public TypeDetails resolveTypeVariable(String identifier) {
35+
public TypeDetails resolveTypeVariable(TypeVariableDetails typeVariable) {
3536
return this;
3637
}
3738

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import java.util.List;
1111

12+
import org.hibernate.models.spi.ClassDetails;
1213
import org.hibernate.models.spi.TypeDetails;
1314
import org.hibernate.models.spi.TypeVariableDetails;
1415

@@ -17,12 +18,14 @@
1718
*/
1819
public class TypeVariableDetailsImpl implements TypeVariableDetails {
1920
private final String identifier;
21+
private final ClassDetails declaringType;
2022
private final List<TypeDetails> bounds;
2123

2224
private final String name;
2325

24-
public TypeVariableDetailsImpl(String identifier, List<TypeDetails> bounds) {
26+
public TypeVariableDetailsImpl(String identifier, ClassDetails declaringType, List<TypeDetails> bounds) {
2527
this.identifier = identifier;
28+
this.declaringType = declaringType;
2629
this.bounds = bounds;
2730

2831
this.name = calculateName( bounds );
@@ -39,6 +42,10 @@ private String calculateName(List<TypeDetails> bounds) {
3942
return identifier;
4043
}
4144

45+
@Override public ClassDetails getDeclaringType() {
46+
return declaringType;
47+
}
48+
4249
@Override public List<TypeDetails> getBounds() {
4350
return bounds;
4451
}
@@ -57,8 +64,8 @@ public boolean isImplementor(Class<?> checkType) {
5764
}
5865

5966
@Override
60-
public TypeDetails resolveTypeVariable(String identifier) {
61-
return this.identifier.equals( identifier ) ? this : null;
67+
public TypeDetails resolveTypeVariable(TypeVariableDetails typeVariable) {
68+
return identifier.equals( typeVariable.getIdentifier() ) ? this : null;
6269
}
6370

6471
@Override

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public boolean isImplementor(Class<?> checkType) {
6161
}
6262

6363
@Override
64-
public TypeDetails resolveTypeVariable(String identifier) {
65-
return this.identifier.equals( identifier ) ? target : null;
64+
public TypeDetails resolveTypeVariable(TypeVariableDetails typeVariable) {
65+
return this.identifier.equals( typeVariable.getIdentifier() ) ? target : null;
6666
}
6767
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import org.hibernate.models.spi.ClassDetails;
1313
import org.hibernate.models.spi.TypeDetails;
14+
import org.hibernate.models.spi.TypeVariableDetails;
1415
import org.hibernate.models.spi.VoidTypeDetails;
1516

1617
/**
@@ -41,7 +42,7 @@ public VoidTypeDetails asVoidType() {
4142
}
4243

4344
@Override
44-
public TypeDetails resolveTypeVariable(String identifier) {
45+
public TypeDetails resolveTypeVariable(TypeVariableDetails typeVariable) {
4546
return this;
4647
}
4748

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import org.hibernate.models.spi.ClassTypeDetails;
1111
import org.hibernate.models.spi.TypeDetails;
12+
import org.hibernate.models.spi.TypeVariableDetails;
1213
import org.hibernate.models.spi.WildcardTypeDetails;
1314

1415
/**
@@ -74,7 +75,7 @@ public boolean isImplementor(Class<?> checkType) {
7475
}
7576

7677
@Override
77-
public TypeDetails resolveTypeVariable(String identifier) {
78+
public TypeDetails resolveTypeVariable(TypeVariableDetails typeVariable) {
7879
return null;
7980
}
8081
}

src/main/java/org/hibernate/models/internal/jandex/JandexBuilders.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
import org.jboss.jandex.MethodInfo;
2020
import org.jboss.jandex.Type;
2121

22-
import static org.hibernate.models.internal.jandex.JandexTypeSwitchStandard.TYPE_SWITCH_STANDARD;
23-
import static org.hibernate.models.internal.jandex.JandexTypeSwitcher.switchType;
22+
import static org.hibernate.models.internal.jandex.JandexTypeSwitchStandard.switchType;
2423

2524
/**
2625
* Jandex based ClassDetailsBuilder
@@ -137,7 +136,7 @@ public static JandexMethodDetails buildMethodDetails(
137136
return new JandexMethodDetails(
138137
method,
139138
MethodDetails.MethodKind.GETTER,
140-
switchType( returnType, TYPE_SWITCH_STANDARD, buildingContext ),
139+
switchType( returnType, declaringType, buildingContext ),
141140
declaringType,
142141
buildingContext
143142
);
@@ -148,7 +147,7 @@ else if ( isBoolean( returnType ) && ( methodName.startsWith( "is" )
148147
return new JandexMethodDetails(
149148
method,
150149
MethodDetails.MethodKind.GETTER,
151-
switchType( returnType, TYPE_SWITCH_STANDARD, buildingContext ),
150+
switchType( returnType, declaringType, buildingContext ),
152151
declaringType,
153152
buildingContext
154153
);
@@ -162,7 +161,7 @@ else if ( isBoolean( returnType ) && ( methodName.startsWith( "is" )
162161
return new JandexMethodDetails(
163162
method,
164163
MethodDetails.MethodKind.SETTER,
165-
switchType( method.parameterType( 0 ), TYPE_SWITCH_STANDARD, buildingContext ),
164+
switchType( method.parameterType( 0 ), declaringType, buildingContext ),
166165
declaringType,
167166
buildingContext
168167
);

src/main/java/org/hibernate/models/internal/jandex/JandexClassDetails.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
import static java.util.Collections.emptyList;
3232
import static org.hibernate.models.internal.ModelsClassLogging.MODELS_CLASS_LOGGER;
33-
import static org.hibernate.models.internal.jandex.JandexTypeSwitchStandard.TYPE_SWITCH_STANDARD;
33+
import static org.hibernate.models.internal.jandex.JandexTypeSwitchStandard.switchType;
3434
import static org.hibernate.models.internal.util.CollectionHelper.arrayList;
3535
import static org.hibernate.models.internal.util.CollectionHelper.isEmpty;
3636

@@ -41,9 +41,9 @@ public class JandexClassDetails extends AbstractAnnotationTarget implements Clas
4141
private final ClassInfo classInfo;
4242

4343
private final ClassDetails superClass;
44-
private final TypeDetails genericSuperType;
44+
private TypeDetails genericSuperType;
4545
private final List<TypeDetails> implementedInterfaces;
46-
private final List<TypeVariableDetails> typeParameters;
46+
private List<TypeVariableDetails> typeParameters;
4747

4848
private List<FieldDetails> fields;
4949
private List<MethodDetails> methods;
@@ -54,9 +54,7 @@ public JandexClassDetails(ClassInfo classInfo, SourceModelBuildingContext buildi
5454
this.classInfo = classInfo;
5555

5656
this.superClass = determineSuperType( classInfo, buildingContext );
57-
this.genericSuperType = determineGenericSuperType( classInfo, buildingContext );
5857
this.implementedInterfaces = determineInterfaces( classInfo, buildingContext );
59-
this.typeParameters = determineTypeParameters( classInfo, buildingContext );
6058
}
6159

6260
private static ClassDetails determineSuperType(
@@ -76,7 +74,7 @@ private TypeDetails determineGenericSuperType(ClassInfo classInfo, SourceModelBu
7674
return null;
7775
}
7876

79-
return JandexTypeSwitcher.switchType( classInfo.superClassType(), TYPE_SWITCH_STANDARD, buildingContext );
77+
return switchType( classInfo.superClassType(), buildingContext );
8078
}
8179

8280
private static List<TypeDetails> determineInterfaces(
@@ -89,9 +87,8 @@ private static List<TypeDetails> determineInterfaces(
8987

9088
final List<TypeDetails> result = arrayList( interfaceTypes.size() );
9189
for ( Type interfaceType : interfaceTypes ) {
92-
final TypeDetails switchedType = JandexTypeSwitcher.switchType(
90+
final TypeDetails switchedType = switchType(
9391
interfaceType,
94-
TYPE_SWITCH_STANDARD,
9592
buildingContext
9693
);
9794
result.add( switchedType );
@@ -107,7 +104,7 @@ private List<TypeVariableDetails> determineTypeParameters(ClassInfo classInfo, S
107104

108105
final ArrayList<TypeVariableDetails> result = arrayList( jandexTypeVariables.size() );
109106
for ( TypeVariable jandexTypeVariable : jandexTypeVariables ) {
110-
result.add( (TypeVariableDetails) JandexTypeSwitcher.switchType( jandexTypeVariable, TYPE_SWITCH_STANDARD, buildingContext ) );
107+
result.add( (TypeVariableDetails) switchType( jandexTypeVariable, this, buildingContext ) );
111108
}
112109
return result;
113110
}
@@ -149,6 +146,9 @@ public ClassDetails getSuperClass() {
149146

150147
@Override
151148
public TypeDetails getGenericSuperType() {
149+
if ( genericSuperType == null && classInfo.superClassType() != null ) {
150+
genericSuperType = determineGenericSuperType( classInfo, getBuildingContext() );
151+
}
152152
return genericSuperType;
153153
}
154154

@@ -159,6 +159,9 @@ public List<TypeDetails> getImplementedInterfaces() {
159159

160160
@Override
161161
public List<TypeVariableDetails> getTypeParameters() {
162+
if ( typeParameters == null ) {
163+
this.typeParameters = determineTypeParameters( classInfo, getBuildingContext() );
164+
}
162165
return typeParameters;
163166
}
164167

src/main/java/org/hibernate/models/internal/jandex/JandexFieldDetails.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,17 @@
1111
import java.util.Collection;
1212
import java.util.Map;
1313

14-
import org.hibernate.models.spi.MutableMemberDetails;
1514
import org.hibernate.models.spi.ClassDetails;
1615
import org.hibernate.models.spi.FieldDetails;
16+
import org.hibernate.models.spi.MutableMemberDetails;
1717
import org.hibernate.models.spi.SourceModelBuildingContext;
1818
import org.hibernate.models.spi.TypeDetails;
1919

2020
import org.jboss.jandex.AnnotationTarget;
2121
import org.jboss.jandex.FieldInfo;
2222
import org.jboss.jandex.Type;
2323

24-
import static org.hibernate.models.internal.jandex.JandexTypeSwitchStandard.TYPE_SWITCH_STANDARD;
25-
import static org.hibernate.models.internal.jandex.JandexTypeSwitcher.switchType;
24+
import static org.hibernate.models.internal.jandex.JandexTypeSwitchStandard.switchType;
2625

2726
/**
2827
* @author Steve Ebersole
@@ -42,7 +41,7 @@ public JandexFieldDetails(
4241
super( buildingContext );
4342
this.fieldInfo = fieldInfo;
4443
this.declaringType = declaringType;
45-
this.type = switchType( fieldInfo.type(), TYPE_SWITCH_STANDARD, buildingContext );
44+
this.type = switchType( fieldInfo.type(), declaringType, buildingContext );
4645

4746
this.isArray = fieldInfo.type().kind() == Type.Kind.ARRAY;
4847
this.isPlural = isArray || type.isImplementor( Collection.class ) || type.isImplementor( Map.class );

src/main/java/org/hibernate/models/internal/jandex/JandexRecordComponentDetails.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020
import org.jboss.jandex.RecordComponentInfo;
2121
import org.jboss.jandex.Type;
2222

23-
import static org.hibernate.models.internal.jandex.JandexTypeSwitchStandard.TYPE_SWITCH_STANDARD;
24-
import static org.hibernate.models.internal.jandex.JandexTypeSwitcher.switchType;
23+
import static org.hibernate.models.internal.jandex.JandexTypeSwitchStandard.switchType;
2524

2625
/**
2726
* @author Steve Ebersole
@@ -41,7 +40,7 @@ public JandexRecordComponentDetails(
4140
super( buildingContext );
4241
this.recordComponentInfo = recordComponentInfo;
4342
this.declaringType = declaringType;
44-
this.type = switchType( recordComponentInfo.type(), TYPE_SWITCH_STANDARD, buildingContext );
43+
this.type = switchType( recordComponentInfo.type(), declaringType, buildingContext );
4544

4645
this.isArray = recordComponentInfo.type().kind() == Type.Kind.ARRAY;
4746
this.isPlural = isArray || type.isImplementor( Collection.class ) || type.isImplementor( Map.class );

0 commit comments

Comments
 (0)