]> source.dussan.org Git - aspectj.git/commitdiff
Collection.toArray() call style
authorLars Grefer <eclipse@larsgrefer.de>
Sat, 15 Aug 2020 14:30:09 +0000 (16:30 +0200)
committerLars Grefer <eclipse@larsgrefer.de>
Sat, 15 Aug 2020 14:36:00 +0000 (16:36 +0200)
There are two styles to convert a collection to an array: either using a pre-sized array (like c.toArray(new String[c.size()])) or using an empty array (like c.toArray(new String[0]).
In older Java versions using pre-sized array was recommended, as the reflection call which is necessary to create an array of proper size was quite slow. However since late updates of OpenJDK 6 this call was intrinsified, making the performance of the empty array version the same and sometimes even better, compared to the pre-sized version. Also passing pre-sized array is dangerous for a concurrent or synchronized collection as a data race is possible between the size and toArray call which may result in extra nulls at the end of the array, if the collection was concurrently shrunk during the operation.

Signed-off-by: Lars Grefer <eclipse@larsgrefer.de>
29 files changed:
ajde.core/src/main/java/org/aspectj/ajde/core/internal/AjdeCoreBuildManager.java
ajdoc/src/main/java/org/aspectj/tools/ajdoc/HtmlDecorator.java
bcel-builder/src/main/java/org/aspectj/apache/bcel/classfile/JavaClass.java
bcel-builder/src/main/java/org/aspectj/apache/bcel/generic/ClassGen.java
build/src/main/java/org/aspectj/internal/tools/build/SampleGatherer.java
loadtime/src/main/java/org/aspectj/weaver/loadtime/ConcreteAspectCodeGen.java
org.aspectj.ajdt.core/src/main/java/org/aspectj/ajdt/ajc/BuildArgParser.java
org.aspectj.ajdt.core/src/main/java/org/aspectj/ajdt/internal/compiler/ast/AstUtil.java
org.aspectj.ajdt.core/src/main/java/org/aspectj/ajdt/internal/compiler/lookup/EclipseScope.java
org.aspectj.ajdt.core/src/main/java/org/aspectj/ajdt/internal/compiler/lookup/EclipseSourceType.java
org.aspectj.ajdt.core/src/main/java/org/aspectj/ajdt/internal/compiler/lookup/InterTypeMemberFinder.java
org.aspectj.ajdt.core/src/test/java/org/aspectj/ajdt/internal/compiler/batch/BasicCommandTestCase.java
org.aspectj.ajdt.core/src/test/java/org/aspectj/ajdt/internal/compiler/batch/CommandTestCase.java
org.aspectj.matcher/src/main/java/org/aspectj/weaver/MemberImpl.java
org.aspectj.matcher/src/main/java/org/aspectj/weaver/ResolvedType.java
org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/AnnotationPatternList.java
org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/BasicTokenSource.java
org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/IfPointcut.java
org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/TypePatternList.java
org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/WildTypePattern.java
org.aspectj.matcher/src/test/java/org/aspectj/weaver/CommonWorldTests.java
org.aspectj.matcher/src/test/java/org/aspectj/weaver/TestUtils.java
taskdefs/src/main/java/org/aspectj/tools/ant/taskdefs/compilers/Ajc.java
testing/src/test/java/org/aspectj/testing/util/options/Values.java
util/src/main/java/org/aspectj/util/FileUtil.java
weaver/src/main/java/org/aspectj/weaver/bcel/AtAjAttributes.java
weaver/src/main/java/org/aspectj/weaver/bcel/BcelObjectType.java
weaver/src/main/java/org/aspectj/weaver/bcel/LazyMethodGen.java
weaver/src/main/java/org/aspectj/weaver/tools/cache/AbstractIndexedFileCacheBacking.java

index 49948db46511a77f14b8018957daa42bb4c816cd..a8b82ce7f2067a1dddf17f3189f923762bd785a6 100644 (file)
@@ -256,7 +256,7 @@ public class AjdeCoreBuildManager {
                                }
                                args[p++] = "-xmlConfigured";
                        } else {
-                               args = l.toArray(new String[l.size()]);
+                               args = l.toArray(new String[0]);
                        }
                }
 
