KickJava   Java API By Example, From Geeks To Geeks.

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


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.rename;
12
13 import org.eclipse.jdt.core.dom.ASTNode;
14 import org.eclipse.jdt.core.dom.CompilationUnit;
15 import org.eclipse.jdt.core.dom.IBinding;
16 import org.eclipse.jdt.core.dom.Name;
17 import org.eclipse.jdt.core.dom.SimpleName;
18 import org.eclipse.jdt.core.dom.VariableDeclaration;
19 import org.eclipse.jdt.core.dom.VariableDeclarationStatement;
20
21 import org.eclipse.jface.text.IRegion;
22 import org.eclipse.jface.text.Region;
23
24 import org.eclipse.jdt.internal.corext.dom.Selection;
25 import org.eclipse.jdt.internal.corext.dom.SelectionAnalyzer;
26
27 public class TempDeclarationFinder {
28
29     //no instances
30
private TempDeclarationFinder(){}
31     
32     /**
33      * @return <code>null</code> if the selection is invalid or does not cover a temp
34      * declaration or reference.
35      */

36     public static VariableDeclaration findTempDeclaration(CompilationUnit cu, int selectionOffset, int selectionLength) {
37         TempSelectionAnalyzer analyzer= new TempSelectionAnalyzer(selectionOffset, selectionLength);
38         cu.accept(analyzer);
39         
40         ASTNode[] selected= analyzer.getSelectedNodes();
41         if (selected == null || selected.length != 1)
42             return null;
43             
44         ASTNode selectedNode= selected[0];
45         if (selectedNode instanceof VariableDeclaration)
46             return (VariableDeclaration)selectedNode;
47         
48         if (selectedNode instanceof Name){
49             Name reference= (Name)selectedNode;
50             IBinding binding= reference.resolveBinding();
51             if (binding == null)
52                 return null;
53             ASTNode declaringNode= cu.findDeclaringNode(binding);
54             if (declaringNode instanceof VariableDeclaration)
55                 return (VariableDeclaration)declaringNode;
56             else
57                 return null;
58         } else if (selectedNode instanceof VariableDeclarationStatement){
59             VariableDeclarationStatement vds= (VariableDeclarationStatement)selectedNode;
60             if (vds.fragments().size() != 1)
61                 return null;
62             return (VariableDeclaration)vds.fragments().get(0);
63         }
64         return null;
65     }
66     
67     /*
68      * Class used to extract selected nodes from an AST.
69      * Subclassing <code>SelectionAnalyzer</code> is needed to support activation
70      * when only a part of the <code>VariableDeclaration</code> node is selected
71      */

72     private static class TempSelectionAnalyzer extends SelectionAnalyzer {
73
74         private ASTNode fNode;
75
76         TempSelectionAnalyzer(int selectionOffset, int selectionLength){
77             super(Selection.createFromStartLength(selectionOffset, selectionLength), true);
78         }
79
80         //overridden
81
public boolean visitNode(ASTNode node) {
82             if (node instanceof VariableDeclaration)
83                 return visitVariableDeclaration((VariableDeclaration)node);
84             else if (node instanceof SimpleName)
85                     return visitSimpleName((SimpleName)node);
86             else
87                 return super.visitNode(node);
88         }
89         
90         private boolean addNodeAndStop(ASTNode node){
91             fNode= node;
92             return false;
93         }
94         
95         private boolean visitSimpleName(SimpleName name) {
96             if (getSelection().coveredBy(name))
97                 return addNodeAndStop(name);
98             return super.visitNode(name);
99         }
100         
101         private boolean visitVariableDeclaration(VariableDeclaration vd) {
102             if (vd.getInitializer() != null){
103                 int start= vd.getStartPosition();
104                 IRegion declarationRange= new Region(start, vd.getInitializer().getStartPosition() - start);
105                 if (getSelection().coveredBy(declarationRange))
106                     return addNodeAndStop(vd);
107                 else
108                     return super.visitNode(vd);
109             } else {
110                 if (getSelection().coveredBy(vd))
111                     return addNodeAndStop(vd);
112                 else
113                     return super.visitNode(vd);
114             }
115         }
116         
117         //overridden
118
public ASTNode[] getSelectedNodes() {
119             if (fNode != null)
120                 return new ASTNode[] { fNode };
121             return super.getSelectedNodes();
122         }
123     }
124
125 }
126
Popular Tags