Skip to content

SONARJAVA-7341 Simplify the parser interface - #6517

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

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

Conversation

@aurelien-coet-sonarsource

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

Copy link
Copy Markdown
Contributor

Summary by Gitar

  • Parser API:
    • Replaced JParserConfig with a JParser interface and JParser.Mode factories in JParser.java
    • Parsing now takes Iterable<InputFile> and reports results through JParseResult
  • ECJ implementation:
    • Split the ECJ code from JDTParserConfig into JDTParser (batch and file-by-file) and JDTAstConverter
  • Tests and docs:
    • Updated tests and test utilities to the new parser API
    • Documented the breaking API changes in documentation.md

This will update automatically on new commits.

@hashicorp-vault-sonar-prod hashicorp-vault-sonar-prod Bot changed the title Simplify the parser interface SONARJAVA-7341 Simplify the parser interface Nov 13, 2026
@hashicorp-vault-sonar-prod

hashicorp-vault-sonar-prod Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

SONARJAVA-7341

@gitar-bot

gitar-bot Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Code Review ⚠️ Changes requested 🔴 High risk 0 closed / 3 findings

Replacing the public parser API and removing string-based parsing overloads breaks existing integrations, which may fail to compile until adapted.

Replaces JParserConfig with a JParser interface and JParser.Mode factories, switching parsing to Iterable<InputFile> with results reported through JParseResult. Changes requested: the 8.47 breaking-change notes omit several removed entry points (JParserConfig#astParser(), JParserConfig#parse(String, String), static JParser.parse(JParser, ...)) and the move of MAXIMUM_SUPPORTED_JAVA_VERSION to JParser, and the "must be recompiled" guidance is wrong since JParserConfig is deleted. The SE test helper JParserTestUtils.parse in sonar-java-symbolic-execution will stop compiling on the java-frontend bump and needs migration to the InputFile-based API. The useJdkReleaseOption support from #6459 is at risk of being lost or failing to compile if it merges alongside this change, since JDTParser.jdtAstParser() does not set the release option.

⚠️ Cross-Repo: SE test helper JParserTestUtils.parse stops compiling on java-frontend bump

📄 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. It also removes the static JParser.parse(JParser, String, String, String) helper and the instance method parse(String, String, String). JParser is left with only the multi-file parse(Iterable<InputFile>, BooleanSupplier, AnalysisProgress, BiConsumer<InputFile, JParseResult>).

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 uses JParserConfig.MAXIMUM_SUPPORTED_JAVA_VERSION, JParserConfig.Mode.FILE_BY_FILE.create(...).astParser() and the static JParser.parse(parser, ...). When that repo upgrades its pinned java-frontend to a build that includes this change, the helper no longer compiles, and neither does any SE test that depends on it.

The removal is deliberate, so the SE repo is the side that has to move. Because only the InputFile-based API is left, its test module also needs a test-scoped sensor-test-fixtures dependency (the same one this PR adds to java-checks-common/pom.xml) or some other way to get an InputFile. The 8.47 notes in documentation.md don't name the removed JParserConfig#astParser(), the static JParser.parse(JParser, …), or the move of MAXIMUM_SUPPORTED_JAVA_VERSION.

Here: In the 8.47 Breaking notes, list the removed JParserConfig#astParser(), JParserConfig#parse(String, String) and static JParser.parse(JParser, String, String, String), and say that MAXIMUM_SUPPORTED_JAVA_VERSION now lives on JParser. Then tell sonar-java-symbolic-execution so it can migrate to the InputFile-based parse when it bumps java-frontend.

There: In JParserTestUtils.parse, remove the JParserConfig, astParser() and 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). Then add a test-scoped org.sonarsource.scanner.engine:sensor-test-fixtures dependency to the plugin's pom.

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: JDK release-option support from #6459 is lost after JParserConfig removal

