477f8d42a72245ea33af53afebd1c989c20fc812
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / core / SourceType.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 java.util.ArrayList;
14
15 import net.sourceforge.phpdt.core.IField;
16 import net.sourceforge.phpdt.core.IJavaElement;
17 import net.sourceforge.phpdt.core.IMember;
18 import net.sourceforge.phpdt.core.IMethod;
19 import net.sourceforge.phpdt.core.IPackageFragment;
20 import net.sourceforge.phpdt.core.IType;
21 import net.sourceforge.phpdt.core.JavaModelException;
22 import net.sourceforge.phpdt.core.jdom.IDOMNode;
23 import net.sourceforge.phpdt.internal.corext.Assert;
24
25
26 /**
27  * Handle for a source type. Info object is a SourceTypeElementInfo.
28  *
29  * Note: Parent is either an IClassFile, an ICompilationUnit or an IType.
30  *
31  * @see IType
32  */
33
34 public class SourceType extends Member implements IType {
35         /**
36          * An empty list of Strings
37          */
38         protected static final String[] fgEmptyList= new String[] {};
39 protected SourceType(IJavaElement parent, String name) {
40         super(TYPE, parent, name);
41         Assert.isTrue(name.indexOf('.') == -1);
42 }
43 /**
44  * @see IType
45  */
46 //public void codeComplete(char[] snippet,int insertion,int position,char[][] localVariableTypeNames,char[][] localVariableNames,int[] localVariableModifiers,boolean isStatic,ICompletionRequestor requestor) throws JavaModelException {
47 //      if (requestor == null) {
48 //              throw new IllegalArgumentException(Util.bind("codeAssist.nullRequestor")); //$NON-NLS-1$
49 //      }
50 //      
51 //      JavaProject project = (JavaProject) getJavaProject();
52 //      SearchableEnvironment environment = (SearchableEnvironment) project.getSearchableNameEnvironment();
53 //      NameLookup nameLookup = project.getNameLookup();
54 //      CompletionEngine engine = new CompletionEngine(environment, new CompletionRequestorWrapper(requestor,nameLookup), project.getOptions(true), project);
55 //      
56 //      String source = getCompilationUnit().getSource();
57 //      if (source != null && insertion > -1 && insertion < source.length()) {
58 //              String encoding = project.getOption(JavaCore.CORE_ENCODING, true);
59 //              
60 //              char[] prefix = CharOperation.concat(source.substring(0, insertion).toCharArray(), new char[]{'{'});
61 //              char[] suffix = CharOperation.concat(new char[]{'}'}, source.substring(insertion).toCharArray());
62 //              char[] fakeSource = CharOperation.concat(prefix, snippet, suffix);
63 //              
64 //              BasicCompilationUnit cu = 
65 //                      new BasicCompilationUnit(
66 //                              fakeSource, 
67 //                              null,
68 //                              getElementName(),
69 //                              encoding); 
70 //
71 //              engine.complete(cu, prefix.length + position, prefix.length);
72 //      } else {
73 //              engine.complete(this, snippet, position, localVariableTypeNames, localVariableNames, localVariableModifiers, isStatic);
74 //      }
75 //}
76 /**
77  * @see IType
78  */
79 //public IField createField(String contents, IJavaElement sibling, boolean force, IProgressMonitor monitor) throws JavaModelException {
80 //      CreateFieldOperation op = new CreateFieldOperation(this, contents, force);
81 //      if (sibling != null) {
82 //              op.createBefore(sibling);
83 //      }
84 //      runOperation(op, monitor);
85 //      return (IField) op.getResultElements()[0];
86 //}
87 /**
88  * @see IType
89  */
90 //public IInitializer createInitializer(String contents, IJavaElement sibling, IProgressMonitor monitor) throws JavaModelException {
91 //      CreateInitializerOperation op = new CreateInitializerOperation(this, contents);
92 //      if (sibling != null) {
93 //              op.createBefore(sibling);
94 //      }
95 //      runOperation(op, monitor);
96 //      return (IInitializer) op.getResultElements()[0];
97 //}
98 /**
99  * @see IType
100  */
101 //public IMethod createMethod(String contents, IJavaElement sibling, boolean force, IProgressMonitor monitor) throws JavaModelException {
102 //      CreateMethodOperation op = new CreateMethodOperation(this, contents, force);
103 //      if (sibling != null) {
104 //              op.createBefore(sibling);
105 //      }
106 //      runOperation(op, monitor);
107 //      return (IMethod) op.getResultElements()[0];
108 //}
109 /**
110  * @see IType
111  */
112 //public IType createType(String contents, IJavaElement sibling, boolean force, IProgressMonitor monitor) throws JavaModelException {
113 //      CreateTypeOperation op = new CreateTypeOperation(this, contents, force);
114 //      if (sibling != null) {
115 //              op.createBefore(sibling);
116 //      }
117 //      runOperation(op, monitor);
118 //      return (IType) op.getResultElements()[0];
119 //}
120 /**
121  * @see JavaElement#equalsDOMNode
122  */
123 protected boolean equalsDOMNode(IDOMNode node) throws JavaModelException {
124         return (node.getNodeType() == IDOMNode.TYPE) && super.equalsDOMNode(node);
125 }
126 /*
127  * @see IType
128  */
129 public IMethod[] findMethods(IMethod method) {
130         try {
131                 return this.findMethods(method, this.getMethods());
132         } catch (JavaModelException e) {
133                 // if type doesn't exist, no matching method can exist
134                 return null;
135         }
136 }
137 /**
138  * @see IMember
139  */
140 public IType getDeclaringType() {
141         IJavaElement parent = getParent();
142         while (parent != null) {
143                 if (parent.getElementType() == IJavaElement.TYPE) {
144                         return (IType) parent;
145                 } else
146                         if (parent instanceof IMember) {
147                                 parent = parent.getParent();
148                         } else {
149                                 return null;
150                         }
151         }
152         return null;
153 }
154 /**
155  * @see IType#getField
156  */
157 public IField getField(String name) {
158         return new SourceField(this, name);
159 }
160 /**
161  * @see IType
162  */
163 public IField[] getFields() throws JavaModelException {
164         ArrayList list = getChildrenOfType(FIELD);
165         IField[] array= new IField[list.size()];
166         list.toArray(array);
167         return array;
168 }
169 /**
170  * @see IType#getFullyQualifiedName
171  */
172 public String getFullyQualifiedName() {
173         return this.getFullyQualifiedName('$');
174 }
175 /**
176  * @see IType#getFullyQualifiedName(char)
177  */
178 public String getFullyQualifiedName(char enclosingTypeSeparator) {
179         String packageName = getPackageFragment().getElementName();
180         if (packageName.equals(IPackageFragment.DEFAULT_PACKAGE_NAME)) {
181                 return getTypeQualifiedName(enclosingTypeSeparator);
182         }
183         return packageName + '.' + getTypeQualifiedName(enclosingTypeSeparator);
184 }
185
186 /**
187  * @see IType
188  */
189 //public IInitializer getInitializer(int occurrenceCount) {
190 //      return new Initializer(this, occurrenceCount);
191 //}
192 /**
193  * @see IType
194  */
195 //public IInitializer[] getInitializers() throws JavaModelException {
196 //      ArrayList list = getChildrenOfType(INITIALIZER);
197 //      IInitializer[] array= new IInitializer[list.size()];
198 //      list.toArray(array);
199 //      return array;
200 //}
201 /**
202  * @see IType#getMethod
203  */
204 public IMethod getMethod(String name, String[] parameterTypeSignatures) {
205         return new SourceMethod(this, name, parameterTypeSignatures);
206 }
207 /**
208  * @see IType
209  */
210 public IMethod[] getMethods() throws JavaModelException {
211         ArrayList list = getChildrenOfType(METHOD);
212         IMethod[] array= new IMethod[list.size()];
213         list.toArray(array);
214         return array;
215 }
216 /**
217  * @see IType
218  */
219 public IPackageFragment getPackageFragment() {
220         IJavaElement parent = fParent;
221         while (parent != null) {
222                 if (parent.getElementType() == IJavaElement.PACKAGE_FRAGMENT) {
223                         return (IPackageFragment) parent;
224                 }
225                 else {
226                         parent = parent.getParent();
227                 }
228         }
229         Assert.isTrue(false);  // should not happen
230         return null;
231 }
232 /**
233  * @see IType
234  */
235 public String getSuperclassName() throws JavaModelException {
236         SourceTypeElementInfo info = (SourceTypeElementInfo) getElementInfo();
237         char[] superclassName= info.getSuperclassName();
238         if (superclassName == null) {
239                 return null;
240         }
241         return new String(superclassName);
242 }
243 /**
244  * @see IType
245  */
246 public String[] getSuperInterfaceNames() throws JavaModelException {
247         SourceTypeElementInfo info = (SourceTypeElementInfo) getElementInfo();
248         char[][] names= info.getInterfaceNames();
249         if (names == null) {
250                 return fgEmptyList;
251         }
252         String[] strings= new String[names.length];
253         for (int i= 0; i < names.length; i++) {
254                 strings[i]= new String(names[i]);
255         }
256         return strings;
257 }
258 /**
259  * @see IType
260  */
261 public IType getType(String name) {
262         return new SourceType(this, name);
263 }
264 /**
265  * @see IType#getTypeQualifiedName
266  */
267 public String getTypeQualifiedName() {
268         return this.getTypeQualifiedName('$');
269 }
270 /**
271  * @see IType#getTypeQualifiedName(char)
272  */
273 public String getTypeQualifiedName(char enclosingTypeSeparator) {
274         if (fParent.getElementType() == IJavaElement.COMPILATION_UNIT) {
275                 return fName;
276         } else {
277                 return ((IType) fParent).getTypeQualifiedName(enclosingTypeSeparator) + enclosingTypeSeparator + fName;
278         }
279 }
280
281 /**
282  * @see IType
283  */
284 public IType[] getTypes() throws JavaModelException {
285         ArrayList list= getChildrenOfType(TYPE);
286         IType[] array= new IType[list.size()];
287         list.toArray(array);
288         return array;
289 }
290 /**
291  * @see IParent 
292  */
293 public boolean hasChildren() throws JavaModelException {
294         return getChildren().length > 0;
295 }
296 /**
297  * @see IType#isAnonymous()
298  */
299 public boolean isAnonymous() throws JavaModelException {
300         return false; // cannot create source handle onto anonymous types
301 }
302 /**
303  * @see IType
304  */
305 public boolean isClass() throws JavaModelException {
306         return !isInterface();
307 }
308 /**
309  * @see IType
310  */
311 public boolean isInterface() throws JavaModelException {
312         SourceTypeElementInfo info = (SourceTypeElementInfo) getElementInfo();
313         return info.isInterface();
314 }
315 /**
316  * @see IType#isLocal()
317  */
318 public boolean isLocal() throws JavaModelException {
319         return false; // cannot create source handle onto local types
320 }
321 /**
322  * @see IType#isMember()
323  */
324 public boolean isMember() throws JavaModelException {
325         return getDeclaringType() != null;
326 }
327 /**
328  * @see IType
329  */
330 //public ITypeHierarchy loadTypeHierachy(InputStream input, IProgressMonitor monitor) throws JavaModelException {
331 //      return TypeHierarchy.load(this, input);
332 //}
333 /**
334  * @see IType
335  */
336 //public ITypeHierarchy newSupertypeHierarchy(IProgressMonitor monitor) throws JavaModelException {
337 //      return this.newSupertypeHierarchy(null, monitor);
338 //}
339 /**
340  * @see IType#newSupertypeHierarchy(IWorkingCopy[], IProgressMonitor)
341  */
342 //public ITypeHierarchy newSupertypeHierarchy(
343 //      IWorkingCopy[] workingCopies,
344 //      IProgressMonitor monitor)
345 //      throws JavaModelException {
346 //
347 //      CreateTypeHierarchyOperation op= new CreateTypeHierarchyOperation(this, workingCopies, SearchEngine.createWorkspaceScope(), false);
348 //      runOperation(op, monitor);
349 //      return op.getResult();
350 //}
351
352 /**
353  * @see IType
354  */
355 //public ITypeHierarchy newTypeHierarchy(IProgressMonitor monitor) throws JavaModelException {
356 //      return this.newTypeHierarchy((IWorkingCopy[])null, monitor);
357 //}
358 /**
359  * @see IType#newTypeHierarchy(IWorkingCopy[], IProgressMonitor)
360  */
361 //public ITypeHierarchy newTypeHierarchy(
362 //      IWorkingCopy[] workingCopies,
363 //      IProgressMonitor monitor)
364 //      throws JavaModelException {
365 //              
366 //      CreateTypeHierarchyOperation op= new CreateTypeHierarchyOperation(this, workingCopies, SearchEngine.createWorkspaceScope(), true);
367 //      runOperation(op, monitor);
368 //      return op.getResult();
369 //}
370
371 /**
372  * @see IType
373  */
374 //public ITypeHierarchy newTypeHierarchy(IJavaProject project, IProgressMonitor monitor) throws JavaModelException {
375 //      if (project == null) {
376 //              throw new IllegalArgumentException(Util.bind("hierarchy.nullProject")); //$NON-NLS-1$
377 //      }
378 //      
379 //      CreateTypeHierarchyOperation op= new CreateTypeHierarchyOperation(
380 //              this, 
381 //              (IWorkingCopy[])null, // no working copies
382 //              project,
383 //              true);
384 //      runOperation(op, monitor);
385 //      return op.getResult();
386 //}
387 /**
388  * See ISourceType.resolveType(...)
389  */
390
391 // public String[][] resolveType(String typeName) throws JavaModelException {
392 //      ISourceType info = (ISourceType) this.getElementInfo();
393 //      ISearchableNameEnvironment environment = ((JavaProject)getJavaProject()).getSearchableNameEnvironment();
394 //
395 //      class TypeResolveRequestor implements ISelectionRequestor {
396 //              String[][] answers = null;
397 //              void acceptType(String[] answer){
398 //                      if (answers == null) {
399 //                              answers = new String[][]{ answer };
400 //                      } else {
401 //                              // grow
402 //                              int length = answers.length;
403 //                              System.arraycopy(answers, 0, answers = new String[length+1][], 0, length);
404 //                              answers[length] = answer;
405 //                      }
406 //              }
407 //              public void acceptClass(char[] packageName, char[] className, boolean needQualification) {
408 //                      acceptType(new String[]  { new String(packageName), new String(className) });
409 //              }
410 //              
411 //              public void acceptInterface(char[] packageName, char[] interfaceName, boolean needQualification) {
412 //                      acceptType(new String[]  { new String(packageName), new String(interfaceName) });
413 //              }
414 //
415 //              public void acceptError(IProblem error) {}
416 //              public void acceptField(char[] declaringTypePackageName, char[] declaringTypeName, char[] name) {}
417 //              public void acceptMethod(char[] declaringTypePackageName, char[] declaringTypeName, char[] selector, char[][] parameterPackageNames, char[][] parameterTypeNames, boolean isConstructor) {}
418 //              public void acceptPackage(char[] packageName){}
419 //
420 //      }
421 //      TypeResolveRequestor requestor = new TypeResolveRequestor();
422 //      SelectionEngine engine = 
423 //              new SelectionEngine(environment, requestor, this.getJavaProject().getOptions(true));
424 //              
425 //      IType[] topLevelTypes = this.getCompilationUnit().getTypes();
426 //      int length = topLevelTypes.length;
427 //      ISourceType[] topLevelInfos = new ISourceType[length];
428 //      for (int i = 0; i < length; i++) {
429 //              topLevelInfos[i] = (ISourceType)((SourceType)topLevelTypes[i]).getElementInfo();
430 //      }
431 //              
432 //      engine.selectType(info, typeName.toCharArray(), topLevelInfos, false);
433 //      return requestor.answers;
434 //}
435 /**
436  * @private Debugging purposes
437  */
438 protected void toStringInfo(int tab, StringBuffer buffer, Object info) {
439         buffer.append(this.tabString(tab));
440         if (info == null) {
441                 buffer.append(this.getElementName());
442                 buffer.append(" (not open)"); //$NON-NLS-1$
443         } else if (info == NO_INFO) {
444                 buffer.append(getElementName());
445         } else {
446                 try {
447                         if (this.isInterface()) {
448                                 buffer.append("interface "); //$NON-NLS-1$
449                         } else {
450                                 buffer.append("class "); //$NON-NLS-1$
451                         }
452                         buffer.append(this.getElementName());
453                 } catch (JavaModelException e) {
454                         buffer.append("<JavaModelException in toString of " + getElementName()); //$NON-NLS-1$
455                 }
456         }
457 }
458 }