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 java.util.ArrayList;
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;
27 * Handle for a source type. Info object is a SourceTypeElementInfo.
29 * Note: Parent is either an IClassFile, an ICompilationUnit or an IType.
34 public class SourceType extends Member implements IType {
36 * An empty list of Strings
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);
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$
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);
56 // String source = getCompilationUnit().getSource();
57 // if (source != null && insertion > -1 && insertion < source.length()) {
58 // String encoding = project.getOption(JavaCore.CORE_ENCODING, true);
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);
64 // BasicCompilationUnit cu =
65 // new BasicCompilationUnit(
71 // engine.complete(cu, prefix.length + position, prefix.length);
73 // engine.complete(this, snippet, position, localVariableTypeNames, localVariableNames, localVariableModifiers, isStatic);
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);
84 // runOperation(op, monitor);
85 // return (IField) op.getResultElements()[0];
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);
95 // runOperation(op, monitor);
96 // return (IInitializer) op.getResultElements()[0];
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);
106 // runOperation(op, monitor);
107 // return (IMethod) op.getResultElements()[0];
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);
117 // runOperation(op, monitor);
118 // return (IType) op.getResultElements()[0];
121 * @see JavaElement#equalsDOMNode
123 protected boolean equalsDOMNode(IDOMNode node) throws JavaModelException {
124 return (node.getNodeType() == IDOMNode.TYPE) && super.equalsDOMNode(node);
129 public IMethod[] findMethods(IMethod method) {
131 return this.findMethods(method, this.getMethods());
132 } catch (JavaModelException e) {
133 // if type doesn't exist, no matching method can exist
140 public IType getDeclaringType() {
141 IJavaElement parent = getParent();
142 while (parent != null) {
143 if (parent.getElementType() == IJavaElement.TYPE) {
144 return (IType) parent;
146 if (parent instanceof IMember) {
147 parent = parent.getParent();
155 * @see IType#getField
157 public IField getField(String name) {
158 return new SourceField(this, name);
163 public IField[] getFields() throws JavaModelException {
164 ArrayList list = getChildrenOfType(FIELD);
165 IField[] array= new IField[list.size()];
170 * @see IType#getFullyQualifiedName
172 public String getFullyQualifiedName() {
173 return this.getFullyQualifiedName('$');
176 * @see IType#getFullyQualifiedName(char)
178 public String getFullyQualifiedName(char enclosingTypeSeparator) {
179 String packageName = getPackageFragment().getElementName();
180 if (packageName.equals(IPackageFragment.DEFAULT_PACKAGE_NAME)) {
181 return getTypeQualifiedName(enclosingTypeSeparator);
183 return packageName + '.' + getTypeQualifiedName(enclosingTypeSeparator);
189 //public IInitializer getInitializer(int occurrenceCount) {
190 // return new Initializer(this, occurrenceCount);
195 //public IInitializer[] getInitializers() throws JavaModelException {
196 // ArrayList list = getChildrenOfType(INITIALIZER);
197 // IInitializer[] array= new IInitializer[list.size()];
198 // list.toArray(array);
202 * @see IType#getMethod
204 public IMethod getMethod(String name, String[] parameterTypeSignatures) {
205 return new SourceMethod(this, name, parameterTypeSignatures);
210 public IMethod[] getMethods() throws JavaModelException {
211 ArrayList list = getChildrenOfType(METHOD);
212 IMethod[] array= new IMethod[list.size()];
219 public IPackageFragment getPackageFragment() {
220 IJavaElement parent = fParent;
221 while (parent != null) {
222 if (parent.getElementType() == IJavaElement.PACKAGE_FRAGMENT) {
223 return (IPackageFragment) parent;
226 parent = parent.getParent();
229 Assert.isTrue(false); // should not happen
235 public String getSuperclassName() throws JavaModelException {
236 SourceTypeElementInfo info = (SourceTypeElementInfo) getElementInfo();
237 char[] superclassName= info.getSuperclassName();
238 if (superclassName == null) {
241 return new String(superclassName);
246 public String[] getSuperInterfaceNames() throws JavaModelException {
247 SourceTypeElementInfo info = (SourceTypeElementInfo) getElementInfo();
248 char[][] names= info.getInterfaceNames();
252 String[] strings= new String[names.length];
253 for (int i= 0; i < names.length; i++) {
254 strings[i]= new String(names[i]);
261 public IType getType(String name) {
262 return new SourceType(this, name);
265 * @see IType#getTypeQualifiedName
267 public String getTypeQualifiedName() {
268 return this.getTypeQualifiedName('$');
271 * @see IType#getTypeQualifiedName(char)
273 public String getTypeQualifiedName(char enclosingTypeSeparator) {
274 if (fParent.getElementType() == IJavaElement.COMPILATION_UNIT) {
277 return ((IType) fParent).getTypeQualifiedName(enclosingTypeSeparator) + enclosingTypeSeparator + fName;
284 public IType[] getTypes() throws JavaModelException {
285 ArrayList list= getChildrenOfType(TYPE);
286 IType[] array= new IType[list.size()];
293 public boolean hasChildren() throws JavaModelException {
294 return getChildren().length > 0;
297 * @see IType#isAnonymous()
299 public boolean isAnonymous() throws JavaModelException {
300 return false; // cannot create source handle onto anonymous types
305 public boolean isClass() throws JavaModelException {
306 return !isInterface();
311 public boolean isInterface() throws JavaModelException {
312 SourceTypeElementInfo info = (SourceTypeElementInfo) getElementInfo();
313 return info.isInterface();
316 * @see IType#isLocal()
318 public boolean isLocal() throws JavaModelException {
319 return false; // cannot create source handle onto local types
322 * @see IType#isMember()
324 public boolean isMember() throws JavaModelException {
325 return getDeclaringType() != null;
330 //public ITypeHierarchy loadTypeHierachy(InputStream input, IProgressMonitor monitor) throws JavaModelException {
331 // return TypeHierarchy.load(this, input);
336 //public ITypeHierarchy newSupertypeHierarchy(IProgressMonitor monitor) throws JavaModelException {
337 // return this.newSupertypeHierarchy(null, monitor);
340 * @see IType#newSupertypeHierarchy(IWorkingCopy[], IProgressMonitor)
342 //public ITypeHierarchy newSupertypeHierarchy(
343 // IWorkingCopy[] workingCopies,
344 // IProgressMonitor monitor)
345 // throws JavaModelException {
347 // CreateTypeHierarchyOperation op= new CreateTypeHierarchyOperation(this, workingCopies, SearchEngine.createWorkspaceScope(), false);
348 // runOperation(op, monitor);
349 // return op.getResult();
355 //public ITypeHierarchy newTypeHierarchy(IProgressMonitor monitor) throws JavaModelException {
356 // return this.newTypeHierarchy((IWorkingCopy[])null, monitor);
359 * @see IType#newTypeHierarchy(IWorkingCopy[], IProgressMonitor)
361 //public ITypeHierarchy newTypeHierarchy(
362 // IWorkingCopy[] workingCopies,
363 // IProgressMonitor monitor)
364 // throws JavaModelException {
366 // CreateTypeHierarchyOperation op= new CreateTypeHierarchyOperation(this, workingCopies, SearchEngine.createWorkspaceScope(), true);
367 // runOperation(op, monitor);
368 // return op.getResult();
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$
379 // CreateTypeHierarchyOperation op= new CreateTypeHierarchyOperation(
381 // (IWorkingCopy[])null, // no working copies
384 // runOperation(op, monitor);
385 // return op.getResult();
388 * See ISourceType.resolveType(...)
391 // public String[][] resolveType(String typeName) throws JavaModelException {
392 // ISourceType info = (ISourceType) this.getElementInfo();
393 // ISearchableNameEnvironment environment = ((JavaProject)getJavaProject()).getSearchableNameEnvironment();
395 // class TypeResolveRequestor implements ISelectionRequestor {
396 // String[][] answers = null;
397 // void acceptType(String[] answer){
398 // if (answers == null) {
399 // answers = new String[][]{ answer };
402 // int length = answers.length;
403 // System.arraycopy(answers, 0, answers = new String[length+1][], 0, length);
404 // answers[length] = answer;
407 // public void acceptClass(char[] packageName, char[] className, boolean needQualification) {
408 // acceptType(new String[] { new String(packageName), new String(className) });
411 // public void acceptInterface(char[] packageName, char[] interfaceName, boolean needQualification) {
412 // acceptType(new String[] { new String(packageName), new String(interfaceName) });
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){}
421 // TypeResolveRequestor requestor = new TypeResolveRequestor();
422 // SelectionEngine engine =
423 // new SelectionEngine(environment, requestor, this.getJavaProject().getOptions(true));
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();
432 // engine.selectType(info, typeName.toCharArray(), topLevelInfos, false);
433 // return requestor.answers;
436 * @private Debugging purposes
438 protected void toStringInfo(int tab, StringBuffer buffer, Object info) {
439 buffer.append(this.tabString(tab));
441 buffer.append(this.getElementName());
442 buffer.append(" (not open)"); //$NON-NLS-1$
443 } else if (info == NO_INFO) {
444 buffer.append(getElementName());
447 if (this.isInterface()) {
448 buffer.append("interface "); //$NON-NLS-1$
450 buffer.append("class "); //$NON-NLS-1$
452 buffer.append(this.getElementName());
453 } catch (JavaModelException e) {
454 buffer.append("<JavaModelException in toString of " + getElementName()); //$NON-NLS-1$