Change Signature: Process internal usages of function parameters when
performing Java refactoring
This commit is contained in:
+52
-4
@@ -26,10 +26,7 @@ import com.intellij.psi.search.SearchScope;
|
||||
import com.intellij.psi.search.searches.OverridingMethodsSearch;
|
||||
import com.intellij.psi.search.searches.ReferencesSearch;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.refactoring.changeSignature.ChangeInfo;
|
||||
import com.intellij.refactoring.changeSignature.ChangeSignatureUsageProcessor;
|
||||
import com.intellij.refactoring.changeSignature.JavaChangeInfo;
|
||||
import com.intellij.refactoring.changeSignature.OverriderUsageInfo;
|
||||
import com.intellij.refactoring.changeSignature.*;
|
||||
import com.intellij.refactoring.rename.ResolveSnapshotProvider;
|
||||
import com.intellij.refactoring.util.CommonRefactoringUtil;
|
||||
import com.intellij.refactoring.util.MoveRenameUsageInfo;
|
||||
@@ -373,6 +370,57 @@ public class JetChangeSignatureUsageProcessor implements ChangeSignatureUsagePro
|
||||
FunctionDescriptor functionDescriptor = (FunctionDescriptor) ResolvePackage.resolveToDescriptor(function);
|
||||
|
||||
result.add(new DeferredJavaMethodOverrideOrSAMUsage(function, functionDescriptor, null));
|
||||
|
||||
findDeferredUsagesOfParameters(changeInfo, result, function, functionDescriptor);
|
||||
}
|
||||
}
|
||||
|
||||
private static void findDeferredUsagesOfParameters(
|
||||
ChangeInfo changeInfo,
|
||||
Set<UsageInfo> result,
|
||||
JetNamedFunction function,
|
||||
FunctionDescriptor functionDescriptor
|
||||
) {
|
||||
final JetCallableDefinitionUsage<?> functionInfoForParameters =
|
||||
new JetCallableDefinitionUsage<PsiElement>(function, functionDescriptor, null, null);
|
||||
List<JetParameter> oldParameters = PsiUtilPackage.getValueParameters(function);
|
||||
ParameterInfo[] parameters = changeInfo.getNewParameters();
|
||||
for (int i = 0; i < parameters.length; i++) {
|
||||
final int paramIndex = i;
|
||||
ParameterInfo parameterInfo = parameters[paramIndex];
|
||||
if (parameterInfo.getOldIndex() >= 0 && parameterInfo.getOldIndex() < oldParameters.size()) {
|
||||
JetParameter oldParam = oldParameters.get(parameterInfo.getOldIndex());
|
||||
String oldParamName = oldParam.getName();
|
||||
|
||||
if (oldParamName != null && !oldParamName.equals(parameterInfo.getName())) {
|
||||
for (PsiReference reference : ReferencesSearch.search(oldParam, oldParam.getUseScope())) {
|
||||
final PsiElement element = reference.getElement();
|
||||
|
||||
if ((element instanceof JetSimpleNameExpression || element instanceof KDocName) &&
|
||||
!(element.getParent() instanceof JetValueArgumentName)) // Usages in named arguments of the calls usage will be changed when the function call is changed
|
||||
{
|
||||
result.add(
|
||||
new JavaMethodDeferredKotlinUsage<JetElement>((JetElement) element) {
|
||||
@NotNull
|
||||
@Override
|
||||
public JavaMethodKotlinUsageWithDelegate<JetElement> resolve(@NotNull JetChangeInfo javaMethodChangeInfo) {
|
||||
return new JavaMethodKotlinUsageWithDelegate<JetElement>((JetElement) element,
|
||||
javaMethodChangeInfo) {
|
||||
@NotNull
|
||||
@Override
|
||||
protected JetUsageInfo<JetElement> getDelegateUsage() {
|
||||
return new JetParameterUsage((JetElement) element,
|
||||
getJavaMethodChangeInfo().getNewParameters()[paramIndex],
|
||||
functionInfoForParameters);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
open class X: A() {
|
||||
fun foo(x: Int): String? {
|
||||
return super.foo(x) + 1
|
||||
}
|
||||
}
|
||||
|
||||
open class Y: B() {
|
||||
fun foo(x: Int): String? {
|
||||
return x.length() * 2
|
||||
}
|
||||
}
|
||||
|
||||
open class Z: X() {
|
||||
fun foo(x: Int): String? {
|
||||
return x.length()
|
||||
}
|
||||
}
|
||||
|
||||
fun test() {
|
||||
A().foo("")
|
||||
B().foo("")
|
||||
X().foo("")
|
||||
Y().foo("")
|
||||
Z().foo("")
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
import java.lang.Override;
|
||||
import java.lang.String;
|
||||
|
||||
class A {
|
||||
String <caret>foo(int x) {
|
||||
return x.length() * 2;
|
||||
}
|
||||
}
|
||||
|
||||
class B extends A {
|
||||
@Override
|
||||
String foo(int x) {
|
||||
return super.foo(x + "_");
|
||||
}
|
||||
}
|
||||
|
||||
class Test {
|
||||
void test() {
|
||||
new A().foo("");
|
||||
new B().foo("");
|
||||
new X().foo("");
|
||||
new Y().foo("");
|
||||
new Z().foo("");
|
||||
}
|
||||
}
|
||||
+3
-3
@@ -1,18 +1,18 @@
|
||||
open class X: A() {
|
||||
fun foo(s: String): Int {
|
||||
return super.foo(s)
|
||||
return super.foo(s) + 1
|
||||
}
|
||||
}
|
||||
|
||||
open class Y: B() {
|
||||
fun foo(s: String): Int {
|
||||
return 0
|
||||
return s.length() * 2
|
||||
}
|
||||
}
|
||||
|
||||
open class Z: X() {
|
||||
fun foo(s: String): Int {
|
||||
return 0
|
||||
return s.length()
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -3,14 +3,14 @@ import java.lang.String;
|
||||
|
||||
class A {
|
||||
int <caret>foo(String s) {
|
||||
return 0;
|
||||
return s.length() * 2;
|
||||
}
|
||||
}
|
||||
|
||||
class B extends A {
|
||||
@Override
|
||||
int foo(String s) {
|
||||
return super.foo(s);
|
||||
return super.foo(s + "_");
|
||||
}
|
||||
}
|
||||
|
||||
+3
-3
@@ -1,18 +1,18 @@
|
||||
open class X: A() {
|
||||
fun foo(x: Int): String? {
|
||||
return super.foo(1)
|
||||
return super.foo(1) + 1
|
||||
}
|
||||
}
|
||||
|
||||
open class Y: B() {
|
||||
fun foo(x: Int): String? {
|
||||
return 0
|
||||
return s.length() * 2
|
||||
}
|
||||
}
|
||||
|
||||
open class Z: X() {
|
||||
fun foo(x: Int): String? {
|
||||
return 0
|
||||
return s.length()
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -3,7 +3,7 @@ import java.lang.String;
|
||||
|
||||
class A {
|
||||
String foo(int x) {
|
||||
return 0;
|
||||
return s.length() * 2;
|
||||
}
|
||||
}
|
||||
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
open class X: A() {
|
||||
fun foo(s: String): Int {
|
||||
return super.foo(s) + 1
|
||||
}
|
||||
}
|
||||
|
||||
open class Y: B() {
|
||||
fun foo(s: String): Int {
|
||||
return s.length() * 2
|
||||
}
|
||||
}
|
||||
|
||||
open class Z: X() {
|
||||
fun foo(s: String): Int {
|
||||
return s.length()
|
||||
}
|
||||
}
|
||||
|
||||
fun test() {
|
||||
A().foo("")
|
||||
B().foo("")
|
||||
X().foo("")
|
||||
Y().foo("")
|
||||
Z().foo("")
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
import java.lang.Override;
|
||||
import java.lang.String;
|
||||
|
||||
class A {
|
||||
int <caret>foo(String s) {
|
||||
return s.length() * 2;
|
||||
}
|
||||
}
|
||||
|
||||
class B extends A {
|
||||
@Override
|
||||
int foo(String s) {
|
||||
return super.foo(s + "_");
|
||||
}
|
||||
}
|
||||
|
||||
class Test {
|
||||
void test() {
|
||||
new A().foo("");
|
||||
new B().foo("");
|
||||
new X().foo("");
|
||||
new Y().foo("");
|
||||
new Z().foo("");
|
||||
}
|
||||
}
|
||||
+22
-1
@@ -975,7 +975,7 @@ public class JetChangeSignatureTest extends KotlinCodeInsightTestCase {
|
||||
doTest(changeInfo);
|
||||
}
|
||||
|
||||
public void testJavaMethodOverrides() throws Exception {
|
||||
public void testJavaMethodOverridesReplaceParam() throws Exception {
|
||||
doJavaTest(
|
||||
new JavaRefactoringProvider() {
|
||||
@Nullable
|
||||
@@ -995,6 +995,27 @@ public class JetChangeSignatureTest extends KotlinCodeInsightTestCase {
|
||||
);
|
||||
}
|
||||
|
||||
public void testJavaMethodOverridesChangeParam() throws Exception {
|
||||
doJavaTest(
|
||||
new JavaRefactoringProvider() {
|
||||
@Nullable
|
||||
@Override
|
||||
PsiType getNewReturnType(@NotNull PsiMethod method) {
|
||||
return PsiType.getJavaLangString(getPsiManager(), GlobalSearchScope.allScope(getProject()));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
ParameterInfoImpl[] getNewParameters(@NotNull PsiMethod method) {
|
||||
ParameterInfoImpl[] newParameters = super.getNewParameters(method);
|
||||
newParameters[0].setName("x");
|
||||
newParameters[0].setType(PsiType.INT);
|
||||
return newParameters;
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
public void testChangeProperty() throws Exception {
|
||||
JetChangeInfo changeInfo = getChangeInfo();
|
||||
changeInfo.setNewName("s");
|
||||
|
||||
Reference in New Issue
Block a user