introduce constant check (IDEA-189238)

method references by second search would be called on lambda parameter so should not influence constant checks
This commit is contained in:
Anna.Kozlova
2018-04-03 16:53:10 +02:00
parent 5ecee5486b
commit 31e02dac07
5 changed files with 60 additions and 0 deletions
@@ -249,6 +249,13 @@ public class IntroduceConstantHandler extends BaseExpressionToFieldHandler {
}
}
@Override
public void visitMethodReferenceExpression(PsiMethodReferenceExpression expression) {
if (!PsiMethodReferenceUtil.isResolvedBySecondSearch(expression)) {
super.visitMethodReferenceExpression(expression);
}
}
@Override
public void visitCallExpression(PsiCallExpression callExpression) {
super.visitCallExpression(callExpression);
@@ -38,6 +38,23 @@ public class PsiMethodReferenceUtil {
return false;
}
public static boolean isResolvedBySecondSearch(@NotNull PsiMethodReferenceExpression methodRef) {
PsiElement resolve = methodRef.resolve();
if (resolve instanceof PsiMethod) {
PsiMethod method = (PsiMethod)resolve;
PsiType functionalInterfaceType = methodRef.getFunctionalInterfaceType();
PsiClassType.ClassResolveResult functionalResolveResult = PsiUtil.resolveGenericsClassInType(functionalInterfaceType);
final PsiMethod interfaceMethod = LambdaUtil.getFunctionalInterfaceMethod(functionalResolveResult);
return interfaceMethod != null &&
isResolvedBySecondSearch(methodRef,
interfaceMethod.getSignature(LambdaUtil.getSubstitutor(interfaceMethod, functionalResolveResult)),
method.isVarArgs(),
method.hasModifierProperty(PsiModifier.STATIC),
method.getParameterList().getParametersCount());
}
return false;
}
public static boolean isResolvedBySecondSearch(@NotNull PsiMethodReferenceExpression methodRef,
@Nullable MethodSignature signature,
boolean varArgs,
@@ -0,0 +1,14 @@
import java.util.Comparator;
class Foo implements Comparable<Foo> {
public String getName() {
return "";
}
@Override
public int compareTo(final Foo o) {
return <selection>Comparator.comparing(Foo::getName, String.CASE_INSENSITIVE_ORDER)</selection>.compare(this, o);
}
}
@@ -0,0 +1,16 @@
import java.util.Comparator;
class Foo implements Comparable<Foo> {
public static final Comparator<Foo> xxx = Comparator.comparing(Foo::getName, String.CASE_INSENSITIVE_ORDER);
public String getName() {
return "";
}
@Override
public int compareTo(final Foo o) {
return xxx.compare(this, o);
}
}
@@ -160,6 +160,12 @@ public class IntroduceConstantTest extends LightCodeInsightTestCase {
checkResultByFile(BASE_PATH + getTestName(false) + "_after.java");
}
public void testWithMethodReferenceBySecondSearch() {
configureByFile(BASE_PATH + getTestName(false) + ".java");
new MockIntroduceConstantHandler(null).invoke(getProject(), getEditor(), getFile(), null);
checkResultByFile(BASE_PATH + getTestName(false) + "_after.java");
}
public void testComments() {
doTestExpr();
}