Thodoris Sotiropoulos created GROOVY-11447:
----------------------------------------------
Summary: Using a conditional when expecting a SAM type leads to
type unsafe results
Key: GROOVY-11447
URL: https://issues.apache.org/jira/browse/GROOVY-11447
Project: Groovy
Issue Type: Bug
Components: Static Type Checker
Reporter: Thodoris Sotiropoulos
I have the following program
{code:java}
@FunctionalInterface
interface Executor {
void execute(Runnable command);
}
class A {
private Executor exec;
A(Executor exec) { this.exec = exec; }
void execute(Runnable command) {
this.exec.execute(command);
}
}
class Test {
public static void main(String[] args) {
Executor exec = null;
A a = new A((true) ? ""::indexOf : (y, z) -> 1);
a.execute(() -> {});
}
} {code}
h3. Actual behavior
The code compiles, but I receive the following exception at runtime
{code:java}
Exception in thread "main" groovy.lang.MissingMethodException: No signature of
method: java.lang.String.indexOf() is applicable for argument types:
(Test$$Lambda/0x00000008010a16b8) values:
[Test$$Lambda/0x00000008010a16b8@2ea227af]
Possible solutions: indexOf(int), indexOf(java.lang.String), indexOf(int, int),
indexOf(java.lang.String, int), indent(int), intern()
at
org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:73)
at
org.codehaus.groovy.runtime.ConversionHandler.invoke(ConversionHandler.java:115)
at jdk.proxy2/jdk.proxy2.$Proxy5.execute(Unknown Source)
at A.execute(test.groovy:12)
at Test.main(test.groovy:21) {code}
h3. Expected behavior
The code should have been rejected.
h3. Notes
The code is rejected as expected when using the following cases:
{code:java}
A a = new A((y, z) -> 1); // the code is rejected
A a = new A(""::indexOf); // the code is rejected
Executor exec = null;
A a = new A((true) ? exec : (y, z) -> 1); // the code is rejected
Executor exec = (true) ? ""::indexOf : (y, z) -> 1; // the code is
rejected{code}
--
This message was sent by Atlassian Jira
(v8.20.10#820010)