-
Notifications
You must be signed in to change notification settings - Fork 874
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
ConvertToLambda hint should ignore default methods. #6658
Conversation
java/java.hints/src/org/netbeans/modules/java/hints/jdk/ConvertToLambdaPreconditionChecker.java
Outdated
Show resolved
Hide resolved
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.
Looks like a reasonable direction to me, with some minor comments inline.
java/java.hints/src/org/netbeans/modules/java/hints/jdk/ConvertToLambdaPreconditionChecker.java
Outdated
Show resolved
Hide resolved
java/java.hints/src/org/netbeans/modules/java/hints/jdk/ConvertToLambdaPreconditionChecker.java
Outdated
Show resolved
Hide resolved
java/java.hints/src/org/netbeans/modules/java/hints/jdk/ConvertToLambdaPreconditionChecker.java
Outdated
Show resolved
Hide resolved
@@ -137,9 +137,48 @@ private MethodTree getMethodFromFunctionalInterface(TreePath pathToNewClassTree) | |||
candidate = (MethodTree)member; | |||
} | |||
} | |||
// default methods can't be implemented with a lambda | |||
ExecutableElement candidateElement = (ExecutableElement) info.getTrees().getElement(new TreePath(pathToNewClassTree, candidate)); |
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.
So, two comments here:
- should we rather check the method implements an abstract interface method, rather than checking it is not default? (Meaning, if the method based is not found, we don't try to do the conversion?)
- should this be moved to be part of
ensurePreconditionsAreChecked
? (And thenpassesAllPreconditions
andpassesFatalPreconditions
?)
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.
should we rather check the method implements an abstract interface method, rather than checking it is not default? (Meaning, if the method based is not found, we don't try to do the conversion?)
changed it to an override-abstract-method check. More straight forward, I agree.
should this be moved to be part of ensurePreconditionsAreChecked? (And then passesAllPreconditions and passesFatalPreconditions?)
I haven't implemented this so far. since the method getMethodFromFunctionalInterface
is wrong without the change - it would return a method even when the interface is not functional. The precondition checks are all working, since the existence of the method is part of the checks.
Please take another look if you agree, if not I can still make it a precondition - but we probably should also rename the method. Also please note that many methods are actually unused. E.g passesAllPreconditions
isn't used anywhere which means the test coverage is also not ideal atm.
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.
@jlahoda forgot to ping
Default methods don't qualify for functional interfaces since they are not abstract and can therefore not be converted into lambdas.
33a73e5
to
b2e0288
Compare
would like to start to merge a few of my PRs this weekend since they start to pile up, this would be a potential candidate, cc @lahodaj |
planning to merge this this weekend before feature freeze unless someone objects. |
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.
Implementation looks sane to me and I was able to reproduce the problem and found it fixed after this.
Default methods don't qualify for functional interfaces since they are not abstract and can therefore not be converted into lambdas.
The hint has to go through the interface hierarchy and check if the implemented method was a
default
method.example:
real world example:
com.sun.source.util.TaskListener