8273261: Replace 'while' cycles with iterator with enhanced-for in java.base

Reviewed-by: dfuchs, rriggs, iris, mullan
This commit is contained in:
Andrey Turbanov 2021-09-23 20:04:36 +00:00 committed by Roger Riggs
parent 0aa63feca8
commit 56b8b35286
11 changed files with 43 additions and 92 deletions

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -314,9 +314,7 @@ public class X509CRLSelector implements CRLSelector {
throws IOException throws IOException
{ {
HashSet<Object> namesCopy = new HashSet<>(); HashSet<Object> namesCopy = new HashSet<>();
Iterator<?> i = names.iterator(); for (Object nameObject : names) {
while (i.hasNext()) {
Object nameObject = i.next();
if (!(nameObject instanceof byte []) && if (!(nameObject instanceof byte []) &&
!(nameObject instanceof String)) !(nameObject instanceof String))
throw new IOException("name not byte array or String"); throw new IOException("name not byte array or String");
@ -573,9 +571,8 @@ public class X509CRLSelector implements CRLSelector {
sb.append("X509CRLSelector: [\n"); sb.append("X509CRLSelector: [\n");
if (issuerNames != null) { if (issuerNames != null) {
sb.append(" IssuerNames:\n"); sb.append(" IssuerNames:\n");
Iterator<Object> i = issuerNames.iterator(); for (Object issuerName : issuerNames)
while (i.hasNext()) sb.append(" " + issuerName + "\n");
sb.append(" " + i.next() + "\n");
} }
if (minCRL != null) if (minCRL != null)
sb.append(" minCRLNumber: " + minCRL + "\n"); sb.append(" minCRLNumber: " + minCRL + "\n");

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -1778,9 +1778,7 @@ public class X509CertSelector implements CertSelector {
+ String.valueOf(matchAllSubjectAltNames) + "\n"); + String.valueOf(matchAllSubjectAltNames) + "\n");
if (subjectAlternativeNames != null) { if (subjectAlternativeNames != null) {
sb.append(" SubjectAlternativeNames:\n"); sb.append(" SubjectAlternativeNames:\n");
Iterator<List<?>> i = subjectAlternativeNames.iterator(); for (List<?> list : subjectAlternativeNames) {
while (i.hasNext()) {
List<?> list = i.next();
sb.append(" type " + list.get(0) + sb.append(" type " + list.get(0) +
", name " + list.get(1) + "\n"); ", name " + list.get(1) + "\n");
} }
@ -1823,9 +1821,8 @@ public class X509CertSelector implements CertSelector {
} }
if (pathToGeneralNames != null) { if (pathToGeneralNames != null) {
sb.append(" Path to names:\n"); sb.append(" Path to names:\n");
Iterator<GeneralNameInterface> i = pathToGeneralNames.iterator(); for (GeneralNameInterface pathToGeneralName : pathToGeneralNames) {
while (i.hasNext()) { sb.append(" " + pathToGeneralName + "\n");
sb.append(" " + i.next() + "\n");
} }
} }
sb.append("]"); sb.append("]");
@ -2399,10 +2396,8 @@ public class X509CertSelector implements CertSelector {
} }
if ((debug != null) && Debug.isOn("certpath")) { if ((debug != null) && Debug.isOn("certpath")) {
debug.println("X509CertSelector.match pathToNames:\n"); debug.println("X509CertSelector.match pathToNames:\n");
Iterator<GeneralNameInterface> i = for (GeneralNameInterface pathToGeneralName : pathToGeneralNames) {
pathToGeneralNames.iterator(); debug.println(" " + pathToGeneralName + "\n");
while (i.hasNext()) {
debug.println(" " + i.next() + "\n");
} }
} }
@ -2439,9 +2434,7 @@ public class X509CertSelector implements CertSelector {
for (Iterator<GeneralSubtree> t = excluded.iterator(); t.hasNext(); ) { for (Iterator<GeneralSubtree> t = excluded.iterator(); t.hasNext(); ) {
GeneralSubtree tree = t.next(); GeneralSubtree tree = t.next();
GeneralNameInterface excludedName = tree.getName().getName(); GeneralNameInterface excludedName = tree.getName().getName();
Iterator<GeneralNameInterface> i = pathToGeneralNames.iterator(); for (GeneralNameInterface pathToName : pathToGeneralNames) {
while (i.hasNext()) {
GeneralNameInterface pathToName = i.next();
if (excludedName.getType() == pathToName.getType()) { if (excludedName.getType() == pathToName.getType()) {
switch (pathToName.constrains(excludedName)) { switch (pathToName.constrains(excludedName)) {
case GeneralNameInterface.NAME_WIDENS: case GeneralNameInterface.NAME_WIDENS:
@ -2468,9 +2461,7 @@ public class X509CertSelector implements CertSelector {
* If not, return false. However, if no subtrees of a given type * If not, return false. However, if no subtrees of a given type
* are listed, all names of that type are permitted. * are listed, all names of that type are permitted.
*/ */
Iterator<GeneralNameInterface> i = pathToGeneralNames.iterator(); for (GeneralNameInterface pathToName : pathToGeneralNames) {
while (i.hasNext()) {
GeneralNameInterface pathToName = i.next();
Iterator<GeneralSubtree> t = permitted.iterator(); Iterator<GeneralSubtree> t = permitted.iterator();
boolean permittedNameFound = false; boolean permittedNameFound = false;
boolean nameTypeFound = false; boolean nameTypeFound = false;

View file

@ -158,9 +158,7 @@ public class AttributedString {
runAttributes[0] = newRunAttributes; runAttributes[0] = newRunAttributes;
runAttributeValues[0] = newRunAttributeValues; runAttributeValues[0] = newRunAttributeValues;
Iterator<? extends Map.Entry<? extends Attribute, ?>> iterator = attributes.entrySet().iterator(); for (Map.Entry<? extends Attribute, ?> entry : attributes.entrySet()) {
while (iterator.hasNext()) {
Map.Entry<? extends Attribute, ?> entry = iterator.next();
newRunAttributes.addElement(entry.getKey()); newRunAttributes.addElement(entry.getKey());
newRunAttributeValues.addElement(entry.getValue()); newRunAttributeValues.addElement(entry.getValue());
} }
@ -264,9 +262,7 @@ public class AttributedString {
// Get and set attribute runs for each attribute name. Need to // Get and set attribute runs for each attribute name. Need to
// scan from the top of the text so that we can discard any // scan from the top of the text so that we can discard any
// Annotation that is no longer applied to a subset text segment. // Annotation that is no longer applied to a subset text segment.
Iterator<Attribute> itr = keys.iterator(); for (Attribute attributeKey : keys) {
while (itr.hasNext()) {
Attribute attributeKey = itr.next();
text.setIndex(textBeginIndex); text.setIndex(textBeginIndex);
while (text.getIndex() < endIndex) { while (text.getIndex() < endIndex) {
int start = text.getRunStart(attributeKey); int start = text.getRunStart(attributeKey);
@ -388,10 +384,7 @@ public class AttributedString {
int beginRunIndex = ensureRunBreak(beginIndex); int beginRunIndex = ensureRunBreak(beginIndex);
int endRunIndex = ensureRunBreak(endIndex); int endRunIndex = ensureRunBreak(endIndex);
Iterator<? extends Map.Entry<? extends Attribute, ?>> iterator = for (Map.Entry<? extends Attribute, ?> entry : attributes.entrySet()) {
attributes.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<? extends Attribute, ?> entry = iterator.next();
addAttributeRunData(entry.getKey(), entry.getValue(), beginRunIndex, endRunIndex); addAttributeRunData(entry.getKey(), entry.getValue(), beginRunIndex, endRunIndex);
} }
} }
@ -656,9 +649,7 @@ public class AttributedString {
// returns whether all specified attributes have equal values in the runs with the given indices // returns whether all specified attributes have equal values in the runs with the given indices
private boolean attributeValuesMatch(Set<? extends Attribute> attributes, int runIndex1, int runIndex2) { private boolean attributeValuesMatch(Set<? extends Attribute> attributes, int runIndex1, int runIndex2) {
Iterator<? extends Attribute> iterator = attributes.iterator(); for (Attribute key : attributes) {
while (iterator.hasNext()) {
Attribute key = iterator.next();
if (!valuesMatch(getAttribute(key, runIndex1), getAttribute(key, runIndex2))) { if (!valuesMatch(getAttribute(key, runIndex1), getAttribute(key, runIndex2))) {
return false; return false;
} }
@ -706,11 +697,8 @@ public class AttributedString {
if (attrs != null && (size = attrs.size()) > 0) { if (attrs != null && (size = attrs.size()) > 0) {
Vector<Attribute> runAttrs = new Vector<>(size); Vector<Attribute> runAttrs = new Vector<>(size);
Vector<Object> runValues = new Vector<>(size); Vector<Object> runValues = new Vector<>(size);
Iterator<Map.Entry<Attribute, Object>> iterator = attrs.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<Attribute, Object> entry = iterator.next();
for (Map.Entry<Attribute, Object> entry : attrs.entrySet()) {
runAttrs.add(entry.getKey()); runAttrs.add(entry.getKey());
runValues.add(entry.getValue()); runValues.add(entry.getValue());
} }

View file

@ -145,9 +145,7 @@ public final class PrivateCredentialPermission extends Permission {
} else { } else {
this.credOwners = new CredOwner[principals.size()]; this.credOwners = new CredOwner[principals.size()];
int index = 0; int index = 0;
Iterator<Principal> i = principals.iterator(); for (Principal p : principals) {
while (i.hasNext()) {
Principal p = i.next();
this.credOwners[index++] = new CredOwner this.credOwners[index++] = new CredOwner
(p.getClass().getName(), (p.getClass().getName(),
p.getName()); p.getName());

View file

@ -886,18 +886,14 @@ public final class Subject implements java.io.Serializable {
String suffix = ""; String suffix = "";
synchronized(principals) { synchronized(principals) {
Iterator<Principal> pI = principals.iterator(); for (Principal p : principals) {
while (pI.hasNext()) {
Principal p = pI.next();
suffix = suffix + ResourcesMgr.getString(".Principal.") + suffix = suffix + ResourcesMgr.getString(".Principal.") +
p.toString() + ResourcesMgr.getString("NEWLINE"); p.toString() + ResourcesMgr.getString("NEWLINE");
} }
} }
synchronized(pubCredentials) { synchronized(pubCredentials) {
Iterator<Object> pI = pubCredentials.iterator(); for (Object o : pubCredentials) {
while (pI.hasNext()) {
Object o = pI.next();
suffix = suffix + suffix = suffix +
ResourcesMgr.getString(".Public.Credential.") + ResourcesMgr.getString(".Public.Credential.") +
o.toString() + ResourcesMgr.getString("NEWLINE"); o.toString() + ResourcesMgr.getString("NEWLINE");
@ -951,17 +947,14 @@ public final class Subject implements java.io.Serializable {
int hashCode = 0; int hashCode = 0;
synchronized(principals) { synchronized(principals) {
Iterator<Principal> pIterator = principals.iterator(); for (Principal p : principals) {
while (pIterator.hasNext()) {
Principal p = pIterator.next();
hashCode ^= p.hashCode(); hashCode ^= p.hashCode();
} }
} }
synchronized(pubCredentials) { synchronized(pubCredentials) {
Iterator<Object> pubCIterator = pubCredentials.iterator(); for (Object pubCredential : pubCredentials) {
while (pubCIterator.hasNext()) { hashCode ^= getCredHashCode(pubCredential);
hashCode ^= getCredHashCode(pubCIterator.next());
} }
} }
return hashCode; return hashCode;
@ -1308,9 +1301,8 @@ public final class Subject implements java.io.Serializable {
}); });
} }
Iterator<?> ce = c.iterator(); for (Object o : c) {
while (ce.hasNext()) { if (next.equals(o)) {
if (next.equals(ce.next())) {
e.remove(); e.remove();
modified = true; modified = true;
break; break;

View file

@ -263,9 +263,8 @@ public class JarIndex {
bw.write(jar + "\n"); bw.write(jar + "\n");
List<String> jarlist = jarMap.get(jar); List<String> jarlist = jarMap.get(jar);
if (jarlist != null) { if (jarlist != null) {
Iterator<String> listitr = jarlist.iterator(); for (String s : jarlist) {
while(listitr.hasNext()) { bw.write(s + "\n");
bw.write(listitr.next() + "\n");
} }
} }
bw.write("\n"); bw.write("\n");
@ -320,14 +319,10 @@ public class JarIndex {
* *
*/ */
public void merge(JarIndex toIndex, String path) { public void merge(JarIndex toIndex, String path) {
Iterator<Map.Entry<String, List<String>>> itr = indexMap.entrySet().iterator(); for (Map.Entry<String, List<String>> e : indexMap.entrySet()) {
while(itr.hasNext()) {
Map.Entry<String, List<String>> e = itr.next();
String packageName = e.getKey(); String packageName = e.getKey();
List<String> from_list = e.getValue(); List<String> from_list = e.getValue();
Iterator<String> listItr = from_list.iterator(); for (String jarName : from_list) {
while(listItr.hasNext()) {
String jarName = listItr.next();
if (path != null) { if (path != null) {
jarName = path.concat(jarName); jarName = path.concat(jarName);
} }

View file

@ -46,7 +46,6 @@ import java.net.Proxy;
import java.net.ProxySelector; import java.net.ProxySelector;
import java.util.List; import java.util.List;
import java.util.StringTokenizer; import java.util.StringTokenizer;
import java.util.Iterator;
import java.security.Permission; import java.security.Permission;
import java.util.Properties; import java.util.Properties;
import sun.net.NetworkClient; import sun.net.NetworkClient;
@ -250,9 +249,8 @@ public class FtpURLConnection extends URLConnection {
} catch (IllegalArgumentException iae) { } catch (IllegalArgumentException iae) {
throw new IOException("Failed to select a proxy", iae); throw new IOException("Failed to select a proxy", iae);
} }
final Iterator<Proxy> it = proxies.iterator(); for (Proxy proxy : proxies) {
while (it.hasNext()) { p = proxy;
p = it.next();
if (p == null || p == Proxy.NO_PROXY || if (p == null || p == Proxy.NO_PROXY ||
p.type() == Proxy.Type.SOCKS) { p.type() == Proxy.Type.SOCKS) {
break; break;

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -79,9 +79,8 @@ public class AuthCacheImpl implements AuthCache {
// list should contain only one element // list should contain only one element
return (AuthenticationInfo)list.get (0); return (AuthenticationInfo)list.get (0);
} }
ListIterator<AuthCacheValue> iter = list.listIterator(); for (AuthCacheValue authCacheValue : list) {
while (iter.hasNext()) { AuthenticationInfo inf = (AuthenticationInfo) authCacheValue;
AuthenticationInfo inf = (AuthenticationInfo)iter.next();
if (skey.startsWith (inf.path)) { if (skey.startsWith (inf.path)) {
return inf; return inf;
} }

View file

@ -384,9 +384,7 @@ class SubjectCodeSource extends CodeSource implements java.io.Serializable {
} }
} }
if (principals != null) { if (principals != null) {
ListIterator<PrincipalEntry> li = principals.listIterator(); for (PrincipalEntry pppe : principals) {
while (li.hasNext()) {
PrincipalEntry pppe = li.next();
returnMe = returnMe + ResourcesMgr.getAuthResourceString("NEWLINE") + returnMe = returnMe + ResourcesMgr.getAuthResourceString("NEWLINE") +
pppe.getPrincipalClass() + " " + pppe.getPrincipalClass() + " " +
pppe.getPrincipalName(); pppe.getPrincipalName();

View file

@ -1489,9 +1489,7 @@ public final class Main {
info.set(X509CertInfo.SUBJECT, info.set(X509CertInfo.SUBJECT,
dname==null?req.getSubjectName():new X500Name(dname)); dname==null?req.getSubjectName():new X500Name(dname));
CertificateExtensions reqex = null; CertificateExtensions reqex = null;
Iterator<PKCS10Attribute> attrs = req.getAttributes().getAttributes().iterator(); for (PKCS10Attribute attr : req.getAttributes().getAttributes()) {
while (attrs.hasNext()) {
PKCS10Attribute attr = attrs.next();
if (attr.getAttributeId().equals(PKCS9Attribute.EXTENSION_REQUEST_OID)) { if (attr.getAttributeId().equals(PKCS9Attribute.EXTENSION_REQUEST_OID)) {
reqex = (CertificateExtensions)attr.getAttributeValue(); reqex = (CertificateExtensions)attr.getAttributeValue();
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -29,7 +29,6 @@ import java.io.IOException;
import java.security.cert.PolicyQualifierInfo; import java.security.cert.PolicyQualifierInfo;
import java.util.Collections; import java.util.Collections;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.Iterator;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.Set; import java.util.Set;
@ -201,9 +200,7 @@ public class PolicyInformation {
"PolicyQualifierInfo can be set."); "PolicyQualifierInfo can be set.");
} }
if (obj instanceof Set) { if (obj instanceof Set) {
Iterator<?> i = ((Set<?>)obj).iterator(); for (Object obj1 : (Set<?>) obj) {
while (i.hasNext()) {
Object obj1 = i.next();
if (!(obj1 instanceof PolicyQualifierInfo)) { if (!(obj1 instanceof PolicyQualifierInfo)) {
throw new IOException("Attribute value must be a" + throw new IOException("Attribute value must be a" +
"Set of PolicyQualifierInfo objects."); "Set of PolicyQualifierInfo objects.");