hg: valhalla/valhalla/langtools: BytecodeMapping generation fixes:

Peter Levart peter.levart at gmail.com
Tue Jan 6 17:27:22 UTC 2015


On 01/06/2015 02:00 PM, maurizio.cimadamore at oracle.com wrote:
> Changeset: 8331682af2ed
> Author:    mcimadamore
> Date:      2015-01-06 12:54 +0000
> URL:       http://hg.openjdk.java.net/valhalla/valhalla/langtools/rev/8331682af2ed
>
> BytecodeMapping generation fixes:
> * missing BMA for eq/ne any comparisons nested within logic operators &&/||
> * missing BMA for field/method access immediately following a constructor call
> * refactor Items code in order to avoid cast to AnyItem in order to access type info associated with a given item
> * added tests
>
> ! src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/Gen.java
> ! src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/Items.java
> + test/tools/javac/valhalla/typespec/items/tests/TestBinary.java
> ! test/tools/javac/valhalla/typespec/items/tests/TestInstanceof.java
> ! test/tools/javac/valhalla/typespec/items/tests/TestNew.java
>

Thanks Maurizio,

It works now. But I have another (seems like javac) problem. Since 
layers are not yet implemented (or I just can't seem to figure out how 
to use them), I tried to use __WhereRef(T) and __WhereVal(T) with 
inheritance to simulate them. My 1st goal was to create a Box<any T> 
class that would implement Object.equals() in a way that would use 
Objects.equals() for reference T and '==' for value T. Here's what I 
came up with:

// the Ref "layer"

abstract class RefBoxBase<any T> {

     public abstract T get();

     public __WhereRef(T) int hashCode() {
         return Objects.hashCode(get());
     }

     public __WhereRef(T) boolean equals(Object obj) {
         return (this == obj) ||
             (obj != null &&
                 this.getClass() == obj.getClass() &&
                 Objects.equals(this.get(), ((RefBoxBase<T>) obj).get())
             );
     }
}

// the Val "overlay"

public final class Box<any T> extends RefBoxBase<T> {

     private T value;

     public Box(T value) {
         this.value = value;
     }

     public Box() {
         // leave default value
     }

     public T get() {
         return value;
     }

     public __WhereVal(T) int hashCode() {
         return 0; // don't know yet how to do something meaningful here
     }

     public __WhereVal(T) boolean equals(Object obj) {
         return (this == obj) ||
             (obj != null &&
                 this.getClass() == obj.getClass() &&
                 this.get() == ((Box<T>)obj).get());
     }
}


And the test:

public class Test {
     public static void main(String[] args) {
         System.out.println(new Box<int>(1).equals(new Box<int>(1))); // 
this compiles fine

         System.out.println(new Box<String>("a").equals(new 
Box<String>("a"))); // compilation error
     }
}

src/util/Test.java:6: error: bad receiver type Box<String> in restricted 
method call
         System.out.println(new Box<String>("a").equals(new 
Box<String>("a")));
                                                       ^
Note: src/util/RefBoxBase.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error


It's true that Box.equals() is restricted for Val receiver, but I 
thought that RefBoxBase.equals() would be "uncovered" in this case an 
inherited in Ref specialization.



Regards, Peter




More information about the valhalla-dev mailing list