From d62a56ecfd4cf2f183873900fd32cffc4743c7fb Mon Sep 17 00:00:00 2001 From: Matteo Mara Date: Mon, 4 Dec 2023 12:50:58 +0100 Subject: SONAR-21174 Fix code smells raised after dropping JDK11 support --- .../api/batch/fs/internal/predicates/AndPredicate.java | 8 +++----- .../internal/predicates/OptimizedFilePredicateAdapter.java | 4 ++-- .../sonar/api/batch/fs/internal/predicates/OrPredicate.java | 4 ++-- .../sonar/api/batch/rule/internal/DefaultActiveRules.java | 2 +- .../batch/sensor/issue/internal/AbstractDefaultIssue.java | 13 +++++-------- .../main/java/org/sonar/api/config/internal/Settings.java | 2 +- .../main/java/org/sonar/api/impl/ws/SimpleGetRequest.java | 3 +-- .../main/java/org/sonar/api/impl/ws/ValidatingRequest.java | 5 ++--- 8 files changed, 17 insertions(+), 24 deletions(-) (limited to 'sonar-plugin-api-impl') diff --git a/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/fs/internal/predicates/AndPredicate.java b/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/fs/internal/predicates/AndPredicate.java index 4df4e05a72c..2239f686637 100644 --- a/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/fs/internal/predicates/AndPredicate.java +++ b/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/fs/internal/predicates/AndPredicate.java @@ -27,8 +27,6 @@ import org.sonar.api.batch.fs.FilePredicate; import org.sonar.api.batch.fs.FileSystem.Index; import org.sonar.api.batch.fs.InputFile; -import static java.util.stream.Collectors.toList; - /** * @since 4.2 */ @@ -49,8 +47,8 @@ class AndPredicate extends AbstractFilePredicate implements OperatorPredicate { continue; } else if (filePredicate == FalsePredicate.FALSE) { return FalsePredicate.FALSE; - } else if (filePredicate instanceof AndPredicate) { - result.predicates.addAll(((AndPredicate) filePredicate).predicates); + } else if (filePredicate instanceof AndPredicate andPredicate) { + result.predicates.addAll(andPredicate.predicates); } else { result.predicates.add(OptimizedFilePredicateAdapter.create(filePredicate)); } @@ -97,7 +95,7 @@ class AndPredicate extends AbstractFilePredicate implements OperatorPredicate { @Override public List operands() { - return predicates.stream().map(p -> (FilePredicate) p).collect(toList()); + return predicates.stream().map(p -> (FilePredicate) p).toList(); } } diff --git a/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/fs/internal/predicates/OptimizedFilePredicateAdapter.java b/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/fs/internal/predicates/OptimizedFilePredicateAdapter.java index 01da55d7730..6ea67e615a1 100644 --- a/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/fs/internal/predicates/OptimizedFilePredicateAdapter.java +++ b/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/fs/internal/predicates/OptimizedFilePredicateAdapter.java @@ -36,8 +36,8 @@ public class OptimizedFilePredicateAdapter extends AbstractFilePredicate { } public static OptimizedFilePredicate create(FilePredicate predicate) { - if (predicate instanceof OptimizedFilePredicate) { - return (OptimizedFilePredicate) predicate; + if (predicate instanceof OptimizedFilePredicate optimizedFilePredicate) { + return optimizedFilePredicate; } else { return new OptimizedFilePredicateAdapter(predicate); } diff --git a/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/fs/internal/predicates/OrPredicate.java b/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/fs/internal/predicates/OrPredicate.java index c9ab3ad5b40..3c3817c33f1 100644 --- a/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/fs/internal/predicates/OrPredicate.java +++ b/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/fs/internal/predicates/OrPredicate.java @@ -45,8 +45,8 @@ class OrPredicate extends AbstractFilePredicate implements OperatorPredicate { return TruePredicate.TRUE; } else if (filePredicate == FalsePredicate.FALSE) { continue; - } else if (filePredicate instanceof OrPredicate) { - result.predicates.addAll(((OrPredicate) filePredicate).predicates); + } else if (filePredicate instanceof OrPredicate orPredicate) { + result.predicates.addAll(orPredicate.predicates); } else { result.predicates.add(filePredicate); } diff --git a/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/rule/internal/DefaultActiveRules.java b/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/rule/internal/DefaultActiveRules.java index efac94ac794..60c2722c989 100644 --- a/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/rule/internal/DefaultActiveRules.java +++ b/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/rule/internal/DefaultActiveRules.java @@ -79,7 +79,7 @@ public class DefaultActiveRules implements ActiveRules { @Override public Collection findAll() { - return activeRulesByRepository.entrySet().stream().flatMap(x -> x.getValue().stream()).collect(Collectors.toList()); + return activeRulesByRepository.entrySet().stream().flatMap(x -> x.getValue().stream()).toList(); } @Override diff --git a/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/sensor/issue/internal/AbstractDefaultIssue.java b/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/sensor/issue/internal/AbstractDefaultIssue.java index 5218d874c36..cea5d4686c4 100644 --- a/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/sensor/issue/internal/AbstractDefaultIssue.java +++ b/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/sensor/issue/internal/AbstractDefaultIssue.java @@ -25,7 +25,6 @@ import java.util.Collections; import java.util.List; import java.util.Objects; import java.util.Optional; -import java.util.stream.Collectors; import javax.annotation.Nullable; import org.sonar.api.batch.fs.InputComponent; import org.sonar.api.batch.fs.internal.DefaultInputDir; @@ -97,12 +96,10 @@ public abstract class AbstractDefaultIssue exten InputComponent component = location.inputComponent(); Optional dirOrModulePath = Optional.empty(); - if (component instanceof DefaultInputDir) { - DefaultInputDir dirComponent = (DefaultInputDir) component; - dirOrModulePath = Optional.of(project.getBaseDir().relativize(dirComponent.path())); - } else if (component instanceof DefaultInputModule && !Objects.equals(project.key(), component.key())) { - DefaultInputModule moduleComponent = (DefaultInputModule) component; - dirOrModulePath = Optional.of(project.getBaseDir().relativize(moduleComponent.getBaseDir())); + if (component instanceof DefaultInputDir defaultInputDir) { + dirOrModulePath = Optional.of(project.getBaseDir().relativize(defaultInputDir.path())); + } else if (component instanceof DefaultInputModule defaultInputModule && !Objects.equals(project.key(), component.key())) { + dirOrModulePath = Optional.of(project.getBaseDir().relativize(defaultInputModule.getBaseDir())); } if (dirOrModulePath.isPresent()) { @@ -122,7 +119,7 @@ public abstract class AbstractDefaultIssue exten List paddedFormattings = location.messageFormattings().stream() .map(m -> padMessageFormatting(m, prefixMessage.length())) - .collect(Collectors.toList()); + .toList(); fixedLocation.message(fullMessage.toString(), paddedFormattings); diff --git a/sonar-plugin-api-impl/src/main/java/org/sonar/api/config/internal/Settings.java b/sonar-plugin-api-impl/src/main/java/org/sonar/api/config/internal/Settings.java index bedda5549dc..45f084a2a24 100644 --- a/sonar-plugin-api-impl/src/main/java/org/sonar/api/config/internal/Settings.java +++ b/sonar-plugin-api-impl/src/main/java/org/sonar/api/config/internal/Settings.java @@ -457,7 +457,7 @@ public abstract class Settings extends org.sonar.api.config.Settings { public List getKeysStartingWith(String prefix) { return getProperties().keySet().stream() .filter(key -> StringUtils.startsWith(key, prefix)) - .collect(Collectors.toList()); + .toList(); } } diff --git a/sonar-plugin-api-impl/src/main/java/org/sonar/api/impl/ws/SimpleGetRequest.java b/sonar-plugin-api-impl/src/main/java/org/sonar/api/impl/ws/SimpleGetRequest.java index 6c5df649c61..2e6d7cce1c0 100644 --- a/sonar-plugin-api-impl/src/main/java/org/sonar/api/impl/ws/SimpleGetRequest.java +++ b/sonar-plugin-api-impl/src/main/java/org/sonar/api/impl/ws/SimpleGetRequest.java @@ -25,7 +25,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional; -import java.util.stream.Collectors; import javax.annotation.CheckForNull; import javax.annotation.Nullable; import org.apache.commons.io.IOUtils; @@ -91,7 +90,7 @@ public class SimpleGetRequest extends Request { return null; } - return Arrays.stream(value.split(",")).map(String::trim).filter(x -> !x.isEmpty()).collect(Collectors.toList()); + return Arrays.stream(value.split(",")).map(String::trim).filter(x -> !x.isEmpty()).toList(); } @Override diff --git a/sonar-plugin-api-impl/src/main/java/org/sonar/api/impl/ws/ValidatingRequest.java b/sonar-plugin-api-impl/src/main/java/org/sonar/api/impl/ws/ValidatingRequest.java index af00beb1ef0..7505018c5cf 100644 --- a/sonar-plugin-api-impl/src/main/java/org/sonar/api/impl/ws/ValidatingRequest.java +++ b/sonar-plugin-api-impl/src/main/java/org/sonar/api/impl/ws/ValidatingRequest.java @@ -23,7 +23,6 @@ import java.io.InputStream; import java.util.Arrays; import java.util.List; import java.util.Set; -import java.util.stream.Collectors; import javax.annotation.CheckForNull; import javax.annotation.Nullable; import org.sonar.api.server.ws.LocalConnector; @@ -132,7 +131,7 @@ public abstract class ValidatingRequest extends Request { List values = Arrays.stream(value.split(COMMA_SPLITTER)) .map(String::trim) .filter(s -> !s.isEmpty()) - .collect(Collectors.toList()); + .toList(); return validateValues(values, definition); } @@ -146,7 +145,7 @@ public abstract class ValidatingRequest extends Request { return values.stream() .filter(s -> !s.isEmpty()) .map(value -> Enum.valueOf(enumClass, value)) - .collect(Collectors.toList()); + .toList(); } @CheckForNull -- cgit v1.2.3