Skip to content

Commit

Permalink
Avoid StackOverflowError in TypeUtils#resolveGenerics (#9858)
Browse files Browse the repository at this point in the history
While the issue (#9855) is related to JDK21 and its new `getFirst` and
`getLast` methods in the `List`/`SequencedCollection` interfaces, the
current behaviour can in theory cause problems on JDK versions before 21
too.

With this fix the `TypeUtils#resolveGenerics` method falls back to
`TypeUtils#ensureBaseType` (already used method-level type variables)
when a type variable is used in the same class where it has been
defined.

This class for example, currently causes a `StackOverflowError`. With
this PR the error is avoided, and the `T` is resolved as
`java.lang.Object`:
```java
class BrokenClass<T> {
    public T getT() {
        return null;
    }
}
```

Fixes #9855
  • Loading branch information
dodgex authored Nov 13, 2023
1 parent be37473 commit 105e63d
Showing 1 changed file with 3 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@ public Type[] getActualTypeArguments() {
Object genericDeclaration = variable.getGenericDeclaration();
if (genericDeclaration instanceof Class<?>) {
Class<?> declaration = (Class<?>) genericDeclaration;
if (declaration.equals(containingType)) {
return ensureBaseType(type);
}
// could probably optimize, but would involve duplicating a bunch of
// getParameterization's code
Type[] types = getParameterization(declaration, containingType);
Expand Down

0 comments on commit 105e63d

Please sign in to comment.