Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JavaType#getAllInvolvedRawTypes() throws StackOverflowError with self-referential generic types #1237

Closed
rashtao opened this issue Jan 23, 2024 · 2 comments · Fixed by #1276

Comments

@rashtao
Copy link

rashtao commented Jan 23, 2024

JavaType#getAllInvolvedRawTypes() throws StackOverflowError when invoked on a self-referential generic type.

The problem can be reproduced with the following code snippet:

import com.tngtech.archunit.base.DescribedPredicate;
import com.tngtech.archunit.core.domain.JavaMethod;
import com.tngtech.archunit.core.importer.ClassFileImporter;

import static com.tngtech.archunit.lang.conditions.ArchConditions.be;
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.methods;

public abstract class Foo<T extends Foo<T>> {
    public abstract T getThis();

    public static void main(String[] args) {
        methods()
                .should(be(new DescribedPredicate<JavaMethod>("allInvolvedRawTypes") {
                    @Override
                    public boolean test(JavaMethod m) {
                        return m.getReturnType().getAllInvolvedRawTypes().stream().allMatch(alwaysTrue());
                    }
                }))
                .check(new ClassFileImporter().importClasses(Foo.class));
    }
}
@codecholeric
Copy link
Collaborator

Thanks for reporting! You're right that this is a bug 🙈 I'm gonna try to fix it as soon as I can find some free time to focus (spontaneously I'm not sure without introducing unwanted public API...)

@codecholeric
Copy link
Collaborator

(if anybody else has more time available feel free to grab this)

codecholeric added a commit that referenced this issue Apr 9, 2024
This fixes the problem of recursing infinitely when calling
`JavaType.getAllInvolvedRawTypes()`
for a recursive type parameter declaration (e.g. `class Example<T
extends Example<T>>`).
It also adds a new public API to conveniently traverse `JavaType`
signatures
without the need to add chains of `instanceof` checks and manually
handle how to traverse further.

Resolves: #1237
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants