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.matcher | |
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.matcher')
9 files changed, 12 insertions, 12 deletions
diff --git a/org.aspectj.matcher/src/main/java/org/aspectj/weaver/MemberImpl.java b/org.aspectj.matcher/src/main/java/org/aspectj/weaver/MemberImpl.java index 86caa48c5..d7bd742b8 100644 --- a/org.aspectj.matcher/src/main/java/org/aspectj/weaver/MemberImpl.java +++ b/org.aspectj.matcher/src/main/java/org/aspectj/weaver/MemberImpl.java @@ -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 { diff --git a/org.aspectj.matcher/src/main/java/org/aspectj/weaver/ResolvedType.java b/org.aspectj.matcher/src/main/java/org/aspectj/weaver/ResolvedType.java index 3d88279af..293a7766c 100644 --- a/org.aspectj.matcher/src/main/java/org/aspectj/weaver/ResolvedType.java +++ b/org.aspectj.matcher/src/main/java/org/aspectj/weaver/ResolvedType.java @@ -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(); diff --git a/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/AnnotationPatternList.java b/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/AnnotationPatternList.java index 2cc737347..f906a397e 100644 --- a/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/AnnotationPatternList.java +++ b/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/AnnotationPatternList.java @@ -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() { diff --git a/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/BasicTokenSource.java b/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/BasicTokenSource.java index ef537d807..15713e76d 100644 --- a/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/BasicTokenSource.java +++ b/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/BasicTokenSource.java @@ -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) { diff --git a/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/IfPointcut.java b/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/IfPointcut.java index 8c2cb36b9..4e614824a 100644 --- a/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/IfPointcut.java +++ b/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/IfPointcut.java @@ -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; diff --git a/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/TypePatternList.java b/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/TypePatternList.java index 2b17520a2..fdbc40d12 100644 --- a/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/TypePatternList.java +++ b/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/TypePatternList.java @@ -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() { diff --git a/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/WildTypePattern.java b/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/WildTypePattern.java index c5f44ae0f..b8c4ac31b 100644 --- a/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/WildTypePattern.java +++ b/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/WildTypePattern.java @@ -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) { diff --git a/org.aspectj.matcher/src/test/java/org/aspectj/weaver/CommonWorldTests.java b/org.aspectj.matcher/src/test/java/org/aspectj/weaver/CommonWorldTests.java index 9389fff8b..613437346 100644 --- a/org.aspectj.matcher/src/test/java/org/aspectj/weaver/CommonWorldTests.java +++ b/org.aspectj.matcher/src/test/java/org/aspectj/weaver/CommonWorldTests.java @@ -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); } diff --git a/org.aspectj.matcher/src/test/java/org/aspectj/weaver/TestUtils.java b/org.aspectj.matcher/src/test/java/org/aspectj/weaver/TestUtils.java index 9f7daa756..69bc674ef 100644 --- a/org.aspectj.matcher/src/test/java/org/aspectj/weaver/TestUtils.java +++ b/org.aspectj.matcher/src/test/java/org/aspectj/weaver/TestUtils.java @@ -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]); } } |