Commit 31e02dac authored by Anna.Kozlova's avatar Anna.Kozlova
Browse files

introduce constant check (IDEA-189238)

method references by second search would be called on lambda parameter so should not influence constant checks
parent 5ecee548
Branches unavailable Tags unavailable
No related merge requests found
Showing with 60 additions and 0 deletions
+60 -0
......@@ -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,
......
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);
}
}
\ No newline at end of file
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);
}
}
\ No newline at end of file
......@@ -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();
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment