Skip to content

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

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

rombirli wants to merge 2 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.

flowchart LR
  A[JParser.Mode.create] --> B[JParserProviders.create]
  B -->|first enabled provider| C[provider.create]
  B -->|none enabled| D[JDTParser.create]
  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: first enabled provider wins, disabled ones are skipped, 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
@gitar-bot

gitar-bot Bot commented Nov 27, 2026

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

ServiceLoader-based dispatch lets enabled providers replace ECJ and fail analyses without fallback, potentially changing parsing and downstream Java analysis results.

Adds JParserProvider and routes every JParser creation through JParserProviders, which uses the first enabled ServiceLoader provider and falls back to ECJ only when none is enabled. Provider loading and configuration failures now fail the analysis instead of falling back to ECJ.

  • Open suggestion: JParserProviders discovers providers eagerly in its static initializer without catching errors, so a broken service entry raises ExceptionInInitializerError and breaks ECJ parsing in SonarLint, which never uses providers. Loading providers lazily inside configure(Configuration) would confine failures to the analysis path.
✅ 3 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.

Verification Rules 3/3 met Repos 4 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 4 inspected PRs 2 inspected
Cross-repo 4 repositories inspected 0 open impacts

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

Cross-PR 2 PRs inspected 0 open impacts

PR 6583: JAVA-7406 Offer the SemSitter frontend behind a flag Inspected
PR 6569: JAVA-7395 Run a SemSitter parsing session 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

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