[java] [rename] correctly handle method reference rename (IDEA-283662)

GitOrigin-RevId: c869ac621c25373fa08e742439f6022fa6052efc
This commit is contained in:
Anna Kozlova
2021-12-03 20:25:34 +00:00
committed by intellij-monorepo-bot
parent e5a1154ba5
commit da987548c0
6 changed files with 52 additions and 17 deletions
@@ -1,10 +1,9 @@
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.psi.impl.source.tree.java;
import com.intellij.icons.AllIcons;
import com.intellij.lang.ASTNode;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.Comparing;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.*;
import com.intellij.psi.impl.CheckUtil;
@@ -14,7 +13,6 @@ import com.intellij.psi.impl.java.stubs.JavaStubElementTypes;
import com.intellij.psi.impl.source.JavaStubPsiElement;
import com.intellij.psi.impl.source.resolve.graphInference.FunctionalInterfaceParameterizationUtil;
import com.intellij.psi.impl.source.resolve.graphInference.InferenceSession;
import com.intellij.psi.impl.source.tree.JavaElementType;
import com.intellij.psi.infos.MethodCandidateInfo;
import com.intellij.psi.scope.ElementClassFilter;
import com.intellij.psi.scope.PsiConflictResolver;
@@ -319,20 +317,11 @@ public class PsiMethodReferenceExpressionImpl extends JavaStubPsiElement<Functio
@Override
public PsiElement handleElementRename(@NotNull String newElementName) throws IncorrectOperationException {
PsiElement oldIdentifier = findChildByType(JavaTokenType.IDENTIFIER);
if (oldIdentifier == null) {
oldIdentifier = findChildByType(JavaElementType.REFERENCE_EXPRESSION);
}
if (isConstructor()) return this;
PsiElement oldIdentifier = getReferenceNameElement();
if (oldIdentifier == null) {
throw new IncorrectOperationException();
}
final String oldRefName = oldIdentifier.getText();
if (PsiKeyword.THIS.equals(oldRefName) ||
PsiKeyword.SUPER.equals(oldRefName) ||
PsiKeyword.NEW.equals(oldRefName) ||
Comparing.strEqual(oldRefName, newElementName)) {
return this;
}
PsiIdentifier identifier = JavaPsiFacade.getElementFactory(getProject()).createIdentifier(newElementName);
oldIdentifier.replace(identifier);
return this;
@@ -1,5 +1,5 @@
public class FooBar {
private static final class B<caret>ar {
static final class B<caret>ar {
private Bar() {
}
}
@@ -15,3 +15,9 @@ public class FooBar {
foo(Bar::new);
}
}
class FooBarBaz {
public static void main(String[] args) throws Exception {
foo(FooBar.Bar::new);
}
}
@@ -1,5 +1,5 @@
public class FooBar {
private static final class Bar1 {
static final class Bar1 {
private Bar1() {
}
}
@@ -15,3 +15,9 @@ public class FooBar {
foo(Bar1::new);
}
}
class FooBarBaz {
public static void main(String[] args) throws Exception {
foo(FooBar.Bar1::new);
}
}
@@ -0,0 +1,15 @@
public class FooBar {
static void f<caret>oo() {}
public static void main(String[] args) throws Exception {
Runnable runnable = FooBar::foo;
}
}
class FooBarBaz {
public static void main(String[] args) throws Exception {
Runnable runnable = FooBar::foo;
}
}
@@ -0,0 +1,15 @@
public class FooBar {
static void bar1() {}
public static void main(String[] args) throws Exception {
Runnable runnable = FooBar::bar1;
}
}
class FooBarBaz {
public static void main(String[] args) throws Exception {
Runnable runnable = FooBar::bar1;
}
}
@@ -1,4 +1,4 @@
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.java.refactoring;
import com.intellij.JavaTestUtil;
@@ -67,6 +67,10 @@ public class RenameLocalTest extends LightRefactoringTestCase {
doTest("Bar1");
}
public void testMethodNameUsedInMethodRefs() {
doTest("bar1");
}
public void testRenameParamUniqueName() {
configureByFile();
final HashSet<String> result = new HashSet<>();