[HS] why creation of array of parametrized type is unsafe

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

[HS] why creation of array of parametrized type is unsafe

Remi Forax
It's a totally HS question, but i know that some Java gurus listen that
list.

It's perhaps a stupid question, but i don't see why array of
parametrized type
is unsafe.

see my blog on that :
http://weblogs.java.net/blog/forax/archive/2006/11/why_array_of_pa.html

Rémi

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: [HS] why creation of array of parametrized type is unsafe

Joshua Bloch-2
Rémi,

Because the the array that is created is generally *not* an array of T (the type parameter), but of its bound.  Automatically generated casts at the call site can fail.  Run this program (which compiles with a warning) and you'll get the idea.  The program was written by Peter van der Ahe:



public class GenericVarargs {
   static <T> T[] m1(T t1, T t2) {
       return m2(t1, t2);
   }
   static <T> T[] m2(T... args) {
       return args;
   }
   public static void main(String... args) {
       String[] strings = m1("bad", "karma");
   }
}



On 11/9/06, Rémi Forax <[hidden email]> wrote:
It's a totally HS question, but i know that some Java gurus listen that
list.

It's perhaps a stupid question, but i don't see why array of
parametrized type
is unsafe.

see my blog on that :
<a href="http://weblogs.java.net/blog/forax/archive/2006/11/why_array_of_pa.html" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)"> http://weblogs.java.net/blog/forax/archive/2006/11/why_array_of_pa.html

Rémi

_______________________________________________
Concurrency-interest mailing list
[hidden email]
<a href="http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest


_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: [HS] why creation of array of parametrized type is unsafe

Dhanji R. Prasanna
A similar problem exists with parameterized collections--at runtime I can add objects not of a reified type via reflection (unless using a checkedSet or list..which throws CCE).
Is this true?

On 11/10/06, Joshua Bloch <[hidden email]> wrote:
Rémi,

Because the the array that is created is generally *not* an array of T (the type parameter), but of its bound.  Automatically generated casts at the call site can fail.  Run this program (which compiles with a warning) and you'll get the idea.  The program was written by Peter van der Ahe:



public class GenericVarargs {
   static <T> T[] m1(T t1, T t2) {
       return m2(t1, t2);
   }
   static <T> T[] m2(T... args) {
       return args;
   }
   public static void main(String... args) {
       String[] strings = m1("bad", "karma");

   }
}



On 11/9/06, Rémi Forax <[hidden email]> wrote:
It's a totally HS question, but i know that some Java gurus listen that
list.

It's perhaps a stupid question, but i don't see why array of
parametrized type
is unsafe.

see my blog on that :
<a href="http://weblogs.java.net/blog/forax/archive/2006/11/why_array_of_pa.html" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)"> http://weblogs.java.net/blog/forax/archive/2006/11/why_array_of_pa.html

Rémi

_______________________________________________
Concurrency-interest mailing list
[hidden email]
<a href="http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest


_______________________________________________
Concurrency-interest mailing list
[hidden email]
<a onclick="return top.js.OpenExtLink(window,event,this)" href="http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest" target="_blank">http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest




_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: [HS] why creation of array of parametrized type is unsafe

Joshua Bloch-2
Dhanji,
 
No, collections are very different.  You can create a collections whose elements are of a non-reified type:
 
   List<List<String>> lls = new ArrayList<List<String>>();
 
As you know, the array analogue is illegal:
   
    List<String>[] als = new List<String>[SIZE];
 
It is, however, the case that you don't get exceptions at run time is you insert an element of the wrong type (unless you're using a checked collection). One way to look at this is that Collections give you static type safety but not runtime, and arrays give you runtime type safety but not static.
 
        Josh
 
On 11/9/06, Dhanji R. Prasanna <[hidden email]> wrote:
A similar problem exists with parameterized collections--at runtime I can add objects not of a reified type via reflection (unless using a checkedSet or list..which throws CCE).
Is this true?


On 11/10/06, Joshua Bloch <[hidden email]> wrote:
Rémi,

Because the the array that is created is generally *not* an array of T (the type parameter), but of its bound.  Automatically generated casts at the call site can fail.  Run this program (which compiles with a warning) and you'll get the idea.  The program was written by Peter van der Ahe:



public class GenericVarargs {
   static <T> T[] m1(T t1, T t2) {
       return m2(t1, t2);
   }
   static <T> T[] m2(T... args) {
       return args;
   }
   public static void main(String... args) {
       String[] strings = m1("bad", "karma");

   }
}



On 11/9/06, Rémi Forax <[hidden email]> wrote:
It's a totally HS question, but i know that some Java gurus listen that
list.

It's perhaps a stupid question, but i don't see why array of
parametrized type
is unsafe.

see my blog on that :
<a onclick="return top.js.OpenExtLink(window,event,this)" href="http://weblogs.java.net/blog/forax/archive/2006/11/why_array_of_pa.html" target="_blank"> http://weblogs.java.net/blog/forax/archive/2006/11/why_array_of_pa.html

Rémi

_______________________________________________
Concurrency-interest mailing list
[hidden email]
<a onclick="return top.js.OpenExtLink(window,event,this)" href="http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest" target="_blank">http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest


_______________________________________________
Concurrency-interest mailing list
[hidden email]
<a onclick="return top.js.OpenExtLink(window,event,this)" href="http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest" target="_blank">http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest





_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: [HS] why creation of array of parametrized type is unsafe

Remi Forax
In reply to this post by Dhanji R. Prasanna
Dhanji R. Prasanna a écrit :
> A similar problem exists with parameterized collections--at runtime I
> can add objects not of a reified type via reflection (unless using a
> checkedSet or list..which throws CCE).
> Is this true?
Reflection and generics doesn't play well together, by example, the
following code is
safe but generates a CCE at le last line.

public static void main(String[] args) throws IllegalAccessException,
InvocationTargetException, NoSuchMethodException {
      ArrayList<String> list=new ArrayList<String>();
     
      List.class.getMethod("add",Object.class).invoke(list,3);
     
      list.get(0).charAt(0);
}

so it is another problem.

Rémi

>
> On 11/10/06, *Joshua Bloch* <[hidden email] <mailto:[hidden email]>> wrote:
>
>     Rémi,
>
>     Because the the array that is created is generally *not* an array
>     of T (the type parameter), but of its bound.  Automatically
>     generated casts at the call site can fail.  Run this program
>     (which compiles with a warning) and you'll get the idea.  The
>     program was written by Peter van der Ahe:
>
>
>
>     public class GenericVarargs {
>        static <T> T[] m1(T t1, T t2) {
>            return m2(t1, t2);
>        }
>        static <T> T[] m2(T... args) {
>            return args;
>        }
>        public static void main(String... args) {
>            String[] strings = m1("bad", "karma");
>
>        }
>     }
>
>
>
>     On 11/9/06, *Rémi Forax * < [hidden email]
>     <mailto:[hidden email]>> wrote:
>
>         It's a totally HS question, but i know that some Java gurus
>         listen that
>         list.
>
>         It's perhaps a stupid question, but i don't see why array of
>         parametrized type
>         is unsafe.
>
>         see my blog on that :
>         http://weblogs.java.net/blog/forax/archive/2006/11/why_array_of_pa.html
>
>         Rémi
>
>         _______________________________________________
>         Concurrency-interest mailing list
>         [hidden email]
>         <mailto:[hidden email]>
>         http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
>         <http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest>
>
>
>
>     _______________________________________________
>     Concurrency-interest mailing list
>     [hidden email]
>     <mailto:[hidden email]>
>     http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
>
>
>


_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest