]> source.dussan.org Git - sonarqube.git/commitdiff
NO JIRA fix code smells after moving to lang3 library
authorMatteo Mara <matteo.mara@sonarsource.com>
Wed, 20 Mar 2024 14:14:10 +0000 (15:14 +0100)
committersonartech <sonartech@sonarsource.com>
Fri, 22 Mar 2024 20:02:32 +0000 (20:02 +0000)
26 files changed:
build.gradle
server/sonar-auth-ldap/src/main/java/org/sonar/auth/ldap/LdapContextFactory.java
server/sonar-auth-ldap/src/main/java/org/sonar/auth/ldap/LdapGroupMapping.java
server/sonar-auth-ldap/src/main/java/org/sonar/auth/ldap/LdapUserMapping.java
server/sonar-ce-task-projectanalysis/build.gradle
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/source/PersistFileSourcesStep.java
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/PersistDuplicationDataStep.java
server/sonar-ce/src/main/java/org/sonar/ce/queue/NextPendingTaskPicker.java
server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/ComponentNewValue.java
server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/DevOpsPlatformSettingNewValue.java
server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/PermissionTemplateNewValue.java
server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/UserNewValue.java
server/sonar-server-common/src/main/java/org/sonar/server/notification/email/EmailNotificationChannel.java
server/sonar-webserver-auth/src/main/java/org/sonar/server/authentication/UserSessionInitializer.java
server/sonar-webserver-webapi/build.gradle
server/sonar-webserver-webapi/src/main/java/org/sonar/server/badge/ws/SvgGenerator.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/source/ws/ScmAction.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/source/ws/ShowAction.java
sonar-core/src/main/java/org/sonar/core/issue/tracking/AbstractTracker.java
sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/rule/internal/NewRule.java
sonar-plugin-api-impl/src/main/java/org/sonar/api/impl/ws/ValidatingRequest.java
sonar-plugin-api-impl/src/test/java/org/sonar/api/impl/ws/ValidatingRequestTest.java [new file with mode: 0644]
sonar-scanner-engine/build.gradle
sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/settings/AbstractSettingsLoader.java
sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/AbstractExclusionFilters.java
sonar-scanner-engine/src/main/java/org/sonar/scm/git/NativeGitBlameCommand.java

