KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > rename > TempOccurrenceAnalyzer


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.jdt.internal.corext.refactoring.rename;
13
14 import java.util.HashSet JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Set JavaDoc;
17
18 import org.eclipse.core.runtime.Assert;
19
20 import org.eclipse.jdt.core.dom.ASTNode;
21 import org.eclipse.jdt.core.dom.ASTVisitor;
22 import org.eclipse.jdt.core.dom.CompilationUnit;
23 import org.eclipse.jdt.core.dom.IBinding;
24 import org.eclipse.jdt.core.dom.Javadoc;
25 import org.eclipse.jdt.core.dom.SimpleName;
26 import org.eclipse.jdt.core.dom.VariableDeclaration;
27
28 import org.eclipse.jdt.internal.corext.dom.ASTNodes;
29
30
31 public class TempOccurrenceAnalyzer extends ASTVisitor {
32     /** Set of SimpleName */
33     private Set JavaDoc fReferenceNodes;
34     /** Set of SimpleName */
35     private Set JavaDoc fJavadocNodes;
36
37     private VariableDeclaration fTempDeclaration;
38     private IBinding fTempBinding;
39     private boolean fAnalyzeJavadoc;
40     
41     private boolean fIsInJavadoc;
42     
43     public TempOccurrenceAnalyzer(VariableDeclaration tempDeclaration, boolean analyzeJavadoc){
44         Assert.isNotNull(tempDeclaration);
45         fReferenceNodes= new HashSet JavaDoc();
46         fJavadocNodes= new HashSet JavaDoc();
47         fAnalyzeJavadoc= analyzeJavadoc;
48         fTempDeclaration= tempDeclaration;
49         fTempBinding= tempDeclaration.resolveBinding();
50         fIsInJavadoc= false;
51     }
52     
53     public void perform() {
54         ASTNode cuNode= ASTNodes.getParent(fTempDeclaration, CompilationUnit.class);
55         cuNode.accept(this);
56     }
57     
58     public int[] getReferenceOffsets(){
59         int[] offsets= new int[fReferenceNodes.size()];
60         addOffsets(offsets, 0, fReferenceNodes);
61         return offsets;
62     }
63     
64     public int[] getReferenceAndJavadocOffsets(){
65         int[] offsets= new int[fReferenceNodes.size() + fJavadocNodes.size()];
66         addOffsets(offsets, 0, fReferenceNodes);
67         addOffsets(offsets, fReferenceNodes.size(), fJavadocNodes);
68         return offsets;
69     }
70     
71     private void addOffsets(int[] offsets, int start, Set JavaDoc nodeSet) {
72         int i= start;
73         for (Iterator JavaDoc iter= nodeSet.iterator(); iter.hasNext(); i++) {
74             ASTNode node= (ASTNode) iter.next();
75             offsets[i]= node.getStartPosition();
76         }
77     }
78     
79     public int getNumberOfReferences() {
80         return fReferenceNodes.size();
81     }
82
83     public SimpleName[] getReferenceNodes() {
84         return (SimpleName[]) fReferenceNodes.toArray(new SimpleName[fReferenceNodes.size()]);
85     }
86     
87     public SimpleName[] getJavadocNodes() {
88         return (SimpleName[]) fJavadocNodes.toArray(new SimpleName[fJavadocNodes.size()]);
89     }
90     
91     public SimpleName[] getReferenceAndDeclarationNodes() {
92         SimpleName[] nodes= (SimpleName[]) fReferenceNodes.toArray(new SimpleName[fReferenceNodes.size() + 1]);
93         nodes[fReferenceNodes.size()]= fTempDeclaration.getName();
94         return nodes;
95     }
96             
97     //------- visit ------ (don't call)
98

99     public boolean visit(Javadoc node) {
100         if (fAnalyzeJavadoc)
101             fIsInJavadoc= true;
102         return fAnalyzeJavadoc;
103     }
104         
105     public void endVisit(Javadoc node) {
106         fIsInJavadoc= false;
107     }
108     
109     public boolean visit(SimpleName node){
110         if (node.getParent() instanceof VariableDeclaration){
111             if (((VariableDeclaration)node.getParent()).getName() == node)
112                 return true; //don't include declaration
113
}
114         
115         if (fTempBinding != null && fTempBinding == node.resolveBinding()) {
116             if (fIsInJavadoc)
117                 fJavadocNodes.add(node);
118             else
119                 fReferenceNodes.add(node);
120         }
121                 
122         return true;
123     }
124 }
125
Popular Tags