KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > code > NameCollector


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.code;
12 import java.util.ArrayList JavaDoc;
13 import java.util.List JavaDoc;
14
15 import org.eclipse.jdt.core.dom.ASTNode;
16 import org.eclipse.jdt.core.dom.SimpleName;
17 import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
18 import org.eclipse.jdt.core.dom.TypeDeclarationStatement;
19 import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
20 import org.eclipse.jdt.core.dom.VariableDeclarationStatement;
21
22 import org.eclipse.jdt.internal.corext.dom.GenericVisitor;
23 import org.eclipse.jdt.internal.corext.dom.Selection;
24
25 class NameCollector extends GenericVisitor {
26     private List JavaDoc names= new ArrayList JavaDoc();
27     private Selection fSelection;
28     public NameCollector(ASTNode node) {
29         fSelection= Selection.createFromStartLength(node.getStartPosition(), node.getLength());
30     }
31     protected boolean visitNode(ASTNode node) {
32         if (node.getStartPosition() > fSelection.getInclusiveEnd())
33             return true;
34         if (fSelection.coveredBy(node))
35             return true;
36         return false;
37     }
38     public boolean visit(SimpleName node) {
39         names.add(node.getIdentifier());
40         return super.visit(node);
41     }
42     public boolean visit(VariableDeclarationStatement node) {
43         return true;
44     }
45     public boolean visit(VariableDeclarationFragment node) {
46         boolean result= super.visit(node);
47         if (!result)
48             names.add(node.getName().getIdentifier());
49         return result;
50     }
51     public boolean visit(SingleVariableDeclaration node) {
52         boolean result= super.visit(node);
53         if (!result)
54             names.add(node.getName().getIdentifier());
55         return result;
56     }
57     public boolean visit(TypeDeclarationStatement node) {
58         names.add(node.getDeclaration().getName().getIdentifier());
59         return false;
60     }
61
62     List JavaDoc getNames() {
63         return names;
64     }
65 }
66
Popular Tags