📄 java-frontend/src/main/java/org/sonar/java/jdt/JDTParser.java:63-77 📄 java-frontend/src/main/java/org/sonar/java/model/JParser.java:36-42

Breaks when this PR and #6459 both merge into the same line. Whichever lands second either fails to compile or silently drops the sonar.java.useJdkReleaseOption behaviour.

How

  1. #6459 adds a shouldUseJdkReleaseOption field, a four-argument Mode.create(...) and useReleaseApiOfConfiguredJdk(...) to JParserConfig (java-frontend/src/main/java/org/sonar/java/model/JParserConfig.java:141). It calls them from JavaFrontend.scanBatch and JavaAstScanner.scan.
  2. This PR deletes JParserConfig. Parser construction moves to JParser.Mode.create(JavaVersion, List<File>, boolean) (java-frontend/src/main/java/org/sonar/java/model/JParser.java:36-42) and JDTParser.create.
  3. JDTParser.jdtAstParser() (java-frontend/src/main/java/org/sonar/java/jdt/JDTParser.java:84-112) builds the ECJ environment from the raw classpath and never sets SystemReleaseClasspath.OPTION_RELEASE. If #6459's call sites are only ported to JParser.Mode, the parser keeps using the running JDK's API instead of the target release's.

Fix

  • This PR: If #6459 lands first, add the shouldUseJdkReleaseOption flag to JParser.Mode.create, JDTParser.create and the JDTParser constructor. Move useReleaseApiOfConfiguredJdk into JDTParser.jdtAstParser() and keep the JavaFrontend/JavaAstScanner wiring.
  • #6459: If this PR lands first, rebase onto JParser.Mode/JDTParser. Put the release-option logic in JDTParser.jdtAstParser() instead of JParserConfig.astParser(), and move the JParserConfigTest cases to JParserModeTest/JDTParserTest.

Compared 5006a8c with #6459 at 71f4165

💡 Quality: 8.47 notes skip removed parser entry points and misstate the migration

📄 sonar-java-plugin/src/main/resources/static/documentation.md:158-163 📄 java-frontend/src/main/java/org/sonar/java/model/JParser.java:28-42

The rewritten 8.47 Breaking notes only mention that the parse(String version, String unitName, String source) overload is gone. This PR also removes other parser entry points that the earlier 8.47 notes advertised: the static JParser.parse(JParser, String, String, String), JParserConfig#astParser(), JParserConfig#parse(String unitName, String source), and the ParserConfigConstructor interface. MAXIMUM_SUPPORTED_JAVA_VERSION has moved to JParser, and the notes don't say that either. Line 162 also tells readers that "Code compiled against JParserConfig must be recompiled", but JParserConfig has been deleted, so that code has to be migrated, and recompiling alone fails. Existing consumers of these calls, such as JParserTestUtils.parse in sonar-java-symbolic-execution, are covered by the SE JParser dependency and the JParserConfig SE dependency notes, and they get no migration guidance. Fix: list the removed symbols, name JParser.MAXIMUM_SUPPORTED_JAVA_VERSION and JParser.Mode#create(...) plus the Iterable<InputFile> parse as the replacements, and replace the 'recompiled' sentence with a migration instruction.

Name the removed entry points and give accurate migration guidance
The input parameter now takes `Iterable<InputFile>` instead of `Iterable<? extends InputFile>`; callers must supply an `Iterable<InputFile>`, and implementations must update their override signatures.
`JParserConfig` has been removed together with `JParserConfig#astParser()`, `JParserConfig#parse(String, String)` and the static `JParser.parse(JParser, String, String, String)`; migrate to `JParser.Mode#create(...)` and the `Iterable<InputFile>` `parse`. `MAXIMUM_SUPPORTED_JAVA_VERSION` is now `JParser.MAXIMUM_SUPPORTED_JAVA_VERSION`.
🤖 Prompt for agents
Code Review: Replaces `JParserConfig` with a `JParser` interface and `JParser.Mode` factories, switching parsing to `Iterable<InputFile>` with results reported through `JParseResult`. Changes requested: the 8.47 breaking-change notes omit several removed entry points (`JParserConfig#astParser()`, `JParserConfig#parse(String, String)`, static `JParser.parse(JParser, ...)`) and the move of `MAXIMUM_SUPPORTED_JAVA_VERSION` to `JParser`, and the "must be recompiled" guidance is wrong since `JParserConfig` is deleted. The SE test helper `JParserTestUtils.parse` in sonar-java-symbolic-execution will stop compiling on the java-frontend bump and needs migration to the `InputFile`-based API. The `useJdkReleaseOption` support from #6459 is at risk of being lost or failing to compile if it merges alongside this change, since `JDTParser.jdtAstParser()` does not set the release option.

