8215281: Use String.isEmpty() when applicable in java.base

Reviewed-by: dfuchs, alanb
This commit is contained in:
Claes Redestad 2018-12-13 15:31:05 +01:00
parent c998ead188
commit a3df1d618e
155 changed files with 340 additions and 382 deletions

View file

@ -149,7 +149,7 @@ public final class HttpCookie implements Cloneable {
*/
HttpCookie(String name, String value, String header, long creationTime) {
name = name.trim();
if (name.length() == 0 || !isToken(name) || name.charAt(0) == '$') {
if (name.isEmpty() || !isToken(name) || name.charAt(0) == '$') {
throw new IllegalArgumentException("Illegal cookie name");
}

View file

@ -433,7 +433,7 @@ class Inet6Address extends InetAddress {
NetworkInterface nif)
throws UnknownHostException
{
if (host != null && host.length() > 0 && host.charAt(0) == '[') {
if (host != null && !host.isEmpty() && host.charAt(0) == '[') {
if (host.charAt(host.length()-1) == ']') {
host = host.substring(1, host.length() -1);
}
@ -466,7 +466,7 @@ class Inet6Address extends InetAddress {
int scope_id)
throws UnknownHostException
{
if (host != null && host.length() > 0 && host.charAt(0) == '[') {
if (host != null && !host.isEmpty() && host.charAt(0) == '[') {
if (host.charAt(host.length()-1) == ']') {
host = host.substring(1, host.length() -1);
}
@ -601,7 +601,7 @@ class Inet6Address extends InetAddress {
boolean scope_ifname_set = gf.get("scope_ifname_set", false);
String ifname = (String)gf.get("ifname", null);
if (ifname != null && !"".equals (ifname)) {
if (ifname != null && !ifname.isEmpty()) {
try {
scope_ifname = NetworkInterface.getByName(ifname);
if (scope_ifname == null) {

View file

@ -1187,7 +1187,7 @@ class InetAddress implements java.io.Serializable {
*/
public static InetAddress getByAddress(String host, byte[] addr)
throws UnknownHostException {
if (host != null && host.length() > 0 && host.charAt(0) == '[') {
if (host != null && !host.isEmpty() && host.charAt(0) == '[') {
if (host.charAt(host.length()-1) == ']') {
host = host.substring(1, host.length() -1);
}
@ -1301,7 +1301,7 @@ class InetAddress implements java.io.Serializable {
private static InetAddress[] getAllByName(String host, InetAddress reqAddr)
throws UnknownHostException {
if (host == null || host.length() == 0) {
if (host == null || host.isEmpty()) {
InetAddress[] ret = new InetAddress[1];
ret[0] = impl.loopbackAddress();
return ret;

View file

@ -460,7 +460,7 @@ public final class SocketPermission extends Permission
}
return;
} else {
if (host.length() > 0) {
if (!host.isEmpty()) {
// see if we are being initialized with an IP address.
char ch = host.charAt(0);
if (ch == ':' || Character.digit(ch, 16) != -1) {
@ -705,8 +705,7 @@ public final class SocketPermission extends Permission
.orElse(b);
}
return cdomain.length() != 0 && hdomain.length() != 0
&& cdomain.equals(hdomain);
return !cdomain.isEmpty() && !hdomain.isEmpty() && cdomain.equals(hdomain);
}
private boolean authorized(String cname, byte[] addr) {

View file

@ -1959,10 +1959,8 @@ public final class URI
throws URISyntaxException
{
if (scheme != null) {
if ((path != null)
&& ((path.length() > 0) && (path.charAt(0) != '/')))
throw new URISyntaxException(s,
"Relative path in absolute URI");
if (path != null && !path.isEmpty() && path.charAt(0) != '/')
throw new URISyntaxException(s, "Relative path in absolute URI");
}
}
@ -2163,7 +2161,7 @@ public final class URI
ru.port = base.port;
String cp = (child.path == null) ? "" : child.path;
if ((cp.length() > 0) && (cp.charAt(0) == '/')) {
if (!cp.isEmpty() && cp.charAt(0) == '/') {
// 5.2 (5): Child path is absolute
ru.path = child.path;
} else {
@ -2187,7 +2185,7 @@ public final class URI
// o.w., return a new URI containing the normalized path.
//
private static URI normalize(URI u) {
if (u.isOpaque() || (u.path == null) || (u.path.length() == 0))
if (u.isOpaque() || u.path == null || u.path.isEmpty())
return u;
String np = normalize(u.path);

View file

@ -1513,7 +1513,7 @@ public final class URL implements java.io.Serializable {
String ref = (String)gf.get("ref", null);
int hashCode = gf.get("hashCode", -1);
if (authority == null
&& ((host != null && host.length() > 0) || port != -1)) {
&& ((host != null && !host.isEmpty()) || port != -1)) {
if (host == null)
host = "";
authority = (port == -1) ? host : host + ":" + port;
@ -1560,7 +1560,7 @@ public final class URL implements java.io.Serializable {
// Construct authority part
if (authority == null
&& ((host != null && host.length() > 0) || port != -1)) {
&& ((host != null && !host.isEmpty()) || port != -1)) {
if (host == null)
host = "";
authority = (port == -1) ? host : host + ":" + port;
@ -1716,7 +1716,7 @@ final class UrlDeserializedState {
// pre-compute length of StringBuffer
int len = protocol.length() + 1;
if (authority != null && authority.length() > 0)
if (authority != null && !authority.isEmpty())
len += 2 + authority.length();
if (file != null) {
len += file.length();
@ -1726,7 +1726,7 @@ final class UrlDeserializedState {
StringBuilder result = new StringBuilder(len);
result.append(protocol);
result.append(":");
if (authority != null && authority.length() > 0) {
if (authority != null && !authority.isEmpty()) {
result.append("//");
result.append(authority);
}

View file

@ -743,7 +743,7 @@ public class URLClassLoader extends SecureClassLoader implements Closeable {
locUrl = ((JarURLConnection)urlConnection).getJarFileURL();
}
String host = locUrl.getHost();
if (host != null && (host.length() > 0))
if (host != null && !host.isEmpty())
p = new SocketPermission(host,
SecurityConstants.SOCKET_CONNECT_ACCEPT_ACTION);
}

View file

@ -133,7 +133,7 @@ public class URLDecoder {
* @since 1.4
*/
public static String decode(String s, String enc) throws UnsupportedEncodingException {
if (enc.length() == 0) {
if (enc.isEmpty()) {
throw new UnsupportedEncodingException ("URLDecoder: empty string enc parameter");
}

View file

@ -409,7 +409,7 @@ public final class URLPermission extends Permission {
char c = methods.charAt(i);
if (c == ',') {
String s = b.toString();
if (s.length() > 0)
if (!s.isEmpty())
l.add(s);
b = new StringBuilder();
} else if (c == ' ' || c == '\t') {
@ -423,7 +423,7 @@ public final class URLPermission extends Permission {
}
}
String s = b.toString();
if (s.length() > 0)
if (!s.isEmpty())
l.add(s);
return l;
}
@ -448,7 +448,7 @@ public final class URLPermission extends Permission {
b.append(c);
} else if (c == ',') {
String s = b.toString();
if (s.length() > 0)
if (!s.isEmpty())
l.add(s);
b = new StringBuilder();
capitalizeNext = true;
@ -458,7 +458,7 @@ public final class URLPermission extends Permission {
}
}
String s = b.toString();
if (s.length() > 0)
if (!s.isEmpty())
l.add(s);
return l;
}

View file

@ -235,7 +235,7 @@ public abstract class URLStreamHandler {
start = i;
// If the authority is defined then the path is defined by the
// spec only; See RFC 2396 Section 5.2.4.
if (authority != null && authority.length() > 0)
if (authority != null && !authority.isEmpty())
path = "";
}
@ -247,7 +247,7 @@ public abstract class URLStreamHandler {
if (start < limit) {
if (spec.charAt(start) == '/') {
path = spec.substring(start, limit);
} else if (path != null && path.length() > 0) {
} else if (path != null && !path.isEmpty()) {
isRelPath = true;
int ind = path.lastIndexOf('/');
String separator = "";
@ -483,11 +483,11 @@ public abstract class URLStreamHandler {
String s;
return u.getProtocol()
+ ':'
+ (((s = u.getAuthority()) != null && s.length() > 0)
+ ((s = u.getAuthority()) != null && !s.isEmpty()
? "//" + s : "")
+ (((s = u.getPath()) != null) ? s : "")
+ (((s = u.getQuery()) != null) ? '?' + s : "")
+ (((s = u.getRef()) != null) ? '#' + s : "");
+ ((s = u.getPath()) != null ? s : "")
+ ((s = u.getQuery()) != null ? '?' + s : "")
+ ((s = u.getRef()) != null ? '#' + s : "");
}
/**
@ -544,7 +544,7 @@ public abstract class URLStreamHandler {
*/
String authority = null;
String userInfo = null;
if (host != null && host.length() != 0) {
if (host != null && !host.isEmpty()) {
authority = (port == -1) ? host : host + ":" + port;
int at = host.lastIndexOf('@');
if (at != -1) {