I have some code with a method reference that compiles fine and fails at runtime.
The exception is this:
Caused by: java.lang.invoke.LambdaConversionException: Invalid receiver type class redacted.BasicEntity; not a subtype of implementation type interface redacted.HasImagesEntity at java.lang.invoke.AbstractValidatingLambdaMetafactory.validateMetafactoryArgs(AbstractValidatingLambdaMetafactory.java:233) at java.lang.invoke.LambdaMetafactory.metafactory(LambdaMetafactory.java:303) at java.lang.invoke.CallSite.makeSite(CallSite.java:289)
The class triggering the exception:
class ImageController<E extends BasicEntity & HasImagesEntity> { void doTheThing(E entity) { Set<String> filenames = entity.getImages().keySet().stream() .map(entity::filename) .collect(Collectors.toSet()); } }
The exception is thrown trying to resolve entity::filename
. filename()
is declared in HasImagesEntity
. As far as I can tell, I get the exception because the erasure of E is BasicEntity
and the JVM doesn't (can't?) consider other bounds on E.
When I rewrite the method reference as a trivial lambda, everything is fine. It seems really fishy to me that one construct works as expected and its semantic equivalent blows up.
Could this possibly be in the spec? I'm trying very hard to find a way for this not to be a problem in the compiler or runtime, and haven't come up with anything.
Here is a simplified example which reproduces the problem and uses only core Java classes:
public static void main(String[] argv) { System.out.println(dummy("foo")); } static <T extends Serializable&CharSequence> int dummy(T value) { return Optional.ofNullable(value).map(CharSequence::length).orElse(0); }
Your assumption is correct, the JRE-specific implementation receives the target method as a MethodHandle
which has no information about generic types. Therefore the only thing it sees is that the raw types mismatch.
Like with a lot of generic constructs, there is a type cast required on the byte code level which doesn’t appear in the source code. Since LambdaMetafactory
explicitly requires a direct method handle, a method reference which encapsulates such a type cast cannot be passed as a MethodHandle
to the factory.
There are two possible ways to deal with it.
First solution would be to change the LambdaMetafactory
to trust the MethodHandle
if the receiver type is an interface
and insert the required type cast by itself in the generated lambda class instead of rejecting it. After all, it does similar for parameter and return types already.
Alternatively, the compiler would be in charge to create a synthetic helper method encapsulating the type cast and method call, just like if you had written a lambda expression. This is not a unique situation. If you use a method reference to a varargs method or an array creation like, e.g. String[]::new
, they can’t be expressed as direct method handles and end up in synthetic helper methods.
In either case, we can consider the current behavior a bug. But obviously, compiler and JRE developers must agree on which way it should be handled before we can say on which side the bug resides.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With