1. 💡 Quality: 8.47 notes skip removed parser entry points and misstate the migration
   Files: sonar-java-plugin/src/main/resources/static/documentation.md:158-163, java-frontend/src/main/java/org/sonar/java/model/JParser.java:28-42

   The rewritten 8.47 Breaking notes only mention that the `parse(String version, String unitName, String source)` overload is gone. This PR also removes other parser entry points that the earlier 8.47 notes advertised: the static `JParser.parse(JParser, String, String, String)`, `JParserConfig#astParser()`, `JParserConfig#parse(String unitName, String source)`, and the `ParserConfigConstructor` interface. `MAXIMUM_SUPPORTED_JAVA_VERSION` has moved to `JParser`, and the notes don't say that either. Line 162 also tells readers that "Code compiled against `JParserConfig` must be recompiled", but `JParserConfig` has been deleted, so that code has to be migrated, and recompiling alone fails. Existing consumers of these calls, such as `JParserTestUtils.parse` in sonar-java-symbolic-execution, are covered by the [SE JParser dependency](https://github.com/SonarSource/sonar-java/pull/6503) and the [JParserConfig SE dependency](https://github.com/SonarSource/sonar-java/pull/6503) notes, and they get no migration guidance. Fix: list the removed symbols, name `JParser.MAXIMUM_SUPPORTED_JAVA_VERSION` and `JParser.Mode#create(...)` plus the `Iterable<InputFile>` parse as the replacements, and replace the 'recompiled' sentence with a migration instruction.

   Fix (Name the removed entry points and give accurate migration guidance):
   The input parameter now takes `Iterable<InputFile>` instead of `Iterable<? extends InputFile>`; callers must supply an `Iterable<InputFile>`, and implementations must update their override signatures.
   `JParserConfig` has been removed together with `JParserConfig#astParser()`, `JParserConfig#parse(String, String)` and the static `JParser.parse(JParser, String, String, String)`; migrate to `JParser.Mode#create(...)` and the `Iterable<InputFile>` `parse`. `MAXIMUM_SUPPORTED_JAVA_VERSION` is now `JParser.MAXIMUM_SUPPORTED_JAVA_VERSION`.

