2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com
.intellij
.refactoring
.util
.occurences
;
18 import com
.intellij
.psi
.PsiClass
;
19 import com
.intellij
.psi
.PsiElement
;
20 import com
.intellij
.psi
.PsiExpression
;
21 import com
.intellij
.psi
.util
.PsiTreeUtil
;
22 import com
.intellij
.refactoring
.introduceField
.ElementToWorkOn
;
23 import com
.intellij
.refactoring
.util
.RefactoringUtil
;
25 import java
.util
.ArrayList
;
30 public abstract class BaseOccurenceManager
implements OccurenceManager
{
31 private PsiExpression
[] myOccurences
= null;
32 private PsiElement myAnchorStatement
= null;
33 protected final OccurenceFilter myFilter
;
35 public BaseOccurenceManager(OccurenceFilter filter
) {
39 public PsiExpression
[] getOccurences() {
40 if(myOccurences
== null) {
41 myOccurences
= findOccurences();
43 if(myFilter
!= null) {
44 ArrayList
<PsiExpression
> result
= new ArrayList
<PsiExpression
>();
45 for (PsiExpression occurence
: myOccurences
) {
46 if (myFilter
.isOK(occurence
)) result
.add(occurence
);
48 if (result
.isEmpty()) {
49 myOccurences
= defaultOccurences();
52 myOccurences
= result
.toArray(new PsiExpression
[result
.size()]);
56 if (getAnchorStatementForAll() == null) {
57 myOccurences
= defaultOccurences();
63 protected abstract PsiExpression
[] defaultOccurences();
65 protected abstract PsiExpression
[] findOccurences();
67 public boolean isInFinalContext() {
68 return needToDeclareFinal(myOccurences
);
71 public PsiElement
getAnchorStatementForAll() {
72 if(myAnchorStatement
== null) {
73 myAnchorStatement
= getAnchorStatementForAllInScope(null);
75 return myAnchorStatement
;
78 public PsiElement
getAnchorStatementForAllInScope(PsiElement scope
) {
79 return RefactoringUtil
.getAnchorElementForMultipleExpressions(myOccurences
, scope
);
82 private static boolean needToDeclareFinal(PsiExpression
[] occurrences
) {
83 PsiElement scopeToDeclare
= null;
84 for (PsiExpression occurrence
: occurrences
) {
85 final PsiElement data
= occurrence
.getUserData(ElementToWorkOn
.PARENT
);
86 if (scopeToDeclare
== null) {
87 scopeToDeclare
= data
!= null ? data
: occurrence
;
90 scopeToDeclare
= PsiTreeUtil
.findCommonParent(scopeToDeclare
, data
!= null ? data
: occurrence
);
93 if(scopeToDeclare
== null) {
97 for (PsiExpression occurrence
: occurrences
) {
98 PsiElement parent
= occurrence
.getUserData(ElementToWorkOn
.PARENT
);
99 if (parent
== null) parent
= occurrence
;
100 while (!parent
.equals(scopeToDeclare
)) {
101 parent
= parent
.getParent();
102 if (parent
instanceof PsiClass
) {