1 11 package org.eclipse.jdt.internal.corext.refactoring.surround; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 15 16 import org.eclipse.jdt.core.dom.ASTNode; 17 import org.eclipse.jdt.core.dom.ASTVisitor; 18 import org.eclipse.jdt.core.dom.BodyDeclaration; 19 import org.eclipse.jdt.core.dom.IVariableBinding; 20 import org.eclipse.jdt.core.dom.SimpleName; 21 import org.eclipse.jdt.core.dom.VariableDeclaration; 22 23 import org.eclipse.jdt.internal.corext.dom.ASTNodes; 24 import org.eclipse.jdt.internal.corext.dom.Selection; 25 26 27 public class LocalDeclarationAnalyzer extends ASTVisitor { 28 29 private Selection fSelection; 30 private List fAffectedLocals; 31 32 public static VariableDeclaration[] perform(BodyDeclaration parent, Selection selection) { 33 LocalDeclarationAnalyzer analyzer= new LocalDeclarationAnalyzer(selection); 34 parent.accept(analyzer); 35 return (VariableDeclaration[]) analyzer.fAffectedLocals.toArray(new VariableDeclaration[analyzer.fAffectedLocals.size()]); 36 } 37 38 private LocalDeclarationAnalyzer(Selection selection) { 39 fSelection= selection; 40 fAffectedLocals= new ArrayList (1); 41 } 42 43 public boolean visit(SimpleName node) { 44 IVariableBinding binding= null; 45 if (node.isDeclaration() || !considerNode(node) || (binding= ASTNodes.getLocalVariableBinding(node)) == null) 46 return false; 47 handleReferenceToLocal(node, binding); 48 return true; 49 } 50 51 private boolean considerNode(ASTNode node) { 52 return fSelection.getVisitSelectionMode(node) == Selection.AFTER; 53 } 54 55 private void handleReferenceToLocal(SimpleName node, IVariableBinding binding) { 56 VariableDeclaration declaration= ASTNodes.findVariableDeclaration(binding, node); 57 if (declaration != null && fSelection.covers(declaration)) 58 addLocalDeclaration(declaration); 59 } 60 61 private void addLocalDeclaration(VariableDeclaration declaration) { 62 if (!fAffectedLocals.contains(declaration)) 63 fAffectedLocals.add(declaration); 64 } 65 } 66 | Popular Tags |