diff options
author | Lars Grefer <eclipse@larsgrefer.de> | 2020-08-08 03:06:37 +0200 |
---|---|---|
committer | Lars Grefer <eclipse@larsgrefer.de> | 2020-08-08 03:06:37 +0200 |
commit | 72194b7982ddfa8e9864d0a9934905bb76b90f33 (patch) | |
tree | ebed806c358c1a3960c5d6be4c13b26ca41809df /util | |
parent | c3289ab86bfb2c97cf34147239b3dde46de92a7c (diff) | |
download | aspectj-72194b7982ddfa8e9864d0a9934905bb76b90f33.tar.gz aspectj-72194b7982ddfa8e9864d0a9934905bb76b90f33.zip |
'for' loop replaceable with enhanced 'for' loop
Reports for loops which iterate over collections or arrays, and can be replaced with an enhanced for loop (i.e. the foreach iteration syntax).
Signed-off-by: Lars Grefer <eclipse@larsgrefer.de>
Diffstat (limited to 'util')
7 files changed, 157 insertions, 163 deletions
diff --git a/util/src/main/java/org/aspectj/util/FileUtil.java b/util/src/main/java/org/aspectj/util/FileUtil.java index fef580e83..6e55742b1 100644 --- a/util/src/main/java/org/aspectj/util/FileUtil.java +++ b/util/src/main/java/org/aspectj/util/FileUtil.java @@ -147,12 +147,11 @@ public class FileUtil { return 0; } - for (Iterator<String> iter = SOURCE_SUFFIXES.iterator(); iter.hasNext();) { - String suffix = iter.next(); - if (path.endsWith(suffix) || path.toLowerCase().endsWith(suffix)) { - return suffix.length(); - } - } + for (String suffix : SOURCE_SUFFIXES) { + if (path.endsWith(suffix) || path.toLowerCase().endsWith(suffix)) { + return suffix.length(); + } + } return 0; } @@ -335,18 +334,18 @@ public class FileUtil { } StringBuffer result = new StringBuffer(); boolean first = true; - for (int i = 0; i < paths.length; i++) { - String path = paths[i]; - if (null == path) { - continue; - } - if (first) { - first = false; - } else { - result.append(infix); - } - result.append(path); - } + for (String path : paths) { + if (null == path) { + continue; + } + if (first) { + first = false; + } + else { + result.append(infix); + } + result.append(path); + } return result.toString(); } @@ -540,24 +539,24 @@ public class FileUtil { return 0; } int result = 0; - for (int i = 0; i < fromFiles.length; i++) { - String string = fromFiles[i]; - File file = new File(dir, string); - if ((null == filter) || filter.accept(file)) { - if (file.isDirectory()) { - result += deleteContents(file, filter, deleteEmptyDirs); - String[] fileContent = file.list(); - if (deleteEmptyDirs && fileContent != null - && 0 == fileContent.length) { - file.delete(); - } - } else { - /* boolean ret = */ - file.delete(); - result++; - } - } - } + for (String string : fromFiles) { + File file = new File(dir, string); + if ((null == filter) || filter.accept(file)) { + if (file.isDirectory()) { + result += deleteContents(file, filter, deleteEmptyDirs); + String[] fileContent = file.list(); + if (deleteEmptyDirs && fileContent != null + && 0 == fileContent.length) { + file.delete(); + } + } + else { + /* boolean ret = */ + file.delete(); + result++; + } + } + } return result; } @@ -752,15 +751,14 @@ public class FileUtil { File[] result = null; if (!LangUtil.isEmpty(suffixes)) { ArrayList<File> list = new ArrayList<File>(); - for (int i = 0; i < paths.length; i++) { - String path = paths[i]; - for (int j = 0; j < suffixes.length; j++) { - if (path.endsWith(suffixes[j])) { - list.add(new File(basedir, paths[i])); - break; - } - } - } + for (String path : paths) { + for (String suffix : suffixes) { + if (path.endsWith(suffix)) { + list.add(new File(basedir, path)); + break; + } + } + } result = list.toArray(new File[0]); } else { result = new File[paths.length]; @@ -1080,9 +1078,9 @@ public class FileUtil { public static void writeBooleanArray(boolean[] a, DataOutputStream s) throws IOException { int len = a.length; s.writeInt(len); - for (int i = 0; i < len; i++) { - s.writeBoolean(a[i]); - } + for (boolean b : a) { + s.writeBoolean(b); + } } /** @@ -1103,9 +1101,9 @@ public class FileUtil { public static void writeIntArray(int[] a, DataOutputStream s) throws IOException { int len = a.length; s.writeInt(len); - for (int i = 0; i < len; i++) { - s.writeInt(a[i]); - } + for (int j : a) { + s.writeInt(j); + } } /** @@ -1130,9 +1128,9 @@ public class FileUtil { } int len = a.length; s.writeInt(len); - for (int i = 0; i < len; i++) { - s.writeUTF(a[i]); - } + for (String value : a) { + s.writeUTF(value); + } } /** @@ -1276,13 +1274,12 @@ public class FileUtil { return Collections.emptyList(); } ArrayList<String> result = new ArrayList<String>(); - for (Iterator<String> iter = sources.iterator(); iter.hasNext();) { - String path = iter.next(); - String error = lineSeek(sought, path, listAll, result); - if ((null != error) && (null != errorSink)) { - errorSink.println(error); - } - } + for (String path : sources) { + String error = lineSeek(sought, path, listAll, result); + if ((null != error) && (null != errorSink)) { + errorSink.println(error); + } + } return result; } @@ -1357,53 +1354,52 @@ public class FileUtil { return true; } long delayUntil = System.currentTimeMillis(); - for (int i = 0; i < files.length; i++) { - File file = files[i]; - if ((null == file) || !file.exists()) { - continue; - } - long nextModTime = file.lastModified(); - if (nextModTime > delayUntil) { - delayUntil = nextModTime; - } - } + for (File file : files) { + if ((null == file) || !file.exists()) { + continue; + } + long nextModTime = file.lastModified(); + if (nextModTime > delayUntil) { + delayUntil = nextModTime; + } + } return LangUtil.sleepUntil(++delayUntil); } private static void listClassFiles(final File baseDir, ArrayList<File> result) { File[] files = baseDir.listFiles(); - for (int i = 0; i < files.length; i++) { - File f = files[i]; - if (f.isDirectory()) { - listClassFiles(f, result); - } else { - if (f.getName().endsWith(".class")) { - result.add(f); - } - } - } + for (File f : files) { + if (f.isDirectory()) { + listClassFiles(f, result); + } + else { + if (f.getName().endsWith(".class")) { + result.add(f); + } + } + } } private static void listFiles(final File baseDir, ArrayList<File> result, FileFilter filter) { File[] files = baseDir.listFiles(); // hack https://bugs.eclipse.org/bugs/show_bug.cgi?id=48650 final boolean skipCVS = (!PERMIT_CVS && (filter == aspectjSourceFileFilter)); - for (int i = 0; i < files.length; i++) { - File f = files[i]; - if (f.isDirectory()) { - if (skipCVS) { - String name = f.getName().toLowerCase(); - if ("cvs".equals(name) || "sccs".equals(name)) { - continue; - } - } - listFiles(f, result, filter); - } else { - if (filter.accept(f)) { - result.add(f); - } - } - } + for (File f : files) { + if (f.isDirectory()) { + if (skipCVS) { + String name = f.getName().toLowerCase(); + if ("cvs".equals(name) || "sccs".equals(name)) { + continue; + } + } + listFiles(f, result, filter); + } + else { + if (filter.accept(f)) { + result.add(f); + } + } + } } /** @return true if input is not null and contains no path separator */ @@ -1415,15 +1411,16 @@ public class FileUtil { final String dirPrefix = (null == dir ? "" : dir + "/"); final File dirFile = (null == dir ? baseDir : new File(baseDir.getPath() + "/" + dir)); final String[] files = dirFile.list(); - for (int i = 0; i < files.length; i++) { - File f = new File(dirFile, files[i]); - String path = dirPrefix + files[i]; - if (f.isDirectory()) { - listFiles(baseDir, path, result); - } else { - result.add(path); - } - } + for (String file : files) { + File f = new File(dirFile, file); + String path = dirPrefix + file; + if (f.isDirectory()) { + listFiles(baseDir, path, result); + } + else { + result.add(path); + } + } } private FileUtil() { @@ -1432,9 +1429,9 @@ public class FileUtil { public static List<String> makeClasspath(URL[] urls) { List<String> ret = new LinkedList<String>(); if (urls != null) { - for (int i = 0; i < urls.length; i++) { - ret.add(toPathString(urls[i])); - } + for (URL url : urls) { + ret.add(toPathString(url)); + } } return ret; } diff --git a/util/src/main/java/org/aspectj/util/GenericSignature.java b/util/src/main/java/org/aspectj/util/GenericSignature.java index bfefc007e..5fa7ac632 100644 --- a/util/src/main/java/org/aspectj/util/GenericSignature.java +++ b/util/src/main/java/org/aspectj/util/GenericSignature.java @@ -30,8 +30,8 @@ public class GenericSignature { StringBuffer ret = new StringBuffer(); ret.append(formalTypeParameters.toString()); ret.append(superclassSignature.toString()); - for (int i = 0; i < superInterfaceSignatures.length; i++) { - ret.append(superInterfaceSignatures[i].toString()); + for (ClassTypeSignature superInterfaceSignature : superInterfaceSignatures) { + ret.append(superInterfaceSignature.toString()); } return ret.toString(); } @@ -55,20 +55,20 @@ public class GenericSignature { StringBuffer sb = new StringBuffer(); if (formalTypeParameters.length > 0) { sb.append("<"); - for (int i = 0; i < formalTypeParameters.length; i++) { - sb.append(formalTypeParameters[i].toString()); + for (FormalTypeParameter formalTypeParameter : formalTypeParameters) { + sb.append(formalTypeParameter.toString()); } sb.append(">"); } sb.append("("); - for (int i = 0; i < parameters.length; i++) { - sb.append(parameters[i].toString()); + for (TypeSignature parameter : parameters) { + sb.append(parameter.toString()); } sb.append(")"); sb.append(returnType.toString()); - for (int i = 0; i < throwsSignatures.length; i++) { + for (FieldTypeSignature throwsSignature : throwsSignatures) { sb.append("^"); - sb.append(throwsSignatures[i].toString()); + sb.append(throwsSignature.toString()); } return sb.toString(); } @@ -89,9 +89,9 @@ public class GenericSignature { ret.append(identifier); ret.append(":"); ret.append(classBound.toString()); - for (int i = 0; i < interfaceBounds.length; i++) { + for (FieldTypeSignature interfaceBound : interfaceBounds) { ret.append(":"); - ret.append(interfaceBounds[i].toString()); + ret.append(interfaceBound.toString()); } return ret.toString(); } @@ -212,8 +212,8 @@ public class GenericSignature { sb.append(identifier); if (typeArguments.length > 0) { sb.append("<"); - for (int i = 0; i < typeArguments.length; i++) { - sb.append(typeArguments[i].toString()); + for (TypeArgument typeArgument : typeArguments) { + sb.append(typeArgument.toString()); } sb.append(">"); } diff --git a/util/src/main/java/org/aspectj/util/GenericSignatureParser.java b/util/src/main/java/org/aspectj/util/GenericSignatureParser.java index 28c8db83d..27670cc00 100644 --- a/util/src/main/java/org/aspectj/util/GenericSignatureParser.java +++ b/util/src/main/java/org/aspectj/util/GenericSignatureParser.java @@ -194,8 +194,8 @@ public class GenericSignatureParser { } else if (tokenStream[tokenIndex].equals("<")) { ret.append("<"); TypeArgument[] tArgs = maybeParseTypeArguments(); - for (int i = 0; i < tArgs.length; i++) { - ret.append(tArgs[i].toString()); + for (TypeArgument tArg : tArgs) { + ret.append(tArg.toString()); } ret.append(">"); outerType = new SimpleClassTypeSignature(identifier, tArgs); diff --git a/util/src/main/java/org/aspectj/util/PartialOrder.java b/util/src/main/java/org/aspectj/util/PartialOrder.java index 8bb9f3b77..14f23b0d6 100644 --- a/util/src/main/java/org/aspectj/util/PartialOrder.java +++ b/util/src/main/java/org/aspectj/util/PartialOrder.java @@ -88,8 +88,7 @@ public class PartialOrder { private static <T extends PartialComparable> void addNewPartialComparable(List<SortObject<T>> graph, T o) { SortObject<T> so = new SortObject<T>(o); - for (Iterator<SortObject<T>> i = graph.iterator(); i.hasNext();) { - SortObject<T> other = i.next(); + for (SortObject<T> other : graph) { so.addDirectedLinks(other); } graph.add(so); @@ -125,8 +124,8 @@ public class PartialOrder { // ??? I don't like creating this data structure, but it does give good // ??? separation of concerns. List<SortObject<T>> sortList = new LinkedList<SortObject<T>>(); - for (Iterator<T> i = objects.iterator(); i.hasNext();) { - addNewPartialComparable(sortList, i.next()); + for (T object : objects) { + addNewPartialComparable(sortList, object); } // System.out.println(sortList); diff --git a/util/src/main/java/org/aspectj/util/Reflection.java b/util/src/main/java/org/aspectj/util/Reflection.java index 3650962cc..d78f67101 100644 --- a/util/src/main/java/org/aspectj/util/Reflection.java +++ b/util/src/main/java/org/aspectj/util/Reflection.java @@ -57,8 +57,7 @@ public class Reflection { public static Method getMatchingMethod(Class<?> class_, String name, Object[] args) { Method[] meths = class_.getMethods(); - for (int i=0; i < meths.length; i++) { - Method meth = meths[i]; + for (Method meth : meths) { if (meth.getName().equals(name) && isCompatible(meth, args)) { return meth; } @@ -101,25 +100,24 @@ public class Reflection { ArrayList<File> libs = new ArrayList<File>(); ArrayList<URL> urls = new ArrayList<URL>(); String[] entries = LangUtil.splitClasspath(classpath); - for (int i = 0; i < entries.length; i++) { - String entry = entries[i]; - URL url = makeURL(entry); - if (null != url) { - urls.add(url); - } - File file = new File(entries[i]); + for (String entry : entries) { + URL url = makeURL(entry); + if (null != url) { + urls.add(url); + } + File file = new File(entry); // tolerate bad entries b/c bootclasspath sometimes has them // if (!file.canRead()) { // throw new IllegalArgumentException("cannot read " + file); // } - if (FileUtil.isZipFile(file)) { - libs.add(file); - } else if (file.isDirectory()) { - dirs.add(file); - } else { - // not URL, zip, or dir - unsure what to do - } - } + if (FileUtil.isZipFile(file)) { + libs.add(file); + } else if (file.isDirectory()) { + dirs.add(file); + } else { + // not URL, zip, or dir - unsure what to do + } + } File[] dirRa = (File[]) dirs.toArray(new File[0]); File[] libRa = (File[]) libs.toArray(new File[0]); URL[] urlRa = (URL[]) urls.toArray(new URL[0]); diff --git a/util/src/main/java/org/aspectj/util/UtilClassLoader.java b/util/src/main/java/org/aspectj/util/UtilClassLoader.java index 091e022a3..4704bfcc9 100644 --- a/util/src/main/java/org/aspectj/util/UtilClassLoader.java +++ b/util/src/main/java/org/aspectj/util/UtilClassLoader.java @@ -102,12 +102,12 @@ public class UtilClassLoader extends URLClassLoader { /** @return null if class not found or byte[] of class otherwise */ private byte[] readClass(String className) throws ClassNotFoundException { final String fileName = className.replace('.', '/')+".class"; - for (Iterator<File> iter = dirs.iterator(); iter.hasNext();) { - File file = new File(iter.next(), fileName); - if (file.canRead()) { - return getClassData(file); - } - } + for (File dir : dirs) { + File file = new File(dir, fileName); + if (file.canRead()) { + return getClassData(file); + } + } return null; } diff --git a/util/src/test/java/org/aspectj/util/FileUtilTest.java b/util/src/test/java/org/aspectj/util/FileUtilTest.java index 2d61a6a79..367c0ea5e 100644 --- a/util/src/test/java/org/aspectj/util/FileUtilTest.java +++ b/util/src/test/java/org/aspectj/util/FileUtilTest.java @@ -166,13 +166,13 @@ public class FileUtilTest extends TestCase { return; } File[] files = dir.listFiles(); - for (int i = 0; i < files.length; i++) { - String path = files[i].getPath(); - if (!files[i].getName().startsWith(".")) { - if (files[i].isFile()) { + for (File file : files) { + String path = file.getPath(); + if (!file.getName().startsWith(".")) { + if (file.isFile()) { paths.add(path); - } else if (files[i].isDirectory()) { - doDirPaths(files[i], paths); + } else if (file.isDirectory()) { + doDirPaths(file, paths); } else { log("not file or dir: " + dir + "/" + path); } @@ -325,8 +325,8 @@ public class FileUtilTest extends TestCase { public void testGetURL() { String[] args = new String[] { ".", "../util/testdata", "../lib/test/aspectjrt.jar" }; - for (int i = 0; i < args.length; i++) { - checkGetURL(args[i]); + for (String arg : args) { + checkGetURL(arg); } } @@ -496,8 +496,8 @@ public class FileUtilTest extends TestCase { sb.append(LangUtil.EOL); } final String contents = sb.toString(); - for (int i = 0; i < sources.length; i++) { - File file = new File(sources[i]); + for (String source : sources) { + File file = new File(source); FileUtil.writeAsString(file, contents); tempFiles.add(file); } @@ -588,9 +588,9 @@ public class FileUtilTest extends TestCase { assertTrue(d.exists()); tempFiles.add(d); assertTrue(d.canWrite()); - for (int i = 0; i < filenames.length; i++) { - File f = new File(d, filenames[i]); - assertTrue(filenames[i], f.createNewFile()); + for (String filename : filenames) { + File f = new File(d, filename); + assertTrue(filename, f.createNewFile()); } return d; } |