getFunctionalInterfaceMethod() fails to recognize method override
On the latest master (f31a2f7). Here's a patch to produce a failing test:
diff --git a/nullaway/src/test/java/com/uber/nullaway/NullAwayTest.java b/nullaway/src/test/java/com/uber/nullaway/NullAwayTest.java
index e318ff0..1863775 100644
--- a/nullaway/src/test/java/com/uber/nullaway/NullAwayTest.java
+++ b/nullaway/src/test/java/com/uber/nullaway/NullAwayTest.java
@@ -139,4 +139,9 @@ public class NullAwayTest {
public void functionalMethodSuperInterface() {
compilationHelper.addSourceFile("NullAwaySuperFunctionalInterface.java").doTest();
}
+
+ @Test
+ public void functionalMethodOverrideSuperInterface() {
+ compilationHelper.addSourceFile("NullAwayOverrideFunctionalInterfaces.java").doTest();
+ }
}
diff --git a/nullaway/src/test/resources/com/uber/nullaway/testdata/NullAwayOverrideFunctionalInterfaces.java b/nullaway/src/test/resources/com/uber/nullaway/testdata/NullAwayOverrideFunctionalInterfaces.java
index e69de29..9383bef 100644
--- a/nullaway/src/test/resources/com/uber/nullaway/testdata/NullAwayOverrideFunctionalInterfaces.java
+++ b/nullaway/src/test/resources/com/uber/nullaway/testdata/NullAwayOverrideFunctionalInterfaces.java
@@ -0,0 +1,27 @@
+package com.uber.nullaway.testdata;
+
+import java.util.function.ToIntFunction;
+
+public class NullAwayOverrideFunctionalInterfaces {
+
+ public void test() {
+ call(str -> 42);
+ }
+
+ private int call(ObjToInt<String> f) {
+ return f.call("The answer to life the universe and everything");
+ }
+
+ @FunctionalInterface
+ private static interface ObjToInt<T> extends ObjToIntE<T, RuntimeException>, ToIntFunction<T> {
+ @Override
+ default int applyAsInt(T t) {
+ return call(t);
+ }
+ }
+
+ @FunctionalInterface
+ private static interface ObjToIntE<T, E extends Exception> {
+ int call(T t) throws E;
+ }
+}
Here's the relevant portion of the error stacktrace:
java.lang.AssertionError: An unhandled exception was thrown by the Error Prone static analysis plugin.
Please report this at https://github.com/google/error-prone/issues/new and include the following:
error-prone version: 2.1.1
Stack Trace:
java.lang.RuntimeException: already found an answer! call(T) applyAsInt(T) applyAsInt(T)
at com.uber.nullaway.NullabilityUtil.getFunctionalInterfaceMethod(NullabilityUtil.java:87)
at com.uber.nullaway.NullAway.matchLambdaExpression(NullAway.java:422)
Ah, I see the issue. Handling default methods correctly is going to be a bit tricky here, since inheriting interfaces can both "redeclare" them, making them abstract once again; or redefine them, making abstract methods into default methods. To handle this properly, we need to know which is the "effective" version of the method. @msridhar Do we have any guarantees that types.closure will return the types in any particular order? If so, we could keep track of which method definitions we have seen and ignore all but the first/nearest definition seen. If not, then it is a bit more involved.
So reading the source of closure I'm pretty sure it returns subtypes before supertypes. So probably we can fix our code by just returning the first valid result it finds, rather than always going through all supertypes and failing on overrides.
That sounds good to me, assuming two things: 1) That we can rely on that implementation not changing, 2) That the java compiler itself guarantees each functional interface has a single abstract method. Can put up a PR if we are reasonably certain of those :)
Thanks for the report. I'll try to cut a new release soon
Thank you!
Also, I'll formally license the test code I provided here under the same terms that Uber has licensed the rest of NullAway:
Copyright (c) 2017 Brandon Mintern
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.