-
-
Notifications
You must be signed in to change notification settings - Fork 351
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix Interface method handling #2023
Fix Interface method handling #2023
Conversation
Filip, welcome! And thank you for this high quality PR 👍 |
@@ -124,6 +119,9 @@ public String getSimpleName() { | |||
lambdaExecutableMethod = lambdaTypeMethods.iterator().next(); | |||
} else { | |||
for (CtMethod<?> method : lambdaTypeMethods) { | |||
if (getFactory().Method().OBJECT_METHODS.contains(method)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. |
@@ -403,6 +404,18 @@ public void testLambdaFilter() throws Exception { | |||
assertHasStrings(methodNames, "m2", "m4", "m7", "m8"); | |||
} | |||
|
|||
@Test | |||
public void testInterfaceWithObjectMethods() throws Exception { | |||
CtInterface<?> checkPersons = factory.Interface().get(Foo.CheckPersons.class); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done :)
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