mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-08 15:09:39 +07:00
introduce constant: reuse existing inner class when extracting from enum constant (IDEA-95810)
(cherry picked from commit d5a3ed5aded47303990ca4a3bc70d9817428edf7)
This commit is contained in:
@@ -726,16 +726,23 @@ public abstract class BaseExpressionToFieldHandler extends IntroduceHandlerBase
|
||||
PsiMember anchorMember = finalAnchorElement instanceof PsiMember ? (PsiMember)finalAnchorElement : null;
|
||||
|
||||
if (anchorMember instanceof PsiEnumConstant && destClass == anchorMember.getContainingClass()) {
|
||||
String constantsClassName = "Constants";
|
||||
final String initialName = "Constants";
|
||||
String constantsClassName = initialName;
|
||||
|
||||
int i = 1;
|
||||
while (destClass.findInnerClassByName(constantsClassName, true) != null) {
|
||||
constantsClassName += constantsClassName + i++;
|
||||
PsiClass innerClass = destClass.findInnerClassByName(constantsClassName, true);
|
||||
if (innerClass == null || !isConstantsClass(innerClass)) {
|
||||
int i = 1;
|
||||
while (destClass.findInnerClassByName(constantsClassName, true) != null) {
|
||||
constantsClassName = initialName + i++;
|
||||
}
|
||||
|
||||
PsiClass psiClass = JavaPsiFacade.getElementFactory(myProject).createClass(constantsClassName);
|
||||
PsiUtil.setModifierProperty(psiClass, PsiModifier.PRIVATE, true);
|
||||
PsiUtil.setModifierProperty(psiClass, PsiModifier.STATIC, true);
|
||||
destClass = (PsiClass)destClass.add(psiClass);
|
||||
} else {
|
||||
destClass = innerClass;
|
||||
}
|
||||
|
||||
PsiClass psiClass = JavaPsiFacade.getElementFactory(myProject).createClass(constantsClassName);
|
||||
PsiUtil.setModifierProperty(psiClass, PsiModifier.PRIVATE, true);
|
||||
destClass = (PsiClass)destClass.add(psiClass);
|
||||
anchorMember = null;
|
||||
}
|
||||
myField = appendField(initializer, initializerPlace, destClass, myParentClass, myField, anchorMember);
|
||||
@@ -842,6 +849,14 @@ public abstract class BaseExpressionToFieldHandler extends IntroduceHandlerBase
|
||||
}
|
||||
}
|
||||
|
||||
static boolean isConstantsClass(PsiClass innerClass) {
|
||||
if (innerClass.getMethods().length != 0) return false;
|
||||
for (PsiField field : innerClass.getFields()) {
|
||||
if (!field.hasModifierProperty(PsiModifier.STATIC) || !field.hasModifierProperty(PsiModifier.FINAL)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static PsiField appendField(final PsiExpression initializer,
|
||||
InitializationPlace initializerPlace, final PsiClass destClass,
|
||||
final PsiClass parentClass,
|
||||
|
||||
@@ -3,4 +3,8 @@ enum TestEnum {
|
||||
|
||||
TestEnum(String str) {
|
||||
}
|
||||
|
||||
private class Constants {
|
||||
void foo(){}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
enum TestEnum {
|
||||
ONE("te<caret>stString");
|
||||
|
||||
TestEnum(String str) {
|
||||
}
|
||||
|
||||
private static class Constants {
|
||||
public static final String FOO = "";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
enum TestEnum {
|
||||
ONE(Constants.xxx);
|
||||
|
||||
TestEnum(String str) {
|
||||
}
|
||||
|
||||
private static class Constants {
|
||||
public static final String FOO = "";
|
||||
public static final String xxx = "testString";
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,14 @@
|
||||
enum TestEnum {
|
||||
ONE(Constants.xxx);
|
||||
ONE(Constants1.xxx);
|
||||
|
||||
TestEnum(String str) {
|
||||
}
|
||||
|
||||
private class Constants {
|
||||
private static class Constants1 {
|
||||
public static final String xxx = "testString";
|
||||
}
|
||||
|
||||
private class Constants {
|
||||
void foo(){}
|
||||
}
|
||||
}
|
||||
@@ -41,6 +41,12 @@ public class IntroduceConstantTest extends LightCodeInsightTestCase {
|
||||
new MockIntroduceConstantHandler(null).invoke(getProject(), getEditor(), getFile(), null);
|
||||
checkResultByFile(BASE_PATH + getTestName(false) + "_after.java");
|
||||
}
|
||||
|
||||
public void testFromEnumConstantInitializer1() throws Exception {
|
||||
configureByFile(BASE_PATH + getTestName(false) + ".java");
|
||||
new MockIntroduceConstantHandler(null).invoke(getProject(), getEditor(), getFile(), null);
|
||||
checkResultByFile(BASE_PATH + getTestName(false) + "_after.java");
|
||||
}
|
||||
|
||||
public void testEnumConstant() throws Exception {
|
||||
doTest(true);
|
||||
|
||||
Reference in New Issue
Block a user