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