mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
make method static: add parameter when method was used in method reference (IDEA-93138)
This commit is contained in:
+13
-11
@@ -167,9 +167,7 @@ public class MakeMethodStaticProcessor extends MakeMethodOrClassStaticProcessor<
|
||||
|
||||
PsiReferenceExpression methodRef = (PsiReferenceExpression) element;
|
||||
PsiElement parent = methodRef.getParent();
|
||||
LOG.assertTrue(parent instanceof PsiMethodCallExpression);
|
||||
|
||||
PsiMethodCallExpression methodCall = (PsiMethodCallExpression) parent;
|
||||
PsiExpression instanceRef;
|
||||
|
||||
instanceRef = methodRef.getQualifierExpression();
|
||||
@@ -192,21 +190,25 @@ public class MakeMethodStaticProcessor extends MakeMethodOrClassStaticProcessor<
|
||||
if (mySettings.getNewParametersNumber() > 1) {
|
||||
int copyingSafetyLevel = RefactoringUtil.verifySafeCopyExpression(instanceRef);
|
||||
if (copyingSafetyLevel == RefactoringUtil.EXPR_COPY_PROHIBITED) {
|
||||
String tempVar = RefactoringUtil.createTempVar(instanceRef, methodCall, true);
|
||||
String tempVar = RefactoringUtil.createTempVar(instanceRef, parent, true);
|
||||
instanceRef = factory.createExpressionFromText(tempVar, null);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
PsiElement anchor = null;
|
||||
PsiExpressionList argList = methodCall.getArgumentList();
|
||||
PsiExpression[] exprs = argList.getExpressions();
|
||||
if (mySettings.isMakeClassParameter()) {
|
||||
if (exprs.length > 0) {
|
||||
anchor = argList.addBefore(instanceRef, exprs[0]);
|
||||
}
|
||||
else {
|
||||
anchor = argList.add(instanceRef);
|
||||
PsiExpressionList argList = null;
|
||||
PsiExpression[] exprs = new PsiExpression[0];
|
||||
if (parent instanceof PsiMethodCallExpression) {
|
||||
argList = ((PsiMethodCallExpression)parent).getArgumentList();
|
||||
exprs = argList.getExpressions();
|
||||
if (mySettings.isMakeClassParameter()) {
|
||||
if (exprs.length > 0) {
|
||||
anchor = argList.addBefore(instanceRef, exprs[0]);
|
||||
}
|
||||
else {
|
||||
anchor = argList.add(instanceRef);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -31,14 +31,17 @@ import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.editor.ScrollType;
|
||||
import com.intellij.openapi.progress.ProgressManager;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
|
||||
import com.intellij.psi.codeStyle.VariableKind;
|
||||
import com.intellij.psi.search.searches.MethodReferencesSearch;
|
||||
import com.intellij.refactoring.HelpID;
|
||||
import com.intellij.refactoring.RefactoringActionHandler;
|
||||
import com.intellij.refactoring.RefactoringBundle;
|
||||
import com.intellij.refactoring.util.CommonRefactoringUtil;
|
||||
import com.intellij.util.Processor;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@@ -83,7 +86,7 @@ public class MakeStaticHandler implements RefactoringActionHandler {
|
||||
invoke(member);
|
||||
}
|
||||
|
||||
public static void invoke(PsiTypeParameterListOwner member) {
|
||||
public static void invoke(final PsiTypeParameterListOwner member) {
|
||||
final Project project = member.getProject();
|
||||
final InternalUsageInfo[] classRefsInMember = MakeStaticUtil.findClassRefsInMember(member, false);
|
||||
|
||||
@@ -95,7 +98,26 @@ public class MakeStaticHandler implements RefactoringActionHandler {
|
||||
AbstractMakeStaticDialog dialog;
|
||||
if (!ApplicationManager.getApplication().isUnitTestMode()) {
|
||||
|
||||
if (classRefsInMember.length > 0) {
|
||||
final boolean[] hasMethodReferenceOnInstance = new boolean[] {false};
|
||||
if (member instanceof PsiMethod) {
|
||||
if (!ProgressManager.getInstance().runProcessWithProgressSynchronously(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
hasMethodReferenceOnInstance[0] = !MethodReferencesSearch.search((PsiMethod)member).forEach(new Processor<PsiReference>() {
|
||||
@Override
|
||||
public boolean process(PsiReference reference) {
|
||||
final PsiElement element = reference.getElement();
|
||||
if (element instanceof PsiMethodReferenceExpression) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
});
|
||||
}
|
||||
}, "Search for method references", true, project)) return;
|
||||
}
|
||||
|
||||
if (classRefsInMember.length > 0 || hasMethodReferenceOnInstance[0]) {
|
||||
final PsiType type = JavaPsiFacade.getInstance(project).getElementFactory().createType(member.getContainingClass());
|
||||
//TODO: callback
|
||||
String[] nameSuggestions =
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
class Test4 {
|
||||
void test() {
|
||||
Foo2<Test4> f = Test4::yyy;
|
||||
}
|
||||
static void yyy(Test4 anObject) {}
|
||||
}
|
||||
interface Foo2<T> {
|
||||
void bar(T j);
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
class Test4 {
|
||||
void test() {
|
||||
Foo2<Test4> f = Test4::yyy;
|
||||
}
|
||||
void yy<caret>y() {}
|
||||
}
|
||||
interface Foo2<T> {
|
||||
void bar(T j);
|
||||
}
|
||||
@@ -19,7 +19,6 @@ import com.intellij.JavaTestUtil;
|
||||
import com.intellij.codeInsight.TargetElementUtilBase;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiMethod;
|
||||
import com.intellij.psi.PsiTypeParameterListOwner;
|
||||
import com.intellij.refactoring.makeStatic.MakeMethodStaticProcessor;
|
||||
import com.intellij.refactoring.makeStatic.MakeStaticUtil;
|
||||
import com.intellij.refactoring.makeStatic.Settings;
|
||||
@@ -186,13 +185,21 @@ public class MakeMethodStaticTest extends LightRefactoringTestCase {
|
||||
assertFalse(MakeStaticUtil.isParameterNeeded((PsiMethod)element));
|
||||
}
|
||||
|
||||
public void testMethodReference() throws Exception {
|
||||
doTest(true);
|
||||
}
|
||||
|
||||
public void testPreserveParametersAlignment() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
|
||||
private void doTest() throws Exception {
|
||||
doTest(false);
|
||||
}
|
||||
|
||||
private void doTest(final boolean addClassParameter) throws Exception {
|
||||
configureByFile("/refactoring/makeMethodStatic/before" + getTestName(false) + ".java");
|
||||
perform(false);
|
||||
perform(addClassParameter);
|
||||
checkResultByFile("/refactoring/makeMethodStatic/after" + getTestName(false) + ".java");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user