Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -39,7 +38,7 @@ public JParser create(JavaVersion javaVersion, List<File> classpath) {
}

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

Expand Down
Original file line number Diff line number Diff line change
@@ -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<File> classpath, boolean shouldIgnoreUnnamedModuleForSplitPackage,
boolean shouldUseJdkReleaseOption);
}
Original file line number Diff line number Diff line change
@@ -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<List<JParserProvider>> 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<JParserProvider> discovered = ServiceLoader.load(JParserProvider.class, JParserProviders.class.getClassLoader())
.stream().map(ServiceLoader.Provider::get).toList();
CONFIGURED.set(configure(discovered, configuration));
}

static List<JParserProvider> configure(List<JParserProvider> providers, Configuration configuration) {
providers.forEach(provider -> provider.configure(configuration));
return providers;
}

static JParser create(JParser.Mode mode, JavaVersion javaVersion, List<File> classpath, boolean shouldIgnoreUnnamedModuleForSplitPackage,
boolean shouldUseJdkReleaseOption) {
return create(CONFIGURED.get(), mode, javaVersion, classpath, shouldIgnoreUnnamedModuleForSplitPackage, shouldUseJdkReleaseOption);
}

static JParser create(List<JParserProvider> providers, JParser.Mode mode, JavaVersion javaVersion, List<File> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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<File> 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<JParserProvider> 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<JParserProvider> 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<JParserProvider> 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<File> 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<File> 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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down