anonymous -> lambda: stop on calls to j.l.Object methods of anonym classes (IDEA-203343)

This commit is contained in:
Anna.Kozlova
2018-11-30 18:37:07 +01:00
parent 8ff5dd251f
commit a9fe383ced
3 changed files with 59 additions and 7 deletions

View File

@@ -472,8 +472,9 @@ public class AnonymousCanBeLambdaInspection extends AbstractBaseJavaLocalInspect
PsiCallExpression callExpression) {
if (psiMethod != null && !psiMethod.hasModifierProperty(PsiModifier.STATIC)) {
final PsiClass containingClass = psiMethod.getContainingClass();
if (containingClass != null && CommonClassNames.JAVA_LANG_OBJECT.equals(containingClass.getQualifiedName())) {
return false;
if (containingClass != null &&
CommonClassNames.JAVA_LANG_OBJECT.equals(containingClass.getQualifiedName())) {
return !(callExpression instanceof PsiMethodCallExpression && ((PsiMethodCallExpression)callExpression).getMethodExpression().isQualified());
}
if (callExpression instanceof PsiMethodCallExpression &&
@@ -517,11 +518,7 @@ public class AnonymousCanBeLambdaInspection extends AbstractBaseJavaLocalInspect
super.visitMethodCallExpression(methodCallExpression);
final PsiMethod psiMethod = methodCallExpression.resolveMethod();
if (psiMethod == myMethod ||
functionalInterfaceMethodReferenced(psiMethod, myAnonymClass, methodCallExpression) ||
psiMethod != null &&
!methodCallExpression.getMethodExpression().isQualified() &&
"getClass".equals(psiMethod.getName()) &&
psiMethod.getParameterList().isEmpty()) {
functionalInterfaceMethodReferenced(psiMethod, myAnonymClass, methodCallExpression)) {
myBodyContainsForbiddenRefs = true;
}
}

View File

@@ -0,0 +1,25 @@
// "Fix all 'Anonymous type can be replaced with lambda' problems in file" "true"
import java.util.concurrent.Callable;
class MyTest {
interface A {
void foo();
default void m() {}
}
static {
Callable<Integer> c = new Callable<Integer>() {
@Override
public Integer call() {
return hashCode();
}
};
A a = new A() {
@Override
public void foo() {
m();
}
};
A b = () -> new Object();
}
}

View File

@@ -0,0 +1,30 @@
// "Fix all 'Anonymous type can be replaced with lambda' problems in file" "true"
import java.util.concurrent.Callable;
class MyTest {
interface A {
void foo();
default void m() {}
}
static {
Callable<Integer> c = new Callable<Integer>() {
@Override
public Integer call() {
return hashCode();
}
};
A a = new A() {
@Override
public void foo() {
m();
}
};
A b = new <caret>A() {
@Override
public void foo() {
new Object();
}
};
}
}