On Friday, November 14, 2014 1:14:08 PM UTC+1, Andreas Kohn wrote:
>
> Hi,
>
> While trying to find the cause for a very odd NPE I noticed that 
> Configuration#setCategories() is implemented like this
>
>     public Builder setCategories(Class<?>... categories) {
>       toReturn.categories =
>           Collections.unmodifiableList(new 
> ArrayList<Class<?>>(Arrays.asList(categories)));
>       return this;
>     }
>
> Could someone explain why there is an explicit creation of the ArrayList 
> done there? Both the result of Arrays.asList() and a java.util.ArrayList 
> implement
> RandomAccess, so for Collections#unmodifiableList() nothing really should 
> change there.
>

This is legal Java:

Class<?>[] classes = new Classes<?>[] { foo, bar };
builder.setCategories(classes);
classes[1] = baz;

This explicit copy prevents the builder (actually and more importantly its 
toReturn) from being modified from the outside: toReturn.categories is 
guaranteed to contain foo and bar, and not baz.

This is a standard pattern. Not doing the copy would actually be flagged as 
a possible source of error by things like Checkstyle and/or Findbugs (can't 
remember which ones flags this).

-- 
You received this message because you are subscribed to the Google Groups "GWT 
Contributors" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/google-web-toolkit-contributors/eae2d98d-b20a-4331-9ed0-8dd075895a7c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to