KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > surround > LocalDeclarationAnalyzer


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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 package org.eclipse.jdt.internal.corext.refactoring.surround;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
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 JavaDoc 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 JavaDoc(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