Browse Source

Insist on using `CollectionUtils.isEmpty()` and `StringUtils.hasLength()`

search `(\w+) != null && !(\1).isEmpty\(\)`
pull/32804/head
Yanming Zhou 2 years ago committed by Juergen Hoeller
parent
commit
a02861f7db
  1. 4
      spring-core/src/main/java/org/springframework/util/MultiValueMapAdapter.java
  2. 3
      spring-test/src/test/java/org/springframework/test/web/servlet/samples/client/standalone/MultipartControllerTests.java
  3. 3
      spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/MultipartControllerTests.java
  4. 2
      spring-webmvc/src/main/java/org/springframework/web/servlet/support/AbstractFlashMapManager.java

4
spring-core/src/main/java/org/springframework/util/MultiValueMapAdapter.java

@ -59,7 +59,7 @@ public class MultiValueMapAdapter<K, V> implements MultiValueMap<K, V>, Serializ @@ -59,7 +59,7 @@ public class MultiValueMapAdapter<K, V> implements MultiValueMap<K, V>, Serializ
@Nullable
public V getFirst(K key) {
List<V> values = this.targetMap.get(key);
return (values != null && !values.isEmpty() ? values.get(0) : null);
return (!CollectionUtils.isEmpty(values) ? values.get(0) : null);
}
@Override
@ -95,7 +95,7 @@ public class MultiValueMapAdapter<K, V> implements MultiValueMap<K, V>, Serializ @@ -95,7 +95,7 @@ public class MultiValueMapAdapter<K, V> implements MultiValueMap<K, V>, Serializ
public Map<K, V> toSingleValueMap() {
Map<K, V> singleValueMap = CollectionUtils.newLinkedHashMap(this.targetMap.size());
this.targetMap.forEach((key, values) -> {
if (values != null && !values.isEmpty()) {
if (!CollectionUtils.isEmpty(values)) {
singleValueMap.put(key, values.get(0));
}
});

3
spring-test/src/test/java/org/springframework/test/web/servlet/samples/client/standalone/MultipartControllerTests.java

@ -36,6 +36,7 @@ import org.springframework.http.client.MultipartBodyBuilder; @@ -36,6 +36,7 @@ import org.springframework.http.client.MultipartBodyBuilder;
import org.springframework.stereotype.Controller;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.test.web.servlet.client.MockMvcWebTestClient;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;
@ -271,7 +272,7 @@ public class MultipartControllerTests { @@ -271,7 +272,7 @@ public class MultipartControllerTests {
public String processMultipartFileList(@RequestParam(required = false) List<MultipartFile> file,
@RequestPart(required = false) Map<String, String> json) throws IOException {
if (file != null && !file.isEmpty()) {
if (!CollectionUtils.isEmpty(file)) {
byte[] content = file.get(0).getBytes();
assertThat(file.get(1).getBytes()).isEqualTo(content);
}

3
spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/MultipartControllerTests.java

@ -39,6 +39,7 @@ import org.springframework.mock.web.MockPart; @@ -39,6 +39,7 @@ import org.springframework.mock.web.MockPart;
import org.springframework.stereotype.Controller;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMultipartHttpServletRequestBuilder;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
@ -271,7 +272,7 @@ class MultipartControllerTests { @@ -271,7 +272,7 @@ class MultipartControllerTests {
public String processMultipartFileList(@RequestParam(required = false) List<MultipartFile> file,
@RequestPart(required = false) Map<String, String> json) throws IOException {
if (file != null && !file.isEmpty()) {
if (!CollectionUtils.isEmpty(file)) {
byte[] content = file.get(0).getBytes();
assertThat(file.get(1).getBytes()).isEqualTo(content);
}

2
spring-webmvc/src/main/java/org/springframework/web/servlet/support/AbstractFlashMapManager.java

@ -220,7 +220,7 @@ public abstract class AbstractFlashMapManager implements FlashMapManager { @@ -220,7 +220,7 @@ public abstract class AbstractFlashMapManager implements FlashMapManager {
@Nullable
private String decodeAndNormalizePath(@Nullable String path, HttpServletRequest request) {
if (path != null && !path.isEmpty()) {
if (StringUtils.hasLength(path)) {
path = getUrlPathHelper().decodeRequestString(request, path);
if (path.charAt(0) != '/') {
String requestUri = getUrlPathHelper().getRequestUri(request);

Loading…
Cancel
Save