Skip to content

Incorrect resolution of extends and implements types #4427

@eldapiiro

Description

@eldapiiro

JavaParserClassDeclaration.toReferenceType is used when trying to resolve class extends and implements lists.
This method attempts to resolve the typename first within the context of the class declaration itself -- which sounds wrong (can the extends list of a class refer to an internally defined type?)

Specifically, here's code that would cause indirect resolution. If we make DerivedClass.BaseClass extend something we can also trigger a stack overflow this way.

BaseClass.java:

public abstract class BaseClass {

    protected int x;
}

DerivedClass.java:

public class DerivedClass extends BaseClass {

    void f() {
        x = x + 1;
    }


    static class BaseClass  {

    }
}

Failing testcase:

import java.nio.file.Path;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;

public class IssueXXXTest extends AbstractSymbolResolutionTest {
  @Test
  public void testIssueXXX() throws IOException {
    Path issueResourcesPath = adaptPath("src/test/resources/issueXXXX");
    ReflectionTypeSolver rts = new ReflectionTypeSolver();
    JavaParserTypeSolver jpts = new JavaParserTypeSolver(issueResourcesPath);
    CombinedTypeSolver cts = new CombinedTypeSolver();
    cts.add(rts);
    cts.add(jpts);
    ParserConfiguration pc = new ParserConfiguration()
        .setSymbolResolver(new JavaSymbolSolver(cts));
    StaticJavaParser.setConfiguration(pc);
    CompilationUnit cu = StaticJavaParser.parse(issueResourcesPath.resolve("DerivedClass.java"));

    // We shouldn't throw a mismatched symbol 
    assertDoesNotThrow(() -> cu.findAll(NameExpr.class).stream()
            .map(NameExpr::resolve)
            .findAny().get());
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions