Skip to content

Commit 9fbf5dc

Browse files
committed
Use String#lastIndexOf(int) where possible
1 parent a683e9e commit 9fbf5dc

File tree

3 files changed

+12
-10
lines changed

3 files changed

+12
-10
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/support/RootBeanDefinition.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ boolean hasAnyExternallyManagedInitMethod(String initMethod) {
508508
}
509509
if (this.externallyManagedInitMethods != null) {
510510
for (String candidate : this.externallyManagedInitMethods) {
511-
int indexOfDot = candidate.lastIndexOf(".");
511+
int indexOfDot = candidate.lastIndexOf('.');
512512
if (indexOfDot >= 0) {
513513
String methodName = candidate.substring(indexOfDot + 1);
514514
if (methodName.equals(initMethod)) {
@@ -585,7 +585,7 @@ boolean hasAnyExternallyManagedDestroyMethod(String destroyMethod) {
585585
}
586586
if (this.externallyManagedDestroyMethods != null) {
587587
for (String candidate : this.externallyManagedDestroyMethods) {
588-
int indexOfDot = candidate.lastIndexOf(".");
588+
int indexOfDot = candidate.lastIndexOf('.');
589589
if (indexOfDot >= 0) {
590590
String methodName = candidate.substring(indexOfDot + 1);
591591
if (methodName.equals(destroyMethod)) {

spring-core/src/main/java/org/springframework/util/StringUtils.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -65,6 +65,8 @@ public abstract class StringUtils {
6565

6666
private static final String FOLDER_SEPARATOR = "/";
6767

68+
private static final char FOLDER_SEPARATOR_CHAR = '/';
69+
6870
private static final String WINDOWS_FOLDER_SEPARATOR = "\\";
6971

7072
private static final String TOP_PATH = "..";
@@ -581,7 +583,7 @@ public static String getFilename(@Nullable String path) {
581583
return null;
582584
}
583585

584-
int separatorIndex = path.lastIndexOf(FOLDER_SEPARATOR);
586+
int separatorIndex = path.lastIndexOf(FOLDER_SEPARATOR_CHAR);
585587
return (separatorIndex != -1 ? path.substring(separatorIndex + 1) : path);
586588
}
587589

@@ -602,7 +604,7 @@ public static String getFilenameExtension(@Nullable String path) {
602604
return null;
603605
}
604606

605-
int folderIndex = path.lastIndexOf(FOLDER_SEPARATOR);
607+
int folderIndex = path.lastIndexOf(FOLDER_SEPARATOR_CHAR);
606608
if (folderIndex > extIndex) {
607609
return null;
608610
}
@@ -622,7 +624,7 @@ public static String stripFilenameExtension(String path) {
622624
return path;
623625
}
624626

625-
int folderIndex = path.lastIndexOf(FOLDER_SEPARATOR);
627+
int folderIndex = path.lastIndexOf(FOLDER_SEPARATOR_CHAR);
626628
if (folderIndex > extIndex) {
627629
return path;
628630
}
@@ -639,11 +641,11 @@ public static String stripFilenameExtension(String path) {
639641
* @return the full file path that results from applying the relative path
640642
*/
641643
public static String applyRelativePath(String path, String relativePath) {
642-
int separatorIndex = path.lastIndexOf(FOLDER_SEPARATOR);
644+
int separatorIndex = path.lastIndexOf(FOLDER_SEPARATOR_CHAR);
643645
if (separatorIndex != -1) {
644646
String newPath = path.substring(0, separatorIndex);
645647
if (!relativePath.startsWith(FOLDER_SEPARATOR)) {
646-
newPath += FOLDER_SEPARATOR;
648+
newPath += FOLDER_SEPARATOR_CHAR;
647649
}
648650
return newPath + relativePath;
649651
}

spring-webmvc/src/test/java/org/springframework/web/servlet/tags/form/AbstractHtmlElementTagTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -136,7 +136,7 @@ protected final void assertAttributeNotPresent(String output, String attributeNa
136136
}
137137

138138
protected final void assertBlockTagContains(String output, String desiredContents) {
139-
String contents = output.substring(output.indexOf(">") + 1, output.lastIndexOf("<"));
139+
String contents = output.substring(output.indexOf(">") + 1, output.lastIndexOf('<'));
140140
assertThat(contents.contains(desiredContents)).as("Expected to find '" + desiredContents + "' in the contents of block tag '" + output + "'").isTrue();
141141
}
142142

0 commit comments

Comments
 (0)