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