1 11 12 package org.eclipse.jdt.internal.corext.refactoring.rename; 13 14 import java.util.HashSet ; 15 import java.util.Iterator ; 16 import java.util.Set ; 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 33 private Set fReferenceNodes; 34 35 private Set 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 (); 46 fJavadocNodes= new HashSet (); 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 nodeSet) { 72 int i= start; 73 for (Iterator 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 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; } 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 |