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.internal.core.SourceTypeElementInfo;
 
  17 import net.sourceforge.phpdt.internal.core.JavaElement;
 
  18 import net.sourceforge.phpdt.internal.core.util.Util;
 
  20 import net.sourceforge.phpdt.core.IField;
 
  21 import net.sourceforge.phpdt.core.IJavaElement;
 
  22 import net.sourceforge.phpdt.core.IMember;
 
  23 import net.sourceforge.phpdt.core.IMethod;
 
  24 import net.sourceforge.phpdt.core.IPackageFragment;
 
  25 import net.sourceforge.phpdt.core.IType;
 
  26 import net.sourceforge.phpdt.core.JavaModelException;
 
  27 import net.sourceforge.phpdt.core.jdom.IDOMNode;
 
  28 import net.sourceforge.phpdt.internal.corext.Assert;
 
  32  * Handle for a source type. Info object is a SourceTypeElementInfo.
 
  34  * Note: Parent is either an IClassFile, an ICompilationUnit or an IType.
 
  39 public class SourceType extends Member implements IType {
 
  41          * An empty list of Strings
 
  43         protected static final String[] fgEmptyList= new String[] {};
 
  44         protected SourceType(JavaElement parent, String name) {
 
  46                 Assert.isTrue(name.indexOf('.') == -1, Util.bind("sourcetype.invalidName", name)); //$NON-NLS-1$
 
  51 //public void codeComplete(char[] snippet,int insertion,int position,char[][] localVariableTypeNames,char[][] localVariableNames,int[] localVariableModifiers,boolean isStatic,ICompletionRequestor requestor) throws JavaModelException {
 
  52 //      if (requestor == null) {
 
  53 //              throw new IllegalArgumentException(Util.bind("codeAssist.nullRequestor")); //$NON-NLS-1$
 
  56 //      JavaProject project = (JavaProject) getJavaProject();
 
  57 //      SearchableEnvironment environment = (SearchableEnvironment) project.getSearchableNameEnvironment();
 
  58 //      NameLookup nameLookup = project.getNameLookup();
 
  59 //      CompletionEngine engine = new CompletionEngine(environment, new CompletionRequestorWrapper(requestor,nameLookup), 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 JavaModelException {
 
  85 //      CreateFieldOperation op = new CreateFieldOperation(this, contents, force);
 
  86 //      if (sibling != null) {
 
  87 //              op.createBefore(sibling);
 
  89 //      runOperation(op, monitor);
 
  90 //      return (IField) op.getResultElements()[0];
 
  95 //public IInitializer createInitializer(String contents, IJavaElement sibling, IProgressMonitor monitor) throws JavaModelException {
 
  96 //      CreateInitializerOperation op = new CreateInitializerOperation(this, contents);
 
  97 //      if (sibling != null) {
 
  98 //              op.createBefore(sibling);
 
 100 //      runOperation(op, monitor);
 
 101 //      return (IInitializer) op.getResultElements()[0];
 
 106 //public IMethod createMethod(String contents, IJavaElement sibling, boolean force, IProgressMonitor monitor) throws JavaModelException {
 
 107 //      CreateMethodOperation op = new CreateMethodOperation(this, contents, force);
 
 108 //      if (sibling != null) {
 
 109 //              op.createBefore(sibling);
 
 111 //      runOperation(op, monitor);
 
 112 //      return (IMethod) op.getResultElements()[0];
 
 117 //public IType createType(String contents, IJavaElement sibling, boolean force, IProgressMonitor monitor) throws JavaModelException {
 
 118 //      CreateTypeOperation op = new CreateTypeOperation(this, contents, force);
 
 119 //      if (sibling != null) {
 
 120 //              op.createBefore(sibling);
 
 122 //      runOperation(op, monitor);
 
 123 //      return (IType) op.getResultElements()[0];
 
 126  * @see JavaElement#equalsDOMNode
 
 128 protected boolean equalsDOMNode(IDOMNode node) throws JavaModelException {
 
 129         return (node.getNodeType() == IDOMNode.TYPE) && super.equalsDOMNode(node);
 
 134 public IMethod[] findMethods(IMethod method) {
 
 136                 return this.findMethods(method, this.getMethods());
 
 137         } catch (JavaModelException e) {
 
 138                 // if type doesn't exist, no matching method can exist
 
 145 public IType getDeclaringType() {
 
 146         IJavaElement parent = getParent();
 
 147         while (parent != null) {
 
 148                 if (parent.getElementType() == IJavaElement.TYPE) {
 
 149                         return (IType) parent;
 
 151                         if (parent instanceof IMember) {
 
 152                                 parent = parent.getParent();
 
 162 public int getElementType() {
 
 166  * @see IType#getField
 
 168 public IField getField(String name) {
 
 169         return new SourceField(this, name);
 
 174 public IField[] getFields() throws JavaModelException {
 
 175         ArrayList list = getChildrenOfType(FIELD);
 
 176         IField[] array= new IField[list.size()];
 
 181  * @see IType#getFullyQualifiedName
 
 183 public String getFullyQualifiedName() {
 
 184         return this.getFullyQualifiedName('$');
 
 187  * @see IType#getFullyQualifiedName(char)
 
 189 public String getFullyQualifiedName(char enclosingTypeSeparator) {
 
 190         String packageName = getPackageFragment().getElementName();
 
 191         if (packageName.equals(IPackageFragment.DEFAULT_PACKAGE_NAME)) {
 
 192                 return getTypeQualifiedName(enclosingTypeSeparator);
 
 194         return packageName + '.' + getTypeQualifiedName(enclosingTypeSeparator);
 
 200 //public IInitializer getInitializer(int occurrenceCount) {
 
 201 //      return new Initializer(this, occurrenceCount);
 
 206 //public IInitializer[] getInitializers() throws JavaModelException {
 
 207 //      ArrayList list = getChildrenOfType(INITIALIZER);
 
 208 //      IInitializer[] array= new IInitializer[list.size()];
 
 209 //      list.toArray(array);
 
 213  * @see IType#getMethod
 
 215 public IMethod getMethod(String name, String[] parameterTypeSignatures) {
 
 216         return new SourceMethod(this, name, parameterTypeSignatures);
 
 221 public IMethod[] getMethods() throws JavaModelException {
 
 222         ArrayList list = getChildrenOfType(METHOD);
 
 223         IMethod[] array= new IMethod[list.size()];
 
 230 public IPackageFragment getPackageFragment() {
 
 231         IJavaElement parentElement = this.parent;
 
 232         while (parentElement != null) {
 
 233                 if (parentElement.getElementType() == IJavaElement.PACKAGE_FRAGMENT) {
 
 234                         return (IPackageFragment)parentElement;
 
 237                         parentElement = parentElement.getParent();
 
 240         Assert.isTrue(false);  // should not happen
 
 247 public String getSuperclassName() throws JavaModelException {
 
 248         SourceTypeElementInfo info = (SourceTypeElementInfo) getElementInfo();
 
 249         char[] superclassName= info.getSuperclassName();
 
 250         if (superclassName == null) {
 
 253         return new String(superclassName);
 
 258 public String[] getSuperInterfaceNames() throws JavaModelException {
 
 259         SourceTypeElementInfo info = (SourceTypeElementInfo) getElementInfo();
 
 260         char[][] names= info.getInterfaceNames();
 
 264         String[] strings= new String[names.length];
 
 265         for (int i= 0; i < names.length; i++) {
 
 266                 strings[i]= new String(names[i]);
 
 274 public IType getType(String name) {
 
 275         return new SourceType(this, name);
 
 278  * @see IType#getTypeQualifiedName
 
 280 public String getTypeQualifiedName() {
 
 281         return this.getTypeQualifiedName('$');
 
 284  * @see IType#getTypeQualifiedName(char)
 
 286 public String getTypeQualifiedName(char enclosingTypeSeparator) {
 
 287         if (parent.getElementType() == IJavaElement.COMPILATION_UNIT) {
 
 290                 return ((IType) parent).getTypeQualifiedName(enclosingTypeSeparator) + enclosingTypeSeparator + name;
 
 297 public IType[] getTypes() throws JavaModelException {
 
 298         ArrayList list= getChildrenOfType(TYPE);
 
 299         IType[] array= new IType[list.size()];
 
 306 public boolean hasChildren() throws JavaModelException {
 
 307         return getChildren().length > 0;
 
 310  * @see IType#isAnonymous()
 
 312 public boolean isAnonymous() throws JavaModelException {
 
 313         return false; // cannot create source handle onto anonymous types
 
 318 public boolean isClass() throws JavaModelException {
 
 319         return !isInterface();
 
 324 public boolean isInterface() throws JavaModelException {
 
 325         SourceTypeElementInfo info = (SourceTypeElementInfo) getElementInfo();
 
 326         return info.isInterface();
 
 329  * @see IType#isLocal()
 
 331 public boolean isLocal() throws JavaModelException {
 
 332         return false; // cannot create source handle onto local types
 
 335  * @see IType#isMember()
 
 337 public boolean isMember() throws JavaModelException {
 
 338         return getDeclaringType() != null;
 
 343 //public ITypeHierarchy loadTypeHierachy(InputStream input, IProgressMonitor monitor) throws JavaModelException {
 
 344 //      return TypeHierarchy.load(this, input);
 
 349 //public ITypeHierarchy newSupertypeHierarchy(IProgressMonitor monitor) throws JavaModelException {
 
 350 //      return this.newSupertypeHierarchy(null, monitor);
 
 353  * @see IType#newSupertypeHierarchy(IWorkingCopy[], IProgressMonitor)
 
 355 //public ITypeHierarchy newSupertypeHierarchy(
 
 356 //      IWorkingCopy[] workingCopies,
 
 357 //      IProgressMonitor monitor)
 
 358 //      throws JavaModelException {
 
 360 //      CreateTypeHierarchyOperation op= new CreateTypeHierarchyOperation(this, workingCopies, SearchEngine.createWorkspaceScope(), false);
 
 361 //      runOperation(op, monitor);
 
 362 //      return op.getResult();
 
 368 //public ITypeHierarchy newTypeHierarchy(IProgressMonitor monitor) throws JavaModelException {
 
 369 //      return this.newTypeHierarchy((IWorkingCopy[])null, monitor);
 
 372  * @see IType#newTypeHierarchy(IWorkingCopy[], IProgressMonitor)
 
 374 //public ITypeHierarchy newTypeHierarchy(
 
 375 //      IWorkingCopy[] workingCopies,
 
 376 //      IProgressMonitor monitor)
 
 377 //      throws JavaModelException {
 
 379 //      CreateTypeHierarchyOperation op= new CreateTypeHierarchyOperation(this, workingCopies, SearchEngine.createWorkspaceScope(), true);
 
 380 //      runOperation(op, monitor);
 
 381 //      return op.getResult();
 
 387 //public ITypeHierarchy newTypeHierarchy(IJavaProject project, IProgressMonitor monitor) throws JavaModelException {
 
 388 //      if (project == null) {
 
 389 //              throw new IllegalArgumentException(Util.bind("hierarchy.nullProject")); //$NON-NLS-1$
 
 392 //      CreateTypeHierarchyOperation op= new CreateTypeHierarchyOperation(
 
 394 //              (IWorkingCopy[])null, // no working copies
 
 397 //      runOperation(op, monitor);
 
 398 //      return op.getResult();
 
 401  * See ISourceType.resolveType(...)
 
 404 // public String[][] resolveType(String typeName) throws JavaModelException {
 
 405 //      ISourceType info = (ISourceType) this.getElementInfo();
 
 406 //      ISearchableNameEnvironment environment = ((JavaProject)getJavaProject()).getSearchableNameEnvironment();
 
 408 //      class TypeResolveRequestor implements ISelectionRequestor {
 
 409 //              String[][] answers = null;
 
 410 //              void acceptType(String[] answer){
 
 411 //                      if (answers == null) {
 
 412 //                              answers = new String[][]{ answer };
 
 415 //                              int length = answers.length;
 
 416 //                              System.arraycopy(answers, 0, answers = new String[length+1][], 0, length);
 
 417 //                              answers[length] = answer;
 
 420 //              public void acceptClass(char[] packageName, char[] className, boolean needQualification) {
 
 421 //                      acceptType(new String[]  { new String(packageName), new String(className) });
 
 424 //              public void acceptInterface(char[] packageName, char[] interfaceName, boolean needQualification) {
 
 425 //                      acceptType(new String[]  { new String(packageName), new String(interfaceName) });
 
 428 //              public void acceptError(IProblem error) {}
 
 429 //              public void acceptField(char[] declaringTypePackageName, char[] declaringTypeName, char[] name) {}
 
 430 //              public void acceptMethod(char[] declaringTypePackageName, char[] declaringTypeName, char[] selector, char[][] parameterPackageNames, char[][] parameterTypeNames, boolean isConstructor) {}
 
 431 //              public void acceptPackage(char[] packageName){}
 
 434 //      TypeResolveRequestor requestor = new TypeResolveRequestor();
 
 435 //      SelectionEngine engine = 
 
 436 //              new SelectionEngine(environment, requestor, this.getJavaProject().getOptions(true));
 
 438 //      IType[] topLevelTypes = this.getCompilationUnit().getTypes();
 
 439 //      int length = topLevelTypes.length;
 
 440 //      ISourceType[] topLevelInfos = new ISourceType[length];
 
 441 //      for (int i = 0; i < length; i++) {
 
 442 //              topLevelInfos[i] = (ISourceType)((SourceType)topLevelTypes[i]).getElementInfo();
 
 445 //      engine.selectType(info, typeName.toCharArray(), topLevelInfos, false);
 
 446 //      return requestor.answers;
 
 449  * @private Debugging purposes
 
 451 protected void toStringInfo(int tab, StringBuffer buffer, Object info) {
 
 452         buffer.append(this.tabString(tab));
 
 454                 buffer.append(this.getElementName());
 
 455                 buffer.append(" (not open)"); //$NON-NLS-1$
 
 456         } else if (info == NO_INFO) {
 
 457                 buffer.append(getElementName());
 
 460                         if (this.isInterface()) {
 
 461                                 buffer.append("interface "); //$NON-NLS-1$
 
 463                                 buffer.append("class "); //$NON-NLS-1$
 
 465                         buffer.append(this.getElementName());
 
 466                 } catch (JavaModelException e) {
 
 467                         buffer.append("<JavaModelException in toString of " + getElementName()); //$NON-NLS-1$