mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
JavaRearranger: fixed field caching in case of multiple classes in java file (IDEA-123733)
This commit is contained in:
+5
-2
@@ -70,7 +70,7 @@ public class JavaArrangementVisitor extends JavaRecursiveElementVisitor {
|
||||
@NotNull private final ArrangementSectionDetector mySectionDetector;
|
||||
@Nullable private final Document myDocument;
|
||||
|
||||
@Nullable private Set<PsiField> classFields;
|
||||
@NotNull private HashMap<PsiClass, Set<PsiField>> myCachedClassFields = ContainerUtil.newHashMap();
|
||||
|
||||
@NotNull private Set<PsiComment> myProcessedSectionsComments = ContainerUtil.newHashSet();
|
||||
|
||||
@@ -266,15 +266,18 @@ public class JavaArrangementVisitor extends JavaRecursiveElementVisitor {
|
||||
return referencedElements;
|
||||
}
|
||||
|
||||
Set<PsiField> classFields = myCachedClassFields.get(containingClass);
|
||||
if (classFields == null) {
|
||||
classFields = ContainerUtil.map2Set(containingClass.getFields(), new Function.Self<PsiField, PsiField>());
|
||||
myCachedClassFields.put(containingClass, classFields);
|
||||
}
|
||||
|
||||
final Set<PsiField> containingClassFields = classFields;
|
||||
fieldInitializer.accept(new JavaRecursiveElementVisitor() {
|
||||
@Override
|
||||
public void visitReferenceExpression(PsiReferenceExpression expression) {
|
||||
PsiElement ref = expression.resolve();
|
||||
if (ref instanceof PsiField && classFields.contains(ref)) {
|
||||
if (ref instanceof PsiField && containingClassFields.contains(ref)) {
|
||||
referencedElements.add((PsiField)ref);
|
||||
}
|
||||
}
|
||||
|
||||
+35
-2
@@ -23,7 +23,6 @@ import static com.intellij.psi.codeStyle.arrangement.std.StdArrangementTokens.Mo
|
||||
class JavaRearrangerFieldReferenceTest extends AbstractJavaRearrangerTest {
|
||||
|
||||
private List<StdArrangementMatchRule> defaultFieldsArrangement = [
|
||||
rule(CLASS),
|
||||
rule(FIELD, STATIC, FINAL),
|
||||
rule(FIELD, PUBLIC),
|
||||
rule(FIELD, PROTECTED),
|
||||
@@ -31,7 +30,6 @@ class JavaRearrangerFieldReferenceTest extends AbstractJavaRearrangerTest {
|
||||
rule(FIELD, PRIVATE)
|
||||
]
|
||||
|
||||
|
||||
void "test keep referenced package private field before public one which has reference through binary expression"() {
|
||||
doTest(initial: '''\
|
||||
public class TestRunnable {
|
||||
@@ -300,4 +298,39 @@ public class Q {
|
||||
)
|
||||
}
|
||||
|
||||
void "test IDEA-123733"() {
|
||||
doTest(
|
||||
initial: '''\
|
||||
class First {
|
||||
protected int test = 12;
|
||||
}
|
||||
|
||||
class Second extends First {
|
||||
void test() {}
|
||||
|
||||
private int q = test;
|
||||
public int t = q;
|
||||
}
|
||||
''',
|
||||
expected: '''\
|
||||
class First {
|
||||
protected int test = 12;
|
||||
}
|
||||
|
||||
class Second extends First {
|
||||
private int q = test;
|
||||
public int t = q;
|
||||
|
||||
void test() {}
|
||||
}
|
||||
''',
|
||||
rules: defaultFieldsArrangement
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user