eliminate gcjh?

Eric Blake ebb9@byu.net
Tue Mar 9 14:24:00 GMT 2004


Per Bothner wrote:
> Tom Tromey wrote:
> 
>> We're going to be facing some real problems with gcjh once we start
>> looking at 1.5.  For instance, 1.5 has covariant methods,
> 
> Are you sure?  I can't find documentation on this.  Is it considered
> part of one the other new features, such as Generics?

Covariance is an artifact of generics, and is (somewhat poorly) documented in 
JSR 14 (the latest document is jsr14-spec10.pdf, bundled with the 2.4 early 
access prototype implementation).  Not only that, but the 1.5 JDK library uses 
covariant methods, so the 1.5 javac allows covariant methods for ALL -source 
levels (even though generics, varargs, foreach loops, static imports, 
autoboxing, and attributes are only valid at -source 1.5).

Binary compatibility also argues that covariant methods exist.  Jacks has a 
couple of test cases that make sure the compiler doesn't crash when a 
covariant return type is acheived by a sequence of compilations, even in 
pre-1.5 compilers.

Covariance is implemented by bridge methods.  If I write:
class A {
   A foo() { return this; }
}
class B extends A {
   B foo() { return this; } // covariant!
}

The compiler generates:
class A {
   A foo() { return this; } // A.foo()LA;
}
class B {
   B foo() { return this; } // B.foo()LB;
   /*ACC_SYNTHETIC ACC_BRIDGE*/ A foo() { return <B.foo()LB;>; } //B.foo()LA;
}

And a third party class calls:

class C {
   void m()
   {
     A a = new A();
     B b = new B();
     a.foo(); // A.foo()LA;, call stack depth 1
     b.foo(); // B.foo()LB;, call stack depth 1
     a = b;
     a.foo(); // bytecode says A.foo()LA;, VM calls B.foo()LA;,
              // call stack depth 2
   }
}

> 
>  > I don't think you can express this in C++.
> 
> C++ has covariant return types.

However, g++ currently does not correctly implement it in the complex corner 
cases.  There are some issues if you try to mix multiple, virtual inheritance 
with covariance in C++.  And other compilers out there (including some of the 
popular non-free ones) don't do covariance at all, even though it is in the 
standard, so portable C++ should not use covariance.

-- 
Someday, I might put a cute statement here.

Eric Blake             ebb9@byu.net



More information about the Java mailing list