Skip to content

Commit a599cba

Browse files
authored
Fix #132: MemberResolver misses members inherited via self-referential interface (ResolvedRecursiveType) (#135)
1 parent c4bf424 commit a599cba

8 files changed

Lines changed: 284 additions & 35 deletions

File tree

‎VERSION.txt‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ Release notes:
5858
can not have a common subtype with bound (as per Java casting rules) are rejected
5959
(fix by @cowtowncoder w/ Claude code)
6060
#132: `MemberResolver` misses members inherited via self-referential interface
61+
(`ResolvedRecursiveType` as implemented interface)
62+
- `getImplementedInterfaces()` returns types self-referential interfaces represent
63+
(same as `getParentClass()`): for expanding generic types (like `D<T>` for
64+
`C<T> extends Base<D<T>>`, `D<T> extends C<List<T>>`), following supertypes and
65+
their type parameters may yield ever deeper types
66+
- `ResolvedRecursiveType.getActualType()` returns stand-alone type if referenced
67+
type is only valid within its resolution context (like `B` in `B extends Base<A>`,
68+
when resolving `A extends Base<B>`)
6169
(fix by @cowtowncoder w/ Claude code)
6270

6371
1.7.3 (02-Jan-2026)

‎src/main/java/com/fasterxml/classmate/ResolvedType.java‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,14 @@ public final boolean canCreateSubtype(Class<?> subtype) {
9898
* {@link java.lang.Object}.
9999
* Also, placeholders for cyclic (recursive) types return null for
100100
* this method.
101+
*<p>
102+
* NOTE: if parent class is a self-reference ({@link ResolvedRecursiveType}),
103+
* type it represents is returned instead (see {@link ResolvedRecursiveType#getActualType()}).
104+
* For generic types that expand, like {@code D<T>} in
105+
* {@code C<T> extends Base<D<T>>} and {@code D<T> extends C<List<T>>}, following
106+
* supertypes and their type parameters may therefore yield ever deeper types
107+
* ({@code D<String>}, {@code C<List<String>>}, {@code D<List<String>>} and so on):
108+
* code traversing types this way needs to limit depth of traversal.
101109
*/
102110
public abstract ResolvedType getParentClass();
103111

@@ -122,6 +130,12 @@ public final boolean canCreateSubtype(Class<?> subtype) {
122130
* NOTE: returned list does NOT include transitively implemented types (that is,
123131
* interfaces implemented by parent classes or interfaces).
124132
*
133+
*<p>
134+
* NOTE: self-referential interfaces ({@link ResolvedRecursiveType}) are replaced with
135+
* types they represent (see {@link ResolvedRecursiveType#getActualType()}): as with
136+
* {@link #getParentClass()}, following supertypes and their type parameters may therefore
137+
* yield ever deeper types for expanding generic types.
138+
*
125139
* @return List of interfaces this type implements, if any; empty list if none (never
126140
* {@code null}).
127141
*/

‎src/main/java/com/fasterxml/classmate/TypeResolver.java‎

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -416,23 +416,31 @@ private ResolvedType _fromClass(ClassStack context, Class<?> rawType, TypeBindin
416416
// If not, need to construct
417417
context = (context == null) ? new ClassStack(rawType) : context.child(rawType);
418418
type = _constructType(context, rawType, typeBindings);
419-
// [classmate#128]: self-references with different bindings (like raw `Mid` within
420-
// `Mid<String>`) represent differently parameterized type, resolved lazily. Except
421-
// if within type parameters of the type itself (like `E` in raw `Enum<E extends Enum<E>>`)
422-
if (context.hasSelfReferences()) {
423-
final ResolvedType resolved = type;
424-
context.resolveSelfReferences(type, ref -> _representsReferenced(ref, resolved)
425-
? null : () -> _standaloneSelfReference(ref));
426-
}
427-
// [classmate#128]: nor can types with self-references to types still being
419+
// [classmate#128]: types with self-references to types still being
428420
// resolved (like `B` in `B extends Base<A>`, when resolving `A extends Base<B>`)
429-
// be cached, whether via type parameters, supertypes or array element types
421+
// can not be cached, whether via type parameters, supertypes or array element types
430422
// (note: self-references created within this frame, like ones in bounds of
431423
// raw type, are accounted for by frame; but type parameters are resolved outside)
432424
boolean incomplete = context.hasOuterReferences();
433425
for (ResolvedType param : typeBindings.typeParameterArray()) {
434426
incomplete |= param._isIncomplete();
435427
}
428+
// [classmate#128]: self-references with different bindings (like raw `Mid` within
429+
// `Mid<String>`) represent differently parameterized type, resolved lazily. Except
430+
// if within type parameters of the type itself (like `E` in raw `Enum<E extends Enum<E>>`)
431+
// [classmate#132]: but if type is incomplete (only valid within its resolution
432+
// context), stand-alone type is needed, except for such type parameters
433+
// (since those are only valid within type itself anyway)
434+
if (context.hasSelfReferences()) {
435+
final ResolvedType resolved = type;
436+
if (incomplete) {
437+
context.resolveSelfReferences(type, ref -> _isSelfBoundParameter(ref, resolved)
438+
? null : () -> _standaloneSelfReference(ref));
439+
} else {
440+
context.resolveSelfReferences(type, ref -> _representsReferenced(ref, resolved)
441+
? null : () -> _actualSelfReference(ref, resolved));
442+
}
443+
}
436444
if (incomplete) {
437445
type._markIncomplete();
438446
context.addIncomplete(key, type);
@@ -957,15 +965,17 @@ private ResolvedType _selfReferenceTarget(ResolvedType selfRef)
957965
{
958966
final ResolvedRecursiveType rrt = (ResolvedRecursiveType) selfRef;
959967
final ResolvedType ref = rrt.getSelfReferencedType();
960-
if ((ref != null) && !ref._isIncomplete()) {
968+
if (ref != null) {
961969
// [classmate#128]: actual type (with own bindings), if differs
970+
// [classmate#132]: (or stand-alone type, if referenced type is incomplete)
962971
ResolvedType actual = rrt.getActualType();
963972
if (actual != ref) {
964973
return actual;
965974
}
966-
// otherwise referenced type, if self-reference represents it (only not known
967-
// if self-reference was not constructed by `TypeResolver`)
968-
if (_representsReferenced(rrt, ref)) {
975+
// otherwise referenced type, if valid outside its resolution context and
976+
// self-reference represents it (only not known if self-reference was not
977+
// constructed by `TypeResolver`)
978+
if (!ref._isIncomplete() && _representsReferenced(rrt, ref)) {
969979
return ref;
970980
}
971981
}
@@ -1010,11 +1020,21 @@ private ResolvedType _rawIfEqual(ResolvedType type)
10101020
* @since 1.8
10111021
*/
10121022
private static boolean _representsReferenced(ResolvedType selfRef, ResolvedType ref)
1023+
{
1024+
return ref.getTypeBindings().equals(selfRef.getTypeBindings())
1025+
|| _isSelfBoundParameter(selfRef, ref);
1026+
}
1027+
1028+
/**
1029+
* Helper method for checking whether given self-reference is the type parameter of
1030+
* the referenced type, for type variable bound to the type itself (like {@code E} in
1031+
* raw {@code Enum<E extends Enum<E>>}).
1032+
*
1033+
* @since 1.8
1034+
*/
1035+
private static boolean _isSelfBoundParameter(ResolvedType selfRef, ResolvedType ref)
10131036
{
10141037
final TypeBindings refBindings = ref.getTypeBindings();
1015-
if (refBindings.equals(selfRef.getTypeBindings())) {
1016-
return true;
1017-
}
10181038
for (int i = 0, len = refBindings.size(); i < len; ++i) {
10191039
if ((refBindings.getBoundType(i) == selfRef)
10201040
&& _isBoundToSelf(ref.getErasedType(), i)) {
@@ -1048,6 +1068,19 @@ private static boolean _isBoundToSelf(Class<?> raw, int index)
10481068
// must be parameterized with own type variables, in order (like `Foo<T, U>`)
10491069
return Arrays.equals(((ParameterizedType) bound).getActualTypeArguments(), vars);
10501070
}
1071+
/**
1072+
* Helper method for resolving the actual type given self-reference (to given
1073+
* resolved type, valid outside of its resolution context) represents: stand-alone
1074+
* type, or resolved type itself if equal to it (to retain identity).
1075+
*
1076+
* @since 1.8
1077+
*/
1078+
private ResolvedType _actualSelfReference(ResolvedType selfRef, ResolvedType resolved)
1079+
{
1080+
ResolvedType actual = _standaloneSelfReference(selfRef);
1081+
return actual.equals(resolved) ? resolved : actual;
1082+
}
1083+
10511084
/**
10521085
* Helper method for resolving stand-alone type that given self-reference represents,
10531086
* using its own type bindings (with self-references in them resolved similarly).

‎src/main/java/com/fasterxml/classmate/types/ResolvedInterfaceType.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,9 @@ public ResolvedType getParentClass() {
5959

6060
@Override
6161
public List<ResolvedType> getImplementedInterfaces() {
62-
return (_superInterfaces.length == 0) ?
63-
Collections.<ResolvedType>emptyList() : Arrays.asList(_superInterfaces);
62+
// [classmate#132]: actual types for self-referential interfaces, if any
63+
// (same as with `getParentClass()`)
64+
return ResolvedRecursiveType.actualTypes(_superInterfaces);
6465
}
6566

6667
@Override

‎src/main/java/com/fasterxml/classmate/types/ResolvedObjectType.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,9 @@ public ResolvedObjectType getParentClass() {
130130

131131
@Override
132132
public List<ResolvedType> getImplementedInterfaces() {
133-
return (_superInterfaces.length == 0) ?
134-
Collections.<ResolvedType>emptyList() : Arrays.asList(_superInterfaces);
133+
// [classmate#132]: actual types for self-referential interfaces, if any
134+
// (same as with `getParentClass()`)
135+
return ResolvedRecursiveType.actualTypes(_superInterfaces);
135136
}
136137

137138
/*

‎src/main/java/com/fasterxml/classmate/types/ResolvedRecursiveType.java‎

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,17 @@ public class ResolvedRecursiveType extends ResolvedType
2828
/**
2929
* For self-references with type bindings different from those of the referenced
3030
* type (like raw {@code Mid} within {@code Mid<String>}, or {@code N<N<T>>} within
31-
* {@code N<T>}): supplier of the actual type this self-reference represents.
31+
* {@code N<T>}), or to a referenced type only valid within its resolution context:
32+
* supplier of the actual type this self-reference represents.
3233
* Resolved lazily since doing so eagerly could lead to infinite recursion.
3334
*
3435
* @since 1.8
3536
*/
3637
protected Supplier<ResolvedType> _actualTypeSupplier;
3738

3839
/**
39-
* Actual type this self-reference represents, if differs from referenced type;
40-
* resolved lazily using {@link #_actualTypeSupplier}.
40+
* Actual type this self-reference represents, if (possibly) differs from referenced
41+
* type; resolved lazily using {@link #_actualTypeSupplier}.
4142
*
4243
* @since 1.8
4344
*/
@@ -67,9 +68,9 @@ public void setReference(ResolvedType ref)
6768

6869
/**
6970
* Alternative to {@link #setReference(ResolvedType)} used when type bindings of
70-
* this self-reference differ from those of the referenced type: in that case,
71-
* {@link #getActualType()} returns type obtained (lazily) from given supplier
72-
* (or referenced type, if equal).
71+
* this self-reference differ from those of the referenced type, or if referenced
72+
* type is only valid within its resolution context: in that case,
73+
* {@link #getActualType()} returns type obtained (lazily) from given supplier.
7374
*
7475
* @since 1.8
7576
*/
@@ -100,7 +101,8 @@ public ResolvedType getParentClass() {
100101
/**
101102
* Accessor for the type being resolved that this self-reference points to.
102103
* Note that type bindings of this self-reference may differ from those of the
103-
* referenced type (like for raw {@code Mid} within {@code Mid<String>}): if so,
104+
* referenced type (like for raw {@code Mid} within {@code Mid<String>}), or
105+
* referenced type may only be valid within its resolution context: if so,
104106
* {@link #getActualType()} returns the type self-reference actually represents.
105107
*/
106108
@Override
@@ -110,8 +112,10 @@ public ResolvedType getParentClass() {
110112
* Accessor for the type this self-reference represents: same as
111113
* {@link #getSelfReferencedType()}, unless type bindings of this self-reference
112114
* differ from those of the referenced type (like raw {@code Mid} within
113-
* {@code Mid<String>}, or {@code N<N<T>>} within {@code N<T>}), in which case
114-
* it is the type with bindings of this self-reference (resolved lazily).
115+
* {@code Mid<String>}, or {@code N<N<T>>} within {@code N<T>}), or referenced
116+
* type is only valid within its resolution context (like {@code B} in
117+
* {@code B extends Base<A>}, when resolving {@code A extends Base<B>}), in which
118+
* case it is the stand-alone type with bindings of this self-reference (resolved lazily).
115119
*<p>
116120
* NOTE: for expanding types (like {@code N<T>} above), following actual types
117121
* repeatedly yields ever deeper types.
@@ -139,10 +143,6 @@ public ResolvedType getActualType()
139143
// resolved outside of lock, since resolution may access other self-references;
140144
// concurrent calls may resolve more than once, but only first one is retained
141145
actual = supplier.get();
142-
// retain identity if equal
143-
if (actual.equals(_referencedType)) {
144-
actual = _referencedType;
145-
}
146146
synchronized (this) {
147147
if (_actualType == null) {
148148
_actualType = actual;
@@ -153,6 +153,34 @@ public ResolvedType getActualType()
153153
}
154154
}
155155

156+
/**
157+
* Helper method for replacing self-references in given supertypes (like
158+
* implemented interfaces) with types they represent (see {@link #getActualType()}),
159+
* since self-references themselves expose no supertypes.
160+
*
161+
* @since 1.8
162+
*/
163+
static List<ResolvedType> actualTypes(ResolvedType[] types)
164+
{
165+
if (types.length == 0) {
166+
return Collections.<ResolvedType>emptyList();
167+
}
168+
ResolvedType[] result = types;
169+
for (int i = 0; i < types.length; ++i) {
170+
if (types[i] instanceof ResolvedRecursiveType) {
171+
ResolvedType actual = ((ResolvedRecursiveType) types[i]).getActualType();
172+
// may still be unresolved, if called during resolution
173+
if (actual != null) {
174+
if (result == types) {
175+
result = types.clone();
176+
}
177+
result[i] = actual;
178+
}
179+
}
180+
}
181+
return Arrays.asList(result);
182+
}
183+
156184
/**
157185
* To avoid infinite loops, will return empty list
158186
*/

‎src/main/java/com/fasterxml/classmate/util/ClassStack.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ public void resolveSelfReferences(ResolvedType resolved)
100100
* Method called when type that this stack frame represents is
101101
* fully resolved, allowing self-references to be completed
102102
* (if there are any). Self-references that represent a type different from the
103-
* resolved one (like ones with different type bindings) get the actual type they
103+
* resolved one (like ones with different type bindings, or any if resolved type
104+
* is only valid within its resolution context) get the actual type they
104105
* represent from supplier given by function (null if self-reference represents the
105106
* resolved type), if any (see {@link ResolvedRecursiveType#setReference(ResolvedType, Supplier)}).
106107
*

0 commit comments

Comments
 (0)