Java: stable method ordering for "Convert to instance method" (IDEA-356128)

GitOrigin-RevId: 81b2e38494d7792dfe3ec13703851e4eb1539e71
This commit is contained in:
Bas Leijdekkers
2024-07-10 19:17:56 +00:00
committed by intellij-monorepo-bot
parent 004e3fb505
commit 610358505b
9 changed files with 33 additions and 29 deletions
@@ -246,12 +246,13 @@ public final class ConvertToInstanceMethodProcessor extends BaseRefactoringProce
if (!markAsDefault) {
for (final PsiClass psiClass : inheritors) {
final PsiMethod newMethod = addMethodToClass(psiClass);
PsiUtil.setModifierProperty(newMethod, myNewVisibility != null && !myNewVisibility.equals(VisibilityUtil.ESCALATE_VISIBILITY) ? myNewVisibility
: PsiModifier.PUBLIC, true);
String modifier = myNewVisibility != null && !myNewVisibility.equals(VisibilityUtil.ESCALATE_VISIBILITY)
? myNewVisibility
: PsiModifier.PUBLIC;
PsiUtil.setModifierProperty(newMethod, modifier, true);
}
}
}
myMethod.delete();
return result;
}
@@ -326,7 +327,16 @@ public final class ConvertToInstanceMethodProcessor extends BaseRefactoringProce
}
private PsiMethod addMethodToClass(final PsiClass targetClass) {
final PsiMethod newMethod = (PsiMethod)targetClass.add(myMethod);
final PsiMethod newMethod;
if (targetClass == myMethod.getContainingClass()) {
newMethod = myMethod;
}
else {
newMethod = (PsiMethod)targetClass.add(myMethod);
PsiMethod copy = (PsiMethod)myMethod.copy();
myMethod.delete();
myMethod = copy;
}
final PsiModifierList modifierList = newMethod.getModifierList();
modifierList.setModifierProperty(PsiModifier.STATIC, false);
ChangeContextUtil.decodeContextInfo(newMethod, null, null);
@@ -1,7 +1,8 @@
class Test {
int i;
void run() {}
Test getDelegate() {
Test getDelegate() {
return new Test() {
int i;
@@ -12,7 +13,4 @@ class Test {
}
};
}
void run() {}
}
@@ -1,9 +1,9 @@
class Bar {
void foo() {
}
void foo() {
}
void m(){
void m(){
foo();
}
@@ -1,7 +1,6 @@
enum E {
A, B;
boolean x() {
return true;
}
boolean x() {
return true;
}
}
@@ -1,9 +1,9 @@
class X {
static class Nested {
void print(X x) {
System.out.println(x);
}
void print(X x) {
System.out.println(x);
}
}
public static void main(String[] args) {
@@ -1,5 +1,5 @@
interface I {
default void foo() {}
default void foo() {}
}
class WithPrivateInner {
@@ -1,9 +1,8 @@
class Bar {
private void foo() {
f();
Runnable r = this::f;
}
void f() {}
void f() {}
private void foo() {
f();
Runnable r = this::f;
}
}
@@ -1,8 +1,7 @@
class C<T> {
T get() { return null; }
void method(T value, X<T> x) {
T v = get();
System.out.println(v + " " + value);
}
T get() { return null; }
}
@@ -1,8 +1,7 @@
class C<T> {
T get() { return null; }
void method(T value, X<T> x) {
T v = get();
System.out.println(v + " " + value);
}
T get() { return null; }
}