KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > structure > ASTNodeSearchUtil


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.structure;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.runtime.Assert;
17
18 import org.eclipse.jdt.core.IField;
19 import org.eclipse.jdt.core.IImportContainer;
20 import org.eclipse.jdt.core.IImportDeclaration;
21 import org.eclipse.jdt.core.IInitializer;
22 import org.eclipse.jdt.core.IJavaElement;
23 import org.eclipse.jdt.core.IMember;
24 import org.eclipse.jdt.core.IMethod;
25 import org.eclipse.jdt.core.IPackageDeclaration;
26 import org.eclipse.jdt.core.ISourceRange;
27 import org.eclipse.jdt.core.IType;
28 import org.eclipse.jdt.core.JavaModelException;
29 import org.eclipse.jdt.core.dom.ASTNode;
30 import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
31 import org.eclipse.jdt.core.dom.AnnotationTypeDeclaration;
32 import org.eclipse.jdt.core.dom.AnnotationTypeMemberDeclaration;
33 import org.eclipse.jdt.core.dom.Block;
34 import org.eclipse.jdt.core.dom.BodyDeclaration;
35 import org.eclipse.jdt.core.dom.ClassInstanceCreation;
36 import org.eclipse.jdt.core.dom.CompilationUnit;
37 import org.eclipse.jdt.core.dom.ConstructorInvocation;
38 import org.eclipse.jdt.core.dom.EnumConstantDeclaration;
39 import org.eclipse.jdt.core.dom.EnumDeclaration;
40 import org.eclipse.jdt.core.dom.FieldDeclaration;
41 import org.eclipse.jdt.core.dom.ImportDeclaration;
42 import org.eclipse.jdt.core.dom.Initializer;
43 import org.eclipse.jdt.core.dom.MethodDeclaration;
44 import org.eclipse.jdt.core.dom.PackageDeclaration;
45 import org.eclipse.jdt.core.dom.SimpleName;
46 import org.eclipse.jdt.core.dom.SuperConstructorInvocation;
47 import org.eclipse.jdt.core.dom.TypeDeclaration;
48 import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
49 import org.eclipse.jdt.core.search.SearchMatch;
50
51 import org.eclipse.jdt.internal.corext.dom.ASTNodes;
52 import org.eclipse.jdt.internal.corext.dom.NodeFinder;
53 import org.eclipse.jdt.internal.corext.dom.Selection;
54 import org.eclipse.jdt.internal.corext.dom.SelectionAnalyzer;
55 import org.eclipse.jdt.internal.corext.util.JdtFlags;
56
57 public class ASTNodeSearchUtil {
58
59     private ASTNodeSearchUtil() {
60         //no instance
61
}
62
63     public static ASTNode[] getAstNodes(SearchMatch[] searchResults, CompilationUnit cuNode) {
64         List JavaDoc result= new ArrayList JavaDoc(searchResults.length);
65         for (int i= 0; i < searchResults.length; i++) {
66             ASTNode node= getAstNode(searchResults[i], cuNode);
67             if (node != null)
68                 result.add(node);
69         }
70         return (ASTNode[]) result.toArray(new ASTNode[result.size()]);
71     }
72
73     public static ASTNode getAstNode(SearchMatch searchResult, CompilationUnit cuNode) {
74         ASTNode selectedNode= getAstNode(cuNode, searchResult.getOffset(), searchResult.getLength());
75         if (selectedNode == null)
76             return null;
77         if (selectedNode.getParent() == null)
78             return null;
79         return selectedNode;
80     }
81
82     public static ASTNode getAstNode(CompilationUnit cuNode, int start, int length){
83         SelectionAnalyzer analyzer= new SelectionAnalyzer(Selection.createFromStartLength(start, length), true);
84         cuNode.accept(analyzer);
85         //XXX workaround for jdt core feature 23527
86
ASTNode node= analyzer.getFirstSelectedNode();
87         if (node == null && analyzer.getLastCoveringNode() instanceof SuperConstructorInvocation)
88             node= analyzer.getLastCoveringNode().getParent();
89         else if (node == null && analyzer.getLastCoveringNode() instanceof ConstructorInvocation)
90             node= analyzer.getLastCoveringNode().getParent();
91         
92         if (node == null)
93             return null;
94         
95         ASTNode parentNode= node.getParent();
96
97         if (parentNode instanceof MethodDeclaration){
98             MethodDeclaration md= (MethodDeclaration)parentNode;
99             if (!(node instanceof SimpleName)
100                 && md.isConstructor()
101                 && md.getBody() != null
102                 && md.getBody().statements().size() > 0
103                 &&(md.getBody().statements().get(0) instanceof ConstructorInvocation || md.getBody().statements().get(0) instanceof SuperConstructorInvocation)
104                 &&((ASTNode)md.getBody().statements().get(0)).getLength() == length + 1)
105             return (ASTNode)md.getBody().statements().get(0);
106         }
107
108         if (parentNode instanceof SuperConstructorInvocation){
109             if (parentNode.getLength() == length + 1)
110                 return parentNode;
111         }
112         if (parentNode instanceof ConstructorInvocation){
113             if (parentNode.getLength() == length + 1)
114                 return parentNode;
115         }
116         return node;
117     }
118
119     public static MethodDeclaration getMethodDeclarationNode(IMethod iMethod, CompilationUnit cuNode) throws JavaModelException {
120         return (MethodDeclaration)ASTNodes.getParent(getNameNode(iMethod, cuNode), MethodDeclaration.class);
121     }
122
123     public static AnnotationTypeMemberDeclaration getAnnotationTypeMemberDeclarationNode(IMethod iMethod, CompilationUnit cuNode) throws JavaModelException {
124         return (AnnotationTypeMemberDeclaration) ASTNodes.getParent(getNameNode(iMethod, cuNode), AnnotationTypeMemberDeclaration.class);
125     }
126
127     public static BodyDeclaration getMethodOrAnnotationTypeMemberDeclarationNode(IMethod iMethod, CompilationUnit cuNode) throws JavaModelException {
128         if (JdtFlags.isAnnotation(iMethod.getDeclaringType()))
129             return getAnnotationTypeMemberDeclarationNode(iMethod, cuNode);
130         else
131             return getMethodDeclarationNode(iMethod, cuNode);
132     }
133
134     public static VariableDeclarationFragment getFieldDeclarationFragmentNode(IField iField, CompilationUnit cuNode) throws JavaModelException {
135         ASTNode node= getNameNode(iField, cuNode);
136         if (node instanceof VariableDeclarationFragment)
137             return (VariableDeclarationFragment)node;
138         return (VariableDeclarationFragment)ASTNodes.getParent(node, VariableDeclarationFragment.class);
139     }
140         
141     public static FieldDeclaration getFieldDeclarationNode(IField iField, CompilationUnit cuNode) throws JavaModelException {
142         return (FieldDeclaration) ASTNodes.getParent(getNameNode(iField, cuNode), FieldDeclaration.class);
143     }
144
145     public static EnumConstantDeclaration getEnumConstantDeclaration(IField iField, CompilationUnit cuNode) throws JavaModelException {
146         return (EnumConstantDeclaration) ASTNodes.getParent(getNameNode(iField, cuNode), EnumConstantDeclaration.class);
147     }
148
149     public static BodyDeclaration getFieldOrEnumConstantDeclaration(IField iField, CompilationUnit cuNode) throws JavaModelException {
150         if (JdtFlags.isEnum(iField))
151             return getEnumConstantDeclaration(iField, cuNode);
152         else
153             return getFieldDeclarationNode(iField, cuNode);
154     }
155
156     public static EnumDeclaration getEnumDeclarationNode(IType iType, CompilationUnit cuNode) throws JavaModelException {
157         return (EnumDeclaration) ASTNodes.getParent(getNameNode(iType, cuNode), EnumDeclaration.class);
158     }
159
160     public static AnnotationTypeDeclaration getAnnotationTypeDeclarationNode(IType iType, CompilationUnit cuNode) throws JavaModelException {
161         return (AnnotationTypeDeclaration) ASTNodes.getParent(getNameNode(iType, cuNode), AnnotationTypeDeclaration.class);
162     }
163
164     public static BodyDeclaration getBodyDeclarationNode(IMember iMember, CompilationUnit cuNode) throws JavaModelException {
165         return (BodyDeclaration) ASTNodes.getParent(getNameNode(iMember, cuNode), BodyDeclaration.class);
166     }
167
168     public static AbstractTypeDeclaration getAbstractTypeDeclarationNode(IType iType, CompilationUnit cuNode) throws JavaModelException {
169         return (AbstractTypeDeclaration) ASTNodes.getParent(getNameNode(iType, cuNode), AbstractTypeDeclaration.class);
170     }
171
172     public static TypeDeclaration getTypeDeclarationNode(IType iType, CompilationUnit cuNode) throws JavaModelException {
173         return (TypeDeclaration) ASTNodes.getParent(getNameNode(iType, cuNode), TypeDeclaration.class);
174     }
175     
176     public static ClassInstanceCreation getClassInstanceCreationNode(IType iType, CompilationUnit cuNode) throws JavaModelException {
177         return (ClassInstanceCreation) ASTNodes.getParent(getNameNode(iType, cuNode), ClassInstanceCreation.class);
178     }
179     
180     public static List JavaDoc getBodyDeclarationList(IType iType, CompilationUnit cuNode) throws JavaModelException {
181         if (iType.isAnonymous())
182             return getClassInstanceCreationNode(iType, cuNode).getAnonymousClassDeclaration().bodyDeclarations();
183         else
184             return getAbstractTypeDeclarationNode(iType, cuNode).bodyDeclarations();
185     }
186     
187     //returns an array because of the import container, which does not represent 1 node but many
188
//for fields, it returns the whole declaration node
189
public static ASTNode[] getDeclarationNodes(IJavaElement element, CompilationUnit cuNode) throws JavaModelException {
190         switch(element.getElementType()){
191             case IJavaElement.FIELD:
192                 return new ASTNode[]{getFieldOrEnumConstantDeclaration((IField) element, cuNode)};
193             case IJavaElement.IMPORT_CONTAINER:
194                 return getImportNodes((IImportContainer)element, cuNode);
195             case IJavaElement.IMPORT_DECLARATION:
196                 return new ASTNode[]{getImportDeclarationNode((IImportDeclaration)element, cuNode)};
197             case IJavaElement.INITIALIZER:
198                 return new ASTNode[]{getInitializerNode((IInitializer)element, cuNode)};
199             case IJavaElement.METHOD:
200                 return new ASTNode[]{getMethodOrAnnotationTypeMemberDeclarationNode((IMethod) element, cuNode)};
201             case IJavaElement.PACKAGE_DECLARATION:
202                 return new ASTNode[]{getPackageDeclarationNode((IPackageDeclaration)element, cuNode)};
203             case IJavaElement.TYPE:
204                 return new ASTNode[]{getAbstractTypeDeclarationNode((IType) element, cuNode)};
205             default:
206                 Assert.isTrue(false, String.valueOf(element.getElementType()));
207                 return null;
208         }
209     }
210
211     private static ASTNode getNameNode(IMember iMember, CompilationUnit cuNode) throws JavaModelException {
212         return NodeFinder.perform(cuNode, iMember.getNameRange());
213     }
214
215     public static PackageDeclaration getPackageDeclarationNode(IPackageDeclaration reference, CompilationUnit cuNode) throws JavaModelException {
216         return (PackageDeclaration) findNode(reference.getSourceRange(), cuNode);
217     }
218
219     public static ImportDeclaration getImportDeclarationNode(IImportDeclaration reference, CompilationUnit cuNode) throws JavaModelException {
220         return (ImportDeclaration) findNode(reference.getSourceRange(), cuNode);
221     }
222
223     public static ASTNode[] getImportNodes(IImportContainer reference, CompilationUnit cuNode) throws JavaModelException {
224         IJavaElement[] imps= reference.getChildren();
225         ASTNode[] result= new ASTNode[imps.length];
226         for (int i= 0; i < imps.length; i++) {
227             result[i]= getImportDeclarationNode((IImportDeclaration)imps[i], cuNode);
228         }
229         return result;
230     }
231
232     public static Initializer getInitializerNode(IInitializer initializer, CompilationUnit cuNode) throws JavaModelException {
233         ASTNode node= findNode(initializer.getSourceRange(), cuNode);
234         if (node instanceof Initializer)
235             return (Initializer) node;
236         if (node instanceof Block && node.getParent() instanceof Initializer)
237             return (Initializer) node.getParent();
238         return null;
239     }
240     
241     private static ASTNode findNode(ISourceRange range, CompilationUnit cuNode){
242         NodeFinder nodeFinder= new NodeFinder(range.getOffset(), range.getLength());
243         cuNode.accept(nodeFinder);
244         ASTNode coveredNode= nodeFinder.getCoveredNode();
245         if (coveredNode != null)
246             return coveredNode;
247         else
248             return nodeFinder.getCoveringNode();
249     }
250     
251     public static ASTNode[] findNodes(SearchMatch[] searchResults, CompilationUnit cuNode) {
252         List JavaDoc result= new ArrayList JavaDoc(searchResults.length);
253         for (int i= 0; i < searchResults.length; i++) {
254             ASTNode node= findNode(searchResults[i], cuNode);
255             if (node != null)
256                 result.add(node);
257         }
258         return (ASTNode[]) result.toArray(new ASTNode[result.size()]);
259     }
260
261     public static ASTNode findNode(SearchMatch searchResult, CompilationUnit cuNode) {
262         ASTNode selectedNode= NodeFinder.perform(cuNode, searchResult.getOffset(), searchResult.getLength());
263         if (selectedNode == null)
264             return null;
265         if (selectedNode.getParent() == null)
266             return null;
267         return selectedNode;
268     }
269 }
270
Popular Tags