KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > util > ASTNodeFinder


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.core.util;
12
13 import org.eclipse.jdt.core.*;
14 import org.eclipse.jdt.core.IJavaElement;
15 import org.eclipse.jdt.core.IMethod;
16 import org.eclipse.jdt.core.IType;
17 import org.eclipse.jdt.core.compiler.CharOperation;
18 import org.eclipse.jdt.internal.compiler.ASTVisitor;
19 import org.eclipse.jdt.internal.compiler.ast.*;
20 import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration;
21 import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
22 import org.eclipse.jdt.internal.compiler.lookup.BlockScope;
23 import org.eclipse.jdt.internal.compiler.lookup.ClassScope;
24 import org.eclipse.jdt.internal.core.SourceRefElement;
25 import org.eclipse.jdt.internal.core.SourceType;
26
27 /**
28  * Finds an ASTNode given an IJavaElement in a CompilationUnitDeclaration
29  */

30 public class ASTNodeFinder {
31     private CompilationUnitDeclaration unit;
32
33     public ASTNodeFinder(CompilationUnitDeclaration unit) {
34         this.unit = unit;
35     }
36
37     /*
38      * Finds the FieldDeclaration in the given ast corresponding to the given field handle.
39      * Returns null if not found.
40      */

41     public FieldDeclaration findField(IField fieldHandle) {
42         TypeDeclaration typeDecl = findType((IType)fieldHandle.getParent());
43         if (typeDecl == null) return null;
44         FieldDeclaration[] fields = typeDecl.fields;
45         if (fields != null) {
46             char[] fieldName = fieldHandle.getElementName().toCharArray();
47             for (int i = 0, length = fields.length; i < length; i++) {
48                 FieldDeclaration field = fields[i];
49                 if (CharOperation.equals(fieldName, field.name)) {
50                     return field;
51                 }
52             }
53         }
54         return null;
55     }
56
57     /*
58      * Finds the Initializer in the given ast corresponding to the given initializer handle.
59      * Returns null if not found.
60      */

61     public Initializer findInitializer(IInitializer initializerHandle) {
62         TypeDeclaration typeDecl = findType((IType)initializerHandle.getParent());
63         if (typeDecl == null) return null;
64         FieldDeclaration[] fields = typeDecl.fields;
65         if (fields != null) {
66             int occurenceCount = ((SourceRefElement)initializerHandle).occurrenceCount;
67             for (int i = 0, length = fields.length; i < length; i++) {
68                 FieldDeclaration field = fields[i];
69                 if (field instanceof Initializer && --occurenceCount == 0) {
70                     return (Initializer)field;
71                 }
72             }
73         }
74         return null;
75     }
76
77     /*
78      * Finds the AbstractMethodDeclaration in the given ast corresponding to the given method handle.
79      * Returns null if not found.
80      */

81     public AbstractMethodDeclaration findMethod(IMethod methodHandle) {
82         TypeDeclaration typeDecl = findType((IType)methodHandle.getParent());
83         if (typeDecl == null) return null;
84         AbstractMethodDeclaration[] methods = typeDecl.methods;
85         if (methods != null) {
86             char[] selector = methodHandle.getElementName().toCharArray();
87             String JavaDoc[] parameterTypeSignatures = methodHandle.getParameterTypes();
88             int parameterCount = parameterTypeSignatures.length;
89             nextMethod: for (int i = 0, length = methods.length; i < length; i++) {
90                 AbstractMethodDeclaration method = methods[i];
91                 if (CharOperation.equals(selector, method.selector)) {
92                     Argument[] args = method.arguments;
93                     int argsLength = args == null ? 0 : args.length;
94                     if (argsLength == parameterCount) {
95                         for (int j = 0; j < parameterCount; j++) {
96                             TypeReference type = args[j].type;
97                             String JavaDoc signature = Util.typeSignature(type);
98                             if (!signature.equals(parameterTypeSignatures[j])) {
99                                 continue nextMethod;
100                             }
101                         }
102                         return method;
103                     }
104                 }
105             }
106         }
107         return null;
108     }
109
110     /*
111      * Finds the TypeDeclaration in the given ast corresponding to the given type handle.
112      * Returns null if not found.
113      */

114     public TypeDeclaration findType(IType typeHandle) {
115         IJavaElement parent = typeHandle.getParent();
116         final char[] typeName = typeHandle.getElementName().toCharArray();
117         final int occurenceCount = ((SourceType)typeHandle).occurrenceCount;
118         final boolean findAnonymous = typeName.length == 0;
119         class Visitor extends ASTVisitor {
120             TypeDeclaration result;
121             int count = 0;
122             public boolean visit(TypeDeclaration typeDeclaration, BlockScope scope) {
123                 if (result != null) return false;
124                 if ((typeDeclaration.bits & ASTNode.IsAnonymousType) != 0) {
125                     if (findAnonymous && ++count == occurenceCount) {
126                         result = typeDeclaration;
127                     }
128                 } else {
129                     if (!findAnonymous && CharOperation.equals(typeName, typeDeclaration.name)) {
130                         result = typeDeclaration;
131                     }
132                 }
133                 return false; // visit only one level
134
}
135         }
136         switch (parent.getElementType()) {
137             case IJavaElement.COMPILATION_UNIT:
138                 TypeDeclaration[] types = this.unit.types;
139                 if (types != null) {
140                     for (int i = 0, length = types.length; i < length; i++) {
141                         TypeDeclaration type = types[i];
142                         if (CharOperation.equals(typeName, type.name)) {
143                             return type;
144                         }
145                     }
146                 }
147                 break;
148             case IJavaElement.TYPE:
149                 TypeDeclaration parentDecl = findType((IType)parent);
150                 if (parentDecl == null) return null;
151                 types = parentDecl.memberTypes;
152                 if (types != null) {
153                     for (int i = 0, length = types.length; i < length; i++) {
154                         TypeDeclaration type = types[i];
155                         if (CharOperation.equals(typeName, type.name)) {
156                             return type;
157                         }
158                     }
159                 }
160                 break;
161             case IJavaElement.FIELD:
162                 FieldDeclaration fieldDecl = findField((IField)parent);
163                 if (fieldDecl == null) return null;
164                 Visitor visitor = new Visitor();
165                 fieldDecl.traverse(visitor, null);
166                 return visitor.result;
167             case IJavaElement.INITIALIZER:
168                 Initializer initializer = findInitializer((IInitializer)parent);
169                 if (initializer == null) return null;
170                 visitor = new Visitor();
171                 initializer.traverse(visitor, null);
172                 return visitor.result;
173             case IJavaElement.METHOD:
174                 AbstractMethodDeclaration methodDecl = findMethod((IMethod)parent);
175                 if (methodDecl == null) return null;
176                 visitor = new Visitor();
177                 methodDecl.traverse(visitor, (ClassScope)null);
178                 return visitor.result;
179         }
180         return null;
181     }
182 }
183
Popular Tags