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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core;
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;
23 * Element info for an IType element that originated from source.
25 public class SourceTypeElementInfo extends MemberElementInfo implements ISourceType {
26 protected static final ISourceField[] NO_FIELDS = new ISourceField[0];
27 protected static final ISourceMethod[] NO_METHODS = new ISourceMethod[0];
28 protected static final ISourceType[] NO_TYPES = new ISourceType[0];
30 * The name of the superclass for this type. This name
31 * is fully qualified for binary types and is NOT
32 * fully qualified for source types.
34 protected char[] fSuperclassName;
37 * The names of the interfaces this type implements or
38 * extends. These names are fully qualified in the case
39 * of a binary type, and are NOT fully qualified in the
40 * case of a source type
42 protected char[][] fSuperInterfaceNames;
45 * The enclosing type name for this type.
47 * @see getEnclosingTypeName
49 protected char[] fEnclosingTypeName = null;
52 * The name of the source file this type is declared in.
54 protected char[] fSourceFileName= null;
57 * The name of the package this type is contained in.
59 protected char[] fPackageName= null;
62 * The qualified name of this type.
64 protected char[] fQualifiedName= null;
73 * The imports in this type's compilation unit
75 protected char[][] fImports= null;
78 * Backpointer to my type handle - useful for translation
79 * from info to handle.
81 protected IType fHandle= null;
92 * Adds the given import to this type's collection of imports
94 protected void addImport(char[] i) {
95 if (fImports == null) {
96 fImports = new char[][] {i};
98 char[][] copy = new char[fImports.length + 1][];
99 System.arraycopy(fImports, 0, copy, 0, fImports.length);
100 copy[fImports.length] = i;
107 * Returns the ISourceType that is the enclosing type for this
108 * type, or <code>null</code> if this type is a top level type.
110 public ISourceType getEnclosingType() {
111 IJavaElement parent= fHandle.getParent();
112 if (parent != null && parent.getElementType() == IJavaElement.TYPE) {
114 return (ISourceType)((JavaElement)parent).getElementInfo();
115 } catch (JavaModelException e) {
125 public char[] getEnclosingTypeName() {
126 return fEnclosingTypeName;
131 public ISourceField[] getFields() {
132 int length = fChildren.length;
133 if (length == 0) return NO_FIELDS;
134 ISourceField[] fields = new ISourceField[length];
136 for (int i = 0; i < length; i++) {
137 IJavaElement child = fChildren[i];
138 if (child instanceof SourceField) {
140 ISourceField field = (ISourceField)((SourceField)child).getElementInfo();
141 fields[fieldIndex++] = field;
142 } catch (JavaModelException e) {
146 if (fieldIndex == 0) return NO_FIELDS;
147 System.arraycopy(fields, 0, fields = new ISourceField[fieldIndex], 0, fieldIndex);
153 public char[] getFileName() {
154 return fSourceFileName;
157 * Returns the handle for this type info
159 public IType getHandle() {
165 public char[][] getImports() {
171 public char[][] getInterfaceNames() {
172 return fSuperInterfaceNames;
177 public ISourceType[] getMemberTypes() {
178 int length = fChildren.length;
179 if (length == 0) return NO_TYPES;
180 ISourceType[] memberTypes = new ISourceType[length];
182 for (int i = 0; i < length; i++) {
183 IJavaElement child = fChildren[i];
184 if (child instanceof SourceType) {
186 ISourceType type = (ISourceType)((SourceType)child).getElementInfo();
187 memberTypes[typeIndex++] = type;
188 } catch (JavaModelException e) {
192 if (typeIndex == 0) return NO_TYPES;
193 System.arraycopy(memberTypes, 0, memberTypes = new ISourceType[typeIndex], 0, typeIndex);
199 public ISourceMethod[] getMethods() {
200 int length = fChildren.length;
201 if (length == 0) return NO_METHODS;
202 ISourceMethod[] methods = new ISourceMethod[length];
204 for (int i = 0; i < length; i++) {
205 IJavaElement child = fChildren[i];
206 if (child instanceof SourceMethod) {
208 ISourceMethod method = (ISourceMethod)((SourceMethod)child).getElementInfo();
209 methods[methodIndex++] = method;
210 } catch (JavaModelException e) {
214 if (methodIndex == 0) return NO_METHODS;
215 System.arraycopy(methods, 0, methods = new ISourceMethod[methodIndex], 0, methodIndex);
221 public char[] getPackageName() {
227 public char[] getQualifiedName() {
228 return fQualifiedName;
233 public char[] getSuperclassName() {
234 return fSuperclassName;
239 public boolean isBinaryType() {
245 public boolean isClass() {
246 return (this.flags & IConstants.AccInterface) == 0;
251 public boolean isInterface() {
252 return (this.flags & IConstants.AccInterface) != 0;
255 * Sets the (unqualified) name of the type that encloses this type.
257 protected void setEnclosingTypeName(char[] enclosingTypeName) {
258 fEnclosingTypeName = enclosingTypeName;
261 * Sets the handle for this type info
263 protected void setHandle(IType handle) {
267 * Sets the name of the package this type is declared in.
269 protected void setPackageName(char[] name) {
273 * Sets this type's qualified name.
275 protected void setQualifiedName(char[] name) {
276 fQualifiedName= name;
279 * Sets the name of the source file this type is declared in.
281 protected void setSourceFileName(char[] name) {
282 fSourceFileName= name;
285 * Sets the (unqualified) name of this type's superclass
287 protected void setSuperclassName(char[] superclassName) {
288 fSuperclassName = superclassName;
291 * Sets the (unqualified) names of the interfaces this type implements or extends
293 protected void setSuperInterfaceNames(char[][] superInterfaceNames) {
294 fSuperInterfaceNames = superInterfaceNames;
296 public String toString() {
297 return "Info for " + fHandle.toString(); //$NON-NLS-1$