java introduce constant: allow to replace multiple occurrences for field-anchor without initializer (IDEA-250412)

fix regression introduced by enabling extract variable from field initializer

GitOrigin-RevId: c6dd97ec10ecec32f48a337482afb6900464f668
This commit is contained in:
Anna Kozlova
2020-09-09 18:08:02 +00:00
committed by intellij-monorepo-bot
parent e429717fa2
commit 6cfefc432f
4 changed files with 16 additions and 1 deletions
@@ -81,7 +81,7 @@ public abstract class BaseOccurrenceManager implements OccurrenceManager {
@Override
public PsiElement getAnchorStatementForAllInScope(PsiElement scope) {
PsiElement anchor = RefactoringUtil.getAnchorElementForMultipleExpressions(myOccurrences, scope);
return anchor instanceof PsiField && !(anchor instanceof PsiEnumConstant) ? ((PsiField)anchor).getInitializer() : anchor;
return anchor instanceof PsiField && ((PsiField)anchor).hasInitializer() && !(anchor instanceof PsiEnumConstant) ? ((PsiField)anchor).getInitializer() : anchor;
}
private static boolean needToDeclareFinal(PsiExpression[] occurrences) {
@@ -0,0 +1,5 @@
class Test {
@SuppressWarnings("unused")
String myField;
String myField1 = "unu<caret>sed";
}
@@ -0,0 +1,6 @@
class Test {
public static final String xxx = "unused";
@SuppressWarnings(xxx)
String myField;
String myField1 = xxx;
}
@@ -180,6 +180,10 @@ public class IntroduceConstantTest extends LightJavaCodeInsightTestCase {
doTestExpr();
}
public void testConstantFromAnnotationOnFieldWithoutInitializer() {
doTestExpr();
}
public void testEscalateVisibility() {
configureByFile(BASE_PATH + getTestName(false) + ".java");
final PsiClass[] classes = ((PsiJavaFile)getFile()).getClasses();