diff options
author | Lars Grefer <eclipse@larsgrefer.de> | 2020-08-15 16:30:09 +0200 |
---|---|---|
committer | Lars Grefer <eclipse@larsgrefer.de> | 2020-08-15 16:36:00 +0200 |
commit | 3641f1626df6b9b1c11dd3f16b01a01495f4662d (patch) | |
tree | 2957ce458f23462b6ec0777ddded5d854233d880 /org.aspectj.ajdt.core/src | |
parent | 2debfa05f43a6fe71e7bc141f4f78c66f85c95b0 (diff) | |
download | aspectj-3641f1626df6b9b1c11dd3f16b01a01495f4662d.tar.gz aspectj-3641f1626df6b9b1c11dd3f16b01a01495f4662d.zip |
Collection.toArray() call style
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>
Diffstat (limited to 'org.aspectj.ajdt.core/src')
7 files changed, 14 insertions, 13 deletions
diff --git a/org.aspectj.ajdt.core/src/main/java/org/aspectj/ajdt/ajc/BuildArgParser.java b/org.aspectj.ajdt.core/src/main/java/org/aspectj/ajdt/ajc/BuildArgParser.java index 07332990f..a68698697 100644 --- a/org.aspectj.ajdt.core/src/main/java/org/aspectj/ajdt/ajc/BuildArgParser.java +++ b/org.aspectj.ajdt.core/src/main/java/org/aspectj/ajdt/ajc/BuildArgParser.java @@ -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()); diff --git a/org.aspectj.ajdt.core/src/main/java/org/aspectj/ajdt/internal/compiler/ast/AstUtil.java b/org.aspectj.ajdt.core/src/main/java/org/aspectj/ajdt/internal/compiler/ast/AstUtil.java index 369e84c51..6dc405653 100644 --- a/org.aspectj.ajdt.core/src/main/java/org/aspectj/ajdt/internal/compiler/ast/AstUtil.java +++ b/org.aspectj.ajdt.core/src/main/java/org/aspectj/ajdt/internal/compiler/ast/AstUtil.java @@ -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) { diff --git a/org.aspectj.ajdt.core/src/main/java/org/aspectj/ajdt/internal/compiler/lookup/EclipseScope.java b/org.aspectj.ajdt.core/src/main/java/org/aspectj/ajdt/internal/compiler/lookup/EclipseScope.java index 06191629f..74805b9c6 100644 --- a/org.aspectj.ajdt.core/src/main/java/org/aspectj/ajdt/internal/compiler/lookup/EclipseScope.java +++ b/org.aspectj.ajdt.core/src/main/java/org/aspectj/ajdt/internal/compiler/lookup/EclipseScope.java @@ -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) { diff --git a/org.aspectj.ajdt.core/src/main/java/org/aspectj/ajdt/internal/compiler/lookup/EclipseSourceType.java b/org.aspectj.ajdt.core/src/main/java/org/aspectj/ajdt/internal/compiler/lookup/EclipseSourceType.java index 1941ad630..4731a86ed 100644 --- a/org.aspectj.ajdt.core/src/main/java/org/aspectj/ajdt/internal/compiler/lookup/EclipseSourceType.java +++ b/org.aspectj.ajdt.core/src/main/java/org/aspectj/ajdt/internal/compiler/lookup/EclipseSourceType.java @@ -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(); diff --git a/org.aspectj.ajdt.core/src/main/java/org/aspectj/ajdt/internal/compiler/lookup/InterTypeMemberFinder.java b/org.aspectj.ajdt.core/src/main/java/org/aspectj/ajdt/internal/compiler/lookup/InterTypeMemberFinder.java index 80ffe4222..1953265c8 100644 --- a/org.aspectj.ajdt.core/src/main/java/org/aspectj/ajdt/internal/compiler/lookup/InterTypeMemberFinder.java +++ b/org.aspectj.ajdt.core/src/main/java/org/aspectj/ajdt/internal/compiler/lookup/InterTypeMemberFinder.java @@ -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 diff --git a/org.aspectj.ajdt.core/src/test/java/org/aspectj/ajdt/internal/compiler/batch/BasicCommandTestCase.java b/org.aspectj.ajdt.core/src/test/java/org/aspectj/ajdt/internal/compiler/batch/BasicCommandTestCase.java index 99ea9b1d9..48a255e91 100644 --- a/org.aspectj.ajdt.core/src/test/java/org/aspectj/ajdt/internal/compiler/batch/BasicCommandTestCase.java +++ b/org.aspectj.ajdt.core/src/test/java/org/aspectj/ajdt/internal/compiler/batch/BasicCommandTestCase.java @@ -116,13 +116,14 @@ public class BasicCommandTestCase extends CommandTestCase { args.add(getSandboxName()); args.add("-classpath"); - args.add(getRuntimeClasspath() + File.pathSeparator +
"../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); } 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 6e163efc0..9e8deb0a1 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 @@ -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())); |