1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core.util;
13 import java.util.ArrayList;
15 import net.sourceforge.phpdt.core.IField;
16 import net.sourceforge.phpdt.core.IJavaElement;
17 import net.sourceforge.phpdt.core.IMethod;
18 import net.sourceforge.phpdt.core.IType;
19 import net.sourceforge.phpdt.core.compiler.CharOperation;
20 import net.sourceforge.phpdt.internal.compiler.ASTVisitor;
21 import net.sourceforge.phpdt.internal.compiler.lookup.BlockScope;
22 import net.sourceforge.phpdt.internal.compiler.lookup.ClassScope;
23 import net.sourceforge.phpeclipse.internal.compiler.ast.AbstractMethodDeclaration;
24 import net.sourceforge.phpeclipse.internal.compiler.ast.Argument;
25 import net.sourceforge.phpeclipse.internal.compiler.ast.CompilationUnitDeclaration;
26 import net.sourceforge.phpeclipse.internal.compiler.ast.FieldDeclaration;
27 import net.sourceforge.phpeclipse.internal.compiler.ast.TypeDeclaration;
28 import net.sourceforge.phpeclipse.internal.compiler.ast.TypeReference;
31 * Finds an ASTNode given an IJavaElement in a CompilationUnitDeclaration
33 public class ASTNodeFinder {
34 private CompilationUnitDeclaration unit;
36 public ASTNodeFinder(CompilationUnitDeclaration unit) {
41 * Finds the FieldDeclaration in the given ast corresponding to the given field handle.
42 * Returns null if not found.
44 public FieldDeclaration findField(IField fieldHandle) {
45 TypeDeclaration typeDecl = findType((IType)fieldHandle.getParent());
46 if (typeDecl == null) return null;
47 FieldDeclaration[] fields = typeDecl.fields;
49 char[] fieldName = fieldHandle.getElementName().toCharArray();
50 for (int i = 0, length = fields.length; i < length; i++) {
51 FieldDeclaration field = fields[i];
52 if (CharOperation.equals(fieldName, field.name)) {
61 * Finds the Initializer in the given ast corresponding to the given initializer handle.
62 * Returns null if not found.
64 // public Initializer findInitializer(IInitializer initializerHandle) {
65 // TypeDeclaration typeDecl = findType((IType)initializerHandle.getParent());
66 // if (typeDecl == null) return null;
67 // FieldDeclaration[] fields = typeDecl.fields;
68 // if (fields != null) {
69 // int occurenceCount = ((JavaElement)initializerHandle).occurrenceCount;
70 // for (int i = 0, length = fields.length; i < length; i++) {
71 // FieldDeclaration field = fields[i];
72 // if (field instanceof Initializer && --occurenceCount == 0) {
73 // return (Initializer)field;
81 * Finds the AbstractMethodDeclaration in the given ast corresponding to the given method handle.
82 * Returns null if not found.
84 public AbstractMethodDeclaration findMethod(IMethod methodHandle) {
85 TypeDeclaration typeDecl = findType((IType)methodHandle.getParent());
86 if (typeDecl == null) return null;
87 AbstractMethodDeclaration[] methods = typeDecl.methods;
88 if (methods != null) {
89 char[] selector = methodHandle.getElementName().toCharArray();
90 String[] parameterTypeSignatures = methodHandle.getParameterTypes();
91 int parameterCount = parameterTypeSignatures.length;
92 nextMethod: for (int i = 0, length = methods.length; i < length; i++) {
93 AbstractMethodDeclaration method = methods[i];
94 if (CharOperation.equals(selector, method.selector)) {
95 Argument[] args = method.arguments;
96 int argsLength = args == null ? 0 : args.length;
97 if (argsLength == parameterCount) {
98 for (int j = 0; j < parameterCount; j++) {
99 TypeReference type = args[j].type;
100 String signature = Util.typeSignature(type);
101 if (!signature.equals(parameterTypeSignatures[j])) {
114 * Finds the TypeDeclaration in the given ast corresponding to the given type handle.
115 * Returns null if not found.
117 public TypeDeclaration findType(IType typeHandle) {
118 IJavaElement parent = typeHandle.getParent();
119 final char[] typeName = typeHandle.getElementName().toCharArray();
120 // final int occurenceCount = ((SourceType)typeHandle).occurrenceCount;
121 final boolean findAnonymous = typeName.length == 0;
122 class Visitor extends ASTVisitor {
123 TypeDeclaration result;
125 public boolean visit(TypeDeclaration typeDeclaration, BlockScope scope) {
126 if (result != null) return false;
127 // if ((typeDeclaration.bits & ASTNode.IsAnonymousTypeMASK) != 0) {
128 // if (findAnonymous && ++count == occurenceCount) {
129 // result = typeDeclaration;
132 if (!findAnonymous && CharOperation.equals(typeName, typeDeclaration.name)) {
133 result = typeDeclaration;
136 return false; // visit only one level
139 switch (parent.getElementType()) {
140 case IJavaElement.COMPILATION_UNIT:
141 ArrayList types = this.unit.types;
143 for (int i = 0, length = types.size(); i < length; i++) {
144 TypeDeclaration type = (TypeDeclaration)types.get(i);//[i];
145 if (CharOperation.equals(typeName, type.name)) {
151 case IJavaElement.TYPE:
152 TypeDeclaration parentDecl = findType((IType)parent);
153 if (parentDecl == null) return null;
154 // types = parentDecl.memberTypes;
155 // if (types != null) {
156 // for (int i = 0, length = types.length; i < length; i++) {
157 // TypeDeclaration type = types[i];
158 // if (CharOperation.equals(typeName, type.name)) {
164 case IJavaElement.FIELD:
165 FieldDeclaration fieldDecl = findField((IField)parent);
166 if (fieldDecl == null) return null;
167 Visitor visitor = new Visitor();
168 fieldDecl.traverse(visitor, null);
169 return visitor.result;
170 // case IJavaElement.INITIALIZER:
171 // Initializer initializer = findInitializer((IInitializer)parent);
172 // if (initializer == null) return null;
173 // visitor = new Visitor();
174 // initializer.traverse(visitor, null);
175 // return visitor.result;
176 case IJavaElement.METHOD:
177 AbstractMethodDeclaration methodDecl = findMethod((IMethod)parent);
178 if (methodDecl == null) return null;
179 visitor = new Visitor();
180 methodDecl.traverse(visitor, (ClassScope)null);
181 return visitor.result;