mbien commented on code in PR #6658:
URL: https://github.com/apache/netbeans/pull/6658#discussion_r1381170918
##########
java/java.hints/src/org/netbeans/modules/java/hints/jdk/ConvertToLambdaPreconditionChecker.java:
##########
@@ -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));
+ if (overridesDefaultMethod(candidateElement, (TypeElement)
baseElement)) {
+ return null;
+ }
return candidate;
}
+ private boolean overridesDefaultMethod(ExecutableElement method,
TypeElement superType) {
+ Boolean overrides = overridesDefaultMethodImpl(method, superType);
+ return overrides != null && overrides;
+ }
+
+ private Boolean overridesDefaultMethodImpl(ExecutableElement method,
TypeElement superType) {
+ List<? extends VariableElement> methodParams = method.getParameters();
+ for (Element e : superType.getEnclosedElements()) {
+ if (e.getKind() == ElementKind.METHOD &&
method.getSimpleName().equals(e.getSimpleName())
+ && parameterTypesMatch(methodParams, ((ExecutableElement)
e).getParameters())) {
+ return e.getModifiers().contains(Modifier.DEFAULT);
+ }
+ }
+ for (TypeMirror otherType : superType.getInterfaces()) {
+ Boolean overrides = overridesDefaultMethodImpl(method,
(TypeElement) types.asElement(otherType));
+ if (overrides != null) {
+ return overrides;
+ }
+ }
+ return null; // no match here but check the rest of the interface tree
+ }
Review Comment:
I was a bit worried that this could loop forever if the sourcecode has an
(invalid) cyclic interface hierarchy. But it looks like the compiler quits in
time so that cycles don't happen at this stage.
Adding a HashSet and checking for endless recursions would be easy but
appears to be not needed.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists