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/src/test/java | |
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/src/test/java')
-rw-r--r-- | org.aspectj.matcher/src/test/java/org/aspectj/weaver/CommonWorldTests.java | 2 | ||||
-rw-r--r-- | org.aspectj.matcher/src/test/java/org/aspectj/weaver/TestUtils.java | 2 |
2 files changed, 2 insertions, 2 deletions
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]); } } |