mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
anonymous->lambda: forbid for recursive calls (IDEA-90964)
This commit is contained in:
@@ -74,9 +74,24 @@ public class AnonymousCanBeLambdaInspection extends BaseJavaLocalInspectionTool
|
||||
final String functionalInterfaceErrorMessage = LambdaUtil.checkInterfaceFunctional(baseClassType);
|
||||
if (functionalInterfaceErrorMessage == null) {
|
||||
final PsiMethod[] methods = aClass.getMethods();
|
||||
if (methods.length == 1 && methods[0].getBody() != null) {
|
||||
holder.registerProblem(aClass.getBaseClassReference(), "Anonymous #ref #loc can be replaced with lambda",
|
||||
ProblemHighlightType.LIKE_UNUSED_SYMBOL, new ReplaceWithLambdaFix());
|
||||
if (methods.length == 1) {
|
||||
final PsiCodeBlock body = methods[0].getBody();
|
||||
if (body != null) {
|
||||
final boolean [] recursive = new boolean[1];
|
||||
body.accept(new JavaRecursiveElementWalkingVisitor() {
|
||||
@Override
|
||||
public void visitMethodCallExpression(PsiMethodCallExpression methodCallExpression) {
|
||||
super.visitMethodCallExpression(methodCallExpression);
|
||||
if (methodCallExpression.resolveMethod() == methods[0]) {
|
||||
recursive[0] = true;
|
||||
}
|
||||
}
|
||||
});
|
||||
if (!recursive[0]) {
|
||||
holder.registerProblem(aClass.getBaseClassReference(), "Anonymous #ref #loc can be replaced with lambda",
|
||||
ProblemHighlightType.LIKE_UNUSED_SYMBOL, new ReplaceWithLambdaFix());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
// "Replace with lambda" "false"
|
||||
class Test {
|
||||
public interface I {
|
||||
int m();
|
||||
}
|
||||
{
|
||||
I i = new <caret>I() {
|
||||
@Override
|
||||
public int m() {
|
||||
m();
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user