KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > SourceTypeElementInfo


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;
12
13 import java.util.HashMap JavaDoc;
14
15 import org.eclipse.jdt.core.*;
16 import org.eclipse.jdt.core.compiler.CharOperation;
17 import org.eclipse.jdt.internal.compiler.env.ISourceField;
18 import org.eclipse.jdt.internal.compiler.env.ISourceImport;
19 import org.eclipse.jdt.internal.compiler.env.ISourceMethod;
20 import org.eclipse.jdt.internal.compiler.env.ISourceType;
21
22 /**
23  * Element info for an IType element that originated from source.
24  */

25 public class SourceTypeElementInfo extends MemberElementInfo implements ISourceType {
26
27     protected static final ISourceImport[] NO_IMPORTS = new ISourceImport[0];
28     protected static final InitializerElementInfo[] NO_INITIALIZERS = new InitializerElementInfo[0];
29     protected static final SourceField[] NO_FIELDS = new SourceField[0];
30     protected static final SourceMethod[] NO_METHODS = new SourceMethod[0];
31     protected static final SourceType[] NO_TYPES = new SourceType[0];
32     /**
33      * The name of the superclass for this type. This name
34      * is fully qualified for binary types and is NOT
35      * fully qualified for source types.
36      */

37     protected char[] superclassName;
38     
39     /**
40      * The names of the interfaces this type implements or
41      * extends. These names are fully qualified in the case
42      * of a binary type, and are NOT fully qualified in the
43      * case of a source type
44      */

45     protected char[][] superInterfaceNames;
46     
47     /**
48      * Backpointer to my type handle - useful for translation
49      * from info to handle.
50      */

51     protected IType handle = null;
52     
53     /*
54      * The type parameters of this source type. Empty if none.
55      */

56     protected ITypeParameter[] typeParameters = TypeParameter.NO_TYPE_PARAMETERS;
57     
58     /*
59      * A map from an IJavaElement (this type or a child of this type) to a String[] (the categories of this element)
60      */

61     protected HashMap JavaDoc categories;
62     
63 protected void addCategories(IJavaElement element, char[][] elementCategories) {
64     if (elementCategories == null) return;
65     if (this.categories == null)
66         this.categories = new HashMap JavaDoc();
67     this.categories.put(element, CharOperation.toStrings(elementCategories));
68 }
69
70 /*
71  * Return a map from an IJavaElement (this type or a child of this type) to a String[] (the categories of this element)
72  */

73 public HashMap JavaDoc getCategories() {
74     return this.categories;
75 }
76
77 /**
78  * Returns the ISourceType that is the enclosing type for this
79  * type, or <code>null</code> if this type is a top level type.
80  */

81 public ISourceType getEnclosingType() {
82     IJavaElement parent= this.handle.getParent();
83     if (parent != null && parent.getElementType() == IJavaElement.TYPE) {
84         try {
85             return (ISourceType)((JavaElement)parent).getElementInfo();
86         } catch (JavaModelException e) {
87             return null;
88         }
89     } else {
90         return null;
91     }
92 }
93 /**
94  * @see ISourceType
95  */

96 public ISourceField[] getFields() {
97     SourceField[] fieldHandles = getFieldHandles();
98     int length = fieldHandles.length;
99     ISourceField[] fields = new ISourceField[length];
100     for (int i = 0; i < length; i++) {
101         try {
102             ISourceField field = (ISourceField) fieldHandles[i].getElementInfo();
103             fields[i] = field;
104         } catch (JavaModelException e) {
105             // ignore
106
}
107     }
108     return fields;
109 }
110 public SourceField[] getFieldHandles() {
111     int length = this.children.length;
112     if (length == 0) return NO_FIELDS;
113     SourceField[] fields = new SourceField[length];
114     int fieldIndex = 0;
115     for (int i = 0; i < length; i++) {
116         IJavaElement child = this.children[i];
117         if (child instanceof SourceField)
118             fields[fieldIndex++] = (SourceField) child;
119     }
120     if (fieldIndex == 0) return NO_FIELDS;
121     if (fieldIndex < length)
122         System.arraycopy(fields, 0, fields = new SourceField[fieldIndex], 0, fieldIndex);
123     return fields;
124 }
125 /**
126  * @see org.eclipse.jdt.internal.compiler.env.IDependent#getFileName()
127  */

128 public char[] getFileName() {
129     return this.handle.getPath().toString().toCharArray();
130 }
131 /**
132  * Returns the handle for this type info
133  */

134 public IType getHandle() {
135     return this.handle;
136 }
137 /*
138  * Returns the InitializerElementInfos for this type.
139  * Returns an empty array if none.
140  */

141 public InitializerElementInfo[] getInitializers() {
142     int length = this.children.length;
143     if (length == 0) return NO_INITIALIZERS;
144     InitializerElementInfo[] initializers = new InitializerElementInfo[length];
145     int initializerIndex = 0;
146     for (int i = 0; i < length; i++) {
147         IJavaElement child = this.children[i];
148         if (child instanceof Initializer) {
149             try {
150                 InitializerElementInfo initializer = (InitializerElementInfo)((Initializer)child).getElementInfo();
151                 initializers[initializerIndex++] = initializer;
152             } catch (JavaModelException e) {
153                 // ignore
154
}
155         }
156     }
157     if (initializerIndex == 0) return NO_INITIALIZERS;
158     System.arraycopy(initializers, 0, initializers = new InitializerElementInfo[initializerIndex], 0, initializerIndex);
159     return initializers;
160 }
161 /**
162  * @see ISourceType
163  */

164 public char[][] getInterfaceNames() {
165     if (this.handle.getElementName().length() == 0) { // if anonymous type
166
return null;
167     }
168     return this.superInterfaceNames;
169 }
170
171 /**
172  * @see ISourceType
173  */

174 public ISourceType[] getMemberTypes() {
175     SourceType[] memberTypeHandles = getMemberTypeHandles();
176     int length = memberTypeHandles.length;
177     ISourceType[] memberTypes = new ISourceType[length];
178     for (int i = 0; i < length; i++) {
179         try {
180             ISourceType type = (ISourceType) memberTypeHandles[i].getElementInfo();
181             memberTypes[i] = type;
182         } catch (JavaModelException e) {
183             // ignore
184
}
185     }
186     return memberTypes;
187 }
188 public SourceType[] getMemberTypeHandles() {
189     int length = this.children.length;
190     if (length == 0) return NO_TYPES;
191     SourceType[] memberTypes = new SourceType[length];
192     int typeIndex = 0;
193     for (int i = 0; i < length; i++) {
194         IJavaElement child = this.children[i];
195         if (child instanceof SourceType)
196             memberTypes[typeIndex++] = (SourceType)child;
197     }
198     if (typeIndex == 0) return NO_TYPES;
199     if (typeIndex < length)
200         System.arraycopy(memberTypes, 0, memberTypes = new SourceType[typeIndex], 0, typeIndex);
201     return memberTypes;
202 }
203 /**
204  * @see ISourceType
205  */

206 public ISourceMethod[] getMethods() {
207     SourceMethod[] methodHandles = getMethodHandles();
208     int length = methodHandles.length;
209     ISourceMethod[] methods = new ISourceMethod[length];
210     int methodIndex = 0;
211     for (int i = 0; i < length; i++) {
212         try {
213             ISourceMethod method = (ISourceMethod) methodHandles[i].getElementInfo();
214             methods[methodIndex++] = method;
215         } catch (JavaModelException e) {
216             // ignore
217
}
218     }
219     return methods;
220 }
221 public SourceMethod[] getMethodHandles() {
222     int length = this.children.length;
223     if (length == 0) return NO_METHODS;
224     SourceMethod[] methods = new SourceMethod[length];
225     int methodIndex = 0;
226     for (int i = 0; i < length; i++) {
227         IJavaElement child = this.children[i];
228         if (child instanceof SourceMethod)
229             methods[methodIndex++] = (SourceMethod) child;
230     }
231     if (methodIndex == 0) return NO_METHODS;
232     if (methodIndex < length)
233         System.arraycopy(methods, 0, methods = new SourceMethod[methodIndex], 0, methodIndex);
234     return methods;
235 }
236 /**
237  * @see org.eclipse.jdt.internal.compiler.env.ISourceType#getName()
238  */

239 public char[] getName() {
240     return this.handle.getElementName().toCharArray();
241 }
242 /**
243  * @see ISourceType
244  */

245 public char[] getSuperclassName() {
246     if (this.handle.getElementName().length() == 0) { // if anonymous type
247
char[][] interfaceNames = this.superInterfaceNames;
248         if (interfaceNames != null && interfaceNames.length > 0) {
249             return interfaceNames[0];
250         }
251     }
252     return this.superclassName;
253 }
254 public char[][][] getTypeParameterBounds() {
255     int length = this.typeParameters.length;
256     char[][][] typeParameterBounds = new char[length][][];
257     for (int i = 0; i < length; i++) {
258         try {
259             TypeParameterElementInfo info = (TypeParameterElementInfo) ((JavaElement)this.typeParameters[i]).getElementInfo();
260             typeParameterBounds[i] = info.bounds;
261         } catch (JavaModelException e) {
262             // type parameter does not exist: ignore
263
}
264     }
265     return typeParameterBounds;
266 }
267 public char[][] getTypeParameterNames() {
268     int length = this.typeParameters.length;
269     if (length == 0) return CharOperation.NO_CHAR_CHAR;
270     char[][] typeParameterNames = new char[length][];
271     for (int i = 0; i < length; i++) {
272         typeParameterNames[i] = this.typeParameters[i].getElementName().toCharArray();
273     }
274     return typeParameterNames;
275 }
276 /**
277  * @see ISourceType
278  */

279 public boolean isBinaryType() {
280     return false;
281 }
282 /*
283  * Returns whether the source type is an anonymous type of a member type.
284  */

285 public boolean isAnonymousMember() {
286     return false;
287 }
288 /**
289  * Sets the handle for this type info
290  */

291 protected void setHandle(IType handle) {
292     this.handle = handle;
293 }
294 /**
295  * Sets the (unqualified) name of this type's superclass
296  */

297 protected void setSuperclassName(char[] superclassName) {
298     this.superclassName = superclassName;
299 }
300 /**
301  * Sets the (unqualified) names of the interfaces this type implements or extends
302  */

303 protected void setSuperInterfaceNames(char[][] superInterfaceNames) {
304     this.superInterfaceNames = superInterfaceNames;
305 }
306 public String JavaDoc toString() {
307     return "Info for " + this.handle.toString(); //$NON-NLS-1$
308
}
309 }
310
Popular Tags