Skip to content

JAVA-7393 Let the Java frontend be swapped through a parser provider - #6567

Open
rombirli wants to merge 10 commits into
masterfrom
rombirli/semsitter-parser-provider
Open

rombirli wants to merge 10 commits into
masterfrom
rombirli/semsitter-parser-provider

Conversation

@rombirli

@rombirli rombirli commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Part 1 of 17 of JAVA-7392. Ticket: JAVA-7393.

Intent

Let another frontend replace ECJ without any caller knowing. Every JParser is now created through JParserProviders, which asks the providers found with ServiceLoader. The frontends are independent: ECJ is used only when no provider is enabled, never as a fallback. At most one provider may be enabled: several enabled providers fail the analysis, since the discovery order is not a priority.

flowchart LR
  A[JParser.Mode.create] --> B[JParserProviders.create]
  B -->|one enabled provider| C[provider.create]
  B -->|none enabled| D[JDTParser.create]
  B -->|several enabled| X2[analysis fails]
  C -->|throws| X[analysis fails]
Loading
// JParser.Mode
public JParser create(JavaVersion javaVersion, List<File> classpath, ...) {
  return JParserProviders.create(this, javaVersion, classpath, ...);
}

JavaSensor calls JParserProviders.configure(context.config()) once per analysis (not in SonarLint), so a provider can read its settings before any parser is created.

State on master

No provider is installed, so ECJ parses exactly as today.

Test plan

  • JParserProvidersTest with two providers registered through ServiceLoader (test only, driven by the configuration): the enabled one creates the parser with the requested arguments, disabled ones are skipped, no enabled provider means ECJ, several enabled providers fail, reconfiguring forgets the previous analysis
  • A provider that fails to configure, to answer isEnabled or to create its parser fails instead of falling back
  • JParserTest, JParserModeTest unchanged

@hashicorp-vault-sonar-prod

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

Copy link
Copy Markdown
Contributor

JAVA-7393

@rombirli
rombirli force-pushed the rombirli/semsitter-parser-provider branch from 08f39fd to 963d76f Compare November 25, 2026 10:11
@rombirli
rombirli added this pull request to stack #6570 November 25, 2026 10:36
@rombirli
rombirli force-pushed the rombirli/semsitter-parser-provider branch from 963d76f to 7008e58 Compare November 25, 2026 16:31
@rombirli
rombirli force-pushed the rombirli/semsitter-parser-provider branch 2 times, most recently from 7625fd7 to d22d127 Compare November 25, 2026 20:45
@rombirli
rombirli removed this pull request from stack #6570 November 25, 2026 20:47
@rombirli
rombirli added this pull request to stack #6589 November 25, 2026 20:48
@rombirli
rombirli force-pushed the rombirli/semsitter-parser-provider branch from d22d127 to 47259eb Compare November 27, 2026 07:55
@rombirli
rombirli force-pushed the rombirli/semsitter-parser-provider branch from 47259eb to eb2f920 Compare November 27, 2026 09:24
@rombirli
rombirli force-pushed the rombirli/semsitter-parser-provider branch from eb2f920 to 30ba4b5 Compare November 27, 2026 10:46

@david-schroeter david-schroeter left a comment

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.

This PR looks good overall, I just have a few questions and suggestion that I think should be addressed before merging.

@datadog-sonarsource

datadog-sonarsource Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Pipelines

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: f5eb477 | Docs | View more details | Give us feedback!

@gitar-bot

gitar-bot Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Code Review ✅ Approved 🟡 Medium risk 4 closed / 4 findings

ServiceLoader-based parser selection can route analysis through an enabled alternate frontend, potentially changing Java parsing results or failing analysis.

Introduces a JParserProviders layer so any JParser can be created through a ServiceLoader-discovered frontend provider, with ECJ used only when no provider is enabled; provider configure and loading failures now fail the analysis instead of falling back to ECJ. The PR description still says the first enabled provider wins, but the code now fails the analysis when several providers are enabled, so the description should be updated to say only one provider may be enabled.

✅ 4 closed
✅ Bug: Provider whose configure() failed is logged as skipped but still used

📄 java-frontend/src/main/java/org/sonar/java/model/JParserProviders.java:37 📄 java-frontend/src/main/java/org/sonar/java/model/JParserProviders.java:52-60 📄 java-frontend/src/main/java/org/sonar/java/model/JParserProviders.java:67-78 📄 sonar-java-plugin/src/main/java/org/sonar/plugins/java/JavaSensor.java:132-134
JParserProviders.configure catches the exception and logs "could not be configured and is skipped", but it never records the failure. create later walks the same DISCOVERED list and still asks that provider isEnabled(). The providers are static singletons that live for the whole scanner JVM, and JavaSensor configures them once per module. Example: module A configures a provider as enabled, then module B's configure throws. The provider keeps A's state (or whatever it set before throwing), so it parses B's files even though the log says it was skipped. Fix: collect the providers that configured successfully on each configure call and have create look only at that list. Alternatively, drop "is skipped" from the message, but then the stale-state problem remains.

✅ Edge Case: Provider loading/linkage errors bypass the ECJ fallback and break parsing

📄 java-frontend/src/main/java/org/sonar/java/model/JParserProviders.java:37 📄 java-frontend/src/main/java/org/sonar/java/model/JParserProviders.java:42-46 📄 java-frontend/src/main/java/org/sonar/java/model/JParserProviders.java:69-77
The fallback only catches RuntimeException. If an installed provider can't be loaded, ServiceLoader iteration in the static initializer discover() throws ServiceConfigurationError, which is an Error. That turns into ExceptionInInitializerError, and every later JParser.Mode.create then fails with NoClassDefFoundError, so all Java analysis fails instead of falling back to ECJ. The same applies when provider.create throws a LinkageError such as UnsatisfiedLinkError or NoClassDefFoundError, which is likely for a native or alternate frontend. This goes against the stated goal of falling back to ECJ when the chosen provider can't be created. It isn't reachable on master, where no provider is installed. Fix: iterate ServiceLoader through its iterator and catch ServiceConfigurationError for each entry, and catch RuntimeException | LinkageError in create (and in configure).

Closed: Edge Case: Eager static discovery breaks SonarLint, which never uses providers

📄 java-frontend/src/main/java/org/sonar/java/model/JParserProviders.java:34-35 📄 java-frontend/src/main/java/org/sonar/java/model/JParserProviders.java:43-45 📄 java-frontend/src/main/java/org/sonar/java/model/JParserProviders.java:52-55
DISCOVERED is now built in the static initializer with ServiceLoader.Provider::get, and nothing catches errors there. JParser.Mode.create calls JParserProviders.create, which initializes the class, so every provider gets instantiated even in SonarLint. SonarLint never calls configure, so CONFIGURED stays List.of() and no provider could ever be picked. If one service entry is broken (a ServiceConfigurationError or a linkage error in a provider constructor), class init throws ExceptionInInitializerError, and every later parse fails with NoClassDefFoundError: Could not initialize class JParserProviders. That stops ECJ parsing in SonarLint, where providers are never used. It also hides the original cause on every call after the first. The fix is to load the providers lazily inside configure(Configuration), so discovery and its failures only happen in the analysis path that is meant to fail.

Closed: Quality: PR description still says first enabled provider wins

📄 java-frontend/src/main/java/org/sonar/java/model/JParserProviders.java:28-31 📄 java-frontend/src/main/java/org/sonar/java/model/JParserProviders.java:59-67 📄 java-frontend/src/test/java/org/sonar/java/model/JParserProvidersTest.java:94-102
This commit changes JParserProviders.create so that more than one enabled provider throws IllegalStateException("Several parser providers are enabled but at most one is allowed: ..."), and the class Javadoc now says the same. The PR description still describes the old rule in several places: the mermaid edge first enabled provider, the Intent text ("asks the providers found with ServiceLoader" and takes the first), and the test plan item "first enabled provider wins". No test checks that rule any more; several_enabled_providers_fail_the_analysis checks the opposite. Anyone writing a provider for the stacked PRs (#6568, #6569) who reads the description will expect discovery order to settle conflicts. Fix: update the description and diagram to say only one provider may be enabled and that several enabled providers fail the analysis.

Verification Rules 3/3 met Repos 5 inspected · PRs 2 inspected

Functional validation No results

Rules 3 passed 2 not applicable

✅ Internal artifact registries
✅ Release secrets usage
✅ Repository license compliance

Broader impact Repos 5 inspected PRs 2 inspected
Cross-repo 5 repositories inspected 0 open impacts

SonarSource/sonar-java-symbolic-execution Inspected
SonarSource/sonar-iac Inspected
SonarSource/sonarlint-vscode Inspected
SonarSource/sonarlint-language-server Inspected
SonarSource/sonarlint-intellij Inspected

Cross-PR 2 PRs inspected 0 open impacts

PR 6569: JAVA-7395 Run a SemSitter parsing session Inspected
PR 6547: SONARJAVA-7339 Make the Java frontend switchable between ECJ and 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

@david-schroeter david-schroeter left a comment

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.

LGTM!

Thanks for addressing all the comments!

@rombirli
rombirli removed this pull request from stack #6589 December 1, 2026 11:07
@rombirli
rombirli added this pull request to stack #6607 December 1, 2026 11:08

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.

2 participants