Skip to content

Simplify the parser interface - #6503

Closed
aurelien-coet-sonarsource wants to merge 3 commits into
ac/implementation-agnostic-frontend-2from
ac/simplify-parser
Closed

aurelien-coet-sonarsource wants to merge 3 commits into
ac/implementation-agnostic-frontend-2from
ac/simplify-parser

Conversation

@aurelien-coet-sonarsource

@aurelien-coet-sonarsource aurelien-coet-sonarsource commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Summary by Gitar

  • Parser interface:
    • Replaced JParserConfig with a JParser interface, created via JParser.Mode#create.
    • Added JParseResult to carry parse results to callbacks.
    • Batch mode falls back to file-by-file parsing for files left unanalyzed after a failure.
  • JDT isolation:
    • Moved the ECJ implementation into org.sonar.java.jdt (JDTParser, JDTAstConverter, JDTSema).
    • AST nodes now expose API semantic types instead of JDT bindings.
  • API additions:
    • Added Symbol#annotationElementDefaultValue for annotation element defaults.
  • Documentation:
    • Documented the 8.47 breaking changes in documentation.md.
  • Tests:
    • Moved JDT-specific tests to org.sonar.java.jdt and updated helpers to use JParser.

This will update automatically on new commits.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice 💃

@gitar-bot

gitar-bot Bot commented Nov 13, 2026

Copy link
Copy Markdown
Code Review ✅ Approved 🔴 High risk 5 closed / 5 findings

Replacing JParserConfig and moving parser/semantic types breaks public APIs, which could disrupt custom rules compiled against the previous interfaces.

Simplifies the parser interface by removing the unused parse overload in JParser and the JParserConfig/astParser plumbing. Resolved the stale 8.47 binary-compatibility notes, the broken JParserTestUtils compilation and 3-arg parse migration path, the orphaned useJdkReleaseOption parameter from #6459, and the unused slf4j imports. No open issues remain.

✅ 5 closed
✅ Quality: 8.47 notes still say multi-file parse keeps binary compatibility

📄 sonar-java-plugin/src/main/resources/static/documentation.md:158-162
After this commit, line 158 says JParser replaces JParserConfig. The multi-file parse(...) now sits on a different type, JParser, and its callback takes JParseResult instead of JParserConfig.Result. Any code compiled against JParserConfig#parse(Iterable, …) will fail to link. Line 161 still says the change "preserves binary compatibility because type erasure is unchanged". That was only true while the method stayed on JParserConfig with an unchanged callback type. Plugin authors reading these notes will think existing binaries still work when they don't. Remove the binary-compatibility sentence, or reword it to say the Iterable<InputFile> signature applies to the new JParser interface.

✅ Cross-Repo: SE repo JParserTestUtils stops compiling: JParserConfig/astParser removed

📄 java-frontend/src/main/java/org/sonar/java/model/JParser.java:28-42 📄 sonar-java-plugin/src/main/resources/static/documentation.md:158-163
Fix here and in SonarSource/sonar-java-symbolic-execution

Breaks in: SonarSource/sonar-java-symbolic-execution - java-symbolic-execution/java-symbolic-execution-plugin/src/test/java/org/sonar/java/se/utils/JParserTestUtils.java - JParserTestUtils.parse

This PR deletes org.sonar.java.model.JParserConfig, which takes astParser() and MAXIMUM_SUPPORTED_JAVA_VERSION with it, and it removes the static JParser.parse(JParser, String, String, String) helper. JParser is now the configurable parser interface. In SonarSource/sonar-java-symbolic-execution, JParserTestUtils.parse (java-symbolic-execution/java-symbolic-execution-plugin/src/test/java/org/sonar/java/se/utils/JParserTestUtils.java) uses JParserConfig.MAXIMUM_SUPPORTED_JAVA_VERSION, JParserConfig.Mode.FILE_BY_FILE.create(...).astParser() and the static JParser.parse(parser, ...). Once that repo upgrades its java-frontend dependency to a build with this change, the helper no longer compiles, and neither does any SE test built on it. The break is intended and is noted in the 8.47 section of documentation.md, so the consumer is the side that has to change. Those notes still do not name the removed astParser() and static JParser.parse(JParser, …) entry points.

Here: In the 8.47 Breaking notes, name the removed entry points JParserConfig#astParser() and the static JParser.parse(JParser, String, String, String), and give JParser.Mode#create(...).parse(version, unitName, source) as their replacement. Also let the SE repo know so it can migrate when it bumps java-frontend.

There: In JParserTestUtils.parse, take out the JParserConfig usages, the astParser() call and the static JParser.parse call. Create the parser with JParser.Mode.FILE_BY_FILE.create(...) and call the instance three-argument parse.

In SonarSource/sonar-java-symbolic-execution - java-symbolic-execution/java-symbolic-execution-plugin/src/test/java/org/sonar/java/se/utils/JParserTestUtils.java:

public static CompilationUnitTree parse(String unitName, String source, List<File> classpath) {
  JavaVersion version = JParser.MAXIMUM_SUPPORTED_JAVA_VERSION;
  return JParser.Mode.FILE_BY_FILE.create(version, classpath)
    .parse(version.effectiveJavaVersionAsString(), unitName, source);
}
✅ Cross-Repo: SE repo JParserTestUtils loses its 3-arg parse migration path

📄 java-frontend/src/main/java/org/sonar/java/model/JParser.java:45 📄 java-frontend/src/main/java/org/sonar/java/jdt/JDTParser.java:84 📄 java-frontend/src/main/java/org/sonar/java/jdt/JDTParser.java:239-253 📄 sonar-java-plugin/src/main/resources/static/documentation.md:158-163
Fix here and in SonarSource/sonar-java-symbolic-execution

Breaks in: SonarSource/sonar-java-symbolic-execution - java-symbolic-execution/java-symbolic-execution-plugin/src/test/java/org/sonar/java/se/utils/JParserTestUtils.java - JParserTestUtils.parse

In SonarSource/sonar-java-symbolic-execution, JParserTestUtils.parse (java-symbolic-execution/java-symbolic-execution-plugin/src/test/java/org/sonar/java/se/utils/JParserTestUtils.java) still calls JParserConfig.MAXIMUM_SUPPORTED_JAVA_VERSION, JParserConfig.Mode.FILE_BY_FILE.create(...).astParser() and the static JParser.parse(parser, version, unitName, source). This PR already removed those entry points. The previous commit gave the SE helper the instance method parse(String version, String unitName, String source) as its replacement. This commit takes that method out of the JParser interface and makes the JDT implementation private to FileByFile (JDTParser.java:239), so that replacement is gone too. Once the SE repo upgrades java-frontend, the helper and every SE test that uses it stop compiling. The only way left is the multi-file parse(Iterable<InputFile>, BooleanSupplier, AnalysisProgress, BiConsumer<InputFile, JParseResult>), which needs an InputFile. That means the SE test module needs a test-scoped sensor-test-fixtures dependency (or another source of InputFile), the same dependency this PR adds to java-checks-common/pom.xml. The removal is intended and the 8.47 notes say the string overload is gone. They still don't name JParserConfig#astParser() or the static JParser.parse(JParser, String, String, String), which are the calls the SE helper makes.

Here: In the 8.47 Breaking notes, list the removed JParserConfig#astParser() and static JParser.parse(JParser, String, String, String), and point to JParser.Mode#create(...) plus the Iterable<InputFile> parse as the replacement. Let sonar-java-symbolic-execution know that the three-argument instance parse is gone too, so it migrates straight to the InputFile-based API.

There: In JParserTestUtils.parse, remove the JParserConfig/astParser()/static JParser.parse usage. Wrap the source in a TestInputFileBuilder InputFile and call the multi-file parse on JParser.Mode.FILE_BY_FILE.create(version, classpath). Add a test-scoped org.sonarsource.scanner.engine:sensor-test-fixtures dependency.

In SonarSource/sonar-java-symbolic-execution - java-symbolic-execution/java-symbolic-execution-plugin/src/test/java/org/sonar/java/se/utils/JParserTestUtils.java:

