Skip to content

StackOverflowError with import static in circular dependencies #4358

@kdunee

Description

@kdunee

Hi 👋 I encountered a StackOverflowError when trying to use javaparser on a popular open source library.

I managed to reproduce the issue in a minimal example.

Note

I detected the problem javaparser 3.25.9, but I was able to reproduce it on older versions.

The code I use for testing:

Main.java

package org.example;

import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.symbolsolver.JavaSymbolSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import java.io.File;
import java.io.FileNotFoundException;

public class Main {
  public static void main(String[] args) throws FileNotFoundException {
    var rts = new ReflectionTypeSolver();
    var jpts = new JavaParserTypeSolver(new File("src/main/java"));
    var cts = new CombinedTypeSolver();
    cts.add(rts);
    cts.add(jpts);
    var pc = new ParserConfiguration()
        .setSymbolResolver(new JavaSymbolSolver(cts));
    StaticJavaParser.setConfiguration(pc);
    var cu = StaticJavaParser.parse(new File("src/main/java/org/example/A.java"));
    cu.findAll(MethodCallExpr.class).forEach(MethodCallExpr::resolve);
  }
}

A.java

package org.example;

import static org.example.B.d;

class A {
  void c() {
    d();
  }
}

B.java

package org.example;

class B extends A {
  static void d() {
  }
}

The issue doesn't happen when I use a fully qualified call (B.d()) and don't use import static.

Same thing happens with an asterisk import.

Note

For the curious, the open source library where I discovered this problem is JUnit 5. Class A corresponds to Filter, class B to CompositeFilter, method c to composeFilters and method d to alwaysIncluded.

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