index 41d13e37e9937686811d650160eb61039070864c..42d029e06b2f89dccf9effc633f665225eb729aa 100644 (file)
@@ -897,7 +897,7 @@ class HtmlDecorator {
                };
 
                file.walk(walker);
-               return (IProgramElement[]) nodes.toArray(new IProgramElement[nodes.size()]);
+               return (IProgramElement[]) nodes.toArray(new IProgramElement[0]);
        }
 
        /**
index bb42a07ee444bede102382efedf1261f66bae229..014ad558d31651b313db030940da568e164611cb 100644 (file)
@@ -741,7 +741,7 @@ public class JavaClass extends Modifiers implements Cloneable, Node {
                for (clazz = clazz.getSuperClass(); clazz != null; clazz = clazz.getSuperClass()) {
                        vec.add(clazz);
                }
-               return vec.toArray(new JavaClass[vec.size()]);
+               return vec.toArray(new JavaClass[0]);
        }
 
        /**
index 371013997e2d2606d13a3a0f1e08625bdeb824a2..03c7c716b785d0bc1edb00fc35556be45ba40886 100644 (file)
@@ -196,7 +196,7 @@ public class ClassGen extends Modifiers implements Cloneable {
                ConstantPool cp = this.cpool.getFinalConstantPool();
 
                return new JavaClass(classnameIndex, superclassnameIndex, filename, major, minor, modifiers, cp, interfaces, fields,
-                               methods, attributes.toArray(new Attribute[attributes.size()]));// OPTIMIZE avoid toArray()?
+                               methods, attributes.toArray(new Attribute[0]));// OPTIMIZE avoid toArray()?
        }
 
        public void addInterface(String name) {
index ce7f2ce27ba853be1331767c4a32487036ffde4e..86782563f8466bab4dd0407a54823273452bb42a 100644 (file)
@@ -221,7 +221,7 @@ public class SampleGatherer {
                             source,
                             startLine,
                             endLine,
-                            flags.toArray(new String[flags.size()]));
+                            flags.toArray(new String[0]));
                     sink.addSample(sample);
 
                     // back to seeking start
@@ -978,7 +978,7 @@ class SampleUtil {
         }
         next  = anchorName.substring(start);
         result.add(next);
-        return result.toArray(new String[result.size()]);
+        return result.toArray(new String[0]);
     }
     /**
      * Replace literals with literals in source string
index fcc89ac75515276c0d89f48b2bd9bcec980c3095..f89d30c87f91b4577f784a4b43f727b40c8cf3ff 100644 (file)
@@ -910,8 +910,7 @@ public class ConcreteAspectCodeGen {
                }
 
                // Time to construct the method itself:
-               LazyMethodGen advice = new LazyMethodGen(Modifier.PUBLIC, returnType, adviceName, paramTypes.toArray(new Type[paramTypes
-                               .size()]), EMPTY_STRINGS, cg);
+               LazyMethodGen advice = new LazyMethodGen(Modifier.PUBLIC, returnType, adviceName, paramTypes.toArray(new Type[0]), EMPTY_STRINGS, cg);
 
                InstructionList adviceBody = advice.getBody();
 
index 07332990f765b7bbae9b74efeb9028a5f3ec0b83..a68698697599d05f46ba119b35e165c2762c7ab7 100644 (file)
@@ -177,7 +177,7 @@ public class BuildArgParser extends Main {
 //                     javaArgList.add("-bootclasspath");
 //                     javaArgList.add(parser.bootclasspath == null ? System.getProperty("user.dir") : parser.bootclasspath);
                        javaArgList.addAll(parser.getUnparsedArgs());
-                       super.configure(javaArgList.toArray(new String[javaArgList.size()]));
+                       super.configure(javaArgList.toArray(new String[0]));
 
                        if (parser.getModuleInfoArgument() != null) {
                                IModule moduleDesc = super.getModuleDesc(parser.getModuleInfoArgument());
index 369e84c51d798aa6184c151878b9ce2a9905a9c8..6dc405653f1884d3e8ff9ee9093ea686c32c679c 100644 (file)
@@ -112,7 +112,7 @@ public class AstUtil {
        }
 
        public static void setStatements(MethodDeclaration ret, List statements) {
-               ret.statements = (Statement[]) statements.toArray(new Statement[statements.size()]);
+               ret.statements = (Statement[]) statements.toArray(new Statement[0]);
        }
 
        public static SingleNameReference makeLocalVariableReference(LocalVariableBinding binding) {
index 06191629fce51560eb4f5fbbb5e34b44d32e74ed..74805b9c6962d8f5a77445457d8a3aecbd173f6b 100644 (file)
@@ -197,9 +197,9 @@ public class EclipseScope implements IScope {
                        importedNamesList.add(world.fromBinding(topType).getName());
                }
 
-               importedNames = importedNamesList.toArray(new String[importedNamesList.size()]);
+               importedNames = importedNamesList.toArray(new String[0]);
 
-               importedPrefixes = importedPrefixesList.toArray(new String[importedPrefixesList.size()]);
+               importedPrefixes = importedPrefixesList.toArray(new String[0]);
        }
 
        private void addClassAndParentsToPrefixes(ReferenceBinding binding, List<String> importedPrefixesList) {
index 1941ad6304758f769c32def779e2c110c34e4e8d..4731a86ed540a01803167ac09fa67f42bacf4366 100644 (file)
@@ -349,9 +349,9 @@ public class EclipseSourceType extends AbstractReferenceTypeDelegate {
                        declaredFields.add(factory.makeResolvedMember(f));
                }
 
-               this.declaredPointcuts = declaredPointcuts.toArray(new ResolvedPointcutDefinition[declaredPointcuts.size()]);
-               this.declaredMethods = declaredMethods.toArray(new ResolvedMember[declaredMethods.size()]);
-               this.declaredFields = declaredFields.toArray(new ResolvedMember[declaredFields.size()]);
+               this.declaredPointcuts = declaredPointcuts.toArray(new ResolvedPointcutDefinition[0]);
+               this.declaredMethods = declaredMethods.toArray(new ResolvedMember[0]);
+               this.declaredFields = declaredFields.toArray(new ResolvedMember[0]);
        }
        
        private final static char[] valuesCharArray = "values".toCharArray();
index 80ffe4222812d1384fcf2436493e4a76c347cfee..1953265c8b6e21cd864605da164ea94a1a580109 100644 (file)
@@ -270,7 +270,7 @@ public class InterTypeMemberFinder implements IMemberFinder {
                if (ret.isEmpty()) {
                        return Binding.NO_METHODS;
                }
-               return ret.toArray(new MethodBinding[ret.size()]);
+               return ret.toArray(new MethodBinding[0]);
        }
 
        private void addPublicITDSFrom(SourceTypeBinding anInterface, List<MethodBinding> accumulator) {
@@ -328,7 +328,7 @@ public class InterTypeMemberFinder implements IMemberFinder {
 
                // System.err.println("got methods: " + ret + " on " + sourceTypeBinding);
 
-               return ret.toArray(new MethodBinding[ret.size()]);
+               return ret.toArray(new MethodBinding[0]);
        }
 
        @Override
index 99ea9b1d95cdbd8fce5422eea66960121750b6ea..48a255e91fcbcca518d0420f0926541bff5ece93 100644 (file)
@@ -116,13 +116,14 @@ public class BasicCommandTestCase extends CommandTestCase {
                args.add(getSandboxName());
                
                args.add("-classpath");
-               args.add(getRuntimeClasspath() + File.pathSeparator +\r                  "../lib/junit/junit.jar;../testing-client/bin;not_found_anywhere.jar");
+               args.add(getRuntimeClasspath() + File.pathSeparator +
+                       "../lib/junit/junit.jar;../testing-client/bin;not_found_anywhere.jar");
                args.add(Constants.TESTDATA_PATH + "/src1/ThisAndModifiers.java");
                
                ICommand command = new AjdtCommand();
                MessageHandler myHandler = new MessageHandler();
                //myHandler.setInterceptor(org.aspectj.tools.ajc.Main.MessagePrinter.TERSE);
-               /*boolean result = */command.runCommand((String[])args.toArray(new String[args.size()]), myHandler);
+               /*boolean result = */command.runCommand((String[])args.toArray(new String[0]), myHandler);
 
                //System.err.println("messages: " + Arrays.asList(myHandler.getMessages(IMessage.INFO, true)));
                // DON'T yet have a way of testing that we actually got a particular info message
