diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2022-03-23 14:53:02 -0500 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2022-03-24 20:03:07 +0000 |
commit | 6f055b4762d71bc42d561974e1c157d5ab036602 (patch) | |
tree | 77b0279af82458c5bc1eb22e45de03fb296b1fbe | |
parent | de2a43957a00a246a9e877a30a04eabae33a6973 (diff) | |
download | sonarqube-6f055b4762d71bc42d561974e1c157d5ab036602.tar.gz sonarqube-6f055b4762d71bc42d561974e1c157d5ab036602.zip |
Improve SQ code quality
11 files changed, 95 insertions, 101 deletions
diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/language/LanguageRepositoryImpl.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/language/LanguageRepositoryImpl.java index 33a36e45fa4..b308a595066 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/language/LanguageRepositoryImpl.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/language/LanguageRepositoryImpl.java @@ -19,19 +19,16 @@ */ package org.sonar.ce.task.projectanalysis.language; -import com.google.common.base.Function; +import java.util.Arrays; import java.util.Collections; import java.util.Map; +import java.util.Objects; import java.util.Optional; -import javax.annotation.Nonnull; +import java.util.function.Function; +import java.util.stream.Collectors; import org.sonar.api.resources.Language; import org.springframework.beans.factory.annotation.Autowired; -import static com.google.common.base.Predicates.notNull; -import static com.google.common.collect.Iterables.filter; -import static com.google.common.collect.Maps.uniqueIndex; -import static java.util.Arrays.asList; - /** * Implementation of {@link LanguageRepository} which find {@link Language} instances available in the container. */ @@ -46,20 +43,11 @@ public class LanguageRepositoryImpl implements LanguageRepository { @Autowired(required = false) public LanguageRepositoryImpl(Language... languages) { - this.languagesByKey = uniqueIndex(filter(asList(languages), notNull()), LanguageToKey.INSTANCE); + this.languagesByKey = Arrays.stream(languages).filter(Objects::nonNull).collect(Collectors.toMap(Language::getKey, Function.identity())); } @Override public Optional<Language> find(String languageKey) { return Optional.ofNullable(languagesByKey.get(languageKey)); } - - private enum LanguageToKey implements Function<Language, String> { - INSTANCE; - - @Override - public String apply(@Nonnull Language input) { - return input.getKey(); - } - } } diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/LoadMeasureComputersStep.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/LoadMeasureComputersStep.java index ee8d604da2d..6cbff6ad88c 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/LoadMeasureComputersStep.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/LoadMeasureComputersStep.java @@ -19,18 +19,17 @@ */ package org.sonar.ce.task.projectanalysis.step; -import com.google.common.base.Function; -import com.google.common.base.Predicate; -import com.google.common.base.Predicates; import java.util.Arrays; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Set; +import java.util.function.Function; +import java.util.stream.Collectors; import javax.annotation.Nonnull; -import javax.annotation.Nullable; import org.sonar.api.ce.measure.MeasureComputer; import org.sonar.api.measures.CoreMetrics; import org.sonar.api.measures.Metric; @@ -48,9 +47,8 @@ import static org.sonar.api.ce.measure.MeasureComputer.MeasureComputerDefinition public class LoadMeasureComputersStep implements ComputationStep { - private static final Set<String> CORE_METRIC_KEYS = from(CoreMetrics.getMetrics()).transform(MetricToKey.INSTANCE).toSet(); - private Set<String> pluginMetricKeys; - + private static final Set<String> CORE_METRIC_KEYS = CoreMetrics.getMetrics().stream().map(Metric::getKey).collect(Collectors.toSet()); + private final Set<String> pluginMetricKeys; private final MutableMeasureComputersHolder measureComputersHolder; private final MeasureComputer[] measureComputers; @@ -58,10 +56,10 @@ public class LoadMeasureComputersStep implements ComputationStep { public LoadMeasureComputersStep(MutableMeasureComputersHolder measureComputersHolder, Metrics[] metricsRepositories, MeasureComputer[] measureComputers) { this.measureComputersHolder = measureComputersHolder; this.measureComputers = measureComputers; - this.pluginMetricKeys = from(Arrays.asList(metricsRepositories)) - .transformAndConcat(MetricsToMetricList.INSTANCE) - .transform(MetricToKey.INSTANCE) - .toSet(); + this.pluginMetricKeys = Arrays.stream(metricsRepositories) + .flatMap(m -> m.getMetrics().stream()) + .map(Metric::getKey) + .collect(Collectors.toSet()); } /** @@ -89,8 +87,8 @@ public class LoadMeasureComputersStep implements ComputationStep { } @Override - public void execute(ComputationStep.Context context) { - List<MeasureComputerWrapper> wrappers = from(Arrays.asList(measureComputers)).transform(ToMeasureWrapper.INSTANCE).toList(); + public void execute(Context context) { + List<MeasureComputerWrapper> wrappers = Arrays.stream(measureComputers).map(ToMeasureWrapper.INSTANCE).collect(Collectors.toList()); validateMetrics(wrappers); measureComputersHolder.setMeasureComputers(sortComputers(wrappers)); } @@ -128,18 +126,24 @@ public class LoadMeasureComputersStep implements ComputationStep { } private void validateMetrics(List<MeasureComputerWrapper> wrappers) { - from(wrappers).transformAndConcat(ToInputMetrics.INSTANCE).filter(new ValidateInputMetric()).size(); - from(wrappers).transformAndConcat(ToOutputMetrics.INSTANCE).filter(new ValidateOutputMetric()).size(); - from(wrappers).filter(new ValidateUniqueOutputMetric()).size(); + wrappers.stream().flatMap(s -> ToInputMetrics.INSTANCE.apply(s).stream()).forEach(this::validateInputMetric); + wrappers.stream().flatMap(s -> ToOutputMetrics.INSTANCE.apply(s).stream()).forEach(this::validateOutputMetric); + ValidateUniqueOutputMetric validateUniqueOutputMetric = new ValidateUniqueOutputMetric(); + wrappers.forEach(validateUniqueOutputMetric::validate); } - private static Iterable<MeasureComputerWrapper> getDependencies(MeasureComputerWrapper measureComputer, ToComputerByKey toComputerByOutputMetricKey) { + private static Collection<MeasureComputerWrapper> getDependencies(MeasureComputerWrapper measureComputer, ToComputerByKey toComputerByOutputMetricKey) { // Remove null computer because a computer can depend on a metric that is only generated by a sensor or on a core metrics - return from(measureComputer.getDefinition().getInputMetrics()).transform(toComputerByOutputMetricKey).filter(Predicates.notNull()); + return measureComputer.getDefinition().getInputMetrics().stream() + .map(toComputerByOutputMetricKey) + .filter(Objects::nonNull) + .collect(Collectors.toList()); } - private static Iterable<MeasureComputerWrapper> getDependents(MeasureComputerWrapper measureComputer, ToComputerByKey toComputerByInputMetricKey) { - return from(measureComputer.getDefinition().getInputMetrics()).transform(toComputerByInputMetricKey); + private static Collection<MeasureComputerWrapper> getDependents(MeasureComputerWrapper measureComputer, ToComputerByKey toComputerByInputMetricKey) { + return measureComputer.getDefinition().getInputMetrics().stream() + .map(toComputerByInputMetricKey) + .collect(Collectors.toList()); } private static class ToComputerByKey implements Function<String, MeasureComputerWrapper> { @@ -155,25 +159,6 @@ public class LoadMeasureComputersStep implements ComputationStep { } } - private enum MetricToKey implements Function<Metric, String> { - INSTANCE; - - @Nullable - @Override - public String apply(@Nonnull Metric input) { - return input.key(); - } - } - - private enum MetricsToMetricList implements Function<Metrics, List<Metric>> { - INSTANCE; - - @Override - public List<Metric> apply(@Nonnull Metrics input) { - return input.getMetrics(); - } - } - private enum ToMeasureWrapper implements Function<MeasureComputer, MeasureComputerWrapper> { INSTANCE; @@ -206,13 +191,9 @@ public class LoadMeasureComputersStep implements ComputationStep { } } - private class ValidateInputMetric implements Predicate<String> { - @Override - public boolean apply(@Nonnull String metric) { - checkState(pluginMetricKeys.contains(metric) || CORE_METRIC_KEYS.contains(metric), - "Metric '%s' cannot be used as an input metric as it's not a core metric and no plugin declare this metric", metric); - return true; - } + private void validateInputMetric(String metric) { + checkState(pluginMetricKeys.contains(metric) || CORE_METRIC_KEYS.contains(metric), + "Metric '%s' cannot be used as an input metric as it's not a core metric and no plugin declare this metric", metric); } private enum ToOutputMetrics implements Function<MeasureComputerWrapper, Collection<String>> { @@ -224,20 +205,15 @@ public class LoadMeasureComputersStep implements ComputationStep { } } - private class ValidateOutputMetric implements Predicate<String> { - @Override - public boolean apply(@Nonnull String metric) { - checkState(!CORE_METRIC_KEYS.contains(metric), "Metric '%s' cannot be used as an output metric because it's a core metric", metric); - checkState(pluginMetricKeys.contains(metric), "Metric '%s' cannot be used as an output metric because no plugins declare this metric", metric); - return true; - } + private void validateOutputMetric(String metric) { + checkState(!CORE_METRIC_KEYS.contains(metric), "Metric '%s' cannot be used as an output metric because it's a core metric", metric); + checkState(pluginMetricKeys.contains(metric), "Metric '%s' cannot be used as an output metric because no plugins declare this metric", metric); } - private static class ValidateUniqueOutputMetric implements Predicate<MeasureComputerWrapper> { - private Set<String> allOutputMetrics = new HashSet<>(); + private static class ValidateUniqueOutputMetric { + private final Set<String> allOutputMetrics = new HashSet<>(); - @Override - public boolean apply(@Nonnull MeasureComputerWrapper wrapper) { + public boolean validate(@Nonnull MeasureComputerWrapper wrapper) { for (String outputMetric : wrapper.getDefinition().getOutputMetrics()) { checkState(!allOutputMetrics.contains(outputMetric), "Output metric '%s' is already defined by another measure computer '%s'", outputMetric, wrapper.getComputer()); diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/QualityGateMeasuresStep.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/QualityGateMeasuresStep.java index b267bd3a1be..4e6ff7ff6db 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/QualityGateMeasuresStep.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/QualityGateMeasuresStep.java @@ -19,7 +19,6 @@ */ package org.sonar.ce.task.projectanalysis.step; -import com.google.common.base.Function; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Multimap; import java.util.ArrayList; @@ -28,7 +27,8 @@ import java.util.List; import java.util.Map; import java.util.Optional; import java.util.Set; -import javax.annotation.Nonnull; +import java.util.function.Function; +import java.util.stream.Collectors; import javax.annotation.Nullable; import org.apache.commons.lang.StringUtils; import org.sonar.api.measures.CoreMetrics; @@ -55,7 +55,6 @@ import org.sonar.ce.task.projectanalysis.qualitygate.QualityGateHolder; import org.sonar.ce.task.step.ComputationStep; import org.sonar.core.util.stream.MoreCollectors; -import static com.google.common.collect.FluentIterable.from; import static java.lang.String.format; import static org.sonar.ce.task.projectanalysis.component.ComponentVisitor.Order.PRE_ORDER; import static org.sonar.ce.task.projectanalysis.qualitygate.ConditionStatus.NO_VALUE_STATUS; @@ -146,9 +145,9 @@ public class QualityGateMeasuresStep implements ComputationStep { } } - private static Map<Condition, ConditionStatus> createStatusPerCondition(Iterable<Condition> conditions, Iterable<EvaluatedCondition> evaluatedConditions) { - Map<Condition, EvaluatedCondition> evaluatedConditionPerCondition = from(evaluatedConditions) - .uniqueIndex(EvaluatedConditionToCondition.INSTANCE); + private static Map<Condition, ConditionStatus> createStatusPerCondition(Collection<Condition> conditions, Collection<EvaluatedCondition> evaluatedConditions) { + Map<Condition, EvaluatedCondition> evaluatedConditionPerCondition = evaluatedConditions.stream() + .collect(Collectors.toMap(EvaluatedCondition::getCondition, Function.identity())); ImmutableMap.Builder<Condition, ConditionStatus> builder = ImmutableMap.builder(); for (Condition condition : conditions) { @@ -164,7 +163,7 @@ public class QualityGateMeasuresStep implements ComputationStep { } private void updateMeasures(Component project, Set<Condition> conditions, QualityGateDetailsDataBuilder builder) { - Multimap<Metric, Condition> conditionsPerMetric = conditions.stream().collect(MoreCollectors.index(Condition::getMetric, java.util.function.Function.identity())); + Multimap<Metric, Condition> conditionsPerMetric = conditions.stream().collect(MoreCollectors.index(Condition::getMetric, Function.identity())); boolean ignoredConditions = false; for (Map.Entry<Metric, Collection<Condition>> entry : conditionsPerMetric.asMap().entrySet()) { Metric metric = entry.getKey(); @@ -267,16 +266,6 @@ public class QualityGateMeasuresStep implements ComputationStep { } } - private enum EvaluatedConditionToCondition implements Function<EvaluatedCondition, Condition> { - INSTANCE; - - @Override - @Nonnull - public Condition apply(@Nonnull EvaluatedCondition input) { - return input.getCondition(); - } - } - static class MetricEvaluationResult { final EvaluationResult evaluationResult; final Condition condition; diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/language/LanguageRepositoryImplTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/language/LanguageRepositoryImplTest.java index 7ce335e7871..c2ef478218a 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/language/LanguageRepositoryImplTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/language/LanguageRepositoryImplTest.java @@ -35,7 +35,7 @@ public class LanguageRepositoryImplTest { @Test public void constructor_fails_is_language_have_the_same_key() { assertThatThrownBy(() -> new LanguageRepositoryImpl(createLanguage(SOME_LANGUAGE_KEY, " 1"), createLanguage(SOME_LANGUAGE_KEY, " 2"))) - .isInstanceOf(IllegalArgumentException.class); + .isInstanceOf(IllegalStateException.class); } @Test diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/engine/MigrationContainerImplTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/engine/MigrationContainerImplTest.java index f0534766f3c..dbc1d2b1c0b 100644 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/engine/MigrationContainerImplTest.java +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/engine/MigrationContainerImplTest.java @@ -64,6 +64,19 @@ public class MigrationContainerImplTest { } @Test + public void add_duplicate_steps_has_no_effect() { + InternalMigrationStepRegistry registry = new MigrationStepRegistryImpl(); + registry.add(1, "test", NoOpMigrationStep.class); + registry.add(2, "test2", NoOpMigrationStep.class); + + SpringComponentContainer parent = new SpringComponentContainer(); + parent.add(registry.build()); + parent.startComponents(); + MigrationContainerImpl underTest = new MigrationContainerImpl(parent, NoOpExecutor.class); + assertThat(underTest.getComponentsByType(MigrationStep.class)).hasSize(1); + } + + @Test public void migration_container_lazily_instance_components() { assertThat(StartCallCounter.startCalls).isZero(); diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/project/package-info.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/project/package-info.java new file mode 100644 index 00000000000..167d4219225 --- /dev/null +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/project/package-info.java @@ -0,0 +1,24 @@ +/* + * SonarQube + * Copyright (C) 2009-2022 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * 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 GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +@ParametersAreNonnullByDefault +package org.sonar.server.project; + +import javax.annotation.ParametersAreNonnullByDefault; + diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/rule/ws/RuleMapper.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/rule/ws/RuleMapper.java index 480be2df5d5..867e323cabd 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/rule/ws/RuleMapper.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/rule/ws/RuleMapper.java @@ -19,15 +19,14 @@ */ package org.sonar.server.rule.ws; -import com.google.common.base.Function; import java.util.Collections; import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Set; +import java.util.function.Function; import java.util.stream.Collectors; import javax.annotation.CheckForNull; -import javax.annotation.Nonnull; import org.sonar.api.resources.Language; import org.sonar.api.resources.Languages; import org.sonar.api.rule.RuleKey; @@ -437,7 +436,7 @@ public class RuleMapper { INSTANCE; @Override - public Rules.Rule.Param apply(@Nonnull RuleParamDto param) { + public Rules.Rule.Param apply(RuleParamDto param) { Rules.Rule.Param.Builder paramResponse = Rules.Rule.Param.newBuilder(); paramResponse.setKey(param.getName()); if (param.getDescription() != null) { diff --git a/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel.java b/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel.java index 9d8bc2b26c2..74eafc913bf 100644 --- a/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel.java +++ b/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel.java @@ -26,7 +26,6 @@ import org.sonar.core.platform.SpringComponentContainer; import org.sonar.server.platform.WebServer; import static com.google.common.base.Preconditions.checkNotNull; -import static java.util.Objects.requireNonNull; public abstract class PlatformLevel { private final String name; diff --git a/sonar-core/src/main/java/org/sonar/core/extension/CoreExtensionsInstaller.java b/sonar-core/src/main/java/org/sonar/core/extension/CoreExtensionsInstaller.java index cc436fbc0d4..cba56638cbd 100644 --- a/sonar-core/src/main/java/org/sonar/core/extension/CoreExtensionsInstaller.java +++ b/sonar-core/src/main/java/org/sonar/core/extension/CoreExtensionsInstaller.java @@ -84,10 +84,6 @@ public abstract class CoreExtensionsInstaller { coreExtension.load(context); } - private <T> boolean hasSupportedAnnotation(T component) { - return AnnotationUtils.getAnnotation(component, supportedAnnotationType) != null; - } - private class ContextImpl implements CoreExtension.Context { private final ExtensionContainer container; private final Predicate<Object> extensionFilter; @@ -147,5 +143,9 @@ public abstract class CoreExtensionsInstaller { } return false; } + + private <T> boolean hasSupportedAnnotation(T component) { + return AnnotationUtils.getAnnotation(component, supportedAnnotationType) != null; + } } } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/ReferenceBranchSupplier.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/ReferenceBranchSupplier.java index b700fa59ada..a389741ff36 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/ReferenceBranchSupplier.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/ReferenceBranchSupplier.java @@ -90,7 +90,7 @@ public class ReferenceBranchSupplier { Optional<String> value = configuration.get(NEW_CODE_PARAM_KEY); if (value.isPresent()) { String referenceBranchName = value.get(); - if (getBranchName().equals(referenceBranchName)) { + if (referenceBranchName.equals(getBranchName())) { throw new IllegalStateException(format("Reference branch set with '%s' points to the current branch '%s'", NEW_CODE_PARAM_KEY, referenceBranchName)); } return referenceBranchName; diff --git a/sonar-scanner-protocol/src/test/java/org/sonar/scanner/protocol/output/ScannerReportReaderTest.java b/sonar-scanner-protocol/src/test/java/org/sonar/scanner/protocol/output/ScannerReportReaderTest.java index 269bacef2d4..e7588076b57 100644 --- a/sonar-scanner-protocol/src/test/java/org/sonar/scanner/protocol/output/ScannerReportReaderTest.java +++ b/sonar-scanner-protocol/src/test/java/org/sonar/scanner/protocol/output/ScannerReportReaderTest.java @@ -226,6 +226,12 @@ public class ScannerReportReaderTest { } @Test + public void read_analysis_cache_returns_null_if_no_file_exists() { + ScannerReportReader reader = new ScannerReportReader(dir); + assertThat(reader.getAnalysisCache()).isNull(); + } + + @Test public void empty_list_if_no_duplication_block_found() { assertThat(underTest.readComponentDuplications(UNKNOWN_COMPONENT_REF)).toIterable().isEmpty(); } |