public static CompilationUnitTree parse(String unitName, String source, List<File> classpath) {
  JavaVersion version = JParser.MAXIMUM_SUPPORTED_JAVA_VERSION;
  InputFile inputFile = new TestInputFileBuilder("", unitName)
    .setContents(source).setCharset(StandardCharsets.UTF_8).build();
  List<JParseResult> results = new ArrayList<>();
  JParser.Mode.FILE_BY_FILE.create(version, classpath)
    .parse(List.of(inputFile), () -> false, new AnalysisProgress(1), (input, result) -> results.add(result));
  try {
    return results.getFirst().get();
  } catch (RuntimeException e) {
    throw e;
  } catch (Exception e) {
    throw new IllegalStateException("Unable to parse source", e);
  }
}
✅ Cross-PR: #6459's useJdkReleaseOption flag has no parameter after JParserConfig removal

📄 java-frontend/src/main/java/org/sonar/java/model/JParser.java:36-42 📄 java-frontend/src/main/java/org/sonar/java/jdt/JDTParser.java:63-71
Breaks when #6459 and this PR merge onto the same line in either order, and a user sets sonar.java.useJdkReleaseOption.

How

  1. SONARJAVA-7279 Resolve the JDK API of the target release through the new jdt-package option #6459 adds a fourth shouldUseJdkReleaseOption argument to JParserConfig.Mode.create(...) and ParserConfigConstructor.apply(...) in java-frontend/src/main/java/org/sonar/java/model/JParserConfig.java, and passes it in from JavaFrontend/JavaAstScanner.
  2. This PR deletes JParserConfig and replaces it with JParser.Mode.create(JavaVersion, List<File>[, boolean]) at java-frontend/src/main/java/org/sonar/java/model/JParser.java:36-42. The only optional flag left is split-package.
  3. JDTParser.create (java-frontend/src/main/java/org/sonar/java/jdt/JDTParser.java:63-71) and the Batch/FileByFile constructors carry only shouldIgnoreUnnamedModuleForSplitPackage, so a release-option flag has nowhere to go. SONARJAVA-7279 Resolve the JDK API of the target release through the new jdt-package option #6459's JParserConfigTest cases also target the deleted class.

Fix

  • This PR: Nothing is required unless this PR is meant to land first. If it is, consider a parameter object or overload on JParser.Mode#create that can carry extra JDT options such as the release flag.
  • SONARJAVA-7279 Resolve the JDK API of the target release through the new jdt-package option #6459: Rebase onto the JParser interface. Add shouldUseJdkReleaseOption to JParser.Mode.create and JDTParser.create/constructors, use it in JDTParser.jdtAstParser(), and move the JParserConfigTest cases to JParserModeTest/JDTParserTest.

Compared 4571795 with #6459 at 555a24a

✅ Quality: Unused slf4j imports left after removing the LOG field

📄 java-frontend/src/main/java/org/sonar/java/jdt/JDTAstConverter.java:261-263
This commit deletes the unused LOG field from JDTAstConverter to fix the quality gate, but leaves import org.slf4j.Logger; and import org.slf4j.LoggerFactory; in place. Nothing else in the file uses them now. The code still compiles, but the quality gate will report them as unused imports (S1128), so this QG fix is only half done. Delete both imports.

Verification Rules 1/1 met PRs 2 inspected

Functional validation No results

Rules 1 passed 3 not applicable

✅ Repository license compliance

Broader impact PRs 2 inspected
Cross-PR 2 PRs inspected 0 open impacts

PR 6485: SONARJAVA-7300 Decouple the Sonar Java frontend from Eclipse JDT core Inspected
PR 6497: SONARJAVA-7236 Experiment: supply only the JDK classpath to semsitter Inspected

Auto-approval Not enabled

Set up

For more verification details, comment gitar display:verbose.

Options

Auto-apply is off → Gitar will not commit updates to this branch.
Display: compact → Showing compact verification results.

Comment with these commands to change the behavior for this request:

Auto-apply Compact
gitar auto-apply:on         
gitar display:verbose         

Was this helpful? React with 👍 / 👎 | Gitar

@sonarqube-next

Copy link
Copy Markdown
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants