From ce5b9e7c4147c40a6368151c5577dc54b655a5da Mon Sep 17 00:00:00 2001 From: rombirli Date: Wed, 25 Nov 2026 10:01:43 +0100 Subject: [PATCH 1/2] JAVA-7393 Add JParserProvider and let JParser delegate to the chosen provider --- .../java/org/sonar/java/model/JParser.java | 3 +- .../org/sonar/java/model/JParserProvider.java | 39 +++++ .../sonar/java/model/JParserProviders.java | 65 +++++++ .../java/model/JParserProvidersTest.java | 164 ++++++++++++++++++ 4 files changed, 269 insertions(+), 2 deletions(-) create mode 100644 java-frontend/src/main/java/org/sonar/java/model/JParserProvider.java create mode 100644 java-frontend/src/main/java/org/sonar/java/model/JParserProviders.java create mode 100644 java-frontend/src/test/java/org/sonar/java/model/JParserProvidersTest.java diff --git a/java-frontend/src/main/java/org/sonar/java/model/JParser.java b/java-frontend/src/main/java/org/sonar/java/model/JParser.java index 41f697f0a4c..6790f7ff981 100644 --- a/java-frontend/src/main/java/org/sonar/java/model/JParser.java +++ b/java-frontend/src/main/java/org/sonar/java/model/JParser.java @@ -23,7 +23,6 @@ import java.util.function.BooleanSupplier; import org.sonar.api.batch.fs.InputFile; import org.sonar.java.AnalysisProgress; -import org.sonar.java.jdt.JDTParser; import org.sonar.plugins.java.api.JavaVersion; public interface JParser { @@ -39,7 +38,7 @@ public JParser create(JavaVersion javaVersion, List classpath) { } public JParser create(JavaVersion javaVersion, List classpath, boolean shouldIgnoreUnnamedModuleForSplitPackage, boolean shouldUseJdkReleaseOption) { - return JDTParser.create(this, javaVersion, classpath, shouldIgnoreUnnamedModuleForSplitPackage, shouldUseJdkReleaseOption); + return JParserProviders.create(this, javaVersion, classpath, shouldIgnoreUnnamedModuleForSplitPackage, shouldUseJdkReleaseOption); } } diff --git a/java-frontend/src/main/java/org/sonar/java/model/JParserProvider.java b/java-frontend/src/main/java/org/sonar/java/model/JParserProvider.java new file mode 100644 index 00000000000..de110d8517b --- /dev/null +++ b/java-frontend/src/main/java/org/sonar/java/model/JParserProvider.java @@ -0,0 +1,39 @@ +/* + * SonarQube Java + * Copyright (C) SonarSource Sàrl + * mailto:info AT sonarsource DOT com + * + * You can redistribute and/or modify this program under the terms of + * the Sonar Source-Available License Version 1, as published by SonarSource Sàrl. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the Sonar Source-Available License for more details. + * + * You should have received a copy of the Sonar Source-Available License + * along with this program; if not, see https://sonarsource.com/license/ssal/ + */ +package org.sonar.java.model; + +import java.io.File; +import java.util.List; +import org.sonar.api.config.Configuration; +import org.sonar.plugins.java.api.JavaVersion; + +/** + * A frontend that can stand in for the default one. Implementations are discovered with {@link java.util.ServiceLoader} + * and consulted every time a {@link JParser} is created, so the frontend can change between analyses without any + * caller knowing about it. + */ +public interface JParserProvider { + + /** Called once at the start of each analysis, before any parser is created. */ + void configure(Configuration configuration); + + /** Whether the parsers of this provider must be used for the analysis in progress. */ + boolean isEnabled(); + + JParser create(JParser.Mode mode, JavaVersion javaVersion, List classpath, boolean shouldIgnoreUnnamedModuleForSplitPackage, + boolean shouldUseJdkReleaseOption); +} diff --git a/java-frontend/src/main/java/org/sonar/java/model/JParserProviders.java b/java-frontend/src/main/java/org/sonar/java/model/JParserProviders.java new file mode 100644 index 00000000000..47b4fcd1244 --- /dev/null +++ b/java-frontend/src/main/java/org/sonar/java/model/JParserProviders.java @@ -0,0 +1,65 @@ +/* + * SonarQube Java + * Copyright (C) SonarSource Sàrl + * mailto:info AT sonarsource DOT com + * + * You can redistribute and/or modify this program under the terms of + * the Sonar Source-Available License Version 1, as published by SonarSource Sàrl. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the Sonar Source-Available License for more details. + * + * You should have received a copy of the Sonar Source-Available License + * along with this program; if not, see https://sonarsource.com/license/ssal/ + */ +package org.sonar.java.model; + +import java.io.File; +import java.util.List; +import java.util.ServiceLoader; +import java.util.concurrent.atomic.AtomicReference; +import org.sonar.api.config.Configuration; +import org.sonar.java.jdt.JDTParser; +import org.sonar.plugins.java.api.JavaVersion; + +/** + * Chooses the frontend that parses the files: the first enabled {@link JParserProvider}, or the ECJ based frontend when + * none is enabled. The frontends are independent: a provider that fails to load, to configure or to create its parser + * fails the analysis instead of handing the files to another frontend. + */ +public final class JParserProviders { + + private static final AtomicReference> CONFIGURED = new AtomicReference<>(List.of()); + + private JParserProviders() { + } + + /** Loads and configures fresh providers for the current analysis. Nothing is loaded where no analysis configures them. */ + public static void configure(Configuration configuration) { + List discovered = ServiceLoader.load(JParserProvider.class, JParserProviders.class.getClassLoader()) + .stream().map(ServiceLoader.Provider::get).toList(); + CONFIGURED.set(configure(discovered, configuration)); + } + + static List configure(List providers, Configuration configuration) { + providers.forEach(provider -> provider.configure(configuration)); + return providers; + } + + static JParser create(JParser.Mode mode, JavaVersion javaVersion, List classpath, boolean shouldIgnoreUnnamedModuleForSplitPackage, + boolean shouldUseJdkReleaseOption) { + return create(CONFIGURED.get(), mode, javaVersion, classpath, shouldIgnoreUnnamedModuleForSplitPackage, shouldUseJdkReleaseOption); + } + + static JParser create(List providers, JParser.Mode mode, JavaVersion javaVersion, List classpath, + boolean shouldIgnoreUnnamedModuleForSplitPackage, boolean shouldUseJdkReleaseOption) { + for (JParserProvider provider : providers) { + if (provider.isEnabled()) { + return provider.create(mode, javaVersion, classpath, shouldIgnoreUnnamedModuleForSplitPackage, shouldUseJdkReleaseOption); + } + } + return JDTParser.create(mode, javaVersion, classpath, shouldIgnoreUnnamedModuleForSplitPackage, shouldUseJdkReleaseOption); + } +} diff --git a/java-frontend/src/test/java/org/sonar/java/model/JParserProvidersTest.java b/java-frontend/src/test/java/org/sonar/java/model/JParserProvidersTest.java new file mode 100644 index 00000000000..f1a174f2800 --- /dev/null +++ b/java-frontend/src/test/java/org/sonar/java/model/JParserProvidersTest.java @@ -0,0 +1,164 @@ +/* + * SonarQube Java + * Copyright (C) SonarSource Sàrl + * mailto:info AT sonarsource DOT com + * + * You can redistribute and/or modify this program under the terms of + * the Sonar Source-Available License Version 1, as published by SonarSource Sàrl. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the Sonar Source-Available License for more details. + * + * You should have received a copy of the Sonar Source-Available License + * along with this program; if not, see https://sonarsource.com/license/ssal/ + */ +package org.sonar.java.model; + +import java.io.File; +import java.util.List; +import org.junit.jupiter.api.Test; +import org.sonar.api.config.Configuration; +import org.sonar.java.jdt.JDTParser; +import org.sonar.plugins.java.api.JavaVersion; +import org.sonar.scanner.plugin.api.impl.config.MapSettings; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +class JParserProvidersTest { + + private static final JavaVersion VERSION = new JavaVersionImpl(17); + + @Test + void default_frontend_is_used_without_providers() { + JParser parser = JParserProviders.create(List.of(), JParser.Mode.BATCH, VERSION, List.of(), false, true); + + assertThat(parser).isInstanceOf(JDTParser.class); + } + + @Test + void default_frontend_is_used_when_no_provider_is_enabled() { + StubProvider disabled = new StubProvider(false); + + JParser parser = JParserProviders.create(List.of(disabled), JParser.Mode.FILE_BY_FILE, VERSION, List.of(), false, true); + + assertThat(parser).isInstanceOf(JDTParser.class); + assertThat(disabled.created).isZero(); + } + + @Test + void first_enabled_provider_creates_the_parser_with_the_requested_arguments() { + StubProvider disabled = new StubProvider(false); + StubProvider enabled = new StubProvider(true); + StubProvider later = new StubProvider(true); + List classpath = List.of(new File("a.jar")); + + JParser parser = JParserProviders.create(List.of(disabled, enabled, later), JParser.Mode.FILE_BY_FILE, VERSION, classpath, true, true); + + assertThat(parser).isSameAs(enabled.parser); + assertThat(enabled.mode).isEqualTo(JParser.Mode.FILE_BY_FILE); + assertThat(enabled.javaVersion).isSameAs(VERSION); + assertThat(enabled.classpath).isEqualTo(classpath); + assertThat(enabled.ignoreUnnamedModule).isTrue(); + assertThat(disabled.created).isZero(); + assertThat(later.created).isZero(); + } + + @Test + void an_enabled_provider_that_cannot_create_its_parser_fails_instead_of_falling_back() { + StubProvider broken = new StubProvider(true); + broken.failure = new IllegalStateException("no executable"); + List providers = List.of(broken); + + assertThatThrownBy(() -> JParserProviders.create(providers, JParser.Mode.BATCH, VERSION, List.of(), false, true)) + .isInstanceOf(IllegalStateException.class).hasMessage("no executable"); + } + + @Test + void a_provider_that_cannot_tell_whether_it_is_enabled_fails_instead_of_falling_back() { + StubProvider broken = new StubProvider(true); + broken.enabledFailure = new IllegalStateException("flag service down"); + List providers = List.of(broken); + + assertThatThrownBy(() -> JParserProviders.create(providers, JParser.Mode.BATCH, VERSION, List.of(), false, true)) + .hasMessage("flag service down"); + } + + @Test + void every_provider_is_configured() { + StubProvider first = new StubProvider(true); + StubProvider second = new StubProvider(false); + Configuration configuration = new MapSettings().asConfig(); + + assertThat(JParserProviders.configure(List.of(first, second), configuration)).containsExactly(first, second); + assertThat(first.configuration).isSameAs(configuration); + assertThat(second.configuration).isSameAs(configuration); + } + + @Test + void a_provider_that_cannot_be_configured_fails_the_analysis() { + StubProvider failing = new StubProvider(true); + failing.failure = new IllegalArgumentException("bad settings"); + List providers = List.of(failing); + Configuration configuration = new MapSettings().asConfig(); + + assertThatThrownBy(() -> JParserProviders.configure(providers, configuration)).hasMessage("bad settings"); + } + + @Test + void default_frontend_parses_when_no_installed_provider_accepted_the_configuration() { + JParserProviders.configure(new MapSettings().asConfig()); + + assertThat(JParserProviders.create(JParser.Mode.BATCH, VERSION, List.of(), false, true)).isInstanceOf(JDTParser.class); + } + + private static final class StubProvider implements JParserProvider { + private final boolean enabled; + private final JParser parser = (files, canceled, progress, action) -> { + }; + private RuntimeException failure; + private RuntimeException enabledFailure; + private Configuration configuration; + private int created; + private JParser.Mode mode; + private JavaVersion javaVersion; + private List classpath; + private boolean ignoreUnnamedModule; + + private StubProvider(boolean enabled) { + this.enabled = enabled; + } + + @Override + public void configure(Configuration configuration) { + this.configuration = configuration; + if (failure != null) { + throw failure; + } + } + + @Override + public boolean isEnabled() { + if (enabledFailure != null) { + throw enabledFailure; + } + return enabled; + } + + @Override + public JParser create(JParser.Mode mode, JavaVersion javaVersion, List classpath, boolean shouldIgnoreUnnamedModuleForSplitPackage, + boolean shouldUseJdkReleaseOption) { + if (failure != null) { + throw failure; + } + created++; + this.mode = mode; + this.javaVersion = javaVersion; + this.classpath = classpath; + this.ignoreUnnamedModule = shouldIgnoreUnnamedModuleForSplitPackage; + return parser; + } + } +} From 30ba4b59108675f4bec632eaaabf3abd0e164311 Mon Sep 17 00:00:00 2001 From: rombirli Date: Wed, 25 Nov 2026 10:01:43 +0100 Subject: [PATCH 2/2] JAVA-7393 Configure parser providers at the start of each analysis --- .../src/main/java/org/sonar/plugins/java/JavaSensor.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/sonar-java-plugin/src/main/java/org/sonar/plugins/java/JavaSensor.java b/sonar-java-plugin/src/main/java/org/sonar/plugins/java/JavaSensor.java index 1c7f63d5757..f0cf2e572a1 100644 --- a/sonar-java-plugin/src/main/java/org/sonar/plugins/java/JavaSensor.java +++ b/sonar-java-plugin/src/main/java/org/sonar/plugins/java/JavaSensor.java @@ -43,6 +43,7 @@ import org.sonar.java.filters.PostAnalysisIssueFilter; import org.sonar.java.jsp.Jasper; import org.sonar.java.model.GeneratedFile; +import org.sonar.java.model.JParserProviders; import org.sonar.java.model.JavaVersionImpl; import org.sonar.java.model.springcontext.SpringContextGatheringModel; import org.sonar.java.model.springcontext.SpringContextModelGatherers; @@ -128,6 +129,10 @@ public void execute(SensorContext context) { telemetry.aggregateAsSortedSet(JAVA_SCANNER_APP, context.config().get("sonar.scanner.app").orElse("none")); telemetry.aggregateAsFlag(JAVA_IS_AUTOSCAN, sonarComponents.isAutoScan()); + if (!sonarComponents.isSonarLintContext()) { + JParserProviders.configure(context.config()); + } + JavaFrontend frontend = new JavaFrontend(javaVersion, sonarComponents, measurer,