Problem with Method Reference
Maurizio Cimadamore
maurizio.cimadamore at oracle.com
Mon Aug 23 01:12:53 PDT 2010
On 23/08/10 02:03, Howard Lovatt wrote:
> Hi,
>
> I think the following should not compile (because
> String.CASE_INSENSITIVE_ORDER isn't a class but an object):
>
> final String s1 = "Amy";
> final String s2 = "Bec";
> final String[] people = new String[] { s2, s1 };
> Arrays.sort( people, String.CASE_INSENSITIVE_ORDER#compare( String,
> String ) );
>
I don't agree... the above code is not different w.r.t. to
new Foo()#bar
which should compile. The left hand side of an infix # operator can be
either an expression or a qualified type.
> but it does. However when run, you get:
>
> Exception in thread "main" java.dyn.WrongMethodTypeException:
> (Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
> cannot be called as
> (Ljava/dyn/MethodHandle;Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
> at java.dyn.MethodHandle.invokeVarargs(MethodHandle.java:330)
> at com.sun.runtime.ProxyHelper$1.invoke(ProxyHelper.java:57)
> at $Proxy0.compare(Unknown Source)
> at java.util.TimSort.countRunAndMakeAscending(TimSort.java:324)
> at java.util.TimSort.sort(TimSort.java:189)
> at java.util.TimSort.sort(TimSort.java:173)
> at java.util.Arrays.sort(Arrays.java:653)
> at lambdas.Main.lambdas(Main.java:67)
> at lambdas.Main.main(Main.java:19)
>
What happens here is that javac thinks (erroneously) that this is a
static qualifier when it's not - as such it does not prepend the 'this'
argument in the method call... (which is a bug)
Maurizio
>
> -- Howard.
>
> PS Full source, as requested previously, to follow
>
More information about the lambda-dev
mailing list