Skip to content

Commit efea289

Browse files
committed
Improve Javadoc for getFilename() & getFilenameExtension() in StringUtils
Closes gh-34932
1 parent 612dc57 commit efea289

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

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

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -614,10 +614,18 @@ private static String changeFirstCharacterCase(String str, boolean capitalize) {
614614
}
615615

616616
/**
617-
* Extract the filename from the given Java resource path,
618-
* for example, {@code "mypath/myfile.txt" → "myfile.txt"}.
617+
* Extract the filename from the given Java resource path.
618+
* <p>Examples:
619+
* <ul>
620+
* <li>{@code "my/path/myfile.txt"} &rarr; {@code "myfile.txt"}
621+
* <li>{@code "myfolder"} &rarr; {@code "myfolder"}
622+
* <li>{@code "myfile.txt"} &rarr; {@code "myfile.txt"}
623+
* <li>{@code ""} &rarr; {@code ""}
624+
* <li>{@code null} &rarr; {@code null}
625+
* </ul>
619626
* @param path the file path (may be {@code null})
620-
* @return the extracted filename, or {@code null} if none
627+
* @return the extracted filename, the original path if it does not contain a
628+
* forward slash ({@code "/"}), or {@code null} if the supplied path is {@code null}
621629
*/
622630
@Contract("null -> null; !null -> !null")
623631
public static @Nullable String getFilename(@Nullable String path) {
@@ -630,10 +638,20 @@ private static String changeFirstCharacterCase(String str, boolean capitalize) {
630638
}
631639

632640
/**
633-
* Extract the filename extension from the given Java resource path,
634-
* for example, "mypath/myfile.txt" &rarr; "txt".
641+
* Extract the filename extension from the given Java resource path.
642+
* <p>Examples:
643+
* <ul>
644+
* <li>{@code "my/path/myfile.txt"} &rarr; {@code "txt"}
645+
* <li>{@code "myfile.txt"} &rarr; {@code "txt"}
646+
* <li>{@code "my/path/myfile."} &rarr; {@code ""}
647+
* <li>{@code "myfile"} &rarr; {@code null}
648+
* <li>{@code ""} &rarr; {@code null}
649+
* <li>{@code null} &rarr; {@code null}
650+
* </ul>
635651
* @param path the file path (may be {@code null})
636-
* @return the extracted filename extension, or {@code null} if none
652+
* @return the extracted filename extension (potentially an empty string), or
653+
* {@code null} if the provided path is {@code null} or does not contain a dot
654+
* ({@code "."})
637655
*/
638656
public static @Nullable String getFilenameExtension(@Nullable String path) {
639657
if (path == null) {

spring-core/src/test/java/org/springframework/util/StringUtilsTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 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.
@@ -359,11 +359,11 @@ void getFilename() {
359359
assertThat(StringUtils.getFilename(null)).isNull();
360360
assertThat(StringUtils.getFilename("")).isEmpty();
361361
assertThat(StringUtils.getFilename("myfile")).isEqualTo("myfile");
362-
assertThat(StringUtils.getFilename("mypath/myfile")).isEqualTo("myfile");
362+
assertThat(StringUtils.getFilename("my/path/myfile")).isEqualTo("myfile");
363363
assertThat(StringUtils.getFilename("myfile.")).isEqualTo("myfile.");
364364
assertThat(StringUtils.getFilename("mypath/myfile.")).isEqualTo("myfile.");
365365
assertThat(StringUtils.getFilename("myfile.txt")).isEqualTo("myfile.txt");
366-
assertThat(StringUtils.getFilename("mypath/myfile.txt")).isEqualTo("myfile.txt");
366+
assertThat(StringUtils.getFilename("my/path/myfile.txt")).isEqualTo("myfile.txt");
367367
}
368368

369369
@Test

0 commit comments

Comments
 (0)