Repository navigation
JAVA-7406 Offer the SemSitter frontend behind a flag #6583
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rombirli
wants to merge
3
commits into
rombirli/semsitter-feature-flags
from
rombirli/semsitter-provider-flag
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
90 changes: 90 additions & 0 deletions
90
java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/SemSitterParserProvider.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. | ||
| */ | ||
| 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); | ||
| } | ||
| } | ||
1 change: 1 addition & 0 deletions
1
...ntend-semsitter/src/main/resources/META-INF/services/org.sonar.java.model.JParserProvider
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| org.sonar.java.semsitter.SemSitterParserProvider |
131 changes: 131 additions & 0 deletions
131
...rontend-semsitter/src/test/java/org/sonar/java/semsitter/SemSitterParserProviderTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.