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.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;
28 * Handle for a source type. Info object is a SourceTypeElementInfo.
30 * Note: Parent is either an IClassFile, an ICompilationUnit or an IType.
35 public class SourceType extends Member implements IType {
37 * An empty list of Strings
39 protected static final String[] fgEmptyList = new String[] {};
41 protected SourceType(JavaElement parent, String name) {
43 Assert.isTrue(name.indexOf('.') == -1, Util.bind("sourcetype.invalidName", name)); //$NON-NLS-1$
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(Util.bind("codeAssist.nullRequestor")); //$NON-NLS-1$
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);
61 // String source = getCompilationUnit().getSource();
62 // if (source != null && insertion > -1 && insertion < source.length()) {
63 // String encoding = project.getOption(JavaCore.CORE_ENCODING, true);
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);
69 // BasicCompilationUnit cu =
70 // new BasicCompilationUnit(
76 // engine.complete(cu, prefix.length + position, prefix.length);
78 // engine.complete(this, snippet, position, localVariableTypeNames, localVariableNames, localVariableModifiers, isStatic);
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);
90 // runOperation(op, monitor);
91 // return (IField) op.getResultElements()[0];
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);
102 // runOperation(op, monitor);
103 // return (IInitializer) op.getResultElements()[0];
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);
114 // runOperation(op, monitor);
115 // return (IMethod) op.getResultElements()[0];
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);
126 // runOperation(op, monitor);
127 // return (IType) op.getResultElements()[0];
130 * @see JavaElement#equalsDOMNode
132 protected boolean equalsDOMNode(IDOMNode node) throws JavaModelException {
133 return (node.getNodeType() == IDOMNode.TYPE) && super.equalsDOMNode(node);
139 public IMethod[] findMethods(IMethod method) {
141 return this.findMethods(method, this.getMethods());
142 } catch (JavaModelException e) {
143 // if type doesn't exist, no matching method can exist
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();
168 public int getElementType() {
173 * @see IType#getField
175 public IField getField(String name) {
176 return new SourceField(this, name);
182 public IField[] getFields() throws JavaModelException {
183 ArrayList list = getChildrenOfType(FIELD);
184 IField[] array = new IField[list.size()];
190 * @see IType#getFullyQualifiedName
192 public String getFullyQualifiedName() {
193 return this.getFullyQualifiedName('$');
197 * @see IType#getFullyQualifiedName(char)
199 public String getFullyQualifiedName(char enclosingTypeSeparator) {
200 String packageName = getPackageFragment().getElementName();
201 if (packageName.equals(IPackageFragment.DEFAULT_PACKAGE_NAME)) {
202 return getTypeQualifiedName(enclosingTypeSeparator);
204 return packageName + '.' + getTypeQualifiedName(enclosingTypeSeparator);
210 //public IInitializer getInitializer(int occurrenceCount) {
211 // return new Initializer(this, occurrenceCount);
216 //public IInitializer[] getInitializers() throws JavaModelException {
217 // ArrayList list = getChildrenOfType(INITIALIZER);
218 // IInitializer[] array= new IInitializer[list.size()];
219 // list.toArray(array);
223 * @see IType#getMethod
225 public IMethod getMethod(String name, String[] parameterTypeSignatures) {
226 return new SourceMethod(this, name, parameterTypeSignatures);
232 public IMethod[] getMethods() throws JavaModelException {
233 ArrayList list = getChildrenOfType(METHOD);
234 IMethod[] array = new IMethod[list.size()];
242 public IPackageFragment getPackageFragment() {
243 IJavaElement parentElement = this.parent;
244 while (parentElement != null) {
245 if (parentElement.getElementType() == IJavaElement.PACKAGE_FRAGMENT) {
246 return (IPackageFragment) parentElement;
248 parentElement = parentElement.getParent();
251 Assert.isTrue(false); // should not happen
258 public String getSuperclassName() throws JavaModelException {
259 SourceTypeElementInfo info = (SourceTypeElementInfo) getElementInfo();
260 char[] superclassName = info.getSuperclassName();
261 if (superclassName == null) {
264 return new String(superclassName);
270 public String[] getSuperInterfaceNames() throws JavaModelException {
271 SourceTypeElementInfo info = (SourceTypeElementInfo) getElementInfo();
272 char[][] names = info.getInterfaceNames();
276 String[] strings = new String[names.length];
277 for (int i = 0; i < names.length; i++) {
278 strings[i] = new String(names[i]);
286 public IType getType(String name) {
287 return new SourceType(this, name);
291 * @see IType#getTypeQualifiedName
293 public String getTypeQualifiedName() {
294 return this.getTypeQualifiedName('$');
298 * @see IType#getTypeQualifiedName(char)
300 public String getTypeQualifiedName(char enclosingTypeSeparator) {
301 if (parent.getElementType() == IJavaElement.COMPILATION_UNIT) {
304 return ((IType) parent).getTypeQualifiedName(enclosingTypeSeparator) + enclosingTypeSeparator + name;
311 public IType[] getTypes() throws JavaModelException {
312 ArrayList list = getChildrenOfType(TYPE);
313 IType[] array = new IType[list.size()];
321 public boolean hasChildren() throws JavaModelException {
322 return getChildren().length > 0;
326 * @see IType#isAnonymous()
328 public boolean isAnonymous() throws JavaModelException {
329 return false; // cannot create source handle onto anonymous types
335 public boolean isClass() throws JavaModelException {
336 return !isInterface();
342 public boolean isInterface() throws JavaModelException {
343 Object obj = getElementInfo();
344 if (obj instanceof SourceTypeElementInfo) {
345 SourceTypeElementInfo info = (SourceTypeElementInfo) getElementInfo();
346 return info.isInterface();
352 * @see IType#isLocal()
354 public boolean isLocal() throws JavaModelException {
355 return false; // cannot create source handle onto local types
359 * @see IType#isMember()
361 public boolean isMember() throws JavaModelException {
362 return getDeclaringType() != null;
368 //public ITypeHierarchy loadTypeHierachy(InputStream input, IProgressMonitor monitor) throws JavaModelException {
369 // return TypeHierarchy.load(this, input);
374 //public ITypeHierarchy newSupertypeHierarchy(IProgressMonitor monitor) throws JavaModelException {
375 // return this.newSupertypeHierarchy(null, monitor);
378 * @see IType#newSupertypeHierarchy(IWorkingCopy[], IProgressMonitor)
380 //public ITypeHierarchy newSupertypeHierarchy(
381 // IWorkingCopy[] workingCopies,
382 // IProgressMonitor monitor)
383 // throws JavaModelException {
385 // CreateTypeHierarchyOperation op= new CreateTypeHierarchyOperation(this, workingCopies, SearchEngine.createWorkspaceScope(),
387 // runOperation(op, monitor);
388 // return op.getResult();
393 //public ITypeHierarchy newTypeHierarchy(IProgressMonitor monitor) throws JavaModelException {
394 // return this.newTypeHierarchy((IWorkingCopy[])null, monitor);
397 * @see IType#newTypeHierarchy(IWorkingCopy[], IProgressMonitor)
399 //public ITypeHierarchy newTypeHierarchy(
400 // IWorkingCopy[] workingCopies,
401 // IProgressMonitor monitor)
402 // throws JavaModelException {
404 // CreateTypeHierarchyOperation op= new CreateTypeHierarchyOperation(this, workingCopies, SearchEngine.createWorkspaceScope(),
406 // runOperation(op, monitor);
407 // return op.getResult();
412 //public ITypeHierarchy newTypeHierarchy(IJavaProject project, IProgressMonitor monitor) throws JavaModelException {
413 // if (project == null) {
414 // throw new IllegalArgumentException(Util.bind("hierarchy.nullProject")); //$NON-NLS-1$
417 // CreateTypeHierarchyOperation op= new CreateTypeHierarchyOperation(
419 // (IWorkingCopy[])null, // no working copies
422 // runOperation(op, monitor);
423 // return op.getResult();
426 * See ISourceType.resolveType(...)
429 // public String[][] resolveType(String typeName) throws JavaModelException {
430 // ISourceType info = (ISourceType) this.getElementInfo();
431 // ISearchableNameEnvironment environment = ((JavaProject)getJavaProject()).getSearchableNameEnvironment();
433 // class TypeResolveRequestor implements ISelectionRequestor {
434 // String[][] answers = null;
435 // void acceptType(String[] answer){
436 // if (answers == null) {
437 // answers = new String[][]{ answer };
440 // int length = answers.length;
441 // System.arraycopy(answers, 0, answers = new String[length+1][], 0, length);
442 // answers[length] = answer;
445 // public void acceptClass(char[] packageName, char[] className, boolean needQualification) {
446 // acceptType(new String[] { new String(packageName), new String(className) });
449 // public void acceptInterface(char[] packageName, char[] interfaceName, boolean needQualification) {
450 // acceptType(new String[] { new String(packageName), new String(interfaceName) });
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){}
460 // TypeResolveRequestor requestor = new TypeResolveRequestor();
461 // SelectionEngine engine =
462 // new SelectionEngine(environment, requestor, this.getJavaProject().getOptions(true));
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();
471 // engine.selectType(info, typeName.toCharArray(), topLevelInfos, false);
472 // return requestor.answers;
475 * @private Debugging purposes
477 protected void toStringInfo(int tab, StringBuffer buffer, Object info) {
478 buffer.append(this.tabString(tab));
480 buffer.append(this.getElementName());
481 buffer.append(" (not open)"); //$NON-NLS-1$
482 } else if (info == NO_INFO) {
483 buffer.append(getElementName());
486 if (this.isInterface()) {
487 buffer.append("interface "); //$NON-NLS-1$
489 buffer.append("class "); //$NON-NLS-1$
491 buffer.append(this.getElementName());
492 } catch (JavaModelException e) {
493 buffer.append("<JavaModelException in toString of " + getElementName()); //$NON-NLS-1$