From 93692ea0a9bc437309b808f511c771a79dcdfb9a Mon Sep 17 00:00:00 2001 From: Andrey Turbanov Date: Tue, 9 Nov 2021 16:58:43 +0000 Subject: [PATCH] 8274395: Use enhanced-for instead of plain 'for' in jdk.internal.jvmstat Reviewed-by: cjplummer, amenkov, sspitsyn --- .../sun/jvmstat/monitor/HostIdentifier.java | 10 ++++---- .../sun/jvmstat/monitor/VmIdentifier.java | 10 ++++---- .../perfdata/monitor/PerfDataBufferImpl.java | 13 +++++----- .../protocol/local/LocalMonitoredVm.java | 7 ++---- .../protocol/local/LocalVmManager.java | 24 +++++++++---------- .../protocol/local/MonitoredHostProvider.java | 12 ++++------ .../perfdata/monitor/v2_0/TypeCode.java | 8 +++---- 7 files changed, 38 insertions(+), 46 deletions(-) diff --git a/src/jdk.internal.jvmstat/share/classes/sun/jvmstat/monitor/HostIdentifier.java b/src/jdk.internal.jvmstat/share/classes/sun/jvmstat/monitor/HostIdentifier.java index 98bbd25c214..b80fccaf52f 100644 --- a/src/jdk.internal.jvmstat/share/classes/sun/jvmstat/monitor/HostIdentifier.java +++ b/src/jdk.internal.jvmstat/share/classes/sun/jvmstat/monitor/HostIdentifier.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2004, 2020, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2004, 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 @@ -507,10 +507,10 @@ public class HostIdentifier { String query = getQuery(); if (query != null) { String[] queryArgs = query.split("\\+"); - for (int i = 0; i < queryArgs.length; i++) { - if (queryArgs[i].startsWith("mode=")) { - int index = queryArgs[i].indexOf('='); - return queryArgs[i].substring(index+1); + for (String queryArg : queryArgs) { + if (queryArg.startsWith("mode=")) { + int index = queryArg.indexOf('='); + return queryArg.substring(index + 1); } } } diff --git a/src/jdk.internal.jvmstat/share/classes/sun/jvmstat/monitor/VmIdentifier.java b/src/jdk.internal.jvmstat/share/classes/sun/jvmstat/monitor/VmIdentifier.java index a37c7998942..6e7f1cc1f77 100644 --- a/src/jdk.internal.jvmstat/share/classes/sun/jvmstat/monitor/VmIdentifier.java +++ b/src/jdk.internal.jvmstat/share/classes/sun/jvmstat/monitor/VmIdentifier.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2004, 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 @@ -385,10 +385,10 @@ public class VmIdentifier { String query = getQuery(); if (query != null) { String[] queryArgs = query.split("\\+"); - for (int i = 0; i < queryArgs.length; i++) { - if (queryArgs[i].startsWith("mode=")) { - int index = queryArgs[i].indexOf('='); - return queryArgs[i].substring(index+1); + for (String queryArg : queryArgs) { + if (queryArg.startsWith("mode=")) { + int index = queryArg.indexOf('='); + return queryArg.substring(index + 1); } } } diff --git a/src/jdk.internal.jvmstat/share/classes/sun/jvmstat/perfdata/monitor/PerfDataBufferImpl.java b/src/jdk.internal.jvmstat/share/classes/sun/jvmstat/perfdata/monitor/PerfDataBufferImpl.java index c83b802306d..9337ef4e8b6 100644 --- a/src/jdk.internal.jvmstat/share/classes/sun/jvmstat/perfdata/monitor/PerfDataBufferImpl.java +++ b/src/jdk.internal.jvmstat/share/classes/sun/jvmstat/perfdata/monitor/PerfDataBufferImpl.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2004, 2014, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2004, 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 @@ -205,9 +205,11 @@ public abstract class PerfDataBufferImpl { if (m == null) { ArrayList al = aliasMap.get(name); if (al != null) { - for (Iterator i = al.iterator(); i.hasNext() && m == null; ) { - String alias = i.next(); + for (String alias : al) { m = monitors.get(alias); + if (m != null) { + break; + } } } } @@ -293,8 +295,7 @@ public abstract class PerfDataBufferImpl { Set> monitorSet = monitors.entrySet(); - for (Iterator> i = monitorSet.iterator(); i.hasNext(); /* empty */) { - Map.Entry me = i.next(); + for (Map.Entry me : monitorSet) { String name = me.getKey(); Monitor m = me.getValue(); @@ -303,7 +304,7 @@ public abstract class PerfDataBufferImpl { // if the pattern matches, then add monitor to list if (matcher.lookingAt()) { - matches.add(me.getValue()); + matches.add(me.getValue()); } } return matches; diff --git a/src/jdk.internal.jvmstat/share/classes/sun/jvmstat/perfdata/monitor/protocol/local/LocalMonitoredVm.java b/src/jdk.internal.jvmstat/share/classes/sun/jvmstat/perfdata/monitor/protocol/local/LocalMonitoredVm.java index ca1b210aaac..a72a5fbcb0b 100644 --- a/src/jdk.internal.jvmstat/share/classes/sun/jvmstat/perfdata/monitor/protocol/local/LocalMonitoredVm.java +++ b/src/jdk.internal.jvmstat/share/classes/sun/jvmstat/perfdata/monitor/protocol/local/LocalMonitoredVm.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2004, 2014, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2004, 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 @@ -26,8 +26,6 @@ package sun.jvmstat.perfdata.monitor.protocol.local; import java.util.*; -import java.lang.reflect.*; -import java.io.*; import sun.jvmstat.monitor.*; import sun.jvmstat.monitor.event.*; @@ -152,8 +150,7 @@ public class LocalMonitoredVm extends AbstractMonitoredVm { registered = (ArrayList)listeners.clone(); } - for (Iterator i = registered.iterator(); i.hasNext(); /* empty */) { - VmListener l = i.next(); + for (VmListener l : registered) { // lazily create the event object; if (ev == null) { ev = new MonitorStatusChangeEvent(this, inserted, removed); diff --git a/src/jdk.internal.jvmstat/share/classes/sun/jvmstat/perfdata/monitor/protocol/local/LocalVmManager.java b/src/jdk.internal.jvmstat/share/classes/sun/jvmstat/perfdata/monitor/protocol/local/LocalVmManager.java index 90837d97984..b21c09cd51a 100644 --- a/src/jdk.internal.jvmstat/share/classes/sun/jvmstat/perfdata/monitor/protocol/local/LocalVmManager.java +++ b/src/jdk.internal.jvmstat/share/classes/sun/jvmstat/perfdata/monitor/protocol/local/LocalVmManager.java @@ -25,8 +25,6 @@ package sun.jvmstat.perfdata.monitor.protocol.local; -import sun.jvmstat.monitor.*; -import sun.jvmstat.monitor.event.*; import java.util.*; import java.util.regex.*; import java.io.*; @@ -105,19 +103,19 @@ public class LocalVmManager { // 1.4.2 and later: Look for the files {tmpdir}/hsperfdata_{any_user_name}/[0-9]+ // that are readable by the current user. File[] dirs = tmpdir.listFiles(userDirFilter); - for (int i = 0 ; i < dirs.length; i ++) { - if (!dirs[i].isDirectory()) { + for (File subDir : dirs) { + if (!subDir.isDirectory()) { continue; } // get a list of files from the directory - File[] files = dirs[i].listFiles(userDirFileFilter); + File[] files = subDir.listFiles(userDirFileFilter); if (files != null) { - for (int j = 0; j < files.length; j++) { - if (files[j].isFile() && files[j].canRead()) { - int vmid = PerfDataFile.getLocalVmId(files[j]); + for (File file : files) { + if (file.isFile() && file.canRead()) { + int vmid = PerfDataFile.getLocalVmId(file); if (vmid != -1) { - jvmSet.add(vmid); + jvmSet.add(vmid); } } } @@ -127,11 +125,11 @@ public class LocalVmManager { // look for any 1.4.1 files that are readable by the current user. File[] files = tmpdir.listFiles(oldtmpFileFilter); if (files != null) { - for (int j = 0; j < files.length; j++) { - if (files[j].isFile() && files[j].canRead()) { - int vmid = PerfDataFile.getLocalVmId(files[j]); + for (File file : files) { + if (file.isFile() && file.canRead()) { + int vmid = PerfDataFile.getLocalVmId(file); if (vmid != -1) { - jvmSet.add(vmid); + jvmSet.add(vmid); } } } diff --git a/src/jdk.internal.jvmstat/share/classes/sun/jvmstat/perfdata/monitor/protocol/local/MonitoredHostProvider.java b/src/jdk.internal.jvmstat/share/classes/sun/jvmstat/perfdata/monitor/protocol/local/MonitoredHostProvider.java index 851cde20b48..742d2ae6892 100644 --- a/src/jdk.internal.jvmstat/share/classes/sun/jvmstat/perfdata/monitor/protocol/local/MonitoredHostProvider.java +++ b/src/jdk.internal.jvmstat/share/classes/sun/jvmstat/perfdata/monitor/protocol/local/MonitoredHostProvider.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2004, 2014, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2004, 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 @@ -170,8 +170,7 @@ public class MonitoredHostProvider extends MonitoredHost { registered = (ArrayList)listeners.clone(); } - for (Iterator i = registered.iterator(); i.hasNext(); /* empty */) { - HostListener l = i.next(); + for (HostListener l : registered) { if (ev == null) { ev = new VmStatusChangeEvent(this, active, started, terminated); } @@ -198,17 +197,14 @@ public class MonitoredHostProvider extends MonitoredHost { Set startedVms = new HashSet<>(); Set terminatedVms = new HashSet<>(); - for (Iterator i = activeVms.iterator(); i.hasNext(); /* empty */) { - Integer vmid = i.next(); + for (Integer vmid : activeVms) { if (!lastActiveVms.contains(vmid)) { // a new file has been detected, add to set startedVms.add(vmid); } } - for (Iterator i = lastActiveVms.iterator(); i.hasNext(); - /* empty */) { - Integer o = i.next(); + for (Integer o : lastActiveVms) { if (!activeVms.contains(o)) { // JVM has terminated, remove it from the active list terminatedVms.add(o); diff --git a/src/jdk.internal.jvmstat/share/classes/sun/jvmstat/perfdata/monitor/v2_0/TypeCode.java b/src/jdk.internal.jvmstat/share/classes/sun/jvmstat/perfdata/monitor/v2_0/TypeCode.java index 2770601676c..8d81dc4fbc2 100644 --- a/src/jdk.internal.jvmstat/share/classes/sun/jvmstat/perfdata/monitor/v2_0/TypeCode.java +++ b/src/jdk.internal.jvmstat/share/classes/sun/jvmstat/perfdata/monitor/v2_0/TypeCode.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2004, 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 @@ -81,9 +81,9 @@ public class TypeCode { * a valid Java TypeCode. */ public static TypeCode toTypeCode(char c) { - for (int j = 0; j < basicTypes.length; j++) { - if (basicTypes[j].value == c) { - return (basicTypes[j]); + for (TypeCode basicType : basicTypes) { + if (basicType.value == c) { + return basicType; } } throw new IllegalArgumentException();