eclipse-jdt / eclipse-jdt/eclipse.jdt.core

MethodHandle::invokeExact infers wrong return type

Open
#1,267 2 comments 0 reactions 1 assignee Claimed by @srikanth-sankaran View on GitHub
Dominant language
Java
Stars
237
Forks
195
Avg merge
1d 12h
Merged PRs (30d)
47

Description

The following code runs OK (no exception thrown) in openjdk Red_Hat-20.0.1.0.9-2.rolling.fc38
In Eclipse, it fails with a `WrongMethodTypeException`. The method reference `mh::invokeExact` should take the `void` return type from the implemented interface, but ends up with `Object` instead.

```java
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.util.function.BiConsumer;

public class TestWrongMethodType {
public static void main(final String[] args) throws Exception {
final var mh = MethodHandles.lookup()
.findVirtual(TestWrongMethodType.class, "setProp", MethodType.methodType(TestWrongMethodType.class, String.class))
.asType(MethodType.methodType(void.class, Object.class, Object.class));
uncheckedBiConsumer(mh::invokeExact).accept(new TestWrongMethodType(), "42");
}

public TestWrongMethodType setProp(final String value) {
return this;
}

public static BiConsumer uncheckedBiConsumer(final CheckedBiConsumer checkedBiConsumer) {
return (x, y) -> {
try {
checkedBiConsumer.accept(x, y);
} catch (final Throwable t) {
throw new RuntimeException(t);
}
};
}

public interface CheckedBiConsumer {
void accept(X x, Y y) throws Throwable;
}
}
```

In Eclipse JDT 3.19.100.v20230605-0440, this fails:

```
Exception in thread "main" java.lang.RuntimeException: java.lang.invoke.WrongMethodTypeException: expected (Object,Object)void but found (Object,Object)Object
at org.jdbi.v3.core.mapper.TestWrongMethodType.lambda$1(TestWrongMethodType.java:37)
at org.jdbi.v3.core.mapper.TestWrongMethodType.main(TestWrongMethodType.java:25)
Caused by: java.lang.invoke.WrongMethodTypeException: expected (Object,Object)void but found (Object,Object)Object
at java.base/java.lang.invoke.Invokers.newWrongMethodTypeException(Invokers.java:523)
at java.base/java.lang.invoke.Invokers.checkExactType(Invokers.java:532)
at org.jdbi.v3.core.mapper.TestWrongMethodType.lambda$1(TestWrongMethodType.java:35)
... 1 more
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.