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