5c412543baacfc02185eac23fe5d4cb13e48828f
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / core / SourceTypeElementInfo.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core;
12
13 import net.sourceforge.phpdt.core.IJavaElement;
14 import net.sourceforge.phpdt.core.IType;
15 import net.sourceforge.phpdt.core.JavaModelException;
16 import net.sourceforge.phpdt.internal.compiler.env.IConstants;
17 import net.sourceforge.phpdt.internal.compiler.env.ISourceField;
18 import net.sourceforge.phpdt.internal.compiler.env.ISourceMethod;
19 import net.sourceforge.phpdt.internal.compiler.env.ISourceType;
20
21 /**
22  * Element info for an IType element that originated from source.
23  */
24 public class SourceTypeElementInfo extends MemberElementInfo implements
25                 ISourceType {
26         protected static final ISourceField[] NO_FIELDS = new ISourceField[0];
27
28         protected static final ISourceMethod[] NO_METHODS = new ISourceMethod[0];
29
30         protected static final ISourceType[] NO_TYPES = new ISourceType[0];
31
32         /**
33          * The name of the superclass for this type. This name is fully qualified
34          * for binary types and is NOT fully qualified for source types.
35          */
36         protected char[] fSuperclassName;
37
38         /**
39          * The names of the interfaces this type implements or extends. These names
40          * are fully qualified in the case of a binary type, and are NOT fully
41          * qualified in the case of a source type
42          */
43         protected char[][] fSuperInterfaceNames;
44
45         /**
46          * The enclosing type name for this type.
47          * 
48          * @see getEnclosingTypeName
49          */
50         protected char[] fEnclosingTypeName = null;
51
52         /**
53          * The name of the source file this type is declared in.
54          */
55         protected char[] fSourceFileName = null;
56
57         /**
58          * The name of the package this type is contained in.
59          */
60         protected char[] fPackageName = null;
61
62         /**
63          * The qualified name of this type.
64          */
65         protected char[] fQualifiedName = null;
66
67         /**
68          * The imports in this type's compilation unit
69          */
70         protected char[][] fImports = null;
71
72         /**
73          * Backpointer to my type handle - useful for translation from info to
74          * handle.
75          */
76         protected IType fHandle = null;
77
78         /**
79          * Adds the given import to this type's collection of imports
80          */
81         protected void addImport(char[] i) {
82                 if (fImports == null) {
83                         fImports = new char[][] { i };
84                 } else {
85                         char[][] copy = new char[fImports.length + 1][];
86                         System.arraycopy(fImports, 0, copy, 0, fImports.length);
87                         copy[fImports.length] = i;
88                         fImports = copy;
89                 }
90         }
91
92         /**
93          * Returns the ISourceType that is the enclosing type for this type, or
94          * <code>null</code> if this type is a top level type.
95          */
96         public ISourceType getEnclosingType() {
97                 IJavaElement parent = fHandle.getParent();
98                 if (parent != null && parent.getElementType() == IJavaElement.TYPE) {
99                         try {
100                                 return (ISourceType) ((JavaElement) parent).getElementInfo();
101                         } catch (JavaModelException e) {
102                                 return null;
103                         }
104                 } else {
105                         return null;
106                 }
107         }
108
109         /**
110          * @see ISourceType
111          */
112         public char[] getEnclosingTypeName() {
113                 return fEnclosingTypeName;
114         }
115
116         /**
117          * @see ISourceType
118          */
119         public ISourceField[] getFields() {
120                 int length = fChildren.length;
121                 if (length == 0)
122                         return NO_FIELDS;
123                 ISourceField[] fields = new ISourceField[length];
124                 int fieldIndex = 0;
125                 for (int i = 0; i < length; i++) {
126                         IJavaElement child = fChildren[i];
127                         if (child instanceof SourceField) {
128                                 try {
129                                         ISourceField field = (ISourceField) ((SourceField) child)
130                                                         .getElementInfo();
131                                         fields[fieldIndex++] = field;
132                                 } catch (JavaModelException e) {
133                                 }
134                         }
135                 }
136                 if (fieldIndex == 0)
137                         return NO_FIELDS;
138                 System.arraycopy(fields, 0, fields = new ISourceField[fieldIndex], 0,
139                                 fieldIndex);
140                 return fields;
141         }
142
143         /**
144          * @see ISourceType
145          */
146         public char[] getFileName() {
147                 return fSourceFileName;
148         }
149
150         /**
151          * Returns the handle for this type info
152          */
153         public IType getHandle() {
154                 return fHandle;
155         }
156
157         /**
158          * @see ISourceType
159          */
160         public char[][] getImports() {
161                 return fImports;
162         }
163
164         /**
165          * @see ISourceType
166          */
167         public char[][] getInterfaceNames() {
168                 return fSuperInterfaceNames;
169         }
170
171         /**
172          * @see ISourceType
173          */
174         public ISourceType[] getMemberTypes() {
175                 int length = fChildren.length;
176                 if (length == 0)
177                         return NO_TYPES;
178                 ISourceType[] memberTypes = new ISourceType[length];
179                 int typeIndex = 0;
180                 for (int i = 0; i < length; i++) {
181                         IJavaElement child = fChildren[i];
182                         if (child instanceof SourceType) {
183                                 try {
184                                         ISourceType type = (ISourceType) ((SourceType) child)
185                                                         .getElementInfo();
186                                         memberTypes[typeIndex++] = type;
187                                 } catch (JavaModelException e) {
188                                 }
189                         }
190                 }
191                 if (typeIndex == 0)
192                         return NO_TYPES;
193                 System.arraycopy(memberTypes, 0,
194                                 memberTypes = new ISourceType[typeIndex], 0, typeIndex);
195                 return memberTypes;
196         }
197
198         /**
199          * @see ISourceType
200          */
201         public ISourceMethod[] getMethods() {
202                 int length = fChildren.length;
203                 if (length == 0)
204                         return NO_METHODS;
205                 ISourceMethod[] methods = new ISourceMethod[length];
206                 int methodIndex = 0;
207                 for (int i = 0; i < length; i++) {
208                         IJavaElement child = fChildren[i];
209                         if (child instanceof SourceMethod) {
210                                 try {
211                                         ISourceMethod method = (ISourceMethod) ((SourceMethod) child)
212                                                         .getElementInfo();
213                                         methods[methodIndex++] = method;
214                                 } catch (JavaModelException e) {
215                                 }
216                         }
217                 }
218                 if (methodIndex == 0)
219                         return NO_METHODS;
220                 System.arraycopy(methods, 0, methods = new ISourceMethod[methodIndex],
221                                 0, methodIndex);
222                 return methods;
223         }
224
225         /**
226          * @see ISourceType
227          */
228         public char[] getPackageName() {
229                 return fPackageName;
230         }
231
232         /**
233          * @see ISourceType
234          */
235         public char[] getQualifiedName() {
236                 return fQualifiedName;
237         }
238
239         /**
240          * @see ISourceType
241          */
242         public char[] getSuperclassName() {
243                 return fSuperclassName;
244         }
245
246         /**
247          * @see ISourceType
248          */
249         public boolean isBinaryType() {
250                 return false;
251         }
252
253         /**
254          * @see ISourceType
255          */
256         public boolean isClass() {
257                 return (this.flags & IConstants.AccInterface) == 0;
258         }
259
260         /**
261          * @see ISourceType
262          */
263         public boolean isInterface() {
264                 return (this.flags & IConstants.AccInterface) != 0;
265         }
266
267         /**
268          * Sets the (unqualified) name of the type that encloses this type.
269          */
270         protected void setEnclosingTypeName(char[] enclosingTypeName) {
271                 fEnclosingTypeName = enclosingTypeName;
272         }
273
274         /**
275          * Sets the handle for this type info
276          */
277         protected void setHandle(IType handle) {
278                 fHandle = handle;
279         }
280
281         /**
282          * Sets the name of the package this type is declared in.
283          */
284         protected void setPackageName(char[] name) {
285                 fPackageName = name;
286         }
287
288         /**
289          * Sets this type's qualified name.
290          */
291         protected void setQualifiedName(char[] name) {
292                 fQualifiedName = name;
293         }
294
295         /**
296          * Sets the name of the source file this type is declared in.
297          */
298         protected void setSourceFileName(char[] name) {
299                 fSourceFileName = name;
300         }
301
302         /**
303          * Sets the (unqualified) name of this type's superclass
304          */
305         protected void setSuperclassName(char[] superclassName) {
306                 fSuperclassName = superclassName;
307         }
308
309         /**
310          * Sets the (unqualified) names of the interfaces this type implements or
311          * extends
312          */
313         protected void setSuperInterfaceNames(char[][] superInterfaceNames) {
314                 fSuperInterfaceNames = superInterfaceNames;
315         }
316
317         public String toString() {
318                 return "Info for " + fHandle.toString(); //$NON-NLS-1$
319         }
320 }