From a85308ec103a69a86a8e02adf02112d472b00568 Mon Sep 17 00:00:00 2001 From: Lars Grefer Date: Sat, 15 Aug 2020 16:06:32 +0200 Subject: [PATCH] Manual array to collection copy Reports the copying of array contents to a collection where each element is added individually using a for loop. Such constructs may be replaced by a call to Collection.addAll(Arrays.asList()) or Collections.addAll(). Signed-off-by: Lars Grefer --- .../src/main/java/org/aspectj/tools/ajdoc/Main.java | 13 ++----------- .../aspectj/apache/bcel/classfile/JavaClass.java | 11 ++--------- .../compiler/lookup/AjLookupEnvironment.java | 13 ++----------- .../internal/compiler/batch/CommandTestCase.java | 9 ++------- .../src/test/java/org/aspectj/tools/ajc/Ajc.java | 5 ++--- .../java/org/aspectj/tools/ajc/AjcTestCase.java | 4 +--- .../weaver/AbstractReferenceTypeDelegate.java | 9 +++------ .../internal/tools/ant/taskdefs/Ajctest.java | 12 ++---------- .../org/aspectj/testing/XMLBasedAjcTestCase.java | 4 +--- .../org/aspectj/testing/util/options/Values.java | 4 +--- .../org/aspectj/weaver/bcel/BcelObjectType.java | 8 ++------ 11 files changed, 20 insertions(+), 72 deletions(-) diff --git a/ajdoc/src/main/java/org/aspectj/tools/ajdoc/Main.java b/ajdoc/src/main/java/org/aspectj/tools/ajdoc/Main.java index 26857b41c..57b7bce6e 100644 --- a/ajdoc/src/main/java/org/aspectj/tools/ajdoc/Main.java +++ b/ajdoc/src/main/java/org/aspectj/tools/ajdoc/Main.java @@ -22,14 +22,7 @@ import java.io.FileOutputStream; import java.io.FileReader; import java.io.FilenameFilter; import java.io.IOException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Hashtable; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; -import java.util.StringTokenizer; -import java.util.Vector; +import java.util.*; import org.aspectj.asm.AsmManager; import org.aspectj.bridge.IMessage; @@ -262,9 +255,7 @@ public class Main implements Config { javadocargs[numExtraArgs + options.size() + packageList.size() + k] = fileList.elementAt(k); } options = new Vector<>(); - for (String a: javadocargs) { - options.add(a); - } + Collections.addAll(options, javadocargs); } else { javadocargs = new String[options.size() + signatureFiles.length]; for (int k = 0; k < options.size(); k++) { diff --git a/bcel-builder/src/main/java/org/aspectj/apache/bcel/classfile/JavaClass.java b/bcel-builder/src/main/java/org/aspectj/apache/bcel/classfile/JavaClass.java index dd1097ff0..bb42a07ee 100644 --- a/bcel-builder/src/main/java/org/aspectj/apache/bcel/classfile/JavaClass.java +++ b/bcel-builder/src/main/java/org/aspectj/apache/bcel/classfile/JavaClass.java @@ -60,12 +60,7 @@ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; -import java.util.ArrayList; -import java.util.Collection; -import java.util.LinkedList; -import java.util.List; -import java.util.Queue; -import java.util.StringTokenizer; +import java.util.*; import org.aspectj.apache.bcel.Constants; import org.aspectj.apache.bcel.classfile.annotation.AnnotationGen; @@ -791,9 +786,7 @@ public class JavaClass extends Modifiers implements Cloneable, Node { } } - for (JavaClass anInterface : interfaces) { - queue.add(anInterface); - } + Collections.addAll(queue, interfaces); } return interfaceList; diff --git a/org.aspectj.ajdt.core/src/main/java/org/aspectj/ajdt/internal/compiler/lookup/AjLookupEnvironment.java b/org.aspectj.ajdt.core/src/main/java/org/aspectj/ajdt/internal/compiler/lookup/AjLookupEnvironment.java index d79755dde..809a73af8 100644 --- a/org.aspectj.ajdt.core/src/main/java/org/aspectj/ajdt/internal/compiler/lookup/AjLookupEnvironment.java +++ b/org.aspectj.ajdt.core/src/main/java/org/aspectj/ajdt/internal/compiler/lookup/AjLookupEnvironment.java @@ -13,14 +13,7 @@ package org.aspectj.ajdt.internal.compiler.lookup; import java.lang.reflect.Modifier; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Set; +import java.util.*; import org.aspectj.ajdt.internal.compiler.CommonPrinter; import org.aspectj.ajdt.internal.compiler.ast.AspectDeclaration; @@ -254,9 +247,7 @@ public class AjLookupEnvironment extends LookupEnvironment implements AnonymousC for (int i = lastCompletedUnitIndex + 1; i <= lastUnitIndex; i++) { CompilationUnitScope cus = units[i].scope; SourceTypeBinding[] stbs = cus.topLevelTypes; - for (SourceTypeBinding stb : stbs) { - typesToProcess.add(stb); - } + Collections.addAll(typesToProcess, stbs); } List stb2 = new ArrayList<>(); diff --git a/org.aspectj.ajdt.core/src/test/java/org/aspectj/ajdt/internal/compiler/batch/CommandTestCase.java b/org.aspectj.ajdt.core/src/test/java/org/aspectj/ajdt/internal/compiler/batch/CommandTestCase.java index 1a3cc12b2..6e163efc0 100644 --- a/org.aspectj.ajdt.core/src/test/java/org/aspectj/ajdt/internal/compiler/batch/CommandTestCase.java +++ b/org.aspectj.ajdt.core/src/test/java/org/aspectj/ajdt/internal/compiler/batch/CommandTestCase.java @@ -14,10 +14,7 @@ package org.aspectj.ajdt.internal.compiler.batch; import java.io.File; import java.io.IOException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Iterator; -import java.util.List; +import java.util.*; import junit.framework.TestCase; @@ -74,9 +71,7 @@ public abstract class CommandTestCase extends TestCase { args.add("-g"); // XXX need this to get sourcefile and line numbers, shouldn't - for (String extraArg : extraArgs) { - args.add(extraArg); - } + Collections.addAll(args, extraArgs); args.add(Constants.TESTDATA_PATH + "/" + source); diff --git a/org.aspectj.ajdt.core/src/test/java/org/aspectj/tools/ajc/Ajc.java b/org.aspectj.ajdt.core/src/test/java/org/aspectj/tools/ajc/Ajc.java index 15fdf0d9f..138e6538c 100644 --- a/org.aspectj.ajdt.core/src/test/java/org/aspectj/tools/ajc/Ajc.java +++ b/org.aspectj.ajdt.core/src/test/java/org/aspectj/tools/ajc/Ajc.java @@ -17,6 +17,7 @@ import java.io.FilenameFilter; import java.io.IOException; import java.io.PrintStream; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.StringTokenizer; @@ -310,9 +311,7 @@ public class Ajc { } private void addMessagesTo(List aList, IMessage[] messages) { - for (IMessage message : messages) { - aList.add(message); - } + Collections.addAll(aList, messages); } private boolean isIncremental(String[] args) { diff --git a/org.aspectj.ajdt.core/src/test/java/org/aspectj/tools/ajc/AjcTestCase.java b/org.aspectj.ajdt.core/src/test/java/org/aspectj/tools/ajc/AjcTestCase.java index d5238b477..518f944c8 100644 --- a/org.aspectj.ajdt.core/src/test/java/org/aspectj/tools/ajc/AjcTestCase.java +++ b/org.aspectj.ajdt.core/src/test/java/org/aspectj/tools/ajc/AjcTestCase.java @@ -523,9 +523,7 @@ public abstract class AjcTestCase extends TestCase { */ protected List newMessageList(Message[] messages) { List ret = new ArrayList(); - for (Message message : messages) { - ret.add(message); - } + Collections.addAll(ret, messages); return ret; } diff --git a/org.aspectj.matcher/src/main/java/org/aspectj/weaver/AbstractReferenceTypeDelegate.java b/org.aspectj.matcher/src/main/java/org/aspectj/weaver/AbstractReferenceTypeDelegate.java index 2d969c2f8..763f78f0b 100644 --- a/org.aspectj.matcher/src/main/java/org/aspectj/weaver/AbstractReferenceTypeDelegate.java +++ b/org.aspectj.matcher/src/main/java/org/aspectj/weaver/AbstractReferenceTypeDelegate.java @@ -13,6 +13,7 @@ package org.aspectj.weaver; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import org.aspectj.bridge.ISourceLocation; @@ -135,15 +136,11 @@ public abstract class AbstractReferenceTypeDelegate implements ReferenceTypeDele AbstractReferenceTypeDelegate outerObjectType = (AbstractReferenceTypeDelegate) outerDelegate; if (outerObjectType.isNested()) { GenericSignature.FormalTypeParameter[] parentParams = outerObjectType.getFormalTypeParametersFromOuterClass(); - for (GenericSignature.FormalTypeParameter parentParam : parentParams) { - typeParameters.add(parentParam); - } + Collections.addAll(typeParameters, parentParams); } GenericSignature.ClassSignature outerSig = outerObjectType.getGenericClassTypeSignature(); if (outerSig != null) { - for (FormalTypeParameter formalTypeParameter : outerSig.formalTypeParameters) { - typeParameters.add(formalTypeParameter); - } + Collections.addAll(typeParameters, outerSig.formalTypeParameters); } GenericSignature.FormalTypeParameter[] ret = new GenericSignature.FormalTypeParameter[typeParameters.size()]; diff --git a/testing/src/test/java/org/aspectj/internal/tools/ant/taskdefs/Ajctest.java b/testing/src/test/java/org/aspectj/internal/tools/ant/taskdefs/Ajctest.java index 546eadba3..98878dcbc 100644 --- a/testing/src/test/java/org/aspectj/internal/tools/ant/taskdefs/Ajctest.java +++ b/testing/src/test/java/org/aspectj/internal/tools/ant/taskdefs/Ajctest.java @@ -36,13 +36,7 @@ import java.io.PrintWriter; import java.io.StringWriter; import java.text.DateFormat; //import java.util.Collection; -import java.util.Date; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.StringTokenizer; -import java.util.Vector; +import java.util.*; import javax.swing.BorderFactory; import javax.swing.BoxLayout; @@ -1426,9 +1420,7 @@ public class Ajctest extends Task implements PropertyChangeListener { public AjdocWrapper(Testset testset, List args) { super(testset, ajdocArgs(args), true); String[] cmds = testset.getAjdoc().getCommandline().getCommandline(); - for (String cmd : cmds) { - this.args.add(cmd); - } + Collections.addAll(this.args, cmds); } String getMainClassName() { return "org.aspectj.tools.ajdoc.Main"; diff --git a/testing/src/test/java/org/aspectj/testing/XMLBasedAjcTestCase.java b/testing/src/test/java/org/aspectj/testing/XMLBasedAjcTestCase.java index e5e2fcdb9..81e2616ea 100644 --- a/testing/src/test/java/org/aspectj/testing/XMLBasedAjcTestCase.java +++ b/testing/src/test/java/org/aspectj/testing/XMLBasedAjcTestCase.java @@ -415,9 +415,7 @@ public abstract class XMLBasedAjcTestCase extends AjcTestCase { public List sortedLocalVariables(LocalVariableTable lvt) { List l = new ArrayList<>(); LocalVariable lv[] = lvt.getLocalVariableTable(); - for (LocalVariable lvEntry : lv) { - l.add(lvEntry); - } + Collections.addAll(l, lv); Collections.sort(l, new MyComparator()); return l; } diff --git a/testing/src/test/java/org/aspectj/testing/util/options/Values.java b/testing/src/test/java/org/aspectj/testing/util/options/Values.java index 7a04fbed0..608d4a610 100644 --- a/testing/src/test/java/org/aspectj/testing/util/options/Values.java +++ b/testing/src/test/java/org/aspectj/testing/util/options/Values.java @@ -398,9 +398,7 @@ public class Values { String s = value.prefix.render(output[0]); if (null != s) { // this means the prefix is set list.add(s); - for (int j = 1; j < output.length; j++) { - list.add(output[j]); - } + list.addAll(Arrays.asList(output).subList(1, output.length)); } } } diff --git a/weaver/src/main/java/org/aspectj/weaver/bcel/BcelObjectType.java b/weaver/src/main/java/org/aspectj/weaver/bcel/BcelObjectType.java index 3e9ac1b75..47984dfdf 100644 --- a/weaver/src/main/java/org/aspectj/weaver/bcel/BcelObjectType.java +++ b/weaver/src/main/java/org/aspectj/weaver/bcel/BcelObjectType.java @@ -771,12 +771,8 @@ public class BcelObjectType extends AbstractReferenceTypeDelegate { GenericSignature.FormalTypeParameter[] extraFormals = getFormalTypeParametersFromOuterClass(); if (extraFormals.length > 0) { List allFormals = new ArrayList<>(); - for (FormalTypeParameter formalTypeParameter : formalsForResolution) { - allFormals.add(formalTypeParameter); - } - for (FormalTypeParameter extraFormal : extraFormals) { - allFormals.add(extraFormal); - } + Collections.addAll(allFormals, formalsForResolution); + Collections.addAll(allFormals, extraFormals); formalsForResolution = new GenericSignature.FormalTypeParameter[allFormals.size()]; allFormals.toArray(formalsForResolution); } -- 2.39.5