@@ -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).
0 commit comments