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;
29 * Handle for a source type. Info object is a SourceTypeElementInfo.
31 * Note: Parent is either an IClassFile, an ICompilationUnit or an IType.
36 public class SourceType extends Member implements IType {
38 * An empty list of Strings
40 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$
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$
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);
58 // String source = getCompilationUnit().getSource();
59 // if (source != null && insertion > -1 && insertion < source.length()) {
60 // String encoding = project.getOption(JavaCore.CORE_ENCODING, true);
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);
66 // BasicCompilationUnit cu =
67 // new BasicCompilationUnit(
73 // engine.complete(cu, prefix.length + position, prefix.length);
75 // engine.complete(this, snippet, position, localVariableTypeNames, localVariableNames, localVariableModifiers, isStatic);
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);
86 // runOperation(op, monitor);
87 // return (IField) op.getResultElements()[0];
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);
97 // runOperation(op, monitor);
98 // return (IInitializer) op.getResultElements()[0];
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);
108 // runOperation(op, monitor);
109 // return (IMethod) op.getResultElements()[0];
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);
119 // runOperation(op, monitor);
120 // return (IType) op.getResultElements()[0];
123 * @see JavaElement#equalsDOMNode
125 protected boolean equalsDOMNode(IDOMNode node) throws JavaModelException {
126 return (node.getNodeType() == IDOMNode.TYPE) && super.equalsDOMNode(node);
131 public IMethod[] findMethods(IMethod method) {
133 return this.findMethods(method, this.getMethods());
134 } catch (JavaModelException e) {
135 // if type doesn't exist, no matching method can exist
142 public IType getDeclaringType() {
143 IJavaElement parent = getParent();
144 while (parent != null) {
145 if (parent.getElementType() == IJavaElement.TYPE) {
146 return (IType) parent;
148 if (parent instanceof IMember) {
149 parent = parent.getParent();
159 public int getElementType() {
163 * @see IType#getField
165 public IField getField(String name) {
166 return new SourceField(this, name);
171 public IField[] getFields() throws JavaModelException {
172 ArrayList list = getChildrenOfType(FIELD);
173 IField[] array= new IField[list.size()];
178 * @see IType#getFullyQualifiedName
180 public String getFullyQualifiedName() {
181 return this.getFullyQualifiedName('$');
184 * @see IType#getFullyQualifiedName(char)
186 public String getFullyQualifiedName(char enclosingTypeSeparator) {
187 String packageName = getPackageFragment().getElementName();
188 if (packageName.equals(IPackageFragment.DEFAULT_PACKAGE_NAME)) {
189 return getTypeQualifiedName(enclosingTypeSeparator);
191 return packageName + '.' + getTypeQualifiedName(enclosingTypeSeparator);
197 //public IInitializer getInitializer(int occurrenceCount) {
198 // return new Initializer(this, occurrenceCount);
203 //public IInitializer[] getInitializers() throws JavaModelException {
204 // ArrayList list = getChildrenOfType(INITIALIZER);
205 // IInitializer[] array= new IInitializer[list.size()];
206 // list.toArray(array);
210 * @see IType#getMethod
212 public IMethod getMethod(String name, String[] parameterTypeSignatures) {
213 return new SourceMethod(this, name, parameterTypeSignatures);
218 public IMethod[] getMethods() throws JavaModelException {
219 ArrayList list = getChildrenOfType(METHOD);
220 IMethod[] array= new IMethod[list.size()];
227 public IPackageFragment getPackageFragment() {
228 IJavaElement parentElement = this.parent;
229 while (parentElement != null) {
230 if (parentElement.getElementType() == IJavaElement.PACKAGE_FRAGMENT) {
231 return (IPackageFragment)parentElement;
234 parentElement = parentElement.getParent();
237 Assert.isTrue(false); // should not happen
244 public String getSuperclassName() throws JavaModelException {
245 SourceTypeElementInfo info = (SourceTypeElementInfo) getElementInfo();
246 char[] superclassName= info.getSuperclassName();
247 if (superclassName == null) {
250 return new String(superclassName);
255 public String[] getSuperInterfaceNames() throws JavaModelException {
256 SourceTypeElementInfo info = (SourceTypeElementInfo) getElementInfo();
257 char[][] names= info.getInterfaceNames();
261 String[] strings= new String[names.length];
262 for (int i= 0; i < names.length; i++) {
263 strings[i]= new String(names[i]);
271 public IType getType(String name) {
272 return new SourceType(this, name);
275 * @see IType#getTypeQualifiedName
277 public String getTypeQualifiedName() {
278 return this.getTypeQualifiedName('$');
281 * @see IType#getTypeQualifiedName(char)
283 public String getTypeQualifiedName(char enclosingTypeSeparator) {
284 if (parent.getElementType() == IJavaElement.COMPILATION_UNIT) {
287 return ((IType) parent).getTypeQualifiedName(enclosingTypeSeparator) + enclosingTypeSeparator + name;
294 public IType[] getTypes() throws JavaModelException {
295 ArrayList list= getChildrenOfType(TYPE);
296 IType[] array= new IType[list.size()];
303 public boolean hasChildren() throws JavaModelException {
304 return getChildren().length > 0;
307 * @see IType#isAnonymous()
309 public boolean isAnonymous() throws JavaModelException {
310 return false; // cannot create source handle onto anonymous types
315 public boolean isClass() throws JavaModelException {
316 return !isInterface();
321 public boolean isInterface() throws JavaModelException {
322 SourceTypeElementInfo info = (SourceTypeElementInfo) getElementInfo();
323 return info.isInterface();
326 * @see IType#isLocal()
328 public boolean isLocal() throws JavaModelException {
329 return false; // cannot create source handle onto local types
332 * @see IType#isMember()
334 public boolean isMember() throws JavaModelException {
335 return getDeclaringType() != null;
340 //public ITypeHierarchy loadTypeHierachy(InputStream input, IProgressMonitor monitor) throws JavaModelException {
341 // return TypeHierarchy.load(this, input);
346 //public ITypeHierarchy newSupertypeHierarchy(IProgressMonitor monitor) throws JavaModelException {
347 // return this.newSupertypeHierarchy(null, monitor);
350 * @see IType#newSupertypeHierarchy(IWorkingCopy[], IProgressMonitor)
352 //public ITypeHierarchy newSupertypeHierarchy(
353 // IWorkingCopy[] workingCopies,
354 // IProgressMonitor monitor)
355 // throws JavaModelException {
357 // CreateTypeHierarchyOperation op= new CreateTypeHierarchyOperation(this, workingCopies, SearchEngine.createWorkspaceScope(), false);
358 // runOperation(op, monitor);
359 // return op.getResult();
365 //public ITypeHierarchy newTypeHierarchy(IProgressMonitor monitor) throws JavaModelException {
366 // return this.newTypeHierarchy((IWorkingCopy[])null, monitor);
369 * @see IType#newTypeHierarchy(IWorkingCopy[], IProgressMonitor)
371 //public ITypeHierarchy newTypeHierarchy(
372 // IWorkingCopy[] workingCopies,
373 // IProgressMonitor monitor)
374 // throws JavaModelException {
376 // CreateTypeHierarchyOperation op= new CreateTypeHierarchyOperation(this, workingCopies, SearchEngine.createWorkspaceScope(), true);
377 // runOperation(op, monitor);
378 // return op.getResult();
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$
389 // CreateTypeHierarchyOperation op= new CreateTypeHierarchyOperation(
391 // (IWorkingCopy[])null, // no working copies
394 // runOperation(op, monitor);
395 // return op.getResult();
398 * See ISourceType.resolveType(...)
401 // public String[][] resolveType(String typeName) throws JavaModelException {
402 // ISourceType info = (ISourceType) this.getElementInfo();
403 // ISearchableNameEnvironment environment = ((JavaProject)getJavaProject()).getSearchableNameEnvironment();
405 // class TypeResolveRequestor implements ISelectionRequestor {
406 // String[][] answers = null;
407 // void acceptType(String[] answer){
408 // if (answers == null) {
409 // answers = new String[][]{ answer };
412 // int length = answers.length;
413 // System.arraycopy(answers, 0, answers = new String[length+1][], 0, length);
414 // answers[length] = answer;
417 // public void acceptClass(char[] packageName, char[] className, boolean needQualification) {
418 // acceptType(new String[] { new String(packageName), new String(className) });
421 // public void acceptInterface(char[] packageName, char[] interfaceName, boolean needQualification) {
422 // acceptType(new String[] { new String(packageName), new String(interfaceName) });
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){}
431 // TypeResolveRequestor requestor = new TypeResolveRequestor();
432 // SelectionEngine engine =
433 // new SelectionEngine(environment, requestor, this.getJavaProject().getOptions(true));
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();
442 // engine.selectType(info, typeName.toCharArray(), topLevelInfos, false);
443 // return requestor.answers;
446 * @private Debugging purposes
448 protected void toStringInfo(int tab, StringBuffer buffer, Object info) {
449 buffer.append(this.tabString(tab));
451 buffer.append(this.getElementName());
452 buffer.append(" (not open)"); //$NON-NLS-1$
453 } else if (info == NO_INFO) {
454 buffer.append(getElementName());
457 if (this.isInterface()) {
458 buffer.append("interface "); //$NON-NLS-1$
460 buffer.append("class "); //$NON-NLS-1$
462 buffer.append(this.getElementName());
463 } catch (JavaModelException e) {
464 buffer.append("<JavaModelException in toString of " + getElementName()); //$NON-NLS-1$