change signature: support changing signature of the method referenced by some method reference (IDEA-152114)

This commit is contained in:
Anna Kozlova
2016-02-24 20:30:09 +01:00
parent 4321968124
commit aa00a3b1ba
8 changed files with 128 additions and 1 deletions

View File

@@ -110,6 +110,17 @@ public class JavaChangeSignatureUsageProcessor implements ChangeSignatureUsagePr
}
return true;
}
else if (usage instanceof MethodReferenceUsageInfo) {
final PsiElement element = usage.getElement();
if (element instanceof PsiMethodReferenceExpression) {
final PsiLambdaExpression lambdaExpression = LambdaRefactoringUtil.convertMethodReferenceToLambda((PsiMethodReferenceExpression)element, false, true);
final PsiExpression expression = LambdaUtil.extractSingleExpressionFromBody(lambdaExpression.getBody());
if (expression instanceof PsiCallExpression) {
((MethodReferenceUsageInfo)usage).setCallExpression((PsiCallExpression)expression);
return true;
}
}
}
else if (usage instanceof FunctionalInterfaceChangedUsageInfo) {
final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(usage.getProject());
final PsiElement element = usage.getElement();
@@ -152,6 +163,14 @@ public class JavaChangeSignatureUsageProcessor implements ChangeSignatureUsagePr
addDefaultConstructor(((JavaChangeInfo)changeInfo), ((NoConstructorClassUsageInfo)usage).getPsiClass(), usages);
return true;
}
else if (usage instanceof MethodReferenceUsageInfo) {
final MethodCallUsageInfo methodCallInfo = ((MethodReferenceUsageInfo)usage).createMethodCallInfo();
if (methodCallInfo != null) {
processMethodUsage(methodCallInfo.getElement(), (JavaChangeInfo)changeInfo, methodCallInfo.isToChangeArguments(),
methodCallInfo.isToCatchExceptions(), methodCallInfo.getReferencedMethod(), methodCallInfo.getSubstitutor(), usages);
return true;
}
}
else if (usage instanceof MethodCallUsageInfo) {
final MethodCallUsageInfo methodCallInfo = (MethodCallUsageInfo)usage;
processMethodUsage(methodCallInfo.getElement(), (JavaChangeInfo)changeInfo, methodCallInfo.isToChangeArguments(),

View File

@@ -257,6 +257,9 @@ class JavaChangeSignatureUsageSearcher {
else if (ref instanceof PsiCallReference) {
result.add(new CallReferenceUsageInfo((PsiCallReference)ref));
}
else if (element instanceof PsiMethodReferenceExpression) {
result.add(new MethodReferenceUsageInfo(element, method, isToModifyArgs, isToCatchExceptions));
}
else {
result.add(new MoveRenameUsageInfo(element, ref, method));
}

View File

@@ -48,6 +48,9 @@ public class MethodCallUsageInfo extends UsageInfo {
private static JavaResolveResult resolveMethod(final PsiElement ref) {
if (ref instanceof PsiEnumConstant) return ((PsiEnumConstant)ref).resolveMethodGenerics();
if (ref instanceof PsiCallExpression) {
return ((PsiCallExpression)ref).resolveMethodGenerics();
}
PsiElement parent = ref.getParent();
if (parent instanceof PsiCall) {
return ((PsiCall)parent).resolveMethodGenerics();

View File

@@ -0,0 +1,57 @@
/*
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.refactoring.changeSignature;
import com.intellij.psi.PsiCallExpression;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiMethod;
import com.intellij.usageView.UsageInfo;
import org.jetbrains.annotations.Nullable;
class MethodReferenceUsageInfo extends UsageInfo {
private final boolean myIsToModifyArgs;
private final boolean myIsToCatchExceptions;
private PsiCallExpression myCallExpression;
public MethodReferenceUsageInfo(PsiElement element, PsiMethod method, boolean isToModifyArgs, boolean isToCatchExceptions) {
super(element);
myIsToModifyArgs = isToModifyArgs;
myIsToCatchExceptions = isToCatchExceptions;
}
public void setCallExpression(PsiCallExpression callExpression) {
myCallExpression = callExpression;
}
@Nullable
public MethodCallUsageInfo createMethodCallInfo() {
if (myCallExpression == null) {
return null;
}
return new MethodCallUsageInfo(myCallExpression, myIsToModifyArgs, myIsToCatchExceptions);
}
@Nullable
@Override
public PsiElement getElement() {
if (myCallExpression != null) {
return myCallExpression;
}
return super.getElement();
}
}

View File

@@ -546,7 +546,7 @@ public class RefactoringUtil {
@Nullable
public static PsiExpressionList getArgumentListByMethodReference(PsiElement ref) {
if (ref instanceof PsiEnumConstant) return ((PsiEnumConstant)ref).getArgumentList();
if (ref instanceof PsiCall) return ((PsiCall)ref).getArgumentList();
PsiElement parent = ref.getParent();
if (parent instanceof PsiCall) {
return ((PsiCall)parent).getArgumentList();

View File

@@ -0,0 +1,20 @@
import java.util.ArrayList;
import java.util.List;
interface P {
boolean m(Integer i);
}
class A {
public static void print() {
List<Integer> someNumbers = A.returnAllNumbers(A::alwaysTrue);
}
private static List<Integer> returnAllNumbers(P predicate) {
return new ArrayList<>();
}
public static boolean alwa<caret>ysTrue(int a) {
return true;
}
}

View File

@@ -0,0 +1,20 @@
import java.util.ArrayList;
import java.util.List;
interface P {
boolean m(Integer i);
}
class A {
public static void print() {
List<Integer> someNumbers = A.returnAllNumbers((a) -> A.alwaysTrue());
}
private static List<Integer> returnAllNumbers(P predicate) {
return new ArrayList<>();
}
public static boolean alwaysTrue() {
return true;
}
}

View File

@@ -348,6 +348,11 @@ public class ChangeSignatureTest extends ChangeSignatureBaseTest {
doTest(null, null, null, genParams, new SimpleExceptionsGen(), false, true);
}
public void testExpandMethodReferenceToDeleteParameter() {
GenParams genParams = method -> new ParameterInfoImpl[0];
doTest(null, null, null, genParams, new SimpleExceptionsGen(), false, true);
}
public void testMethodParametersAlignmentAfterMethodNameChange() {
getJavaSettings().ALIGN_MULTILINE_PARAMETERS = true;
getJavaSettings().ALIGN_MULTILINE_PARAMETERS_IN_CALLS = true;