introduce parameter: expand method reference to lambda when new parameter introduced (IDEA-152125)

This commit is contained in:
Anna Kozlova
2016-02-25 16:17:33 +01:00
parent 716c8794ae
commit 61816006f6
5 changed files with 45 additions and 3 deletions
@@ -31,6 +31,7 @@ import com.intellij.psi.javadoc.PsiDocTag;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import com.intellij.refactoring.util.FieldConflictsResolver;
import com.intellij.refactoring.util.LambdaRefactoringUtil;
import com.intellij.refactoring.util.RefactoringUtil;
import com.intellij.refactoring.util.javadoc.MethodJavaDocHelper;
import com.intellij.usageView.UsageInfo;
@@ -60,8 +61,18 @@ public class JavaIntroduceParameterMethodUsagesProcessor implements IntroducePar
}
public boolean processChangeMethodUsage(IntroduceParameterData data, UsageInfo usage, UsageInfo[] usages) throws IncorrectOperationException {
if (!isMethodUsage(usage)) return true;
final PsiElement ref = usage.getElement();
PsiElement ref = usage.getElement();
if (ref instanceof PsiMethodReferenceExpression) {
final PsiLambdaExpression lambdaExpression = LambdaRefactoringUtil.convertMethodReferenceToLambda((PsiMethodReferenceExpression)ref, false, true);
final PsiExpression callExpression = LambdaUtil.extractSingleExpressionFromBody(lambdaExpression.getBody());
if (callExpression == null) {
return true;
}
ref = callExpression;
}
else if (!isMethodUsage(usage)) {
return true;
}
PsiCall callExpression = RefactoringUtil.getCallExpressionByMethodReference(ref);
PsiExpressionList argList = RefactoringUtil.getArgumentListByMethodReference(ref);
if (argList == null) return true;
@@ -559,7 +559,7 @@ public class RefactoringUtil {
}
public static PsiCall getCallExpressionByMethodReference(PsiElement ref) {
if (ref instanceof PsiEnumConstant) return (PsiCall)ref;
if (ref instanceof PsiCall) return (PsiCall)ref;
PsiElement parent = ref.getParent();
if (parent instanceof PsiMethodCallExpression) {
return (PsiMethodCallExpression)parent;
@@ -0,0 +1,13 @@
interface I {
boolean m(int a);
}
class A {
{
I predicate = (a) -> A.alwaysTrue(a, "");
}
private static boolean alwaysTrue(int a, String anObject) {
return true;
}
}
@@ -0,0 +1,14 @@
interface I {
boolean m(int a);
}
class A {
{
I predicate = A::alwaysTrue;
}
private static boolean alwaysTrue(int a) {
<selection>""</selection>
return true;
}
}
@@ -142,6 +142,10 @@ public class IntroduceParameterTest extends LightRefactoringTestCase {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, false, false);
}
public void testExpandMethodReference() throws Exception {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, false, false);
}
public void testSuperWithSideEffect() {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, false, false,
"Parameter initializer contains <b><code>super</code></b>, but not all calls to method are in its class");