2. ⚠️ Cross-Repo: SE test helper JParserTestUtils.parse stops compiling on java-frontend bump
   Files: 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`](https://github.com/SonarSource/sonar-java-symbolic-execution)**
   
   **Breaks in:** [`SonarSource/sonar-java-symbolic-execution`](https://github.com/SonarSource/sonar-java-symbolic-execution) - [`java-symbolic-execution/java-symbolic-execution-plugin/src/test/java/org/sonar/java/se/utils/JParserTestUtils.java`](https://github.com/SonarSource/sonar-java-symbolic-execution/blob/HEAD/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. It also removes the static `JParser.parse(JParser, String, String, String)` helper and the instance method `parse(String, String, String)`. `JParser` is left with only the multi-file `parse(Iterable<InputFile>, BooleanSupplier, AnalysisProgress, BiConsumer<InputFile, JParseResult>)`.
   
   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 uses `JParserConfig.MAXIMUM_SUPPORTED_JAVA_VERSION`, `JParserConfig.Mode.FILE_BY_FILE.create(...).astParser()` and the static `JParser.parse(parser, ...)`. When that repo upgrades its pinned java-frontend to a build that includes this change, the helper no longer compiles, and neither does any SE test that depends on it.
   
   The removal is deliberate, so the SE repo is the side that has to move. Because only the `InputFile`-based API is left, its test module also needs a test-scoped `sensor-test-fixtures` dependency (the same one this PR adds to `java-checks-common/pom.xml`) or some other way to get an `InputFile`. The 8.47 notes in `documentation.md` don't name the removed `JParserConfig#astParser()`, the static `JParser.parse(JParser, …)`, or the move of `MAXIMUM_SUPPORTED_JAVA_VERSION`.
   
   **Here:** In the 8.47 Breaking notes, list the removed `JParserConfig#astParser()`, `JParserConfig#parse(String, String)` and static `JParser.parse(JParser, String, String, String)`, and say that `MAXIMUM_SUPPORTED_JAVA_VERSION` now lives on `JParser`. Then tell sonar-java-symbolic-execution so it can migrate to the `InputFile`-based `parse` when it bumps java-frontend.
   
   **There:** In `JParserTestUtils.parse`, remove the `JParserConfig`, `astParser()` and 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)`. Then add a test-scoped `org.sonarsource.scanner.engine:sensor-test-fixtures` dependency to the plugin's pom.
   
   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);
     }
   }
   ````

3. ⚠️ Cross-PR: JDK release-option support from [#6459](<https://github.com/SonarSource/sonar-java/pull/6459>) is lost after JParserConfig removal
   Files: java-frontend/src/main/java/org/sonar/java/jdt/JDTParser.java:63-77, java-frontend/src/main/java/org/sonar/java/model/JParser.java:36-42

   **Breaks when** this PR and [#6459](<https://github.com/SonarSource/sonar-java/pull/6459>) both merge into the same line. Whichever lands second either fails to compile or silently drops the `sonar.java.useJdkReleaseOption` behaviour.
   
   **How**
   
   1. [#6459](<https://github.com/SonarSource/sonar-java/pull/6459>) adds a `shouldUseJdkReleaseOption` field, a four-argument `Mode.create(...)` and `useReleaseApiOfConfiguredJdk(...)` to `JParserConfig` (`java-frontend/src/main/java/org/sonar/java/model/JParserConfig.java:141`). It calls them from `JavaFrontend.scanBatch` and `JavaAstScanner.scan`.
   2. This PR deletes `JParserConfig`. Parser construction moves to `JParser.Mode.create(JavaVersion, List<File>, boolean)` (`java-frontend/src/main/java/org/sonar/java/model/JParser.java:36-42`) and `JDTParser.create`.
   3. `JDTParser.jdtAstParser()` (`java-frontend/src/main/java/org/sonar/java/jdt/JDTParser.java:84-112`) builds the ECJ environment from the raw classpath and never sets `SystemReleaseClasspath.OPTION_RELEASE`. If [#6459](<https://github.com/SonarSource/sonar-java/pull/6459>)'s call sites are only ported to `JParser.Mode`, the parser keeps using the running JDK's API instead of the target release's.
   
   **Fix**
   
   - **This PR:** If [#6459](<https://github.com/SonarSource/sonar-java/pull/6459>) lands first, add the `shouldUseJdkReleaseOption` flag to `JParser.Mode.create`, `JDTParser.create` and the `JDTParser` constructor. Move `useReleaseApiOfConfiguredJdk` into `JDTParser.jdtAstParser()` and keep the `JavaFrontend`/`JavaAstScanner` wiring.
   - **[#6459](<https://github.com/SonarSource/sonar-java/pull/6459>):** If this PR lands first, rebase onto `JParser.Mode`/`JDTParser`. Put the release-option logic in `JDTParser.jdtAstParser()` instead of `JParserConfig.astParser()`, and move the `JParserConfigTest` cases to `JParserModeTest`/`JDTParserTest`.
   
   _Compared `5006a8c` with [#6459](<https://github.com/SonarSource/sonar-java/pull/6459>) at `71f4165`_

Verification Rules 1/2 met Knowledge 🔎 2 Repos 4 inspected · PRs 2 inspected
Rules 1 failed 1 passed 2 not applicable

❌ Repository license compliance (license_compliance) - LICENSE.txt contains the SONAR Source-Available License v1.0, but the central exception for sonar-java approves LGPL-3.0 in LICENSE.txt; confirm which license applies and align the file or the exception (PR does not modify it).
✅ Internal artifact registries (artifactory_registries)

Functional validation No results

Knowledge 🔎 2 informed a finding

🔎 JParser.parse(String version, String unitName, String source) / JParserConfig.Mode.create(...).astParser() / static JParser.parse(...): dependency with SonarSource/sonar-java-symbolic-execution informed a finding, open -> 8.47 notes skip removed parser entry points and misstate the migration
🔎 org.sonar.java.model.JParserConfig / static JParser.parse(JParser, String, String, String) / JParserConfig.Mode.create(...).astParser(): dependency with SonarSource/sonar-java-symbolic-execution informed a finding, open -> 8.47 notes skip removed parser entry points and misstate the migration

Broader impact Repos 4 inspected PRs 2 inspected
Cross-repo 4 repositories inspected 1 open impact

SonarSource/sonarlint-vscode Inspected
SonarSource/sonarlint-intellij Inspected
SonarSource/sonarlint-eclipse Inspected
SonarSource/sonar-java-symbolic-execution Inspected

Cross-PR 2 PRs inspected 1 open impact

PR 6485: SONARJAVA-7300 Decouple the Sonar Java frontend from Eclipse JDT core Inspected
PR 6459: SONARJAVA-7279 Resolve the JDK API of the target release through the new jdt-package option 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.
Unblock → Override a blocking verdict and allow merging.

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

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

Was this helpful? React with 👍 / 👎 | Gitar

Comment on lines +158 to +163
* `org.sonar.java.model.JParser` is now an interface, independent of the underlying parser implementation, and replaces `JParserConfig`.
Create parsers with `JParser.Mode.BATCH.create(JavaVersion, List<File>)` or `JParser.Mode.FILE_BY_FILE.create(JavaVersion, List<File>)`. Both factories also accept an optional third `boolean` argument to ignore the unnamed module for split packages.
The string-based `parse(String version, String unitName, String source)` overload has been removed. Use `JParser#parse(Iterable<InputFile>, BooleanSupplier, AnalysisProgress, BiConsumer<InputFile, JParseResult>)` for all parsing, including a single file supplied as `List.of(inputFile)`.
The Java version and classpath are set when creating the parser; source contents, filename, and charset come from each `InputFile`.
The input parameter now takes `Iterable<InputFile>` instead of `Iterable<? extends InputFile>`; callers must supply an `Iterable<InputFile>`, and implementations must update their override signatures. Code compiled against `JParserConfig` must be recompiled.
Parsing callbacks now receive `org.sonar.java.model.JParseResult` instead of `JParserConfig.Result`. `JParseResult` is final and provides `success(tree)` and `failure(exception)` factories; `get()` returns the tree or rethrows the parsing exception.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Quality: 8.47 notes skip removed parser entry points and misstate the migration

The rewritten 8.47 Breaking notes only mention that the parse(String version, String unitName, String source) overload is gone. This PR also removes other parser entry points that the earlier 8.47 notes advertised: the static JParser.parse(JParser, String, String, String), JParserConfig#astParser(), JParserConfig#parse(String unitName, String source), and the ParserConfigConstructor interface. MAXIMUM_SUPPORTED_JAVA_VERSION has moved to JParser, and the notes don't say that either. Line 162 also tells readers that "Code compiled against JParserConfig must be recompiled", but JParserConfig has been deleted, so that code has to be migrated, and recompiling alone fails. Existing consumers of these calls, such as JParserTestUtils.parse in sonar-java-symbolic-execution, are covered by the SE JParser dependency and the JParserConfig SE dependency notes, and they get no migration guidance. Fix: list the removed symbols, name JParser.MAXIMUM_SUPPORTED_JAVA_VERSION and JParser.Mode#create(...) plus the Iterable<InputFile> parse as the replacements, and replace the 'recompiled' sentence with a migration instruction.

Name the removed entry points and give accurate migration guidance:

The input parameter now takes `Iterable<InputFile>` instead of `Iterable<? extends InputFile>`; callers must supply an `Iterable<InputFile>`, and implementations must update their override signatures.
`JParserConfig` has been removed together with `JParserConfig#astParser()`, `JParserConfig#parse(String, String)` and the static `JParser.parse(JParser, String, String, String)`; migrate to `JParser.Mode#create(...)` and the `Iterable<InputFile>` `parse`. `MAXIMUM_SUPPORTED_JAVA_VERSION` is now `JParser.MAXIMUM_SUPPORTED_JAVA_VERSION`.
  • Apply fix

