Skip to content

Commit 5a7875e

Browse files
[MNG-6829] refactor: Replace any StringUtils#isEmpty(String) and #isNotEmpty(String) (#169)
* [MNG-6829] refactor: Replace any StringUtils#isEmpty(String) and #isNotEmpty(String) ### [Replace any StringUtils#isEmpty(String) and #isNotEmpty(String)](https://public.moderne.io/recipes/org.openrewrite.java.migrate.apache.commons.lang.IsNotEmptyToJdk) A continuation of https://issues.apache.org/jira/browse/MNG-6825 and https://issues.apache.org/jira/browse/MNG-6829, where previously a request was made to instead of switching implementation to a different StringUtils, we should instead switch to using JDK internals where we can. This is a first such pull request to gauge interest before I can potentially make 47 more PRs to replace a total of 210 uses of isEmpty / isNotEmpty. Co-authored-by: Moderne <team@moderne.io> * Apply Spotless --------- Co-authored-by: Moderne <team@moderne.io>
1 parent cbdee3b commit 5a7875e

File tree

17 files changed

+61
-69
lines changed

17 files changed

+61
-69
lines changed

maven-scm-api/src/main/java/org/apache/maven/scm/ChangeSet.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import java.util.List;
2929
import java.util.Set;
3030

31-
import org.apache.commons.lang3.StringUtils;
3231
import org.apache.maven.scm.provider.ScmProviderRepository;
3332
import org.apache.maven.scm.util.FilenameUtils;
3433
import org.apache.maven.scm.util.ThreadSafeDateFormat;
@@ -302,15 +301,15 @@ public void setDate(String date) {
302301
*/
303302
public void setDate(String date, String userDatePattern) {
304303
try {
305-
if (!StringUtils.isEmpty(userDatePattern)) {
304+
if (!(userDatePattern == null || userDatePattern.isEmpty())) {
306305
SimpleDateFormat format = new SimpleDateFormat(userDatePattern);
307306

308307
this.date = format.parse(date);
309308
} else {
310309
this.date = TIMESTAMP_FORMAT_3.parse(date);
311310
}
312311
} catch (ParseException e) {
313-
if (!StringUtils.isEmpty(userDatePattern)) {
312+
if (!(userDatePattern == null || userDatePattern.isEmpty())) {
314313
try {
315314
this.date = TIMESTAMP_FORMAT_3.parse(date);
316315
} catch (ParseException pe) {

maven-scm-api/src/main/java/org/apache/maven/scm/provider/AbstractScmProvider.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.util.Date;
2424
import java.util.List;
2525

26-
import org.apache.commons.lang3.StringUtils;
2726
import org.apache.maven.scm.CommandParameter;
2827
import org.apache.maven.scm.CommandParameters;
2928
import org.apache.maven.scm.NoSuchCommandScmException;
@@ -193,7 +192,7 @@ public BranchScmResult branch(ScmRepository repository, ScmFileSet fileSet, Stri
193192
throws ScmException {
194193
ScmBranchParameters scmBranchParameters = new ScmBranchParameters();
195194

196-
if (StringUtils.isNotEmpty(message)) {
195+
if (message != null && !message.isEmpty()) {
197196
scmBranchParameters.setMessage(message);
198197
}
199198

@@ -251,7 +250,7 @@ public ChangeLogScmResult changeLog(
251250
throws ScmException {
252251
ScmBranch scmBranch = null;
253252

254-
if (StringUtils.isNotEmpty(branch)) {
253+
if (branch != null && !branch.isEmpty()) {
255254
scmBranch = new ScmBranch(branch);
256255
}
257256
return changeLog(repository, fileSet, startDate, endDate, numDays, scmBranch, null);
@@ -324,11 +323,11 @@ public ChangeLogScmResult changeLog(
324323
ScmVersion startRevision = null;
325324
ScmVersion endRevision = null;
326325

327-
if (StringUtils.isNotEmpty(startTag)) {
326+
if (startTag != null && !startTag.isEmpty()) {
328327
startRevision = new ScmRevision(startTag);
329328
}
330329

331-
if (StringUtils.isNotEmpty(endTag)) {
330+
if (endTag != null && !endTag.isEmpty()) {
332331
endRevision = new ScmRevision(endTag);
333332
}
334333

@@ -385,7 +384,7 @@ public CheckInScmResult checkIn(ScmRepository repository, ScmFileSet fileSet, St
385384
throws ScmException {
386385
ScmVersion scmVersion = null;
387386

388-
if (StringUtils.isNotEmpty(tag)) {
387+
if (tag != null && !tag.isEmpty()) {
389388
scmVersion = new ScmBranch(tag);
390389
}
391390

@@ -444,7 +443,7 @@ public CheckOutScmResult checkOut(ScmRepository repository, ScmFileSet fileSet,
444443
throws ScmException {
445444
ScmVersion scmVersion = null;
446445

447-
if (StringUtils.isNotEmpty(tag)) {
446+
if (tag != null && !tag.isEmpty()) {
448447
scmVersion = new ScmBranch(tag);
449448
}
450449

@@ -524,11 +523,11 @@ public DiffScmResult diff(ScmRepository repository, ScmFileSet fileSet, String s
524523
ScmVersion startVersion = null;
525524
ScmVersion endVersion = null;
526525

527-
if (StringUtils.isNotEmpty(startRevision)) {
526+
if (startRevision != null && !startRevision.isEmpty()) {
528527
startVersion = new ScmRevision(startRevision);
529528
}
530529

531-
if (StringUtils.isNotEmpty(endRevision)) {
530+
if (endRevision != null && !endRevision.isEmpty()) {
532531
endVersion = new ScmRevision(endRevision);
533532
}
534533

@@ -601,7 +600,7 @@ public ExportScmResult export(ScmRepository repository, ScmFileSet fileSet, Stri
601600
throws ScmException {
602601
ScmVersion scmVersion = null;
603602

604-
if (StringUtils.isNotEmpty(tag)) {
603+
if (tag != null && !tag.isEmpty()) {
605604
scmVersion = new ScmRevision(tag);
606605
}
607606

@@ -656,7 +655,7 @@ public ListScmResult list(ScmRepository repository, ScmFileSet fileSet, boolean
656655
throws ScmException {
657656
ScmVersion scmVersion = null;
658657

659-
if (StringUtils.isNotEmpty(tag)) {
658+
if (tag != null && !tag.isEmpty()) {
660659
scmVersion = new ScmRevision(tag);
661660
}
662661

@@ -804,7 +803,7 @@ public TagScmResult tag(ScmRepository repository, ScmFileSet fileSet, String tag
804803

805804
parameters.setString(CommandParameter.TAG_NAME, tagName);
806805

807-
if (StringUtils.isNotEmpty(message)) {
806+
if (message != null && !message.isEmpty()) {
808807
parameters.setString(CommandParameter.MESSAGE, message);
809808
}
810809

@@ -957,7 +956,7 @@ private UpdateScmResult update(
957956
throws ScmException {
958957
ScmBranch scmBranch = null;
959958

960-
if (StringUtils.isNotEmpty(tag)) {
959+
if (tag != null && !tag.isEmpty()) {
961960
scmBranch = new ScmBranch(tag);
962961
}
963962

@@ -1017,7 +1016,7 @@ public UpdateScmResult update(
10171016
throws ScmException {
10181017
ScmBranch scmBranch = null;
10191018

1020-
if (StringUtils.isNotEmpty(tag)) {
1019+
if (tag != null && !tag.isEmpty()) {
10211020
scmBranch = new ScmBranch(tag);
10221021
}
10231022

maven-scm-api/src/main/java/org/apache/maven/scm/util/AbstractConsumer.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.util.Date;
2525
import java.util.Locale;
2626

27-
import org.apache.commons.lang3.StringUtils;
2827
import org.codehaus.plexus.util.cli.StreamConsumer;
2928
import org.slf4j.Logger;
3029
import org.slf4j.LoggerFactory;
@@ -63,7 +62,7 @@ protected Date parseDate(String date, String userPattern, String defaultPattern,
6362
String patternUsed = null;
6463
Locale localeUsed = null;
6564

66-
if (StringUtils.isNotEmpty(userPattern)) {
65+
if (userPattern != null && !userPattern.isEmpty()) {
6766
if (locale != null) {
6867
format = new SimpleDateFormat(userPattern, locale);
6968
localeUsed = locale;
@@ -73,7 +72,7 @@ protected Date parseDate(String date, String userPattern, String defaultPattern,
7372
}
7473
patternUsed = userPattern;
7574
} else {
76-
if (StringUtils.isNotEmpty(defaultPattern)) {
75+
if (defaultPattern != null && !defaultPattern.isEmpty()) {
7776
if (locale != null) {
7877
format = new SimpleDateFormat(defaultPattern, locale);
7978
localeUsed = locale;

maven-scm-client/src/main/java/org/apache/maven/scm/client/cli/MavenScmCli.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.io.File;
2222
import java.util.List;
2323

24-
import org.apache.commons.lang3.StringUtils;
2524
import org.apache.maven.scm.ScmBranch;
2625
import org.apache.maven.scm.ScmException;
2726
import org.apache.maven.scm.ScmFile;
@@ -292,15 +291,15 @@ private void showError(ScmResult result) {
292291

293292
String providerMessage = result.getProviderMessage();
294293

295-
if (!StringUtils.isEmpty(providerMessage)) {
294+
if (!(providerMessage == null || providerMessage.isEmpty())) {
296295
System.err.println("Error message from the provider: " + providerMessage);
297296
} else {
298297
System.err.println("The provider didn't give a error message.");
299298
}
300299

301300
String output = result.getCommandOutput();
302301

303-
if (!StringUtils.isEmpty(output)) {
302+
if (!(output == null || output.isEmpty())) {
304303
System.err.println("Command output:");
305304

306305
System.err.println(output);

maven-scm-plugin/src/main/java/org/apache/maven/scm/plugin/AbstractScmMojo.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,9 @@ protected void setConnectionType(String connectionType) {
218218

219219
public String getConnectionUrl() {
220220
boolean requireDeveloperConnection = !"connection".equals(connectionType.toLowerCase());
221-
if (StringUtils.isNotEmpty(connectionUrl) && !requireDeveloperConnection) {
221+
if ((connectionUrl != null && !connectionUrl.isEmpty()) && !requireDeveloperConnection) {
222222
return connectionUrl;
223-
} else if (StringUtils.isNotEmpty(developerConnectionUrl)) {
223+
} else if (developerConnectionUrl != null && !developerConnectionUrl.isEmpty()) {
224224
return developerConnectionUrl;
225225
}
226226
if (requireDeveloperConnection) {
@@ -272,15 +272,15 @@ public ScmRepository getScmRepository() throws ScmException {
272272

273273
providerRepo.setPushChanges(pushChanges);
274274

275-
if (!StringUtils.isEmpty(workItem)) {
275+
if (!(workItem == null || workItem.isEmpty())) {
276276
providerRepo.setWorkItem(workItem);
277277
}
278278

279-
if (!StringUtils.isEmpty(username)) {
279+
if (!(username == null || username.isEmpty())) {
280280
providerRepo.setUser(username);
281281
}
282282

283-
if (!StringUtils.isEmpty(password)) {
283+
if (!(password == null || password.isEmpty())) {
284284
providerRepo.setPassword(password);
285285
}
286286

@@ -289,24 +289,25 @@ public ScmRepository getScmRepository() throws ScmException {
289289

290290
loadInfosFromSettings(repo);
291291

292-
if (!StringUtils.isEmpty(username)) {
292+
if (!(username == null || username.isEmpty())) {
293293
repo.setUser(username);
294294
}
295295

296-
if (!StringUtils.isEmpty(password)) {
296+
if (!(password == null || password.isEmpty())) {
297297
repo.setPassword(password);
298298
}
299299

300-
if (!StringUtils.isEmpty(privateKey)) {
300+
if (!(privateKey == null || privateKey.isEmpty())) {
301301
repo.setPrivateKey(privateKey);
302302
}
303303

304-
if (!StringUtils.isEmpty(passphrase)) {
304+
if (!(passphrase == null || passphrase.isEmpty())) {
305305
repo.setPassphrase(passphrase);
306306
}
307307
}
308308

309-
if (!StringUtils.isEmpty(tagBase) && repository.getProvider().equals("svn")) {
309+
if (!(tagBase == null || tagBase.isEmpty())
310+
&& repository.getProvider().equals("svn")) {
310311
SvnScmProviderRepository svnRepo = (SvnScmProviderRepository) repository.getProviderRepository();
311312

312313
svnRepo.setTagBase(tagBase);
@@ -405,11 +406,11 @@ public void setExcludes(String excludes) {
405406
}
406407

407408
public ScmVersion getScmVersion(String versionType, String version) throws MojoExecutionException {
408-
if (StringUtils.isEmpty(versionType) && StringUtils.isNotEmpty(version)) {
409+
if ((versionType == null || versionType.isEmpty()) && (version != null && !version.isEmpty())) {
409410
throw new MojoExecutionException("You must specify the version type.");
410411
}
411412

412-
if (StringUtils.isEmpty(version)) {
413+
if (version == null || version.isEmpty()) {
413414
return null;
414415
}
415416

maven-scm-plugin/src/main/java/org/apache/maven/scm/plugin/BootstrapMojo.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,13 +159,13 @@ private void runGoals(String relativePathProjectDirectory) throws MojoExecutionE
159159
protected String determineWorkingDirectoryPath(
160160
File checkoutDirectory, String relativePathProjectDirectory, String goalsDirectory) {
161161
File projectDirectory;
162-
if (StringUtils.isNotEmpty(relativePathProjectDirectory)) {
162+
if (relativePathProjectDirectory != null && !relativePathProjectDirectory.isEmpty()) {
163163
projectDirectory = new File(checkoutDirectory, relativePathProjectDirectory);
164164
} else {
165165
projectDirectory = checkoutDirectory;
166166
}
167167

168-
if (StringUtils.isEmpty(goalsDirectory)) {
168+
if (goalsDirectory == null || goalsDirectory.isEmpty()) {
169169
return projectDirectory.getPath();
170170
}
171171

maven-scm-plugin/src/main/java/org/apache/maven/scm/plugin/ChangeLogMojo.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.text.SimpleDateFormat;
2424
import java.util.Date;
2525

26-
import org.apache.commons.lang3.StringUtils;
2726
import org.apache.maven.plugin.MojoExecutionException;
2827
import org.apache.maven.plugins.annotations.Mojo;
2928
import org.apache.maven.plugins.annotations.Parameter;
@@ -143,24 +142,28 @@ public void execute() throws MojoExecutionException {
143142

144143
request.setDatePattern(dateFormat);
145144

146-
if (StringUtils.isNotEmpty(startDate)) {
145+
if (startDate != null && !startDate.isEmpty()) {
147146
request.setStartDate(parseDate(localFormat, startDate));
148147
}
149148

150-
if (StringUtils.isNotEmpty(endDate)) {
149+
if (endDate != null && !endDate.isEmpty()) {
151150
request.setEndDate(parseDate(localFormat, endDate));
152151
}
153152

154-
if (StringUtils.isNotEmpty(startScmVersion)) {
153+
if (startScmVersion != null && !startScmVersion.isEmpty()) {
155154
ScmVersion startRev = getScmVersion(
156-
StringUtils.isEmpty(startScmVersionType) ? VERSION_TYPE_REVISION : startScmVersionType,
155+
(startScmVersionType == null || startScmVersionType.isEmpty())
156+
? VERSION_TYPE_REVISION
157+
: startScmVersionType,
157158
startScmVersion);
158159
request.setStartRevision(startRev);
159160
}
160161

161-
if (StringUtils.isNotEmpty(endScmVersion)) {
162+
if (endScmVersion != null && !endScmVersion.isEmpty()) {
162163
ScmVersion endRev = getScmVersion(
163-
StringUtils.isEmpty(endScmVersionType) ? VERSION_TYPE_REVISION : endScmVersionType,
164+
(endScmVersionType == null || endScmVersionType.isEmpty())
165+
? VERSION_TYPE_REVISION
166+
: endScmVersionType,
164167
endScmVersion);
165168
request.setEndRevision(endRev);
166169
}
@@ -171,13 +174,14 @@ public void execute() throws MojoExecutionException {
171174
request.setNumDays(numDays);
172175
}
173176

174-
if (StringUtils.isNotEmpty(scmVersion)) {
177+
if (scmVersion != null && !scmVersion.isEmpty()) {
175178
ScmVersion rev = getScmVersion(
176-
StringUtils.isEmpty(scmVersionType) ? VERSION_TYPE_REVISION : scmVersionType, scmVersion);
179+
(scmVersionType == null || scmVersionType.isEmpty()) ? VERSION_TYPE_REVISION : scmVersionType,
180+
scmVersion);
177181
request.setRevision(rev);
178182
}
179183

180-
if (StringUtils.isNotEmpty(scmBranch)) {
184+
if (scmBranch != null && !scmBranch.isEmpty()) {
181185
request.setScmBranch(new ScmBranch(scmBranch));
182186
}
183187

maven-scm-providers/maven-scm-providers-git/maven-scm-provider-git-commons/src/main/java/org/apache/maven/scm/provider/git/GitConfigFileReader.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
import java.util.Iterator;
2727
import java.util.List;
2828

29-
import org.apache.commons.lang3.StringUtils;
30-
3129
/**
3230
* @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
3331
*
@@ -110,7 +108,7 @@ private List<String> getConfLines() {
110108
new BufferedReader(new FileReader(new File(getConfigDirectory(), "config")))) {
111109
String line;
112110
while ((line = reader.readLine()) != null) {
113-
if (!line.startsWith("#") && StringUtils.isNotEmpty(line)) {
111+
if (!line.startsWith("#") && (line != null && !line.isEmpty())) {
114112
lines.add(line);
115113
}
116114
}

maven-scm-providers/maven-scm-providers-git/maven-scm-provider-gitexe/src/main/java/org/apache/maven/scm/provider/git/gitexe/command/diff/GitDiffRawConsumer.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.util.ArrayList;
2222
import java.util.List;
2323

24-
import org.apache.commons.lang3.StringUtils;
2524
import org.apache.maven.scm.ScmFile;
2625
import org.apache.maven.scm.ScmFileStatus;
2726
import org.apache.maven.scm.util.AbstractConsumer;
@@ -44,7 +43,7 @@ public void consumeLine(String line) {
4443
if (logger.isDebugEnabled()) {
4544
logger.debug(line);
4645
}
47-
if (StringUtils.isEmpty(line)) {
46+
if (line == null || line.isEmpty()) {
4847
return;
4948
}
5049

maven-scm-providers/maven-scm-providers-git/maven-scm-provider-gitexe/src/main/java/org/apache/maven/scm/provider/git/gitexe/command/info/GitInfoConsumer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public void consumeLine(String line) {
5252
}
5353

5454
if (infoItems.isEmpty()) {
55-
if (!StringUtils.isEmpty(line)) {
55+
if (!(line == null || line.isEmpty())) {
5656
InfoItem infoItem = new InfoItem();
5757
infoItem.setRevision(StringUtils.trim(line));
5858
infoItem.setURL(scmFileSet.getBasedir().toPath().toUri().toASCIIString());

0 commit comments

Comments
 (0)