java - Lambda/default methods/type erasure quirk/bug using ECJ? -
came accross today , spent ages trying reproduce/figure out happening. can explain why happens or bug type erasure/default methods/lambda's/polymorphism? uncommenting default method makes run fine, have expected work is
output:
works fine object calling consume hello calling accept context hello calling accept via consumer... exception in thread "main" java.lang.abstractmethoderror: method test/lambdatest$$lambda$1.accept(ljava/lang/object;)v abstract @ test.lambdatest$$lambda$1/834600351.accept(unknown source) @ test.lambdatest.main(lambdatest.java:24)
code
package test; import java.util.function.consumer; public class lambdatest { public static void main(string[] args) { consumer<context> contextignoringobject = new contextunawareobject(); contextignoringobject.accept(new context()); contextignorer contextignoringlambda = () -> { system.err.println("hello"); }; system.err.println("calling consume"); contextignoringlambda.consume(); system.err.println("calling accept context"); contextignoringlambda.accept(new context()); consumer<context> consumer = contextignoringlambda; system.err.println("calling accept via consumer..."); consumer.accept(new context()); } @functionalinterface public interface contextignorer extends consumer<context> { // default void accept(object object) { // system.err.println("manual bridge method"); // accept((context)object); // } @override default void accept(context context) { consume(); } void consume(); } public static class contextunawareobject implements contextignorer { @override public void consume() { system.err.println("works fine object"); } } public static class context { } }
the problem appears older ecj compiler (3.10.0):
$ java -jar org.eclipse.jdt.core-3.10.0.v20140604-1726.jar -source 1.8 lambdatest.java $ java lambdatest works fine object calling consume hello calling accept context hello calling accept via consumer... exception in thread "main" java.lang.abstractmethoderror: method lambdatest$$lambda$1.accept(ljava/lang/object;)v abstract @ lambdatest$$lambda$1/424058530.accept(unknown source) @ lambdatest.main(lambdatest.java:24)
using org.eclipse.jdt.core_3.10.0.v20140902-0626.jar or newer solves problem. oracle javac compiler has no such problem. solution update ecj compiler or move javac.
Comments
Post a Comment