|
|
||
Rémi Forax's BlogMay 2008 ArchivesParameterized type are NOT inherently unsafePosted by forax on May 27, 2008 at 06:17 AM | Permalink | Comments (6)
Time to time, i heard that sentence
"array of generics a inherently unsafe"
or a variation.
Array of generics ? First, there are two kinds of "generics", type variable and parameterized type, and they behave differently. An example of array of type variable:
class A<T> { // T is a type variable
T[] m() { // T[] is an array of type variable
...
}
}
...
A<String> a =new A<String>();
Here, T is a type variable. A>String> is a parameterized type, it's an instantiation of A<T> with T = String.
As Gilad Bracha in the
generics tutorial
wrote
"The component type of an array object may not be a type variable or a parameterized type, unless it is an (unbounded) wildcard type",
Why ? Let's take the example given by Gilad: List<String>[] lsa = new List<String>[10]; // not really allowed Object o = lsa; Object[] oa = (Object[]) o; List<Integer> li = new ArrayList<Integer>(); li.add(new Integer(3)); oa[1] = li; // problem here String s = lsa[1].get(0);
Generics in Java are not reified, i.e. at runtime there
is no difference between a List<String> and
a List<Integer>, they are all List.
So the VM can perform runtime check to distinguish
between the two parameterized type.
So our example is erased to: List[] lsa = new List[10]; // not really allowed Object o = lsa; Object[] oa = (Object[]) o; List li = new ArrayList(); li.add(new Integer(3)); oa[1] = li; // problem but not detected here String s = lsa[1].get(0); // run-time error - ClassCastException So when VM execute the line oa[1] = li; it doesn't detect a problem. The VM generate a ClassCastException later here at the next line. To avoid to have a code that can generate a CCE without any warning, the JSR15 experts decide that array of parameterized type can not be created. Here comes the pain
Because you often need to create an array of parametrized type,
you have to use painful workaround.
An example of such tricks, the creation of an array of parametrized type is replaced by the creation of an array of wildcard then a cast.
List<String>[] list =
(List<String>[])new ArrayList<?>[1]; // warning
These workaround codes are very dangerous because if one day in the future generics are reified (JDK8 :), these codes will not work anymore and throw a ClassCastException. So what ?
So array creation is prohibited because it can lead
to unsafe code. I think it's time to say:
In general terms, the problem is that when converting an array of parameterized type to a type without type argument, the type is lost (by definition) so neither the compiler nor the VM (because of the erasure) can garantee the type safety. Unsafe convertion
Using a JLS like wording,
let T and U such as U is the declared type of u and T is
defined by, T t=u;
If this rule is added, creation of array of parameterized type is safe and then can be allowed. And what about the backward compatibility ?
With this new rule, program that compile will continue to
compile. Workarounds are not needed any more.
But some code that currently don't raise a warning
can now raise a warning.
SelectionKey key = ... List<String> list = ... key.attach(list); // will generate a new warning It's not a big deal because the following code already generate a warning.
List<String> list =
(List<String>)key.attachment(); // already generate a warning
The second case is more problematic List<String> list = ... System.out.println(list); // warning
Here, println take an Object, so the parameterized list
is assigned to an Object.
Hum, such warning is less desirable.
public void println(@SuppressWarnings("parameterized-type") Object x) {
I wish and hope, these changes can be included in JDK7. Is array of type variable inherently unsafe ? Yes, array of type variable are inherently unsafe. See the following code.
class A<T> {
T[] m() {
return new T[1]; // erasure create a, array of Object
}
}
...
A<String> a = new A<String>();
String[] s = a.m(); // oups
cheers,
Rémi Javac + invokedynamicPosted by forax on May 22, 2008 at 06:37 AM | Permalink | Comments (0)Just for fun, this morning, i've patched the java compiler to be able to generate classes that use invokedynamic instead of invokevirtual/invokeinterface when invoking a method. following the JSR292 EDR The patch is based on the source of the langtools repository of the hotspot project, so to apply the patch, first clone the repository hg clone http://hg.openjdk.java.net/jdk7/hotspot/langtools/An then apply the following patch invokedynamic.patch then run ant in langtools/make. Now, you have a patched javac that will insert an invokedynamic instead of an invokevirtual when calling a method tagged by @InvokeDynamic.
import java.dyn.InvokeDynamic;
public class Test {
@InvokeDynamic
public void m(String message) {
System.out.println(message);
}
public static void main(String[] args) {
// here m is invoked dynamically
new Test().m("hello invoke dynamic");
}
}
And now, how to run it.
Cheers,
| ||
|
|