diff --git a/spring-beans/src/main/java/org/springframework/beans/PropertyMatches.java b/spring-beans/src/main/java/org/springframework/beans/PropertyMatches.java index 2190b067494..7834bb8189d 100644 --- a/spring-beans/src/main/java/org/springframework/beans/PropertyMatches.java +++ b/spring-beans/src/main/java/org/springframework/beans/PropertyMatches.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -150,10 +150,10 @@ public abstract class PropertyMatches { * @return the distance value */ private static int calculateStringDistance(String s1, String s2) { - if (s1.length() == 0) { + if (s1.isEmpty()) { return s2.length(); } - if (s2.length() == 0) { + if (s2.isEmpty()) { return s1.length(); } int d[][] = new int[s1.length() + 1][s2.length() + 1]; diff --git a/spring-core/src/main/java/org/springframework/core/convert/support/StringToCharacterConverter.java b/spring-core/src/main/java/org/springframework/core/convert/support/StringToCharacterConverter.java index 7a076d335cc..5a8fd3c2bff 100644 --- a/spring-core/src/main/java/org/springframework/core/convert/support/StringToCharacterConverter.java +++ b/spring-core/src/main/java/org/springframework/core/convert/support/StringToCharacterConverter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,7 +28,7 @@ final class StringToCharacterConverter implements Converter { @Override public Character convert(String source) { - if (source.length() == 0) { + if (source.isEmpty()) { return null; } if (source.length() > 1) { diff --git a/spring-core/src/main/java/org/springframework/core/convert/support/StringToEnumConverterFactory.java b/spring-core/src/main/java/org/springframework/core/convert/support/StringToEnumConverterFactory.java index 865d1e83a32..aa4ad50d048 100644 --- a/spring-core/src/main/java/org/springframework/core/convert/support/StringToEnumConverterFactory.java +++ b/spring-core/src/main/java/org/springframework/core/convert/support/StringToEnumConverterFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -45,7 +45,7 @@ final class StringToEnumConverterFactory implements ConverterFactory= 0 ? mimeType.substring(0, index) : mimeType).trim(); - if (fullType.length() == 0) { + if (fullType.isEmpty()) { throw new InvalidMimeTypeException(mimeType, "'mimeType' must not be empty"); } diff --git a/spring-core/src/main/java/org/springframework/util/StringUtils.java b/spring-core/src/main/java/org/springframework/util/StringUtils.java index 5f9e785ded7..60384861a02 100644 --- a/spring-core/src/main/java/org/springframework/util/StringUtils.java +++ b/spring-core/src/main/java/org/springframework/util/StringUtils.java @@ -373,7 +373,7 @@ public abstract class StringUtils { * @param sub string to search for. Return 0 if this is {@code null}. */ public static int countOccurrencesOf(String str, String sub) { - if (str == null || sub == null || str.length() == 0 || sub.length() == 0) { + if (!hasLength(str) || !hasLength(sub)) { return 0; } int count = 0; @@ -515,7 +515,7 @@ public abstract class StringUtils { } private static String changeFirstCharacterCase(String str, boolean capitalize) { - if (str == null || str.length() == 0) { + if (!hasLength(str)) { return str; } else { diff --git a/spring-core/src/main/java/org/springframework/util/xml/AbstractStaxHandler.java b/spring-core/src/main/java/org/springframework/util/xml/AbstractStaxHandler.java index dfc7cee9709..bc688928931 100644 --- a/spring-core/src/main/java/org/springframework/util/xml/AbstractStaxHandler.java +++ b/spring-core/src/main/java/org/springframework/util/xml/AbstractStaxHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -223,8 +223,8 @@ abstract class AbstractStaxHandler implements ContentHandler, LexicalHandler { protected boolean isNamespaceDeclaration(QName qName) { String prefix = qName.getPrefix(); String localPart = qName.getLocalPart(); - return (XMLConstants.XMLNS_ATTRIBUTE.equals(localPart) && prefix.length() == 0) || - (XMLConstants.XMLNS_ATTRIBUTE.equals(prefix) && localPart.length() != 0); + return (XMLConstants.XMLNS_ATTRIBUTE.equals(localPart) && prefix.isEmpty()) || + (XMLConstants.XMLNS_ATTRIBUTE.equals(prefix) && !localPart.isEmpty()); } diff --git a/spring-expression/src/main/java/org/springframework/expression/common/TemplateAwareExpressionParser.java b/spring-expression/src/main/java/org/springframework/expression/common/TemplateAwareExpressionParser.java index f00055b6bf7..bbdc2bc7472 100644 --- a/spring-expression/src/main/java/org/springframework/expression/common/TemplateAwareExpressionParser.java +++ b/spring-expression/src/main/java/org/springframework/expression/common/TemplateAwareExpressionParser.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -79,7 +79,7 @@ public abstract class TemplateAwareExpressionParser implements ExpressionParser private Expression parseTemplate(String expressionString, ParserContext context) throws ParseException { - if (expressionString.length() == 0) { + if (expressionString.isEmpty()) { return new LiteralExpression(""); } Expression[] expressions = parseExpressions(expressionString, context); @@ -145,7 +145,7 @@ public abstract class TemplateAwareExpressionParser implements ExpressionParser suffixIndex); expr = expr.trim(); - if (expr.length() == 0) { + if (expr.isEmpty()) { throw new ParseException(expressionString, prefixIndex, "No expression defined within delimiter '" + prefix + suffix + "' at character " + prefixIndex); diff --git a/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/support/AbstractNamedValueMethodArgumentResolver.java b/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/support/AbstractNamedValueMethodArgumentResolver.java index 65904c3223d..65ac468a389 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/support/AbstractNamedValueMethodArgumentResolver.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/support/AbstractNamedValueMethodArgumentResolver.java @@ -142,7 +142,7 @@ public abstract class AbstractNamedValueMethodArgumentResolver implements Handle */ private NamedValueInfo updateNamedValueInfo(MethodParameter parameter, NamedValueInfo info) { String name = info.name; - if (info.name.length() == 0) { + if (info.name.isEmpty()) { name = parameter.getParameterName(); if (name == null) { throw new IllegalArgumentException("Name for argument type [" + parameter.getParameterType().getName() + diff --git a/spring-web/src/main/java/org/springframework/web/method/annotation/AbstractNamedValueMethodArgumentResolver.java b/spring-web/src/main/java/org/springframework/web/method/annotation/AbstractNamedValueMethodArgumentResolver.java index 5c08b33aacc..ba6b3c17032 100644 --- a/spring-web/src/main/java/org/springframework/web/method/annotation/AbstractNamedValueMethodArgumentResolver.java +++ b/spring-web/src/main/java/org/springframework/web/method/annotation/AbstractNamedValueMethodArgumentResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -160,7 +160,7 @@ public abstract class AbstractNamedValueMethodArgumentResolver implements Handle */ private NamedValueInfo updateNamedValueInfo(MethodParameter parameter, NamedValueInfo info) { String name = info.name; - if (info.name.length() == 0) { + if (info.name.isEmpty()) { name = parameter.getParameterName(); if (name == null) { throw new IllegalArgumentException( diff --git a/spring-web/src/main/java/org/springframework/web/util/patterns/PathPattern.java b/spring-web/src/main/java/org/springframework/web/util/patterns/PathPattern.java index 71d4e2be73a..6bf7fca5e6b 100644 --- a/spring-web/src/main/java/org/springframework/web/util/patterns/PathPattern.java +++ b/spring-web/src/main/java/org/springframework/web/util/patterns/PathPattern.java @@ -21,6 +21,7 @@ import java.util.HashMap; import java.util.Map; import org.springframework.util.PathMatcher; +import static org.springframework.util.StringUtils.hasLength; /** * Represents a parsed path pattern. Includes a chain of path elements @@ -132,9 +133,9 @@ public class PathPattern implements Comparable { */ public boolean matches(String path) { if (head == null) { - return (path == null) || (path.length() == 0); + return !hasLength(path); } - else if (path == null || path.length() == 0) { + else if (!hasLength(path)) { if (head instanceof WildcardTheRestPathElement || head instanceof CaptureTheRestPathElement) { path = ""; // Will allow CaptureTheRest to bind the variable to empty } @@ -152,9 +153,9 @@ public class PathPattern implements Comparable { */ public boolean matchStart(String path) { if (head == null) { - return (path == null || path.length() == 0); + return !hasLength(path); } - else if (path == null || path.length() == 0) { + else if (!hasLength(path)) { return true; } MatchingContext matchingContext = new MatchingContext(path, false); @@ -172,7 +173,7 @@ public class PathPattern implements Comparable { return matchingContext.getExtractedVariables(); } else { - if (path == null || path.length() == 0) { + if (!hasLength(path)) { return NO_VARIABLES_MAP; } else { @@ -434,15 +435,15 @@ public class PathPattern implements Comparable { */ public String combine(String pattern2string) { // If one of them is empty the result is the other. If both empty the result is "" - if (patternString == null || patternString.length() == 0) { - if (pattern2string == null || pattern2string.length() == 0) { + if (!hasLength(patternString)) { + if (!hasLength(pattern2string)) { return ""; } else { return pattern2string; } } - else if (pattern2string == null || pattern2string.length() == 0) { + else if (!hasLength(pattern2string)) { return patternString; } @@ -504,4 +505,4 @@ public class PathPattern implements Comparable { } } -} \ No newline at end of file +} diff --git a/spring-web/src/test/java/org/springframework/web/multipart/commons/CommonsMultipartResolverTests.java b/spring-web/src/test/java/org/springframework/web/multipart/commons/CommonsMultipartResolverTests.java index f59b1486c23..ebad85d5cb8 100644 --- a/spring-web/src/test/java/org/springframework/web/multipart/commons/CommonsMultipartResolverTests.java +++ b/spring-web/src/test/java/org/springframework/web/multipart/commons/CommonsMultipartResolverTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -258,13 +258,13 @@ public class CommonsMultipartResolverTests { binder.setBindEmptyMultipartFiles(false); String firstBound = mtb2.getField2(); binder.bind(request); - assertTrue(mtb2.getField2().length() > 0); + assertFalse(mtb2.getField2().isEmpty()); assertEquals(firstBound, mtb2.getField2()); request = resolver.resolveMultipart(originalRequest); binder.setBindEmptyMultipartFiles(true); binder.bind(request); - assertTrue(mtb2.getField2().length() == 0); + assertTrue(mtb2.getField2().isEmpty()); } @Test diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/AbstractNamedValueArgumentResolver.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/AbstractNamedValueArgumentResolver.java index d10b5102767..02975376098 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/AbstractNamedValueArgumentResolver.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/AbstractNamedValueArgumentResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -132,7 +132,7 @@ public abstract class AbstractNamedValueArgumentResolver implements HandlerMetho */ private NamedValueInfo updateNamedValueInfo(MethodParameter parameter, NamedValueInfo info) { String name = info.name; - if (info.name.length() == 0) { + if (info.name.isEmpty()) { name = parameter.getParameterName(); if (name == null) { String type = parameter.getNestedParameterType().getName(); diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestPartMethodArgumentResolver.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestPartMethodArgumentResolver.java index a46d3a99f38..bddd91907d3 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestPartMethodArgumentResolver.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestPartMethodArgumentResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -159,7 +159,7 @@ public class RequestPartMethodArgumentResolver extends AbstractMessageConverterM private String getPartName(MethodParameter methodParam, RequestPart requestPart) { String partName = (requestPart != null ? requestPart.name() : ""); - if (partName.length() == 0) { + if (partName.isEmpty()) { partName = methodParam.getParameterName(); if (partName == null) { throw new IllegalArgumentException("Request part name for argument type [" + diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/SseEmitter.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/SseEmitter.java index eeeb7dfd8c7..6a17f52ce4c 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/SseEmitter.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/SseEmitter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,6 +25,7 @@ import java.util.Set; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.http.server.ServerHttpResponse; +import org.springframework.util.StringUtils; /** * A specialization of {@link ResponseBodyEmitter} for sending @@ -234,7 +235,7 @@ public class SseEmitter extends ResponseBodyEmitter { @Override public Set build() { - if ((this.sb == null || this.sb.length() == 0) && this.dataToSend.isEmpty()) { + if (!StringUtils.hasLength(this.sb) && this.dataToSend.isEmpty()) { return Collections.emptySet(); } append("\n");