Fix Interface method handling#2023
Conversation
|
Filip, welcome! And thank you for this high quality PR 👍 |
| lambdaExecutableMethod = lambdaTypeMethods.iterator().next(); | ||
| } else { | ||
| for (CtMethod<?> method : lambdaTypeMethods) { | ||
| if (getFactory().Method().OBJECT_METHODS.contains(method)) { |
There was a problem hiding this comment.
I am just not sure whether we can check whether method overrides Object method this way. I guess CtMethod equals compares everythink including body of method. So this fix will probably fail on the class whose sources are in spoon model. Because this method will have body and therefore will be not equal to Object method.
What is correct and fast enough? I am not sure ...
A) to compare method signatures?
B) to use isoverriding method
C) ...
There was a problem hiding this comment.
Thanks for the incredibly fast feedback!
I just tried using CtMethod.isOverriding.
for (CtMethod<?> method : lambdaTypeMethods) {
if (getFactory().Method().OBJECT_METHODS.stream().anyMatch(method::isOverriding)) {
continue;
}
...Unfortunately the method does not return true since the Interface is no subtype of java.lang.Object (Reference).
It seems the only solution is comparing method signatures, as you said.
|
API changes: 1 (Detected by Revapi) Old API: fr.inria.gforge.spoon:spoon-core:jar:6.3.0-20180601.225315-107 / New API: fr.inria.gforge.spoon:spoon-core:jar:6.3.0-SNAPSHOT
|
I think it is a bug, which should be fixed first (in another PR). Then we can use isOverriding here too. |
|
The comparation of signatures doesn't work with java generics class B<T> extends A<T> {
void m(T arg){}
}
class A<U> {
void m(U arg){}
}here B#m overrides A#m, but they have different signatures. So do not invest more effort on signatures, because it is probably wrong way. |
34739fe to
2ed24f6
Compare
|
CtLambdaImpl now checks if the interface's methods are overriding |
|
#2025 was merged, please do some little change in your PR so the next CI build is triggered using latest sources. |
2ed24f6 to
083bd5c
Compare
|
Thanks Pavel! I rebased on top of master and the test succeeded on my local machine. |
|
|
||
| @Test | ||
| public void testInterfaceWithObjectMethods() throws Exception { | ||
| CtInterface<?> checkPersons = factory.Interface().get(Foo.CheckPersons.class); |
There was a problem hiding this comment.
please add contract to the first line of the test body. Something like
//contract: Lambda expression works on interfaces with methods inherited from java.lang.Object
|
Thank You Filip 👍 |
Description
From FunctionalInterface's JavaDoc:
CtLambdaImpl doesn't take this into account resulting in Exceptions whenever an Interface declaring one or more of
java.lang.Object's methods is used in combination with a Lambda Expression.Related Issues