Check the box to apply the fix or reply for a change | Was this helpful? React with 👍 / 👎

Comment on lines 28 to +42
public interface JParser {

JavaTree.CompilationUnitTreeImpl parse(String version, String unitName, String source);
JavaVersion MAXIMUM_SUPPORTED_JAVA_VERSION = new JavaVersionImpl(JavaVersionImpl.MAX_SUPPORTED, true);

enum Mode {
BATCH,
FILE_BY_FILE;

static JavaTree.CompilationUnitTreeImpl parse(JParser parser, String version, String unitName, String source) {
return parser.parse(version, unitName, source);
public JParser create(JavaVersion javaVersion, List<File> classpath) {
return create(javaVersion, classpath, false);
}

public JParser create(JavaVersion javaVersion, List<File> classpath, boolean shouldIgnoreUnnamedModuleForSplitPackage) {
return JDTParser.create(this, javaVersion, classpath, shouldIgnoreUnnamedModuleForSplitPackage);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Cross-Repo: SE test helper JParserTestUtils.parse stops compiling on java-frontend bump

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. It also removes the static JParser.parse(JParser, String, String, String) helper and the instance method parse(String, String, String). JParser is left with only the multi-file parse(Iterable<InputFile>, BooleanSupplier, AnalysisProgress, BiConsumer<InputFile, JParseResult>).

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 uses JParserConfig.MAXIMUM_SUPPORTED_JAVA_VERSION, JParserConfig.Mode.FILE_BY_FILE.create(...).astParser() and the static JParser.parse(parser, ...). When that repo upgrades its pinned java-frontend to a build that includes this change, the helper no longer compiles, and neither does any SE test that depends on it.

The removal is deliberate, so the SE repo is the side that has to move. Because only the InputFile-based API is left, its test module also needs a test-scoped sensor-test-fixtures dependency (the same one this PR adds to java-checks-common/pom.xml) or some other way to get an InputFile. The 8.47 notes in documentation.md don't name the removed JParserConfig#astParser(), the static JParser.parse(JParser, …), or the move of MAXIMUM_SUPPORTED_JAVA_VERSION.

Here: In the 8.47 Breaking notes, list the removed JParserConfig#astParser(), JParserConfig#parse(String, String) and static JParser.parse(JParser, String, String, String), and say that MAXIMUM_SUPPORTED_JAVA_VERSION now lives on JParser. Then tell sonar-java-symbolic-execution so it can migrate to the InputFile-based parse when it bumps java-frontend.

There: In JParserTestUtils.parse, remove the JParserConfig, astParser() and 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). Then add a test-scoped org.sonarsource.scanner.engine:sensor-test-fixtures dependency to the plugin's pom.

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

Was this helpful? React with 👍 / 👎

Comment on lines +63 to 77
public static JParser create(JParser.Mode mode, JavaVersion javaVersion, List<File> classpath, boolean shouldIgnoreUnnamedModuleForSplitPackage) {
if (shouldIgnoreUnnamedModuleForSplitPackage) {
LOG.info("The Java analyzer will ignore the unnamed module for split packages.");
}
return ((JavaTree) node).getChildren().iterator();
return switch (mode) {
case BATCH -> new Batch(javaVersion, classpath, shouldIgnoreUnnamedModuleForSplitPackage);
case FILE_BY_FILE -> new FileByFile(javaVersion, classpath, shouldIgnoreUnnamedModuleForSplitPackage);
};
}

@VisibleForTesting
static List<Token> lex(String version, String unitName, char[] sourceChars) {
List<Token> tokens = new ArrayList<>();
Scanner scanner = new Scanner(
true,
false,
false,
CompilerOptions.versionToJdkLevel(version),
null,
null,
false
);
scanner.fakeInModule = "module-info.java".equals(unitName);
scanner.setSource(sourceChars);
while (true) {
try {
TerminalToken tokenType = scanner.getNextToken();
Token token = Token.fromCurrent(scanner, tokenType);
tokens.add(token);
if (tokenType == TerminalToken.TokenNameEOF) {
break;
}
} catch (InvalidInputException e) {
throw new IllegalStateException(e);
}
}
return tokens;
}

private CompilationUnit compilationUnit;

private TokenManager tokenManager;
private LineColumnConverter lineColumnConverter;

private JDTSema sema;

private final Deque<JLabelSymbol> labels = new LinkedList<>();

private void declaration(@Nullable IBinding binding, Tree node) {
if (binding == null) {
return;
}
sema.declarations.put(binding, node);
static boolean shouldEnablePreviewFlag(JavaVersion currentVersion) {
return currentVersion.arePreviewFeaturesEnabled();
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Cross-PR: JDK release-option support from #6459 is lost after JParserConfig removal

Breaks when this PR and #6459 both merge into the same line. Whichever lands second either fails to compile or silently drops the sonar.java.useJdkReleaseOption behaviour.

How

  1. #6459 adds a shouldUseJdkReleaseOption field, a four-argument Mode.create(...) and useReleaseApiOfConfiguredJdk(...) to JParserConfig (java-frontend/src/main/java/org/sonar/java/model/JParserConfig.java:141). It calls them from JavaFrontend.scanBatch and JavaAstScanner.scan.
  2. This PR deletes JParserConfig. Parser construction moves to JParser.Mode.create(JavaVersion, List<File>, boolean) (java-frontend/src/main/java/org/sonar/java/model/JParser.java:36-42) and JDTParser.create.
  3. JDTParser.jdtAstParser() (java-frontend/src/main/java/org/sonar/java/jdt/JDTParser.java:84-112) builds the ECJ environment from the raw classpath and never sets SystemReleaseClasspath.OPTION_RELEASE. If #6459's call sites are only ported to JParser.Mode, the parser keeps using the running JDK's API instead of the target release's.

Fix

  • This PR: If #6459 lands first, add the shouldUseJdkReleaseOption flag to JParser.Mode.create, JDTParser.create and the JDTParser constructor. Move useReleaseApiOfConfiguredJdk into JDTParser.jdtAstParser() and keep the JavaFrontend/JavaAstScanner wiring.
  • #6459: If this PR lands first, rebase onto JParser.Mode/JDTParser. Put the release-option logic in JDTParser.jdtAstParser() instead of JParserConfig.astParser(), and move the JParserConfigTest cases to JParserModeTest/JDTParserTest.

Compared 5006a8c with #6459 at 71f4165

Was this helpful? React with 👍 / 👎

@sonarqube-next

Copy link
Copy Markdown
Contributor

This branch has not been deployed

No deployments
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.

1 participant