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
@@ -0,0 +1,90 @@
/*
* 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.semsitter;

import java.io.File;
import java.nio.file.Path;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.UnaryOperator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.config.Configuration;
import org.sonar.java.model.JParser;
import org.sonar.java.model.JParserProvider;
import org.sonar.plugins.java.api.JavaVersion;

/**
* Offers the SemSitter frontend when the analysis is allowed to use it: a SemSitter executable is known and
* {@link SemSitterFlag} is on, that is when SonarQube Cloud sent {@value SemSitterFlag#ENABLED_PROPERTY}. Once chosen,
* the SemSitter frontend never hands files to another frontend.
*/
Comment thread
rombirli marked this conversation as resolved.
Resolved
public final class SemSitterParserProvider implements JParserProvider {

public static final String EXECUTABLE_PROPERTY = "sonar.java.semsitter.executable";
public static final String EXECUTABLE_ENVIRONMENT = "SEMSITTER_ANALYZER";

private static final Logger LOG = LoggerFactory.getLogger(SemSitterParserProvider.class);

private final UnaryOperator<String> environment;
private final DocumentTranslator translator;
private volatile boolean enabled;
private final AtomicReference<Path> executable = new AtomicReference<>();

public SemSitterParserProvider() {
this(System::getenv, SemSitterTranslator::translate);
}

SemSitterParserProvider(UnaryOperator<String> environment, DocumentTranslator translator) {
this.environment = environment;
this.translator = translator;
}

@Override
public void configure(Configuration configuration) {
enabled = false;
executable.set(null);
Optional<String> path = configuredExecutable(configuration);
if (path.isEmpty()) {
return;
}
executable.set(Path.of(path.get()));
enabled = SemSitterFlag.isEnabled(configuration);
if (enabled) {
LOG.info("The Java analyzer will use the SemSitter frontend.");
}
}

/** The non-blank {@value #EXECUTABLE_PROPERTY}, or else the non-blank environment variable {@value #EXECUTABLE_ENVIRONMENT}. */
private Optional<String> configuredExecutable(Configuration configuration) {
Optional<String> fromVariable = Optional.ofNullable(environment.apply(EXECUTABLE_ENVIRONMENT)).filter(value -> !value.isBlank());
return configuration.get(EXECUTABLE_PROPERTY).filter(value -> !value.isBlank()).or(() -> fromVariable);
}

@Override
public boolean isEnabled() {
return enabled;
}

@Override
public JParser create(JParser.Mode mode, JavaVersion javaVersion, List<File> classpath, boolean shouldIgnoreUnnamedModuleForSplitPackage,
boolean shouldUseJdkReleaseOption) {
Path server = executable.get();
return new SemSitterParser(mode, javaVersion, classpath, root -> SemSitterClient.start(server, root), translator);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.sonar.java.semsitter.SemSitterParserProvider
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
* 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.semsitter;

import java.io.File;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.function.UnaryOperator;
import org.junit.jupiter.api.Test;
import org.sonar.api.config.Configuration;
import org.sonar.java.model.JParser;
import org.sonar.java.model.JavaVersionImpl;
import org.sonar.scanner.plugin.api.impl.config.MapSettings;

import static org.assertj.core.api.Assertions.assertThat;

class SemSitterParserProviderTest {

private static final String ENABLED = SemSitterFlag.ENABLED_PROPERTY;
private static final String EXECUTABLE = SemSitterParserProvider.EXECUTABLE_PROPERTY;

private final DocumentTranslator translator = (document, loaders, noLibraries, javaVersion) -> {
throw new UnsupportedOperationException();
};

@Test
void provider_is_disabled_until_configured() {
assertThat(provider(Map.of()).isEnabled()).isFalse();
}

@Test
void provider_is_disabled_without_executable() {
SemSitterParserProvider provider = provider(Map.of());

provider.configure(settings(Map.of(ENABLED, "true")));

assertThat(provider.isEnabled()).isFalse();
}

@Test
void blank_executable_is_ignored() {
SemSitterParserProvider provider = provider(Map.of());

provider.configure(settings(Map.of(EXECUTABLE, " ", ENABLED, "true")));

assertThat(provider.isEnabled()).isFalse();
}

@Test
void property_enables_with_an_executable() {
SemSitterParserProvider provider = provider(Map.of());

provider.configure(settings(Map.of(EXECUTABLE, "/bin/semsitter", ENABLED, "true")));

assertThat(provider.isEnabled()).isTrue();
}

@Test
void property_false_keeps_the_default_frontend() {
SemSitterParserProvider provider = provider(Map.of());

provider.configure(settings(Map.of(EXECUTABLE, "/bin/semsitter", ENABLED, "false")));

assertThat(provider.isEnabled()).isFalse();
}

@Test
void without_property_the_flag_default_applies() {
SemSitterParserProvider provider = provider(Map.of());

provider.configure(settings(Map.of(EXECUTABLE, "/bin/semsitter")));

assertThat(provider.isEnabled()).isEqualTo(SemSitterFlag.DEFAULT);
}

@Test
void executable_can_come_from_the_environment() {
SemSitterParserProvider provider = provider(Map.of(SemSitterParserProvider.EXECUTABLE_ENVIRONMENT, "/bin/semsitter"));

provider.configure(settings(Map.of(ENABLED, "true")));

assertThat(provider.isEnabled()).isTrue();
}

@Test
void configuring_again_forgets_the_previous_analysis() {
SemSitterParserProvider provider = provider(Map.of());
provider.configure(settings(Map.of(EXECUTABLE, "/bin/semsitter", ENABLED, "true")));
assertThat(provider.isEnabled()).isTrue();

provider.configure(settings(Map.of()));

assertThat(provider.isEnabled()).isFalse();
}

@Test
void created_parser_is_the_semsitter_parser() {
SemSitterParserProvider provider = provider(Map.of());
provider.configure(settings(Map.of(EXECUTABLE, Path.of("semsitter").toString(), ENABLED, "true")));

JParser parser = provider.create(JParser.Mode.BATCH, new JavaVersionImpl(17), List.of(new File("a.jar")), false, true);

assertThat(parser).isInstanceOf(SemSitterParser.class);
}

private SemSitterParserProvider provider(Map<String, String> environment) {
UnaryOperator<String> lookup = environment::get;
return new SemSitterParserProvider(lookup, translator);
}

private static Configuration settings(Map<String, String> properties) {
MapSettings settings = new MapSettings();
properties.forEach(settings::setProperty);
return settings.asConfig();
}
}
32 changes: 32 additions & 0 deletions sonar-java-plugin/license-resources/bsd-3-clause.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
Copyright 2008 Google Inc. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Code generated by the Protocol Buffer compiler is owned by the owner
of the input file used when generating it. This code is not
standalone and requires a support library to be linked with it. This
support library is itself covered by the above license.
Loading
Loading