mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
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:
+7
@@ -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,
|
||||
|
||||
+14
@@ -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);
|
||||
}
|
||||
}
|
||||
+16
@@ -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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user