This is the mail archive of the
kawa@sourceware.org
mailing list for the Kawa project.
Re: Parameterizing on incomplete types
On Dec 1, 2011, at 5:32 PM, Per Bothner wrote:
At all, as far as I can remember.
[snip]
That's one kind of bridge method.
There's another related kind when a parameterized type is
instantiated.
In your example, the Kawa compiler generates the equivalent of
public int compareTo(Foo o)
{
return x - o.x;
}
However, the JVM knows nothing of generic parameters The
Comparable interface
takes an Object parameter, and the method taking a Foo does not
match for dynamic
dispatch. Hence we need a synthetic/bridge method to implement the
interface:
public int compareTo(Object o) { return compareTo((Foo) o); }
Right.
OK, so in summary, classes generated by Kawa (at least as of r7092 or
so) do not
have any bridge methods, but that usually doesn't matter and so for
most code the
Kawa-compiled classes work just fine. It just so happens, though, that
the way
Java generics are defined/implemented requires synthetic bridge
methods to be
inserted by the compiler, and so it is now an issue.
The two cases we've mentioned are (1) covariant return types and (2)
parameterized
type erasure requiring methods with the erased signature (which is
sort of like a
covariant parameter type). Are there others? The JLS book appears to
have exactly
one mention of bridge methods, in Chapter 15 [1], and it's a
combination of the two:
class C<T> { abstract T id(T x); }
class D extends C<String> { String id(String x) { return x; } }
C c = new D();
c.id(new Object()); // fails with a ClassCastException
noting that the compiler would insert into D the bridge method
Object id(Object x) { return id((String) x); }
and that this is what would actually trigger the class cast exception.
For the most part, the Internet seems a little hazy on the whole issue.
Thanks, Per!
-Jamie
[1] http://java.sun.com/docs/books/jls/third_edition/html/expressions.html
--
Jamison Hope
The PTR Group
www.theptrgroup.com