anonymous->lambda: forbid for recursive calls (IDEA-90964)

This commit is contained in:
Anna Kozlova
2012-09-03 21:27:12 +04:00
parent 2996ce4b20
commit 49a08fce9e
2 changed files with 34 additions and 3 deletions
@@ -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());
}
}
}
}
}
@@ -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;
}
};
}
}