Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8274949: Use String.contains() instead of String.indexOf() in java.base #5559

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/java.base/share/classes/java/net/HttpCookie.java
Original file line number Diff line number Diff line change
Expand Up @@ -1078,13 +1078,13 @@ private static int guessCookieVersion(String header) {
int version = 0;

header = header.toLowerCase();
if (header.indexOf("expires=") != -1) {
if (header.contains("expires=")) {
// only netscape cookie using 'expires'
version = 0;
} else if (header.indexOf("version=") != -1) {
} else if (header.contains("version=")) {
// version is mandatory for rfc 2965/2109 cookie
version = 1;
} else if (header.indexOf("max-age") != -1) {
} else if (header.contains("max-age")) {
// rfc 2965/2109 use 'max-age'
version = 1;
} else if (startsWithIgnoreCase(header, SET_COOKIE2)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ public String getResponseMessage() throws IOException {
public long getHeaderFieldDate(String name, long Default) {
String dateString = getHeaderField(name);
try {
if (dateString.indexOf("GMT") == -1) {
if (!dateString.contains("GMT")) {
dateString = dateString+" GMT";
}
return Date.parse(dateString);
Expand Down
4 changes: 1 addition & 3 deletions src/java.base/share/classes/java/net/SocketPermission.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,10 @@
import java.io.ObjectOutputStream;
import java.io.ObjectStreamField;
import java.io.Serializable;
import java.net.InetAddress;
import java.security.AccessController;
import java.security.Permission;
import java.security.PermissionCollection;
import java.security.PrivilegedAction;
import java.security.Security;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Map;
Expand Down Expand Up @@ -333,7 +331,7 @@ private static String getHost(String host) {
ind = host.lastIndexOf(':');
host = "[" + host.substring(0, ind) + "]" +
host.substring(ind);
} else if (tokens == 8 && host.indexOf("::") == -1) {
} else if (tokens == 8 && !host.contains("::")) {
// IPv6 address only, not followed by port
host = "[" + host + "]";
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/java.base/share/classes/jdk/internal/jrtfs/JrtPath.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -485,7 +485,7 @@ private static String normalize(String path, int off) {
// Remove DotSlash(./) and resolve DotDot (..) components
private String getResolved() {
int length = path.length();
if (length == 0 || (path.indexOf("./") == -1 && path.charAt(length - 1) != '.')) {
if (length == 0 || (!path.contains("./") && path.charAt(length - 1) != '.')) {
return path;
} else {
return resolvePath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -556,11 +556,11 @@ public static void check(URL url) throws IOException {
// fallback to checkRead/checkConnect for pre 1.2
// security managers
if ((perm instanceof java.io.FilePermission) &&
perm.getActions().indexOf("read") != -1) {
perm.getActions().contains("read")) {
security.checkRead(perm.getName());
} else if ((perm instanceof
java.net.SocketPermission) &&
perm.getActions().indexOf("connect") != -1) {
perm.getActions().contains("connect")) {
URL locUrl = url;
if (urlConnection instanceof JarURLConnection) {
locUrl = ((JarURLConnection)urlConnection).getJarFileURL();
Expand Down Expand Up @@ -1233,7 +1233,7 @@ Resource getResource(final String name, boolean check) {
URLClassPath.check(url);

final File file;
if (name.indexOf("..") != -1) {
if (name.contains("..")) {
file = (new File(dir, name.replace('/', File.separatorChar)))
.getCanonicalFile();
if ( !((file.getPath()).startsWith(dir.getPath())) ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public static HttpCapture getCapture(java.net.URL url) {
if (p.matcher(s).find()) {
String f = capFiles.get(i);
File fi;
if (f.indexOf("%d") >= 0) {
if (f.contains("%d")) {
java.util.Random rand = new java.util.Random();
do {
String f2 = f.replace("%d", Integer.toString(rand.nextInt()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ private void checkURLSpoofing(HostnameVerifier hostnameVerifier)
// ignore
}

if ((cipher != null) && (cipher.indexOf("_anon_") != -1)) {
if ((cipher != null) && (cipher.contains("_anon_"))) {
return;
} else if ((hostnameVerifier != null) &&
(hostnameVerifier.verify(host, session))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@ private void addGrantEntry(PolicyParser.GrantEntry ge,
+ SELF;
}
// check for self
if (pe.name != null && pe.name.indexOf(SELF) != -1) {
if (pe.name != null && pe.name.contains(SELF)) {
// Create a "SelfPermission" , it could be an
// an unresolved permission which will be resolved
// when implies is called
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public class RSAPSSSignature extends SignatureSpi {
private boolean isDigestEqual(String stdAlg, String givenAlg) {
if (stdAlg == null || givenAlg == null) return false;

if (givenAlg.indexOf("-") != -1) {
if (givenAlg.contains("-")) {
return stdAlg.equalsIgnoreCase(givenAlg);
} else {
if (stdAlg.equals("SHA-1")) {
Expand Down
9 changes: 4 additions & 5 deletions src/java.base/share/classes/sun/security/rsa/RSAUtil.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -25,7 +25,6 @@

package sun.security.rsa;

import java.io.IOException;
import java.security.*;
import java.security.spec.*;
import sun.security.util.ObjectIdentifier;
Expand Down Expand Up @@ -61,9 +60,9 @@ public static KeyType lookup(String name) throws ProviderException {

// match loosely in order to work with 3rd party providers which
// may not follow the standard names
if (name.indexOf("PSS") != -1) {
if (name.contains("PSS")) {
return PSS;
} else if (name.indexOf("RSA") != -1) {
} else if (name.contains("RSA")) {
return RSA;
} else { // no match
throw new ProviderException("Unsupported algorithm " + name);
Expand Down Expand Up @@ -151,7 +150,7 @@ public static Object[] getTypeAndParamSpec(AlgorithmId algid)
} catch (ProviderException pe) {
// accommodate RSA keys encoded with various RSA signature oids
// for backward compatibility
if (algName.indexOf("RSA") != -1) {
if (algName.contains("RSA")) {
result[0] = KeyType.RSA;
} else {
// pass it up
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1471,10 +1471,10 @@ private void doGenCert(String alias, String sigAlgName, InputStream in, PrintStr
if (s == null) break;
// OpenSSL does not use NEW
//if (s.startsWith("-----BEGIN NEW CERTIFICATE REQUEST-----")) {
if (s.startsWith("-----BEGIN") && s.indexOf("REQUEST") >= 0) {
if (s.startsWith("-----BEGIN") && s.contains("REQUEST")) {
canRead = true;
//} else if (s.startsWith("-----END NEW CERTIFICATE REQUEST-----")) {
} else if (s.startsWith("-----END") && s.indexOf("REQUEST") >= 0) {
} else if (s.startsWith("-----END") && s.contains("REQUEST")) {
break;
} else if (canRead) {
sb.append(s);
Expand Down
6 changes: 3 additions & 3 deletions src/java.base/share/classes/sun/security/util/Debug.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -161,10 +161,10 @@ public static boolean isOn(String option)
if (args == null)
return false;
else {
if (args.indexOf("all") != -1)
if (args.contains("all"))
return true;
else
return (args.indexOf(option) != -1);
return (args.contains(option));
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/java.base/share/classes/sun/security/util/SignatureUtil.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -54,7 +54,7 @@ public class SignatureUtil {
* form of an OID, or the OID value if no match is found.
*/
private static String checkName(String algName) {
if (algName.indexOf(".") == -1) {
if (!algName.contains(".")) {
return algName;
} else {
// convert oid to String
Expand Down Expand Up @@ -100,7 +100,7 @@ public static AlgorithmParameterSpec getParamSpec(String sigName,
// AlgorithmParameters.getAlgorithm() may returns oid if it's
// created during DER decoding. Convert to use the standard name
// before passing it to RSAUtil
if (params.getAlgorithm().indexOf(".") != -1) {
if (params.getAlgorithm().contains(".")) {
try {
params = createAlgorithmParameters(sigName,
params.getEncoded());
Expand All @@ -109,9 +109,9 @@ public static AlgorithmParameterSpec getParamSpec(String sigName,
}
}

if (sigName.indexOf("RSA") != -1) {
if (sigName.contains("RSA")) {
paramSpec = RSAUtil.getParamSpec(params);
} else if (sigName.indexOf("ECDSA") != -1) {
} else if (sigName.contains("ECDSA")) {
try {
paramSpec = params.getParameterSpec(ECParameterSpec.class);
} catch (Exception e) {
Expand Down Expand Up @@ -141,11 +141,11 @@ public static AlgorithmParameterSpec getParamSpec(String sigName,

if (paramBytes != null) {
sigName = checkName(sigName).toUpperCase(Locale.ENGLISH);
if (sigName.indexOf("RSA") != -1) {
if (sigName.contains("RSA")) {
AlgorithmParameters params =
createAlgorithmParameters(sigName, paramBytes);
paramSpec = RSAUtil.getParamSpec(params);
} else if (sigName.indexOf("ECDSA") != -1) {
} else if (sigName.contains("ECDSA")) {
try {
Provider p = Signature.getInstance(sigName).getProvider();
paramSpec = ECUtil.getECParameterSpec(p, paramBytes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ private static ObjectIdentifier algOID(String name) throws IOException {
}

// unknown algorithm oids
if (name.indexOf(".") == -1) {
if (!name.contains(".")) {
// see if there is a matching oid string alias mapping from
// 3rd party providers
name = name.toUpperCase(Locale.ENGLISH);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -83,7 +83,7 @@ public static List<String> filterTags(List<LanguageRange> priorityList,
for (LanguageRange lr : priorityList) {
String range = lr.getRange();
if (range.startsWith("*-")
|| range.indexOf("-*") != -1) { // Extended range
|| range.contains("-*")) { // Extended range
if (mode == AUTOSELECT_FILTERING) {
return filterExtended(priorityList, tags);
} else if (mode == MAP_EXTENDED_RANGES) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,11 @@ private JarFile getCachedJarFile(URL url) {
// fallback to checkRead/checkConnect for pre 1.2
// security managers
if ((perm instanceof java.io.FilePermission) &&
perm.getActions().indexOf("read") != -1) {
perm.getActions().contains("read")) {
sm.checkRead(perm.getName());
} else if ((perm instanceof
java.net.SocketPermission) &&
perm.getActions().indexOf("connect") != -1) {
perm.getActions().contains("connect")) {
sm.checkConnect(url.getHost(), url.getPort());
} else {
throw se;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,11 @@ private JarFile getCachedJarFile(URL url) {
// fallback to checkRead/checkConnect for pre 1.2
// security managers
if ((perm instanceof java.io.FilePermission) &&
perm.getActions().indexOf("read") != -1) {
perm.getActions().contains("read")) {
sm.checkRead(perm.getName());
} else if ((perm instanceof
java.net.SocketPermission) &&
perm.getActions().indexOf("connect") != -1) {
perm.getActions().contains("connect")) {
sm.checkConnect(url.getHost(), url.getPort());
} else {
throw se;
Expand Down