@@ -142,7 +143,7 @@ public class BasicCommandTestCase extends CommandTestCase {
                ICommand command = new AjdtCommand();
                MessageHandler myHandler = new MessageHandler();
                myHandler.setInterceptor(org.aspectj.tools.ajc.Main.MessagePrinter.TERSE);
-               /*boolean result = */command.runCommand((String[])args.toArray(new String[args.size()]), myHandler);
+               /*boolean result = */command.runCommand((String[])args.toArray(new String[0]), myHandler);
 
                assertEquals("error for org.aspectj.lang.JoinPoint not found", 1, myHandler.getErrors().length);
        }
index 6e163efc067f6272fe16a801385aef46d0137923..9e8deb0a13f61afa9debb2812964342efcfe0892 100644 (file)
@@ -112,7 +112,7 @@ public abstract class CommandTestCase extends TestCase {
                ICommand command = new AjdtCommand();
                MessageHandler myHandler = new MessageHandler();
                myHandler.setInterceptor(org.aspectj.tools.ajc.Main.MessagePrinter.TERSE);
-               boolean result = command.runCommand((String[]) args.toArray(new String[args.size()]), myHandler);
+               boolean result = command.runCommand((String[]) args.toArray(new String[0]), myHandler);
                System.out.println("result: " + result);
                // System.out.println("errors: " + Arrays.asList(myHandler.getErrors()));
                // System.out.println("warnings: " + Arrays.asList(myHandler.getWarnings()));
index 86caa48c5b8372fe9a21fbd8ed3bd0586ca96204..d7bd742b82d61ecb304efe2ab75fb5032bc7aade 100644 (file)
@@ -211,7 +211,7 @@ public class MemberImpl implements Member {
                                        l.add(UnresolvedType.forSignature(sig.substring(start, i)));
                                }
                        }
-                       UnresolvedType[] paramTypes = l.toArray(new UnresolvedType[l.size()]);
+                       UnresolvedType[] paramTypes = l.toArray(new UnresolvedType[0]);
                        UnresolvedType returnType = UnresolvedType.forSignature(sig.substring(i + 1, sig.length()));
                        return new Object[] { returnType, paramTypes };
                } else {
index 3d88279af0b1b029dad74ad6043773894ea134bf..293a7766c769ac1a6853d0ff9d9bc8bbd2aa7e80 100644 (file)
@@ -1082,7 +1082,7 @@ public abstract class ResolvedType extends UnresolvedType implements AnnotatedEl
                                l.add(m);
                        }
                }
-               return l.toArray(new ResolvedMember[l.size()]);
+               return l.toArray(new ResolvedMember[0]);
        }
 
        public abstract ISourceContext getSourceContext();
index 2cc737347d6ea7f680bc0b7fb63fc7aa04f51702..f906a397e9174d80761b10fdfabe0b9be2f477b5 100644 (file)
@@ -50,7 +50,7 @@ public class AnnotationPatternList extends PatternNode {
        }
 
        public AnnotationPatternList(List<AnnotationTypePattern> l) {
-               this((AnnotationTypePattern[]) l.toArray(new AnnotationTypePattern[l.size()]));
+               this((AnnotationTypePattern[]) l.toArray(new AnnotationTypePattern[0]));
        }
 
        protected AnnotationTypePattern[] getAnnotationPatterns() {
index ef537d8072b43de75faccf3c7b1a6b614977bed0..15713e76d20e235a9bf51307c2dab86185e73700 100644 (file)
@@ -160,7 +160,7 @@ public class BasicTokenSource implements ITokenSource {
 
                //System.out.println(tokens);
                
-               return new BasicTokenSource((IToken[])tokens.toArray(new IToken[tokens.size()]), context);
+               return new BasicTokenSource((IToken[])tokens.toArray(new IToken[0]), context);
        }
 
        private static String makeString(char ch) {
index 8c2cb36b92d5538234893369c3c616f9b010e5fe..4e614824a247bebfeded5ff94375e711b7368adb 100644 (file)
@@ -308,7 +308,7 @@ public class IfPointcut extends Pointcut {
                                }
                        }
 
-                       ret = Test.makeAnd(ret, Test.makeCall(testMethod, (Expr[]) args.toArray(new Expr[args.size()])));
+                       ret = Test.makeAnd(ret, Test.makeCall(testMethod, (Expr[]) args.toArray(new Expr[0])));
 
                        // Remember...
                        ifLastMatchedShadowId = shadow.shadowId;
index 2b17520a2d11214c8175345dd31be11b45b2a683..fdbc40d1230f74b6f413afd844817e0802753b17 100644 (file)
@@ -59,7 +59,7 @@ public class TypePatternList extends PatternNode {
        }
 
        public TypePatternList(List<TypePattern> l) {
-               this((TypePattern[]) l.toArray(new TypePattern[l.size()]));
+               this((TypePattern[]) l.toArray(new TypePattern[0]));
        }
 
        public int size() {
index c5f44ae0fe20bf2b967c4286ec4e582d6d4e9914..b8c4ac31b90b977a80f4948fa4b26731b0c3ff00 100644 (file)
@@ -110,7 +110,7 @@ public class WildTypePattern extends TypePattern {
        }
 
        public WildTypePattern(List<NamePattern> names, boolean includeSubtypes, int dim) {
-               this((NamePattern[]) names.toArray(new NamePattern[names.size()]), includeSubtypes, dim, false, TypePatternList.EMPTY);
+               this((NamePattern[]) names.toArray(new NamePattern[0]), includeSubtypes, dim, false, TypePatternList.EMPTY);
 
        }
 
@@ -127,7 +127,7 @@ public class WildTypePattern extends TypePattern {
 
        public WildTypePattern(List<NamePattern> names, boolean includeSubtypes, int dim, int endPos, boolean isVarArg, TypePatternList typeParams,
                        TypePattern upperBound, TypePattern[] additionalInterfaceBounds, TypePattern lowerBound) {
-               this((NamePattern[]) names.toArray(new NamePattern[names.size()]), includeSubtypes, dim, isVarArg, typeParams);
+               this((NamePattern[]) names.toArray(new NamePattern[0]), includeSubtypes, dim, isVarArg, typeParams);
                this.end = endPos;
                this.upperBound = upperBound;
                this.lowerBound = lowerBound;
@@ -135,7 +135,7 @@ public class WildTypePattern extends TypePattern {
        }
 
        public WildTypePattern(List<NamePattern> names, boolean includeSubtypes, int dim, int endPos, boolean isVarArg, TypePatternList typeParams) {
-               this((NamePattern[]) names.toArray(new NamePattern[names.size()]), includeSubtypes, dim, isVarArg, typeParams);
+               this((NamePattern[]) names.toArray(new NamePattern[0]), includeSubtypes, dim, isVarArg, typeParams);
                this.end = endPos;
        }
 
@@ -1169,7 +1169,7 @@ public class WildTypePattern extends TypePattern {
                                }
                        }
                }
-               return ret.toArray(new String[ret.size()]);
+               return ret.toArray(new String[0]);
        }
 
        // public void postRead(ResolvedType enclosingType) {
index 9389fff8be8b91e1f3b3d27c8411f209b2cc746b..613437346d0cac7296cc6f1057a59ebe5e25647f 100644 (file)
@@ -189,7 +189,7 @@ public abstract class CommonWorldTests extends TestCase {
 
        protected void mungersTest(ResolvedType ty, ShadowMunger[] x) {
                List<ShadowMunger> l = ty.getDeclaredShadowMungers();
-               ShadowMunger[] array = (ShadowMunger[]) l.toArray(new ShadowMunger[l.size()]);
+               ShadowMunger[] array = (ShadowMunger[]) l.toArray(new ShadowMunger[0]);
                TestUtil.assertSetEquals(ty + " mungers:", x, array);
        }
 
index 9f7daa756ff88b2c17adeacbfcc6cb4498987d89..69bc674ef95860d9bf64404d7d28116db1bbdfec 100644 (file)
@@ -298,7 +298,7 @@ public class TestUtils {
                        l.add(str.substring(start, i).trim());
                        start = i + 1;
                }
-               return (String[]) l.toArray(new String[l.size()]);
+               return (String[]) l.toArray(new String[0]);
        }
 
 }
index 022dcae22904f04453ceba5758186e2ab70a6313..25128bb61c228152b7b0236e9d6907765bcddb91 100644 (file)
@@ -138,7 +138,7 @@ public class Ajc extends DefaultCompilerAdapter {
                 argsList.add(args[i]);
             }
         }
-        return (String[])argsList.toArray(new String[argsList.size()]);
+        return (String[])argsList.toArray(new String[0]);
     }
     
     /**
index af81692b7da07bed38ce07e46ad2389a0abb3305..de35c8c67a0da4ec5fb708d9d6d768aafbd4d460 100644 (file)
@@ -402,7 +402,7 @@ public class Values {
                                }
                        }
                }
-        return (String[]) list.toArray(new String[list.size()]);
+        return (String[]) list.toArray(new String[0]);
     }
 
     private final Option.Value[] values;
index 976c3ccbcefe28381c7a97f8f2fb922cc26f62f5..a586b69f69999941ad0df378b8fcca8a2cfad1ab 100644 (file)
@@ -710,7 +710,7 @@ public class FileUtil {
                if ((null != srcDir) && srcDir.canRead()) {
                        listFiles(srcDir, result, fileFilter);
                }
-               return result.toArray(new File[result.size()]);
+               return result.toArray(new File[0]);
        }
 
        /**
index 96222fba2dc0ac863ce14451fd9df51270f4a1d0..5f20327049b2b37e337d012f9c25974021d4ca16 100644 (file)
@@ -1905,7 +1905,7 @@ public class AtAjAttributes {
                                ignores.add(formalBinding.getName());
                        }
                }
-               pointcut.m_ignoreUnboundBindingForNames = ignores.toArray(new String[ignores.size()]);
+               pointcut.m_ignoreUnboundBindingForNames = ignores.toArray(new String[0]);
        }
 
        /**
index 47984dfdffe1bcfd27acf1f23ca58bb29e37574c..922769dc0e780c63e3073e758d29a22cacd0f139 100644 (file)
@@ -399,7 +399,7 @@ public class BcelObjectType extends AbstractReferenceTypeDelegate {
                if (pointcuts.size() == 0) {
                        this.pointcuts = ResolvedPointcutDefinition.NO_POINTCUTS;
                } else {
-                       this.pointcuts = pointcuts.toArray(new ResolvedPointcutDefinition[pointcuts.size()]);
+                       this.pointcuts = pointcuts.toArray(new ResolvedPointcutDefinition[0]);
                }
 
                resolveAnnotationDeclares(l);
index 3ab0c5daf51a149e742bf8a1bb85907be781fa0e..7e2541e6c7be200ff5725dfb6b1276a2c38c99ae 100644 (file)
@@ -336,7 +336,7 @@ public final class LazyMethodGen implements Traceable {
        public AnnotationAJ[] getAnnotations() {
                initialize();
                if (memberView == null && newAnnotations!=null && newAnnotations.size()!=0) {
-                       return newAnnotations.toArray(new AnnotationAJ[newAnnotations.size()]);
+                       return newAnnotations.toArray(new AnnotationAJ[0]);
                }
                return null;
        }
index cb05b10e056ebe664789bd5178c720448a4faaed..32dba3d90aca3ebab21f2e84c7baecccfb82282f 100644 (file)
@@ -71,7 +71,7 @@ public abstract class AbstractIndexedFileCacheBacking extends AbstractFileCacheB
         if (matches.isEmpty()) {
             return EMPTY_KEYS;
         } else {
-            return matches.toArray(new String[matches.size()]);
+            return matches.toArray(new String[0]);
         }
     }