[java-refactoring] Minor fixes in AnonymousToInnerHandler

1. Adapt messages to speak about local class when applicable
2. Fix when name is not changed, but we have type parameters

GitOrigin-RevId: bd3dbda21922473c9c4e52bccf840a35c55c9d59
This commit is contained in:
Tagir Valeev
2023-07-26 12:45:58 +00:00
committed by intellij-monorepo-bot
parent 60ee92a99b
commit 013651b75d
6 changed files with 86 additions and 4 deletions
@@ -449,7 +449,7 @@ public class AnonymousToInnerHandler implements RefactoringActionHandlerOnPsiEle
private void updateSelfReferences(@NotNull PsiClass aClass, String name) {
if (aClass instanceof PsiAnonymousClass) return;
if (name.equals(aClass.getName())) return;
if (name.equals(aClass.getName()) && myTypeParametersToCreate.isEmpty()) return;
PsiElementFactory factory = JavaPsiFacade.getElementFactory(aClass.getProject());
int origCount = aClass.getTypeParameters().length;
for (PsiReference reference : ReferencesSearch.search(aClass, aClass.getUseScope()).findAll()) {
@@ -5,6 +5,7 @@ import com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement;
import com.intellij.java.refactoring.JavaRefactoringBundle;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiAnonymousClass;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
@@ -31,12 +32,14 @@ public class MoveAnonymousOrLocalToInnerFix extends LocalQuickFixAndIntentionAct
@Override
public @NotNull String getText() {
return JavaRefactoringBundle.message("convert.anonymous.to.inner.fix.name");
PsiClass psiClass = (PsiClass)getStartElement();
return psiClass instanceof PsiAnonymousClass ? JavaRefactoringBundle.message("convert.anonymous.to.inner.fix.name") :
JavaRefactoringBundle.message("convert.local.to.inner.fix.name");
}
@Override
public @NotNull String getFamilyName() {
return getText();
return JavaRefactoringBundle.message("convert.anonymous.or.local.to.inner.fix.name");
}
@Override
@@ -0,0 +1,34 @@
import java.util.function.IntFunction;
public class LocalClass {
<T> void test(int x, T t) {
class Hell<caret>o {
Hello(int a) {}
Hello(String a) {}
static {
System.out.println("hello");
}
{
System.out.println("hi"+x);
}
void run(T t) {
System.out.println(x);
System.out.println(Hello.class);
var xHello = new Hello(3);
System.out.println(xHello);
xHello.run(t);
}
}
Hello h = new Hello(1);
IntFunction<Hello> ic = Hello::new;
System.out.println(new Hello(1) {
void test() {}
});
h.run(t);
}
}
@@ -0,0 +1,39 @@
import java.util.function.IntFunction;
public class LocalClass {
<T> void test(int x, T t) {
Hello<T> h = new Hello<>(1, x);
IntFunction<Hello<T>> ic = a -> new Hello<>(a, x);
System.out.println(new Hello<T>(1, x) {
void test() {}
});
h.run(t);
}
private static class Hello<T> {
private final int x;
Hello(int a, int x) {
this.x = x;
System.out.println("hi"+x);
}
Hello(String a, int x) {
this.x = x;
System.out.println("hi"+x);
}
static {
System.out.println("hello");
}
void run(T t) {
System.out.println(x);
System.out.println(Hello.class);
var xHello = new Hello<T>(3, x);
System.out.println(xHello);
xHello.run(t);
}
}
}
@@ -69,6 +69,10 @@ public class AnonymousToInnerTest extends LightJavaCodeInsightTestCase {
doTest("InnerClass", true);
}
public void testLocalClassNoRename() {
doTest("Hello", true);
}
public void testLocalClassVarargCtor() {
doTest("InnerClass", true);
}
@@ -131,6 +131,8 @@ constructor.with.builder.rename.setters.prefix.action.name=Rename Setters Prefix
convert.anonymous.to.inner.action.name=Convert Anonymous to Inner\u2026
convert.local.to.inner.action.name=Convert Local to Inner\u2026
convert.anonymous.to.inner.fix.name=Convert anonymous to inner class
convert.local.to.inner.fix.name=Convert local class to inner class
convert.anonymous.or.local.to.inner.fix.name=Convert anonymous or local class to inner
convert.local.to.field.title=Convert Local to Field
convert.to.instance.method.title=Convert To Instance Method
convertToInstanceMethod.all.reference.type.parameters.are.not.in.project=No target class for the instance method is found: all referenced types are not in project.
@@ -213,7 +215,7 @@ error.incorrect.data=Incorrect Data
error.not.supported.for.jsp={0} refactoring is not supported for JSP
error.not.supported.for.local={0} refactoring is not supported for local classes
error.not.supported.for.package.info={0} refactoring is not supported for package-info.java
error.wrong.caret.position.anonymous=Caret should be positioned inside anonymous class to be refactored
error.wrong.caret.position.anonymous=Caret should be positioned inside anonymous or local class to be refactored
error.wrong.caret.position.constructor=Caret should be positioned inside constructor to be refactored
error.wrong.caret.position.local.or.expression.name=Caret should be positioned at the name of local variable or expression to be refactored
error.wrong.caret.position.method=Caret should be positioned inside method to be refactored