jsweet icon indicating copy to clipboard operation
jsweet copied to clipboard

No error, `compare` called instead of `reversed` on Comparator

Open rendaw opened this issue 3 years ago • 0 comments

The following code should raise an error that the method reversed doesn't exist:

public class ThingA {
  private static Comparator<Integer> z = build().reversed();

  public static Comparator<Integer> build() {
    return new X();
  }

  public static class X implements Comparator<Integer> {
    @Override
    public int compare(Integer integer, Integer t1) {
      throw new RuntimeException();
    }
  }
}

Instead it generates code that calls compare during initialization at runtime, with no error. Here's the code that was generated:

/* Generated from Java with JSweet 2.4.0-SNAPSHOT - http://www.jsweet.org */
namespace com.zarbosoft.jsweettest.moda {
    export class ThingA {
        static z : java.util.Comparator<number>; public static z_$LI$() : java.util.Comparator<number> { if (ThingA.z == null) { ThingA.z = <any>(((target => (target['reversed'] === undefined)?target:target['reversed'])(ThingA.build()))()); }return ThingA.z; };

        public static build() : java.util.Comparator<number> {
            return (arg0, arg1) => { return new ThingA.X().compare(arg0, arg1); };
        }
    }
    ThingA["__class"] = "com.zarbosoft.jsweettest.moda.ThingA";


    export namespace ThingA {

        export class X {
            /**
             * 
             * @param {number} integer
             * @param {number} t1
             * @return {number}
             */
            public compare(integer : number, t1 : number) : number {
                throw new java.lang.RuntimeException();
            }

            constructor() {
            }
        }
        X["__class"] = "com.zarbosoft.jsweettest.moda.ThingA.X";
        X["__interfaces"] = ["java.util.Comparator"];


    }

}


com.zarbosoft.jsweettest.moda.ThingA.z_$LI$();

This doesn't happen if X is instantiated directly at the initialization of x - the misrouting occurs only when a static method instantiates the object.

Since lots of the standard library isn't implemented in j4ts, this could silently introduce unexpected behavior.

rendaw avatar Feb 07 '21 14:02 rendaw