index a4561ea8083acbbef7753fd586eeeaf451c6a56e..d83ee59d191b553cc80ded21444b41cffdbb1547 100644 (file)
@@ -378,6 +378,7 @@ subprojects {
       dependency 'org.apache.commons:commons-csv:1.10.0'
       dependency 'org.apache.commons:commons-lang3:3.14.0'
       dependency 'org.apache.commons:commons-email:1.6.0'
+      dependency 'org.apache.commons:commons-text:1.11.0'
       dependency 'com.zaxxer:HikariCP:5.1.0'
       dependency('org.apache.httpcomponents:httpclient:4.5.14') {
         exclude 'commons-logging:commons-logging'
index bbcbf8b9f27580387779b1a197972eb80f9b9310..caa5da7f799a94d030ef1dc9bf1f567e6ac9a157 100644 (file)
@@ -78,8 +78,8 @@ public class LdapContextFactory {
   private final String saslMaxbuf;
 
   public LdapContextFactory(org.sonar.api.config.Configuration config, String settingsPrefix, String ldapUrl) {
-    this.authentication = StringUtils.defaultString(config.get(settingsPrefix + ".authentication").orElse(null), DEFAULT_AUTHENTICATION);
-    this.factory = StringUtils.defaultString(config.get(settingsPrefix + ".contextFactoryClass").orElse(null), DEFAULT_FACTORY);
+    this.authentication = config.get(settingsPrefix + ".authentication").orElse(DEFAULT_AUTHENTICATION);
+    this.factory = config.get(settingsPrefix + ".contextFactoryClass").orElse(DEFAULT_FACTORY);
     this.realm = config.get(settingsPrefix + ".realm").orElse(null);
     this.providerUrl = ldapUrl;
     this.startTLS = config.getBoolean(settingsPrefix + ".StartTLS").orElse(false);
index 365016a20f6caf5117db7b239cf095a352ccbddc..3627db17362afaa85ef732032e2bc7049f591dad 100644 (file)
@@ -44,9 +44,9 @@ public class LdapGroupMapping {
    */
   public LdapGroupMapping(Configuration config, String settingsPrefix) {
     this.baseDn = config.get(settingsPrefix + ".group.baseDn").orElse(null);
-    this.idAttribute = StringUtils.defaultString(config.get(settingsPrefix + ".group.idAttribute").orElse(null), DEFAULT_ID_ATTRIBUTE);
+    this.idAttribute = config.get(settingsPrefix + ".group.idAttribute").orElse(DEFAULT_ID_ATTRIBUTE);
 
-    String req = StringUtils.defaultString(config.get(settingsPrefix + ".group.request").orElse(null), DEFAULT_REQUEST);
+    String req = config.get(settingsPrefix + ".group.request").orElse(DEFAULT_REQUEST);
     this.requiredUserAttributes = StringUtils.substringsBetween(req, "{", "}");
     for (int i = 0; i < requiredUserAttributes.length; i++) {
       req = StringUtils.replace(req, "{" + requiredUserAttributes[i] + "}", "{" + i + "}");
index a32eb37eccd24ec5fd559c614be61cd8228d423f..ad99b8203219a8ecc874a89c10db7f332b48f854 100644 (file)
@@ -44,10 +44,10 @@ public class LdapUserMapping {
   public LdapUserMapping(Configuration config, String settingsPrefix) {
     String userBaseDnSettingKey = settingsPrefix + ".user.baseDn";
     this.baseDn = config.get(userBaseDnSettingKey).orElseThrow(() -> new LdapException(String.format(MANDATORY_LDAP_PROPERTY_ERROR, userBaseDnSettingKey)));
-    this.realNameAttribute = StringUtils.defaultString(config.get(settingsPrefix + ".user.realNameAttribute").orElse(null), DEFAULT_NAME_ATTRIBUTE);
-    this.emailAttribute = StringUtils.defaultString(config.get(settingsPrefix + ".user.emailAttribute").orElse(null), DEFAULT_EMAIL_ATTRIBUTE);
+    this.realNameAttribute = config.get(settingsPrefix + ".user.realNameAttribute").orElse(DEFAULT_NAME_ATTRIBUTE);
+    this.emailAttribute = config.get(settingsPrefix + ".user.emailAttribute").orElse(DEFAULT_EMAIL_ATTRIBUTE);
 
-    String req = StringUtils.defaultString(config.get(settingsPrefix + ".user.request").orElse(null), DEFAULT_REQUEST);
+    String req = config.get(settingsPrefix + ".user.request").orElse(DEFAULT_REQUEST);
     req = StringUtils.replace(req, "{login}", "{0}");
     this.request = req;
   }
index 2468ec47745bd03426974b3ba55d6d5b2b6de4bc..4cca46f43b0913f05e5d124874bd51c8b238cd24 100644 (file)
@@ -23,6 +23,7 @@ dependencies {
   api 'commons-codec:commons-codec'
   api 'commons-io:commons-io'
   api 'org.apache.commons:commons-lang3'
+  implementation 'org.apache.commons:commons-text'
   api 'com.google.code.gson:gson'
   api 'com.google.guava:guava'
   api 'com.google.code.findbugs:jsr305'
index 82d6d759ba92015b2906d2368f4c0d0962662d39..b50b4786145526a46a2e88ab1c5ef2021a9c4358 100644 (file)
 package org.sonar.ce.task.projectanalysis.source;
 
 import java.util.List;
+import java.util.Objects;
 import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
 import org.apache.commons.codec.digest.DigestUtils;
-import org.apache.commons.lang3.ObjectUtils;
 import org.sonar.api.utils.System2;
 import org.sonar.ce.task.projectanalysis.component.Component;
 import org.sonar.ce.task.projectanalysis.component.CrawlerDepthLimit;
@@ -130,7 +130,7 @@ public class PersistFileSourcesStep implements ComputationStep {
         boolean binaryDataUpdated = !dataHash.equals(previousDto.getDataHash());
         boolean srcHashUpdated = !srcHash.equals(previousDto.getSrcHash());
         String revision = computeRevision(latestChangeWithRevision);
-        boolean revisionUpdated = !ObjectUtils.equals(revision, previousDto.getRevision());
+        boolean revisionUpdated = !Objects.equals(revision, previousDto.getRevision());
         boolean lineHashesVersionUpdated = previousDto.getLineHashesVersion() != lineHashesVersion;
         if (binaryDataUpdated || srcHashUpdated || revisionUpdated || lineHashesVersionUpdated) {
           FileSourceDto updatedDto = new FileSourceDto()
index 74323644cd8c268b499cc3ef2c3a815c2fb34e76..1e04b79de3da0cf910cc3df6a6a65ef8712e692f 100644 (file)
@@ -21,7 +21,7 @@ package org.sonar.ce.task.projectanalysis.step;
 
 import java.util.ArrayList;
 import java.util.List;
-import org.apache.commons.lang3.StringEscapeUtils;
+import org.apache.commons.text.StringEscapeUtils;
 import org.sonar.ce.task.projectanalysis.component.Component;
 import org.sonar.ce.task.projectanalysis.component.CrawlerDepthLimit;
 import org.sonar.ce.task.projectanalysis.component.DepthTraversalTypeAwareCrawler;
@@ -183,7 +183,7 @@ public class PersistDuplicationDataStep implements ComputationStep {
       xml.append("<b s=\"").append(textBlock.getStart())
         .append("\" l=\"").append(length)
         .append("\" t=\"").append(disableLink)
-        .append("\" r=\"").append(StringEscapeUtils.escapeXml(componentDbKey))
+        .append("\" r=\"").append(StringEscapeUtils.escapeXml10(componentDbKey))
         .append("\"/>");
     }
   }
index fa6ac94fdb4ea7788eb2bba779f6487491055a4e..854d988cd84c262bf0c83923e97d965ec6f314b3 100644 (file)
@@ -74,7 +74,7 @@ public class NextPendingTaskPicker {
    * priority is always given to the task that is waiting longer - to avoid starvation
    */
   private Optional<CeQueueDto> submitOldest(DbSession session, String workerUuid, @Nullable CeTaskDtoLight eligibleForPeek, @Nullable CeTaskDtoLight eligibleForPeekInParallel) {
-    CeTaskDtoLight oldest = (CeTaskDtoLight) ObjectUtils.min(eligibleForPeek, eligibleForPeekInParallel);
+    CeTaskDtoLight oldest = ObjectUtils.min(eligibleForPeek, eligibleForPeekInParallel);
     Optional<CeQueueDto> ceQueueDto = ceQueueDao.tryToPeek(session, oldest.getCeTaskUuid(), workerUuid);
     if (!Objects.equals(oldest, eligibleForPeek)) {
       ceQueueDto.ifPresent(t -> LOG.info("Task [uuid = " + t.getUuid() + "] will be run concurrently with other tasks for the same project"));
index 146ae5d33b36a475321f819d0efcb51f790719ca..c3ad625f4b9f3a640e2aa03461f429137f48b54c 100644 (file)
@@ -19,9 +19,9 @@
  */
 package org.sonar.db.audit.model;
 
+import java.util.Objects;
 import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
-import org.apache.commons.lang3.ObjectUtils;
 import org.sonar.db.component.ComponentDto;
 import org.sonar.db.project.ProjectDto;
 
@@ -116,8 +116,8 @@ public class ComponentNewValue extends NewValue {
     addField(sb, "\"qualifier\": ", getQualifier(qualifier), true);
     addField(sb, "\"description\": ", this.description, true);
     addField(sb, "\"path\": ", this.path, true);
-    addField(sb, "\"isPrivate\": ", ObjectUtils.toString(this.isPrivate), false);
-    addField(sb, "\"isEnabled\": ", ObjectUtils.toString(this.isEnabled), false);
+    addField(sb, "\"isPrivate\": ", Objects.toString(this.isPrivate, ""), false);
+    addField(sb, "\"isEnabled\": ", Objects.toString(this.isEnabled, ""), false);
     endString(sb);
     return sb.toString();
   }
index 39040b313caccf58891b146a8223b5d68b08b229..c5ae6b046159161d3a9ef36f24dae1e99400a309 100644 (file)
@@ -19,9 +19,9 @@
  */
 package org.sonar.db.audit.model;
 
+import java.util.Objects;
 import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
-import org.apache.commons.lang3.ObjectUtils;
 import org.sonar.db.alm.setting.AlmSettingDto;
 import org.sonar.db.alm.setting.ProjectAlmSettingDto;
 import org.sonar.db.project.ProjectDto;
@@ -194,8 +194,8 @@ public class DevOpsPlatformSettingNewValue extends NewValue {
     addField(sb, "\"projectName\": ", this.projectName, true);
     addField(sb, "\"almRepo\": ", this.almRepo, true);
     addField(sb, "\"almSlug\": ", this.almSlug, true);
-    addField(sb, "\"isSummaryCommentEnabled\": ", ObjectUtils.toString(this.isSummaryCommentEnabled), false);
-    addField(sb, "\"isMonorepo\": ", ObjectUtils.toString(this.isMonorepo), false);
+    addField(sb, "\"isSummaryCommentEnabled\": ", Objects.toString(this.isSummaryCommentEnabled, ""), false);
+    addField(sb, "\"isMonorepo\": ", Objects.toString(this.isMonorepo, ""), false);
     endString(sb);
     return sb.toString();
   }
index d7f135f1dd623d604bccb13d7c666af9da717c97..eeb09433cbc40c2b0706628b2af93a6f97a01279 100644 (file)
@@ -19,9 +19,9 @@
  */
 package org.sonar.db.audit.model;
 
+import java.util.Objects;
 import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
-import org.apache.commons.lang3.ObjectUtils;
 import org.sonar.db.permission.template.PermissionTemplateDto;
 
 public class PermissionTemplateNewValue extends NewValue {
@@ -168,7 +168,7 @@ public class PermissionTemplateNewValue extends NewValue {
     addField(sb, "\"userLogin\": ", this.userLogin, true);
     addField(sb, "\"groupUuid\": ", this.groupUuid, true);
     addField(sb, "\"groupName\": ", this.groupName, true);
-    addField(sb, "\"withProjectCreator\": ", ObjectUtils.toString(this.withProjectCreator), false);
+    addField(sb, "\"withProjectCreator\": ", Objects.toString(this.withProjectCreator, ""), false);
     endString(sb);
     return sb.toString();
   }
index 70a4d389a5cb676633c21b0f52cb1550f97c7fed..b8a6e40473095c0bea86061a36f612692d6103a2 100644 (file)
@@ -21,9 +21,9 @@ package org.sonar.db.audit.model;
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Objects;
 import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
-import org.apache.commons.lang3.ObjectUtils;
 import org.sonar.api.utils.DateUtils;
 import org.sonar.db.user.UserDto;
 
@@ -146,12 +146,12 @@ public class UserNewValue extends NewValue {
     addField(sb, "\"userLogin\": ", this.userLogin, true);
     addField(sb, "\"name\": ", this.name, true);
     addField(sb, "\"email\": ", this.email, true);
-    addField(sb, "\"isActive\": ", ObjectUtils.toString(this.isActive), false);
+    addField(sb, "\"isActive\": ", Objects.toString(this.isActive, ""), false);
     addField(sb, "\"scmAccounts\": ", String.join(",", scmAccounts), true);
     addField(sb, "\"externalId\": ", this.externalId, true);
     addField(sb, "\"externalLogin\": ", this.externalLogin, true);
     addField(sb, "\"externalIdentityProvider\": ", this.externalIdentityProvider, true);
-    addField(sb, "\"local\": ", ObjectUtils.toString(this.local), false);
+    addField(sb, "\"local\": ", Objects.toString(this.local, ""), false);
     addField(sb, "\"lastConnectionDate\": ", this.lastConnectionDate == null ?
       "" : DateUtils.formatDateTime(this.lastConnectionDate), true);
     endString(sb);
index b052d3110a58d89409173641ca13cfd26bd28903..f91a2fef12a880cf3005e265c6f14c6744f8df6b 100644 (file)
@@ -246,7 +246,7 @@ public class EmailNotificationChannel extends NotificationChannel {
 
   private void setSubject(Email email, EmailMessage emailMessage) {
     String subject = StringUtils.defaultIfBlank(StringUtils.trimToEmpty(configuration.getPrefix()) + " ", "")
-      + StringUtils.defaultString(emailMessage.getSubject(), SUBJECT_DEFAULT);
+      + Objects.toString(emailMessage.getSubject(), SUBJECT_DEFAULT);
     email.setSubject(subject);
   }
 
index 1cbb469704a49adf3cf5cf5eaae11c8a4e6faf84..3e8a530f530dfbdec4e3a25f733ae41bd3557de3 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.server.authentication;
 
+import java.util.Objects;
 import java.util.Optional;
 import java.util.Set;
 import org.slf4j.MDC;
@@ -37,7 +38,6 @@ import org.sonar.server.user.TokenUserSession;
 import org.sonar.server.user.UserSession;
 
 import static java.net.HttpURLConnection.HTTP_UNAUTHORIZED;
-import static org.apache.commons.lang3.StringUtils.defaultString;
 import static org.sonar.api.CoreProperties.CORE_FORCE_AUTHENTICATION_DEFAULT_VALUE;
 import static org.sonar.api.CoreProperties.CORE_FORCE_AUTHENTICATION_PROPERTY;
 import static org.sonar.api.utils.DateUtils.formatDateTime;
@@ -140,8 +140,8 @@ public class UserSessionInitializer {
     }
     threadLocalSession.set(session);
     checkTokenUserSession(response, session);
-    request.setAttribute(ACCESS_LOG_LOGIN, defaultString(session.getLogin(), "-"));
-    MDC.put(USER_LOGIN_MDC_KEY, defaultString(session.getLogin(), "-"));
+    request.setAttribute(ACCESS_LOG_LOGIN, Objects.toString(session.getLogin(), "-"));
+    MDC.put(USER_LOGIN_MDC_KEY, Objects.toString(session.getLogin(), "-"));
   }
 
   private static void checkTokenUserSession(HttpResponse response, UserSession session) {
index 198c26ee1045c93d8cf9a7d35c56fe6a4affb4f9..9e924b126cfe89aba885113d70c2c2e51b78aba9 100644 (file)
@@ -11,6 +11,7 @@ dependencies {
   implementation 'com.github.erosb:json-sKema'
   api 'io.prometheus:simpleclient_common'
   api 'io.prometheus:simpleclient_servlet'
+  implementation 'org.apache.commons:commons-text'
 
   api project(':server:sonar-ce-common')
   api project(':server:sonar-ce-task')
index 5a71ad54a9e738a9eaaaa17d9f377ea6ea7c7368..47d241556b1776e8b1d9f3eacf305b5a90fb2038 100644 (file)
@@ -23,7 +23,7 @@ import com.google.common.collect.ImmutableMap;
 import java.io.IOException;
 import java.util.Map;
 import org.apache.commons.io.IOUtils;
-import org.apache.commons.lang3.text.StrSubstitutor;
+import org.apache.commons.text.StringSubstitutor;
 import org.sonar.api.measures.Metric;
 import org.sonar.api.server.ServerSide;
 
@@ -150,7 +150,7 @@ public class SvgGenerator {
       .put(PARAMETER_LABEL, label)
       .put(PARAMETER_VALUE, value)
       .build();
-    StrSubstitutor strSubstitutor = new StrSubstitutor(values);
+    StringSubstitutor strSubstitutor = new StringSubstitutor(values);
     return strSubstitutor.replace(badgeTemplate);
   }
 
@@ -162,7 +162,7 @@ public class SvgGenerator {
     Map<String, String> values = ImmutableMap.of(
       PARAMETER_TOTAL_WIDTH, valueOf(MARGIN + computeWidth(error) + MARGIN),
       PARAMETER_LABEL, error);
-    StrSubstitutor strSubstitutor = new StrSubstitutor(values);
+    StringSubstitutor strSubstitutor = new StringSubstitutor(values);
     return strSubstitutor.replace(errorTemplate);
   }
 
index 1f0bf728f0f71d12cfcc2be1d24e4b3f7f5b1ea6..a504735a0633df6400e210ca2f32d3eae974bb2c 100644 (file)
@@ -98,7 +98,7 @@ public class ScmAction implements SourcesWsAction {
   public void handle(Request request, Response response) {
     String fileKey = request.mandatoryParam("key");
     int from = Math.max(request.mandatoryParamAsInt("from"), 1);
-    int to = (Integer) ObjectUtils.defaultIfNull(request.paramAsInt("to"), Integer.MAX_VALUE);
+    int to = ObjectUtils.defaultIfNull(request.paramAsInt("to"), Integer.MAX_VALUE);
     boolean commitsByLine = request.mandatoryParamAsBoolean("commits_by_line");
 
     try (DbSession dbSession = dbClient.openSession(false)) {
index 58c4ca0795bdf628a9f69b3929374bfe130d3ec3..fda6dc302c8f18a3e135980ab410555567a9697f 100644 (file)
@@ -84,7 +84,7 @@ public class ShowAction implements SourcesWsAction {
   public void handle(Request request, Response response) {
     String fileKey = request.mandatoryParam("key");
     int from = Math.max(request.paramAsInt("from"), 1);
-    int to = (Integer) ObjectUtils.defaultIfNull(request.paramAsInt("to"), Integer.MAX_VALUE);
+    int to = ObjectUtils.defaultIfNull(request.paramAsInt("to"), Integer.MAX_VALUE);
 
     try (DbSession dbSession = dbClient.openSession(false)) {
       ComponentDto file = componentFinder.getByKey(dbSession, fileKey);
index 41b4aa79f870e056428951b09a427a3f96b90973..46978ee5fb1ddc2c253d856b63eec6b6394e0e13 100644 (file)
@@ -25,7 +25,6 @@ import java.util.Collection;
 import java.util.Objects;
 import java.util.function.Function;
 import javax.annotation.Nonnull;
-import org.apache.commons.lang3.StringUtils;
 import org.sonar.api.rule.RuleKey;
 
 import static java.util.Comparator.comparing;
@@ -66,7 +65,7 @@ public class AbstractTracker<RAW extends Trackable, BASE extends Trackable> {
     protected LineAndLineHashKey(Trackable trackable) {
       this.ruleKey = trackable.getRuleKey();
       this.line = trackable.getLine();
-      this.lineHash = StringUtils.defaultString(trackable.getLineHash(), "");
+      this.lineHash = Objects.toString(trackable.getLineHash(), "");
     }
 
     @Override
@@ -98,7 +97,7 @@ public class AbstractTracker<RAW extends Trackable, BASE extends Trackable> {
       this.ruleKey = trackable.getRuleKey();
       this.line = trackable.getLine();
       this.message = trackable.getMessage();
-      this.lineHash = StringUtils.defaultString(trackable.getLineHash(), "");
+      this.lineHash = Objects.toString(trackable.getLineHash(), "");
     }
 
     @Override
@@ -128,7 +127,7 @@ public class AbstractTracker<RAW extends Trackable, BASE extends Trackable> {
     LineHashAndMessageKey(Trackable trackable) {
       this.ruleKey = trackable.getRuleKey();
       this.message = trackable.getMessage();
-      this.lineHash = StringUtils.defaultString(trackable.getLineHash(), "");
+      this.lineHash = Objects.toString(trackable.getLineHash(), "");
     }
 
     @Override
@@ -186,7 +185,7 @@ public class AbstractTracker<RAW extends Trackable, BASE extends Trackable> {
 
     LineHashKey(Trackable trackable) {
       this.ruleKey = trackable.getRuleKey();
-      this.lineHash = StringUtils.defaultString(trackable.getLineHash(), "");
+      this.lineHash = Objects.toString(trackable.getLineHash(), "");
     }
 
     @Override
index f30a0b5837113e44d0c0ea59f93ead1b5839b9ae..660b31fa69b854fb7d6526e8eba329296d43657a 100644 (file)
@@ -66,7 +66,7 @@ public class NewRule {
   }
 
   public NewRule setStatus(@Nullable RuleStatus s) {
-    this.status = (RuleStatus) ObjectUtils.defaultIfNull(s, RuleStatus.defaultStatus());
+    this.status = ObjectUtils.defaultIfNull(s, RuleStatus.defaultStatus());
     return this;
   }
 
index cc62c256c08d18fd6ec6add5e9a4839adf555705..adb0d1a01718d12df3f1ba462cbb5a833378bb4d 100644 (file)
@@ -22,6 +22,7 @@ package org.sonar.api.impl.ws;
 import java.io.InputStream;
 import java.util.Arrays;
 import java.util.List;
+import java.util.Objects;
 import java.util.Set;
 import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
@@ -33,7 +34,6 @@ import static java.lang.String.format;
 import static java.util.Collections.emptyList;
 import static java.util.Collections.singletonList;
 import static java.util.Objects.requireNonNull;
-import static org.apache.commons.lang3.StringUtils.defaultString;
 import static org.sonar.api.utils.Preconditions.checkArgument;
 
 /**
@@ -68,7 +68,7 @@ public abstract class ValidatingRequest extends Request {
   public String param(String key) {
     WebService.Param definition = action.param(key);
     String rawValue = readParam(key, definition);
-    String rawValueOrDefault = defaultString(rawValue, definition.defaultValue());
+    String rawValueOrDefault = Objects.toString(rawValue, definition.defaultValue());
     String value = rawValueOrDefault == null ? null : trim(rawValueOrDefault);
     validateRequiredValue(key, definition, rawValue);
     if (value == null) {
@@ -124,7 +124,7 @@ public abstract class ValidatingRequest extends Request {
   @Override
   public List<String> paramAsStrings(String key) {
     WebService.Param definition = action.param(key);
-    String value = defaultString(readParam(key, definition), definition.defaultValue());
+    String value = Objects.toString(readParam(key, definition), definition.defaultValue());
     if (value == null) {
       return null;
     }
@@ -152,7 +152,7 @@ public abstract class ValidatingRequest extends Request {
   private String readParam(String key, @Nullable WebService.Param definition) {
     checkArgument(definition != null, "BUG - parameter '%s' is undefined for action '%s'", key, action.key());
     String deprecatedKey = definition.deprecatedKey();
-    String param = deprecatedKey != null ? defaultString(readParam(deprecatedKey), readParam(key)) : readParam(key);
+    String param = deprecatedKey != null ? Objects.toString(readParam(deprecatedKey), readParam(key)) : readParam(key);
     if (param != null && param.contains("\0")) {
       throw new IllegalArgumentException("Request parameters are not allowed to contain NUL character");
     }
diff --git a/sonar-plugin-api-impl/src/test/java/org/sonar/api/impl/ws/ValidatingRequestTest.java b/sonar-plugin-api-impl/src/test/java/org/sonar/api/impl/ws/ValidatingRequestTest.java
new file mode 100644 (file)
index 0000000..1c017da
--- /dev/null
@@ -0,0 +1,63 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2024 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.
+ */
+package org.sonar.api.impl.ws;
+
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.sonar.api.server.ws.WebService;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class ValidatingRequestTest {
+
+  ValidatingRequest underTest = Mockito.mock(ValidatingRequest.class, Mockito.CALLS_REAL_METHODS);
+  @Test
+  public void paramReturnsTheDefaultValue_whenDefaultIsSetAndValueIsNot() {
+    underTest.setAction(mock(WebService.Action.class));
+    WebService.Param param = mockParamWithDefaultValue("default");
+
+    when(underTest.action().param("param")).thenReturn(param);
+
+    assertThat(underTest.param("param")).isEqualTo("default");
+  }
+
+  @Test
+  public void paramAsStringsReturnsTheDefaultValues_whenDefaultIsSetAndValueIsNot() {
+    underTest.setAction(mock(WebService.Action.class));
+    WebService.Param param = mockParamWithDefaultValue("default,values");
+
+    when(underTest.action().param("param")).thenReturn(param);
+
+    assertThat(underTest.paramAsStrings("param")).containsExactly("default", "values");
+  }
+
+  private static WebService.Param mockParamWithDefaultValue(String defaultValue) {
+    WebService.Param param = mock(WebService.Param.class);
+    when(param.defaultValue()).thenReturn(defaultValue);
+    when(param.possibleValues()).thenReturn(null);
+    when(param.maximumLength()).thenReturn(10);
+    when(param.maximumLength()).thenReturn(10);
+    when(param.maximumValue()).thenReturn(null);
+    when(param.maxValuesAllowed()).thenReturn(2);
+    return param;
+  }
+}
index 40ff8e0a2baefe7fdf627e2eafbc23a30ed64afe..de8669b288d5de92e921586da10a1067ed592dfc 100644 (file)
@@ -21,6 +21,7 @@ dependencies {
   api 'commons-codec:commons-codec'
   api 'commons-io:commons-io'
   api 'org.apache.commons:commons-lang3'
+  implementation 'org.apache.commons:commons-text'
   api 'com.google.code.gson:gson'
   api 'org.apache.commons:commons-csv'
   api 'com.google.protobuf:protobuf-java'
index 9c17a76a8c2be953bada5f30c88f668df3fd6d00..6df3e293910e30b3806d9c7ec526a86678169482 100644 (file)
@@ -29,7 +29,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.stream.Collectors;
 import javax.annotation.Nullable;
-import org.apache.commons.lang3.StringEscapeUtils;
+import org.apache.commons.text.StringEscapeUtils;
 import org.sonar.api.impl.utils.ScannerUtils;
 import org.sonar.api.utils.log.Logger;
 import org.sonar.api.utils.log.Loggers;
index 36840fb6ac8bf446079ce7a028e757345b407844..235f297fc69dc4847e62631aedf16be4bc361b43 100644 (file)
@@ -179,7 +179,7 @@ public abstract class AbstractExclusionFilters {
   }
 
   static PathPattern[] prepareMainExclusions(String[] sourceExclusions, String[] testInclusions) {
-    String[] patterns = (String[]) ArrayUtils.addAll(
+    String[] patterns = ArrayUtils.addAll(
       sourceExclusions, testInclusions);
     return PathPattern.create(patterns);
   }
index a4dede66e08b463b6ffff782f2c2facd321b6166..c219c831fa126453a37ded901fc7c7b536937e3d 100644 (file)
@@ -199,7 +199,7 @@ public class NativeGitBlameCommand {
   private static String formatGitSemanticVersion(String version) {
     return semanticVersionDelimiter
       .splitAsStream(version)
-      .takeWhile(NumberUtils::isNumber)
+      .takeWhile(NumberUtils::isCreatable)
       .collect(Collectors.joining("."));
   }