Skip to content

Inner class instantiation statement cause StackOverflowError when trying to resolve method call to the declaration #3710

@Maccimo

Description

@Maccimo

Consider the following example:

public class JavaParserStackOverflowErrorTest {

    public static Object test(SomeType value) {
        return value.getFoo().getBar().new SomeAnotherType();
    }

}

If we try to parse above source with the JavaParser 3.24.4 (latest available version on Maven Central at the time of writing) and then resolve method call chain value.getFoo().getBar() to the declaration with the MethodCallExpr.resolve() call we will got StackOverflowError instead of expected UnsolvedSymbolException.

However, if we drop the .new SomeAnotherType() part from the source, then we will got UnsolvedSymbolException as expected.

Similar result with com.github.javaparser.ast.expr.Expression::calculateResolvedType() invocation on the same method call chain.

Quick dig show that without inner class instantiation statement type resolution chain will be just

getType on method call value.getFoo()
getType on name expr value

In contast, inner class instantiation statement cause infinite loop:

getType on method call value.getFoo()
getType on method call value.getFoo().getBar()
getType on method call value.getFoo()
getType on method call value.getFoo().getBar()
getType on method call value.getFoo()
getType on method call value.getFoo().getBar()
getType on method call value.getFoo()
getType on method call value.getFoo().getBar()
getType on method call value.getFoo()
getType on method call value.getFoo().getBar()
getType on method call value.getFoo()
getType on method call value.getFoo().getBar()
getType on method call value.getFoo()
getType on method call value.getFoo().getBar()
...

Here is the reproducer project: https://github.com/Maccimo/JavaParserSOE
Complete source of the above sample for reference: https://github.com/Maccimo/JavaParserSOE/blob/master/src/test/resources/JavaParserStackOverflowErrorTest.java

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions