]> source.dussan.org Git - aspectj.git/commitdiff
'while' loop replaceable with enhanced 'for' loop
authorLars Grefer <eclipse@larsgrefer.de>
Sat, 8 Aug 2020 01:09:01 +0000 (03:09 +0200)
committerLars Grefer <eclipse@larsgrefer.de>
Sat, 8 Aug 2020 01:09:01 +0000 (03:09 +0200)
Reports while loops which iterate over collections, and can be replaced with an enhanced for loop (i.e. foreach iteration syntax).

Signed-off-by: Lars Grefer <eclipse@larsgrefer.de>
12 files changed:
ajde.core/src/main/java/org/aspectj/ajde/core/internal/AjdeCoreBuildManager.java
ajde/src/main/java/org/aspectj/ajde/internal/LstBuildConfigFileUpdater.java
bcel-builder/src/main/java/org/aspectj/apache/bcel/generic/InstructionList.java
org.aspectj.ajdt.core/src/main/java/org/aspectj/org/eclipse/jdt/core/dom/AjASTConverter.java
org.aspectj.matcher/src/main/java/org/aspectj/weaver/Dump.java
taskdefs/src/main/java/org/aspectj/tools/ant/taskdefs/Ajc2.java
weaver/src/main/java/org/aspectj/weaver/bcel/BcelClassWeaver.java
weaver/src/main/java/org/aspectj/weaver/bcel/BcelShadow.java
weaver/src/main/java/org/aspectj/weaver/bcel/LazyMethodGen.java
weaver/src/main/java/org/aspectj/weaver/bcel/Range.java
weaver/src/main/java/org/aspectj/weaver/bcel/ShadowRange.java
weaver/src/main/java/org/aspectj/weaver/bcel/Utility.java

index a83ab5be25e2d6ed42a9db92604fb02743955b61..b64588001fc83248a57baa73db22051024e79cc7 100644 (file)
@@ -177,9 +177,8 @@ public class AjdeCoreBuildManager {
                }
 
                StringBuffer formattedOptions = new StringBuffer();
-               Iterator<?> it = options.iterator();
-               while (it.hasNext()) {
-                       String o = it.next().toString();
+               for (Object option : options) {
+                       String o = option.toString();
                        if (formattedOptions.length() > 0) {
                                formattedOptions.append(", ");
                        }
index f3e2154cd025fee4ddf91a82aad48531766be698..4e59e5d744ed92def655fbe7e5d05bb911fa7df9 100644 (file)
@@ -81,9 +81,8 @@ class LstBuildConfigFileUpdater {
        }
 
        public boolean exists(String entry, String configFile, String rootPath) {
-               Iterator it = readConfigFile(configFile).iterator();
-               while (it.hasNext()) {
-                       if ((entry).equals(rootPath + "/" + (String) it.next())) {
+               for (String s : readConfigFile(configFile)) {
+                       if ((entry).equals(rootPath + "/" + s)) {
                                return true;
                        }
                }
@@ -188,9 +187,8 @@ class LstBuildConfigFileUpdater {
        public void writeConfigFile(String filePath, List<String> fileContents) {
                Set<String> contentsSet = new TreeSet<>(fileContents);
                StringBuffer fileContentsSB = new StringBuffer();
-               Iterator<String> it = contentsSet.iterator();
-               while (it.hasNext()) {
-                       fileContentsSB.append(it.next().toString());
+               for (String s : contentsSet) {
+                       fileContentsSB.append(s.toString());
                        fileContentsSB.append("\n");
                }
                writeFile(fileContentsSB.toString(), filePath);
index dbdfb7fdb871ec6398027142a41b6a55dbe1d743..dd010b758ff90f926f38dd30450ab3bfa6cea1b2 100644 (file)
@@ -703,9 +703,7 @@ public class InstructionList implements Serializable {
 
                        Set<InstructionTargeter> targeters = ih.getTargeters();
                        boolean isOK = false;
-                       Iterator<InstructionTargeter> tIter = targeters.iterator();
-                       while (tIter.hasNext()) {
-                               InstructionTargeter instructionTargeter = tIter.next();
+                       for (InstructionTargeter instructionTargeter : targeters) {
                                if (instructionTargeter.getClass().getName().endsWith("ShadowRange")
                                                || instructionTargeter.getClass().getName().endsWith("ExceptionRange")
                                                || instructionTargeter.getClass().getName().endsWith("LineNumberTag")) {
index 57602a440d7c75bafeca11528286bb29f75bd847..32794ef4476c003ea70dbfa0dcbcd879014fc138 100644 (file)
@@ -1993,9 +1993,8 @@ public class AjASTConverter extends ASTConverter {
                                        if (this.resolveBindings) {
                                                recordNodes(docComment, javadoc);
                                                // resolve member and method references binding
-                                               Iterator tags = docComment.tags().listIterator();
-                                               while (tags.hasNext()) {
-                                                       recordNodes(javadoc, (TagElement) tags.next());
+                                               for (Object o : docComment.tags()) {
+                                                       recordNodes(javadoc, (TagElement) o);
                                                }
                                        }
                                        bodyDeclaration.setJavadoc(docComment);
@@ -2016,9 +2015,8 @@ public class AjASTConverter extends ASTConverter {
                                        if (this.resolveBindings) {
                                                recordNodes(docComment, javadoc);
                                                // resolve member and method references binding
-                                               Iterator tags = docComment.tags().listIterator();
-                                               while (tags.hasNext()) {
-                                                       recordNodes(javadoc, (TagElement) tags.next());
+                                               for (Object o : docComment.tags()) {
+                                                       recordNodes(javadoc, (TagElement) o);
                                                }
                                        }
                                        packageDeclaration.setJavadoc(docComment);
@@ -3389,9 +3387,8 @@ public class AjASTConverter extends ASTConverter {
        }
 
        protected void recordNodes(org.aspectj.org.eclipse.jdt.internal.compiler.ast.Javadoc javadoc, TagElement tagElement) {
-               Iterator fragments = tagElement.fragments().listIterator();
-               while (fragments.hasNext()) {
-                       ASTNode node = (ASTNode) fragments.next();
+               for (Object value : tagElement.fragments()) {
+                       ASTNode node = (ASTNode) value;
                        if (node.getNodeType() == ASTNode.MEMBER_REF) {
                                MemberRef memberRef = (MemberRef) node;
                                Name name = memberRef.getName();
@@ -3404,16 +3401,16 @@ public class AjASTConverter extends ASTConverter {
                                }
                                // Replace qualifier to have all nodes recorded
                                if (memberRef.getQualifier() != null) {
-                                       org.aspectj.org.eclipse.jdt.internal.compiler.ast.TypeReference typeRef = null;
+                                       TypeReference typeRef = null;
                                        if (compilerNode instanceof JavadocFieldReference) {
                                                org.aspectj.org.eclipse.jdt.internal.compiler.ast.Expression expression = ((JavadocFieldReference) compilerNode).receiver;
-                                               if (expression instanceof org.aspectj.org.eclipse.jdt.internal.compiler.ast.TypeReference) {
-                                                       typeRef = (org.aspectj.org.eclipse.jdt.internal.compiler.ast.TypeReference) expression;
+                                               if (expression instanceof TypeReference) {
+                                                       typeRef = (TypeReference) expression;
                                                }
                                        } else if (compilerNode instanceof JavadocMessageSend) {
                                                org.aspectj.org.eclipse.jdt.internal.compiler.ast.Expression expression = ((JavadocMessageSend) compilerNode).receiver;
-                                               if (expression instanceof org.aspectj.org.eclipse.jdt.internal.compiler.ast.TypeReference) {
-                                                       typeRef = (org.aspectj.org.eclipse.jdt.internal.compiler.ast.TypeReference) expression;
+                                               if (expression instanceof TypeReference) {
+                                                       typeRef = (TypeReference) expression;
                                                }
                                        }
                                        if (typeRef != null) {
@@ -3431,16 +3428,16 @@ public class AjASTConverter extends ASTConverter {
                                if (compilerNode != null) {
                                        recordNodes(methodRef, compilerNode);
                                        // get type ref
-                                       org.aspectj.org.eclipse.jdt.internal.compiler.ast.TypeReference typeRef = null;
+                                       TypeReference typeRef = null;
                                        if (compilerNode instanceof org.aspectj.org.eclipse.jdt.internal.compiler.ast.JavadocAllocationExpression) {
                                                typeRef = ((org.aspectj.org.eclipse.jdt.internal.compiler.ast.JavadocAllocationExpression) compilerNode).type;
                                                if (typeRef != null) {
                                                        recordNodes(name, compilerNode);
                                                }
-                                       } else if (compilerNode instanceof org.aspectj.org.eclipse.jdt.internal.compiler.ast.JavadocMessageSend) {
-                                               org.aspectj.org.eclipse.jdt.internal.compiler.ast.Expression expression = ((org.aspectj.org.eclipse.jdt.internal.compiler.ast.JavadocMessageSend) compilerNode).receiver;
-                                               if (expression instanceof org.aspectj.org.eclipse.jdt.internal.compiler.ast.TypeReference) {
-                                                       typeRef = (org.aspectj.org.eclipse.jdt.internal.compiler.ast.TypeReference) expression;
+                                       } else if (compilerNode instanceof JavadocMessageSend) {
+                                               org.aspectj.org.eclipse.jdt.internal.compiler.ast.Expression expression = ((JavadocMessageSend) compilerNode).receiver;
+                                               if (expression instanceof TypeReference) {
+                                                       typeRef = (TypeReference) expression;
                                                }
                                                // TODO (frederic) remove following line to fix bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=62650
                                                recordNodes(name, compilerNode);
@@ -3451,16 +3448,15 @@ public class AjASTConverter extends ASTConverter {
                                        }
                                }
                                // Resolve parameters
-                               Iterator parameters = methodRef.parameters().listIterator();
-                               while (parameters.hasNext()) {
-                                       MethodRefParameter param = (MethodRefParameter) parameters.next();
+                               for (Object o : methodRef.parameters()) {
+                                       MethodRefParameter param = (MethodRefParameter) o;
                                        org.aspectj.org.eclipse.jdt.internal.compiler.ast.Expression expression = (org.aspectj.org.eclipse.jdt.internal.compiler.ast.Expression) javadoc
                                                        .getNodeStartingAt(param.getStartPosition());
                                        if (expression != null) {
                                                recordNodes(param, expression);
                                                if (expression instanceof JavadocArgumentExpression) {
                                                        JavadocArgumentExpression argExpr = (JavadocArgumentExpression) expression;
-                                                       org.aspectj.org.eclipse.jdt.internal.compiler.ast.TypeReference typeRef = argExpr.argument.type;
+                                                       TypeReference typeRef = argExpr.argument.type;
                                                        if (this.ast.apiLevel >= AST.JLS3) {
                                                                param.setVarargs(argExpr.argument.isVarArgs());
                                                        }
index 6e48de0d2e8468cad89f939fbe2804cdebc9cab5..2e2f9e9df2fa8f75169f85291a71423df3e55ac9 100644 (file)
@@ -424,9 +424,8 @@ public class Dump {
        }
 
        private void println(Properties props) {
-               Iterator iter = props.keySet().iterator();
-               while (iter.hasNext()) {
-                       String key = (String) iter.next();
+               for (Object o : props.keySet()) {
+                       String key = (String) o;
                        String value = props.getProperty(key);
                        print.println(key + "=" + value);
                }
index a089f00b905821e7ed4dacf7580433344a0d647d..9aea9ae6d6a08c4232bf506493d9758b5e3e433f 100644 (file)
@@ -416,11 +416,10 @@ public class Ajc2 extends Javac {
         List newIncludes = new ArrayList();
         List newArguments = new ArrayList();
         if (argfiles != null) {
-            Iterator iter = argfiles.iterator();
-            while (iter.hasNext()) {
-                File argfile = ((Argfile)iter.next()).getFile();
-                expandArgfile(argfile, newIncludes, newArguments);
-            }
+                       for (Object o : argfiles) {
+                               File argfile = ((Argfile) o).getFile();
+                               expandArgfile(argfile, newIncludes, newArguments);
+                       }
         }
 
         // If there aren't any includes, but we've used an argfile then we should
@@ -445,10 +444,9 @@ public class Ajc2 extends Javac {
         }
 
         // Add the new included files
-        Iterator iter = newIncludes.iterator();
-        while (iter.hasNext()) {
-            newFiles.add((File)iter.next());
-        }
+               for (Object newInclude : newIncludes) {
+                       newFiles.add((File) newInclude);
+               }
 
         // This is the same behavior found in Javac
         int newFileSize = newFiles.size();
index 3c4f64aa2f181046e72c0099c4b0a73a9cec2a37..36cd2686eac3fadff88518835d84d9bf343e9fe5 100644 (file)
@@ -2324,9 +2324,7 @@ class BcelClassWeaver implements IClassWeaver {
 
                        // copy over tags and range attributes
 
-                       Iterator<InstructionTargeter> tIter = src.getTargeters().iterator();
-                       while (tIter.hasNext()) {
-                               InstructionTargeter old = tIter.next();
+                       for (InstructionTargeter old : src.getTargeters()) {
                                if (old instanceof Tag) {
                                        Tag oldTag = (Tag) old;
                                        Tag fresh = tagMap.get(oldTag);
index ca167e2ef50103aadc62c4fa1ed955858893c453..e92667b2e8416473274feef43d5843d7014858f3 100644 (file)
@@ -555,9 +555,7 @@ public class BcelShadow extends Shadow {
                if (startOfHandler.getInstruction().isStoreInstruction() && startOfHandler.getNext() != null) {
                        int slot = startOfHandler.getInstruction().getIndex();
                        // System.out.println("got store: " + startOfHandler.getInstruction() + ", " + index);
-                       Iterator<InstructionTargeter> tIter = startOfHandler.getNext().getTargeters().iterator();
-                       while (tIter.hasNext()) {
-                               InstructionTargeter targeter = tIter.next();
+                       for (InstructionTargeter targeter : startOfHandler.getNext().getTargeters()) {
                                if (targeter instanceof LocalVariableTag) {
                                        LocalVariableTag t = (LocalVariableTag) targeter;
                                        if (t.getSlot() == slot) {
index ed9dd2c7a7338b383dfc886a47eb84a345fddca6..049257d48c06a59dce1aca96acfecc6f1c128145 100644 (file)
@@ -658,9 +658,8 @@ public final class LazyMethodGen implements Traceable {
                        // boolean hasPendingTargeters = false;
                        int lcounter = 0;
                        for (InstructionHandle ih = body.getStart(); ih != null; ih = ih.getNext()) {
-                               Iterator<InstructionTargeter> tIter = ih.getTargeters().iterator();
-                               while (tIter.hasNext()) {
-                                       InstructionTargeter t = tIter.next();// targeters
+                               // targeters
+                               for (InstructionTargeter t : ih.getTargeters()) {
                                        // [
                                        // i
                                        // ]
@@ -1708,9 +1707,8 @@ public final class LazyMethodGen implements Traceable {
 //     }
 
        private static void assertTargetedBy(InstructionHandle target, InstructionTargeter targeter, String from) {
-               Iterator tIter = target.getTargeters().iterator();
-               while (tIter.hasNext()) {
-                       if (((InstructionTargeter) tIter.next()) == targeter) {
+               for (InstructionTargeter instructionTargeter : target.getTargeters()) {
+                       if (instructionTargeter == targeter) {
                                return;
                        }
                }
index fa162c5264d0ef6ce424935d9e86ca662f641dbc..ebcb4251cf583e095f434a6843b276768395e6fd 100644 (file)
@@ -194,9 +194,7 @@ abstract class Range implements InstructionTargeter {
        protected static final Range getRange(InstructionHandle ih) {
                // assert isRangeHandle(ih)
                Range ret = null;
-               Iterator<InstructionTargeter> tIter = ih.getTargeters().iterator();
-               while (tIter.hasNext()) {
-                       InstructionTargeter targeter = tIter.next();
+               for (InstructionTargeter targeter : ih.getTargeters()) {
                        if (targeter instanceof Range) {
                                Range r = (Range) targeter;
                                if (r.getStart() != ih && r.getEnd() != ih) {
index 47c8400c54c391e130fa27492d2b638c6f97611c..966699c04f5430bfd5090b7dd1e8f315ce00cb84 100644 (file)
@@ -190,9 +190,7 @@ final class ShadowRange extends Range {
                // now go through again and update variable slots that have been altered as a result
                // of remapping...
                for (InstructionHandle newIh = freshBody.getStart(); newIh != freshBody.getEnd(); newIh = newIh.getNext()) {
-                       Iterator<InstructionTargeter> tIter = newIh.getTargeters().iterator();
-                       while (tIter.hasNext()) {
-                               InstructionTargeter source = tIter.next();
+                       for (InstructionTargeter source : newIh.getTargeters()) {
                                if (source instanceof LocalVariableTag) {
                                        LocalVariableTag lvt = (LocalVariableTag) source;
                                        if (!lvt.isRemapped() && remap.hasKey(lvt.getSlot())) {
index 7279070bc0c1feb1da91aae4df3021bc5cfdaec1..bb4a38817cc80760ad015cbdf493812dfcf228da 100644 (file)
@@ -580,9 +580,7 @@ public class Utility {
                        if (ih == null) {
                                return -1;
                        }
-                       Iterator<InstructionTargeter> tIter = ih.getTargeters().iterator();
-                       while (tIter.hasNext()) {
-                               InstructionTargeter t = tIter.next();
+                       for (InstructionTargeter t : ih.getTargeters()) {
                                if (t instanceof LineNumberTag) {
                                        return ((LineNumberTag) t).getLineNumber();
                                }