ArchUnit icon indicating copy to clipboard operation
ArchUnit copied to clipboard

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

Open rashtao opened this issue 1 year ago • 2 comments

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));
    }
}

rashtao avatar Jan 23 '24 12:01 rashtao

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 avatar Mar 06 '24 23:03 codecholeric

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

codecholeric avatar Mar 07 '24 00:03 codecholeric