mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
move instance method: update method reference qualifier according to the method reference shape (IDEA-155147)
This commit is contained in:
+19
-3
@@ -23,6 +23,7 @@ import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.Comparing;
|
||||
import com.intellij.openapi.util.Ref;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
|
||||
import com.intellij.psi.javadoc.PsiDocTagValue;
|
||||
import com.intellij.psi.search.GlobalSearchScope;
|
||||
import com.intellij.psi.search.searches.ClassInheritorsSearch;
|
||||
@@ -240,9 +241,24 @@ public class MoveInstanceMethodProcessor extends BaseRefactoringProcessor{
|
||||
final PsiElement expression = ((MethodCallUsageInfo)usage).getMethodCallExpression();
|
||||
if (expression instanceof PsiMethodCallExpression) {
|
||||
correctMethodCall((PsiMethodCallExpression)expression, false);
|
||||
} else if (expression instanceof PsiMethodReferenceExpression) {
|
||||
PsiExpression newQualifier = JavaPsiFacade.getInstance(myProject).getElementFactory().createExpressionFromText(myTargetVariable.getType().getCanonicalText(), null);
|
||||
((PsiMethodReferenceExpression)expression).setQualifierExpression(newQualifier);
|
||||
}
|
||||
else if (expression instanceof PsiMethodReferenceExpression) {
|
||||
PsiMethodReferenceExpression methodReferenceExpression = (PsiMethodReferenceExpression)expression;
|
||||
PsiExpression qualifierExpression = methodReferenceExpression.getQualifierExpression();
|
||||
String exprText;
|
||||
if (myTargetVariable instanceof PsiParameter ||
|
||||
qualifierExpression instanceof PsiReferenceExpression && ((PsiReferenceExpression)qualifierExpression).resolve() == myMethod.getContainingClass()) {
|
||||
exprText = myTargetVariable.getType().getCanonicalText();
|
||||
}
|
||||
else if (qualifierExpression instanceof PsiReferenceExpression) {
|
||||
exprText = qualifierExpression.getText() + "." + myTargetVariable.getName();
|
||||
}
|
||||
else {
|
||||
exprText = myTargetVariable.getName();
|
||||
}
|
||||
PsiExpression newQualifier = JavaPsiFacade.getInstance(myProject).getElementFactory().createExpressionFromText(exprText, null);
|
||||
((PsiMethodReferenceExpression)expression).setQualifierExpression(
|
||||
(PsiExpression)JavaCodeStyleManager.getInstance(myProject).shortenClassReferences(newQualifier));
|
||||
}
|
||||
}
|
||||
else if (usage instanceof JavadocUsageInfo) {
|
||||
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
import java.util.stream.Stream;
|
||||
|
||||
class Test {
|
||||
private static class Destination{ }
|
||||
|
||||
private final Destination destination = new Destination();
|
||||
|
||||
public void main(Stream<String> stream, Test ref){
|
||||
stream.filter(ref::notNull);
|
||||
}
|
||||
|
||||
private boolean not<caret>Null(String it) {
|
||||
return it != null;
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
import java.util.stream.Stream;
|
||||
|
||||
class Test {
|
||||
private static class Destination{
|
||||
private boolean notNull(String it) {
|
||||
return it != null;
|
||||
}
|
||||
}
|
||||
|
||||
private final Destination destination = new Destination();
|
||||
|
||||
public void main(Stream<String> stream, Test ref){
|
||||
stream.filter(ref.destination::notNull);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import java.util.stream.Stream;
|
||||
|
||||
class Test {
|
||||
private static class Destination{ }
|
||||
|
||||
private final Destination destination = new Destination();
|
||||
|
||||
public void main(Stream<Destination> stream){
|
||||
stream.filter(this::notNull);
|
||||
}
|
||||
|
||||
private boolean not<caret>Null(Destination d) {
|
||||
return d != null;
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
import java.util.stream.Stream;
|
||||
|
||||
class Test {
|
||||
private static class Destination{
|
||||
private boolean notNull() {
|
||||
return this != null;
|
||||
}
|
||||
}
|
||||
|
||||
private final Destination destination = new Destination();
|
||||
|
||||
public void main(Stream<Destination> stream){
|
||||
stream.filter(Test.Destination::notNull);
|
||||
}
|
||||
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
import java.util.stream.Stream;
|
||||
|
||||
class Test {
|
||||
private static class Destination{ }
|
||||
|
||||
private final Destination destination = new Destination();
|
||||
|
||||
public void main(Stream<String> stream){
|
||||
stream.filter(this::notNull);
|
||||
}
|
||||
|
||||
private boolean not<caret>Null(String it) {
|
||||
return it != null;
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
import java.util.stream.Stream;
|
||||
|
||||
class Test {
|
||||
private static class Destination{
|
||||
private boolean notNull(String it) {
|
||||
return it != null;
|
||||
}
|
||||
}
|
||||
|
||||
private final Destination destination = new Destination();
|
||||
|
||||
public void main(Stream<String> stream){
|
||||
stream.filter(destination::notNull);
|
||||
}
|
||||
|
||||
}
|
||||
+18
@@ -93,6 +93,24 @@ public class MoveInstanceMethodTest extends LightRefactoringTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
public void testThisMethodReferenceWithTargetField() throws Exception {
|
||||
doTest(false, 0);
|
||||
}
|
||||
|
||||
public void testForeignMethodReferenceWithTargetField() throws Exception {
|
||||
doTest(false, 0);
|
||||
}
|
||||
|
||||
public void testParameterMethodReference() throws Exception {
|
||||
try {
|
||||
BaseRefactoringProcessor.ConflictsInTestsException.setTestIgnore(true);
|
||||
doTest(true, 0);
|
||||
}
|
||||
finally {
|
||||
BaseRefactoringProcessor.ConflictsInTestsException.setTestIgnore(false);
|
||||
}
|
||||
}
|
||||
|
||||
private void doTest(boolean isTargetParameter, final int targetIndex) throws Exception {
|
||||
doTest(isTargetParameter, targetIndex, null);
|
||||
}
|
||||
|
||||
@@ -566,7 +566,7 @@ public abstract class BaseRefactoringProcessor implements Runnable {
|
||||
}
|
||||
|
||||
@TestOnly
|
||||
static void setTestIgnore(boolean myIgnore) {
|
||||
public static void setTestIgnore(boolean myIgnore) {
|
||||
myTestIgnore = myIgnore;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user