KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > SourceType


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.core;
12
13 import java.io.InputStream JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.HashMap JavaDoc;
16
17 import org.eclipse.core.runtime.Assert;
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.jdt.core.*;
20 import org.eclipse.jdt.core.compiler.*;
21 import org.eclipse.jdt.core.search.SearchEngine;
22 import org.eclipse.jdt.internal.codeassist.CompletionEngine;
23 import org.eclipse.jdt.internal.codeassist.ISelectionRequestor;
24 import org.eclipse.jdt.internal.codeassist.SelectionEngine;
25 import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
26 import org.eclipse.jdt.internal.compiler.env.ISourceType;
27 import org.eclipse.jdt.internal.compiler.lookup.Binding;
28 import org.eclipse.jdt.internal.core.hierarchy.TypeHierarchy;
29 import org.eclipse.jdt.internal.core.util.MementoTokenizer;
30 import org.eclipse.jdt.internal.core.util.Messages;
31
32 /**
33  * Handle for a source type. Info object is a SourceTypeElementInfo.
34  *
35  * Note: Parent is either an IClassFile, an ICompilationUnit or an IType.
36  *
37  * @see IType
38  */

39
40 public class SourceType extends NamedMember implements IType {
41     
42 protected SourceType(JavaElement parent, String JavaDoc name) {
43     super(parent, name);
44 }
45 protected void closing(Object JavaDoc info) throws JavaModelException {
46     super.closing(info);
47     SourceTypeElementInfo elementInfo = (SourceTypeElementInfo) info;
48     ITypeParameter[] typeParameters = elementInfo.typeParameters;
49     for (int i = 0, length = typeParameters.length; i < length; i++) {
50         ((TypeParameter) typeParameters[i]).close();
51     }
52 }
53 /**
54  * @see IType
55  * @deprecated
56  */

57 public void codeComplete(char[] snippet,int insertion,int position,char[][] localVariableTypeNames,char[][] localVariableNames,int[] localVariableModifiers,boolean isStatic,ICompletionRequestor requestor) throws JavaModelException {
58     codeComplete(snippet, insertion, position, localVariableTypeNames, localVariableNames, localVariableModifiers, isStatic, requestor, DefaultWorkingCopyOwner.PRIMARY);
59 }
60 /**
61  * @see IType
62  * @deprecated
63  */

64 public void codeComplete(char[] snippet,int insertion,int position,char[][] localVariableTypeNames,char[][] localVariableNames,int[] localVariableModifiers,boolean isStatic,ICompletionRequestor requestor, WorkingCopyOwner owner) throws JavaModelException {
65     if (requestor == null) {
66         throw new IllegalArgumentException JavaDoc("Completion requestor cannot be null"); //$NON-NLS-1$
67
}
68     codeComplete(snippet, insertion, position, localVariableTypeNames, localVariableNames, localVariableModifiers, isStatic, new org.eclipse.jdt.internal.codeassist.CompletionRequestorWrapper(requestor), owner);
69
70 }
71 /**
72  * @see IType
73  */

74 public void codeComplete(char[] snippet,int insertion,int position,char[][] localVariableTypeNames,char[][] localVariableNames,int[] localVariableModifiers,boolean isStatic,CompletionRequestor requestor) throws JavaModelException {
75     codeComplete(snippet, insertion, position, localVariableTypeNames, localVariableNames, localVariableModifiers, isStatic, requestor, DefaultWorkingCopyOwner.PRIMARY);
76 }
77 /**
78  * @see IType
79  */

80 public void codeComplete(char[] snippet,int insertion,int position,char[][] localVariableTypeNames,char[][] localVariableNames,int[] localVariableModifiers,boolean isStatic,CompletionRequestor requestor, WorkingCopyOwner owner) throws JavaModelException {
81     if (requestor == null) {
82         throw new IllegalArgumentException JavaDoc("Completion requestor cannot be null"); //$NON-NLS-1$
83
}
84     
85     JavaProject project = (JavaProject) getJavaProject();
86     SearchableEnvironment environment = project.newSearchableNameEnvironment(owner);
87     CompletionEngine engine = new CompletionEngine(environment, requestor, project.getOptions(true), project);
88
89     String JavaDoc source = getCompilationUnit().getSource();
90     if (source != null && insertion > -1 && insertion < source.length()) {
91         
92         char[] prefix = CharOperation.concat(source.substring(0, insertion).toCharArray(), new char[]{'{'});
93         char[] suffix = CharOperation.concat(new char[]{'}'}, source.substring(insertion).toCharArray());
94         char[] fakeSource = CharOperation.concat(prefix, snippet, suffix);
95         
96         BasicCompilationUnit cu =
97             new BasicCompilationUnit(
98                 fakeSource,
99                 null,
100                 getElementName(),
101                 getParent());
102
103         engine.complete(cu, prefix.length + position, prefix.length);
104     } else {
105         engine.complete(this, snippet, position, localVariableTypeNames, localVariableNames, localVariableModifiers, isStatic);
106     }
107     if (NameLookup.VERBOSE) {
108         System.out.println(Thread.currentThread() + " TIME SPENT in NameLoopkup#seekTypesInSourcePackage: " + environment.nameLookup.timeSpentInSeekTypesInSourcePackage + "ms"); //$NON-NLS-1$ //$NON-NLS-2$
109
System.out.println(Thread.currentThread() + " TIME SPENT in NameLoopkup#seekTypesInBinaryPackage: " + environment.nameLookup.timeSpentInSeekTypesInBinaryPackage + "ms"); //$NON-NLS-1$ //$NON-NLS-2$
110
}
111 }
112 /**
113  * @see IType
114  */

115 public IField createField(String JavaDoc contents, IJavaElement sibling, boolean force, IProgressMonitor monitor) throws JavaModelException {
116     CreateFieldOperation op = new CreateFieldOperation(this, contents, force);
117     if (sibling != null) {
118         op.createBefore(sibling);
119     }
120     op.runOperation(monitor);
121     return (IField) op.getResultElements()[0];
122 }
123 /**
124  * @see IType
125  */

126 public IInitializer createInitializer(String JavaDoc contents, IJavaElement sibling, IProgressMonitor monitor) throws JavaModelException {
127     CreateInitializerOperation op = new CreateInitializerOperation(this, contents);
128     if (sibling != null) {
129         op.createBefore(sibling);
130     }
131     op.runOperation(monitor);
132     return (IInitializer) op.getResultElements()[0];
133 }
134 /**
135  * @see IType
136  */

137 public IMethod createMethod(String JavaDoc contents, IJavaElement sibling, boolean force, IProgressMonitor monitor) throws JavaModelException {
138     CreateMethodOperation op = new CreateMethodOperation(this, contents, force);
139     if (sibling != null) {
140         op.createBefore(sibling);
141     }
142     op.runOperation(monitor);
143     return (IMethod) op.getResultElements()[0];
144 }
145 /**
146  * @see IType
147  */

148 public IType createType(String JavaDoc contents, IJavaElement sibling, boolean force, IProgressMonitor monitor) throws JavaModelException {
149     CreateTypeOperation op = new CreateTypeOperation(this, contents, force);
150     if (sibling != null) {
151         op.createBefore(sibling);
152     }
153     op.runOperation(monitor);
154     return (IType) op.getResultElements()[0];
155 }
156 public boolean equals(Object JavaDoc o) {
157     if (!(o instanceof SourceType)) return false;
158     return super.equals(o);
159 }
160 /*
161  * @see IType
162  */

163 public IMethod[] findMethods(IMethod method) {
164     try {
165         return findMethods(method, getMethods());
166     } catch (JavaModelException e) {
167         // if type doesn't exist, no matching method can exist
168
return null;
169     }
170 }
171 public IJavaElement[] getChildrenForCategory(String JavaDoc category) throws JavaModelException {
172     IJavaElement[] children = getChildren();
173     int length = children.length;
174     if (length == 0) return NO_ELEMENTS;
175     SourceTypeElementInfo info = (SourceTypeElementInfo) getElementInfo();
176     HashMap JavaDoc categories = info.getCategories();
177     if (categories == null) return NO_ELEMENTS;
178     IJavaElement[] result = new IJavaElement[length];
179     int index = 0;
180     for (int i = 0; i < length; i++) {
181         IJavaElement child = children[i];
182         String JavaDoc[] elementCategories = (String JavaDoc[]) categories.get(child);
183         if (elementCategories != null)
184             for (int j = 0, length2 = elementCategories.length; j < length2; j++) {
185                 if (elementCategories[j].equals(category))
186                     result[index++] = child;
187             }
188     }
189     if (index == 0) return NO_ELEMENTS;
190     if (index < length)
191         System.arraycopy(result, 0, result = new IJavaElement[index], 0, index);
192     return result;
193 }
194 /**
195  * @see IMember
196  */

197 public IType getDeclaringType() {
198     IJavaElement parentElement = getParent();
199     while (parentElement != null) {
200         if (parentElement.getElementType() == IJavaElement.TYPE) {
201             return (IType) parentElement;
202         } else
203             if (parentElement instanceof IMember) {
204                 parentElement = parentElement.getParent();
205             } else {
206                 return null;
207             }
208     }
209     return null;
210 }
211 /**
212  * @see IJavaElement
213  */

214 public int getElementType() {
215     return TYPE;
216 }
217 /**
218  * @see IType#getField
219  */

220 public IField getField(String JavaDoc fieldName) {
221     return new SourceField(this, fieldName);
222 }
223 /**
224  * @see IType
225  */

226 public IField[] getFields() throws JavaModelException {
227     ArrayList JavaDoc list = getChildrenOfType(FIELD);
228     IField[] array= new IField[list.size()];
229     list.toArray(array);
230     return array;
231 }
232 /**
233  * @see IType#getFullyQualifiedName()
234  */

235 public String JavaDoc getFullyQualifiedName() {
236     return this.getFullyQualifiedName('$');
237 }
238 /**
239  * @see IType#getFullyQualifiedName(char)
240  */

241 public String JavaDoc getFullyQualifiedName(char enclosingTypeSeparator) {
242     try {
243         return getFullyQualifiedName(enclosingTypeSeparator, false/*don't show parameters*/);
244     } catch (JavaModelException e) {
245         // exception thrown only when showing parameters
246
return null;
247     }
248 }
249 /*
250  * @see IType#getFullyQualifiedParameterizedName()
251  */

252 public String JavaDoc getFullyQualifiedParameterizedName() throws JavaModelException {
253     return getFullyQualifiedName('.', true/*show parameters*/);
254 }
255 /*
256  * @see JavaElement
257  */

258 public IJavaElement getHandleFromMemento(String JavaDoc token, MementoTokenizer memento, WorkingCopyOwner workingCopyOwner) {
259     switch (token.charAt(0)) {
260         case JEM_COUNT:
261             return getHandleUpdatingCountFromMemento(memento, workingCopyOwner);
262         case JEM_FIELD:
263             if (!memento.hasMoreTokens()) return this;
264             String JavaDoc fieldName = memento.nextToken();
265             JavaElement field = (JavaElement)getField(fieldName);
266             return field.getHandleFromMemento(memento, workingCopyOwner);
267         case JEM_INITIALIZER:
268             if (!memento.hasMoreTokens()) return this;
269             String JavaDoc count = memento.nextToken();
270             JavaElement initializer = (JavaElement)getInitializer(Integer.parseInt(count));
271             return initializer.getHandleFromMemento(memento, workingCopyOwner);
272         case JEM_METHOD:
273             if (!memento.hasMoreTokens()) return this;
274             String JavaDoc selector = memento.nextToken();
275             ArrayList JavaDoc params = new ArrayList JavaDoc();
276             nextParam: while (memento.hasMoreTokens()) {
277                 token = memento.nextToken();
278                 switch (token.charAt(0)) {
279                     case JEM_TYPE:
280                     case JEM_TYPE_PARAMETER:
281                         break nextParam;
282                     case JEM_METHOD:
283                         if (!memento.hasMoreTokens()) return this;
284                         String JavaDoc param = memento.nextToken();
285                         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
286                         while (param.length() == 1 && Signature.C_ARRAY == param.charAt(0)) { // backward compatible with 3.0 mementos
287
buffer.append(Signature.C_ARRAY);
288                             if (!memento.hasMoreTokens()) return this;
289                             param = memento.nextToken();
290                         }
291                         params.add(buffer.toString() + param);
292                         break;
293                     default:
294                         break nextParam;
295                 }
296             }
297             String JavaDoc[] parameters = new String JavaDoc[params.size()];
298             params.toArray(parameters);
299             JavaElement method = (JavaElement)getMethod(selector, parameters);
300             switch (token.charAt(0)) {
301                 case JEM_TYPE:
302                 case JEM_TYPE_PARAMETER:
303                 case JEM_LOCALVARIABLE:
304                     return method.getHandleFromMemento(token, memento, workingCopyOwner);
305                 default:
306                     return method;
307             }
308         case JEM_TYPE:
309             String JavaDoc typeName;
310             if (memento.hasMoreTokens()) {
311                 typeName = memento.nextToken();
312                 char firstChar = typeName.charAt(0);
313                 if (firstChar == JEM_FIELD || firstChar == JEM_INITIALIZER || firstChar == JEM_METHOD || firstChar == JEM_TYPE || firstChar == JEM_COUNT) {
314                     token = typeName;
315                     typeName = ""; //$NON-NLS-1$
316
} else {
317                     token = null;
318                 }
319             } else {
320                 typeName = ""; //$NON-NLS-1$
321
token = null;
322             }
323             JavaElement type = (JavaElement)getType(typeName);
324             if (token == null) {
325                 return type.getHandleFromMemento(memento, workingCopyOwner);
326             } else {
327                 return type.getHandleFromMemento(token, memento, workingCopyOwner);
328             }
329         case JEM_TYPE_PARAMETER:
330             if (!memento.hasMoreTokens()) return this;
331             String JavaDoc typeParameterName = memento.nextToken();
332             JavaElement typeParameter = new TypeParameter(this, typeParameterName);
333             return typeParameter.getHandleFromMemento(memento, workingCopyOwner);
334             
335     }
336     return null;
337 }
338 /**
339  * @see IType
340  */

341 public IInitializer getInitializer(int count) {
342     return new Initializer(this, count);
343 }
344 /**
345  * @see IType
346  */

347 public IInitializer[] getInitializers() throws JavaModelException {
348     ArrayList JavaDoc list = getChildrenOfType(INITIALIZER);
349     IInitializer[] array= new IInitializer[list.size()];
350     list.toArray(array);
351     return array;
352 }
353 /* (non-Javadoc)
354  * @see org.eclipse.jdt.core.IType#getKey()
355  */

356 public String JavaDoc getKey() {
357     try {
358         return getKey(this, false/*don't open*/);
359     } catch (JavaModelException e) {
360         // happen only if force open is true
361
return null;
362     }
363 }
364 /**
365  * @see IType#getMethod
366  */

367 public IMethod getMethod(String JavaDoc selector, String JavaDoc[] parameterTypeSignatures) {
368     return new SourceMethod(this, selector, parameterTypeSignatures);
369 }
370 /**
371  * @see IType
372  */

373 public IMethod[] getMethods() throws JavaModelException {
374     ArrayList JavaDoc list = getChildrenOfType(METHOD);
375     IMethod[] array= new IMethod[list.size()];
376     list.toArray(array);
377     return array;
378 }
379 /**
380  * @see IType
381  */

382 public IPackageFragment getPackageFragment() {
383     IJavaElement parentElement = this.parent;
384     while (parentElement != null) {
385         if (parentElement.getElementType() == IJavaElement.PACKAGE_FRAGMENT) {
386             return (IPackageFragment)parentElement;
387         }
388         else {
389             parentElement = parentElement.getParent();
390         }
391     }
392     Assert.isTrue(false); // should not happen
393
return null;
394 }
395 /*
396  * @see JavaElement#getPrimaryElement(boolean)
397  */

398 public IJavaElement getPrimaryElement(boolean checkOwner) {
399     if (checkOwner) {
400         CompilationUnit cu = (CompilationUnit)getAncestor(COMPILATION_UNIT);
401         if (cu.isPrimary()) return this;
402     }
403     IJavaElement primaryParent = this.parent.getPrimaryElement(false);
404     switch (primaryParent.getElementType()) {
405         case IJavaElement.COMPILATION_UNIT:
406             return ((ICompilationUnit)primaryParent).getType(this.name);
407         case IJavaElement.TYPE:
408             return ((IType)primaryParent).getType(this.name);
409         case IJavaElement.FIELD:
410         case IJavaElement.INITIALIZER:
411         case IJavaElement.METHOD:
412             return ((IMember)primaryParent).getType(this.name, this.occurrenceCount);
413     }
414     return this;
415 }
416 /**
417  * @see IType
418  */

419 public String JavaDoc getSuperclassName() throws JavaModelException {
420     SourceTypeElementInfo info = (SourceTypeElementInfo) getElementInfo();
421     char[] superclassName= info.getSuperclassName();
422     if (superclassName == null) {
423         return null;
424     }
425     return new String JavaDoc(superclassName);
426 }
427
428 /**
429  * @see IType#getSuperclassTypeSignature()
430  * @since 3.0
431  */

432 public String JavaDoc getSuperclassTypeSignature() throws JavaModelException {
433     SourceTypeElementInfo info = (SourceTypeElementInfo) getElementInfo();
434     char[] superclassName= info.getSuperclassName();
435     if (superclassName == null) {
436         return null;
437     }
438     return new String JavaDoc(Signature.createTypeSignature(superclassName, false));
439 }
440
441 /**
442  * @see IType
443  */

444 public String JavaDoc[] getSuperInterfaceNames() throws JavaModelException {
445     SourceTypeElementInfo info = (SourceTypeElementInfo) getElementInfo();
446     char[][] names= info.getInterfaceNames();
447     return CharOperation.toStrings(names);
448 }
449
450 /**
451  * @see IType#getSuperInterfaceTypeSignatures()
452  * @since 3.0
453  */

454 public String JavaDoc[] getSuperInterfaceTypeSignatures() throws JavaModelException {
455     SourceTypeElementInfo info = (SourceTypeElementInfo) getElementInfo();
456     char[][] names= info.getInterfaceNames();
457     if (names == null) {
458         return CharOperation.NO_STRINGS;
459     }
460     String JavaDoc[] strings= new String JavaDoc[names.length];
461     for (int i= 0; i < names.length; i++) {
462         strings[i]= new String JavaDoc(Signature.createTypeSignature(names[i], false));
463     }
464     return strings;
465 }
466
467 public ITypeParameter[] getTypeParameters() throws JavaModelException {
468     SourceTypeElementInfo info = (SourceTypeElementInfo) getElementInfo();
469     return info.typeParameters;
470 }
471
472 /**
473  * @see IType#getTypeParameterSignatures()
474  * @since 3.0
475  */

476 public String JavaDoc[] getTypeParameterSignatures() throws JavaModelException {
477     ITypeParameter[] typeParameters = getTypeParameters();
478     int length = typeParameters.length;
479     String JavaDoc[] typeParameterSignatures = new String JavaDoc[length];
480     for (int i = 0; i < length; i++) {
481         TypeParameter typeParameter = (TypeParameter) typeParameters[i];
482         TypeParameterElementInfo info = (TypeParameterElementInfo) typeParameter.getElementInfo();
483         char[][] bounds = info.bounds;
484         if (bounds == null) {
485             typeParameterSignatures[i] = Signature.createTypeParameterSignature(typeParameter.getElementName(), CharOperation.NO_STRINGS);
486         } else {
487             int boundsLength = bounds.length;
488             char[][] boundSignatures = new char[boundsLength][];
489             for (int j = 0; j < boundsLength; j++) {
490                 boundSignatures[j] = Signature.createCharArrayTypeSignature(bounds[j], false);
491             }
492             typeParameterSignatures[i] = new String JavaDoc(Signature.createTypeParameterSignature(typeParameter.getElementName().toCharArray(), boundSignatures));
493         }
494     }
495     return typeParameterSignatures;
496 }
497
498 /**
499  * @see IType
500  */

501 public IType getType(String JavaDoc typeName) {
502     return new SourceType(this, typeName);
503 }
504 public ITypeParameter getTypeParameter(String JavaDoc typeParameterName) {
505     return new TypeParameter(this, typeParameterName);
506 }
507 /**
508  * @see IType#getTypeQualifiedName()
509  */

510 public String JavaDoc getTypeQualifiedName() {
511     return this.getTypeQualifiedName('$');
512 }
513 /**
514  * @see IType#getTypeQualifiedName(char)
515  */

516 public String JavaDoc getTypeQualifiedName(char enclosingTypeSeparator) {
517     try {
518         return getTypeQualifiedName(enclosingTypeSeparator, false/*don't show parameters*/);
519     } catch (JavaModelException e) {
520         // exception thrown only when showing parameters
521
return null;
522     }
523 }
524
525 /**
526  * @see IType
527  */

528 public IType[] getTypes() throws JavaModelException {
529     ArrayList JavaDoc list= getChildrenOfType(TYPE);
530     IType[] array= new IType[list.size()];
531     list.toArray(array);
532     return array;
533 }
534 /**
535  * @see IType#isAnonymous()
536  */

537 public boolean isAnonymous() {
538     return this.name.length() == 0;
539 }
540
541 /**
542  * @see IType
543  */

544 public boolean isClass() throws JavaModelException {
545     SourceTypeElementInfo info = (SourceTypeElementInfo) getElementInfo();
546     return TypeDeclaration.kind(info.getModifiers()) == TypeDeclaration.CLASS_DECL;
547 }
548
549 /**
550  * @see IType#isEnum()
551  * @since 3.0
552  */

553 public boolean isEnum() throws JavaModelException {
554     SourceTypeElementInfo info = (SourceTypeElementInfo) getElementInfo();
555     return TypeDeclaration.kind(info.getModifiers()) == TypeDeclaration.ENUM_DECL;
556 }
557
558 /**
559  * @see IType
560  */

561 public boolean isInterface() throws JavaModelException {
562     SourceTypeElementInfo info = (SourceTypeElementInfo) getElementInfo();
563     switch (TypeDeclaration.kind(info.getModifiers())) {
564         case TypeDeclaration.INTERFACE_DECL:
565         case TypeDeclaration.ANNOTATION_TYPE_DECL: // annotation is interface too
566
return true;
567     }
568     return false;
569 }
570
571 /**
572  * @see IType#isAnnotation()
573  * @since 3.0
574  */

575 public boolean isAnnotation() throws JavaModelException {
576     SourceTypeElementInfo info = (SourceTypeElementInfo) getElementInfo();
577     return TypeDeclaration.kind(info.getModifiers()) == TypeDeclaration.ANNOTATION_TYPE_DECL;
578 }
579
580 /**
581  * @see IType#isLocal()
582  */

583 public boolean isLocal() {
584     switch (this.parent.getElementType()) {
585         case IJavaElement.METHOD:
586         case IJavaElement.INITIALIZER:
587         case IJavaElement.FIELD:
588             return true;
589         default:
590             return false;
591     }
592 }
593 /**
594  * @see IType#isMember()
595  */

596 public boolean isMember() {
597     return getDeclaringType() != null;
598 }
599 /* (non-Javadoc)
600  * @see org.eclipse.jdt.core.IType#isResolved()
601  */

602 public boolean isResolved() {
603     return false;
604 }
605 /**
606  * @see IType
607  */

608 public ITypeHierarchy loadTypeHierachy(InputStream JavaDoc input, IProgressMonitor monitor) throws JavaModelException {
609     return loadTypeHierachy(input, DefaultWorkingCopyOwner.PRIMARY, monitor);
610 }
611 /**
612  * NOTE: This method is not part of the API has it is not clear clients would easily use it: they would need to
613  * first make sure all working copies for the given owner exist before calling it. This is especially har at startup
614  * time.
615  * In case clients want this API, here is how it should be specified:
616  * <p>
617  * Loads a previously saved ITypeHierarchy from an input stream. A type hierarchy can
618  * be stored using ITypeHierachy#store(OutputStream). A compilation unit of a
619  * loaded type has the given owner if such a working copy exists, otherwise the type's
620  * compilation unit is a primary compilation unit.
621  *
622  * Only hierarchies originally created by the following methods can be loaded:
623  * <ul>
624  * <li>IType#newSupertypeHierarchy(IProgressMonitor)</li>
625  * <li>IType#newSupertypeHierarchy(WorkingCopyOwner, IProgressMonitor)</li>
626  * <li>IType#newTypeHierarchy(IJavaProject, IProgressMonitor)</li>
627  * <li>IType#newTypeHierarchy(IJavaProject, WorkingCopyOwner, IProgressMonitor)</li>
628  * <li>IType#newTypeHierarchy(IProgressMonitor)</li>
629  * <li>IType#newTypeHierarchy(WorkingCopyOwner, IProgressMonitor)</li>
630  * </u>
631  *
632  * @param input stream where hierarchy will be read
633  * @param monitor the given progress monitor
634  * @return the stored hierarchy
635  * @exception JavaModelException if the hierarchy could not be restored, reasons include:
636  * - type is not the focus of the hierarchy or
637  * - unable to read the input stream (wrong format, IOException during reading, ...)
638  * @see ITypeHierarchy#store(java.io.OutputStream, IProgressMonitor)
639  * @since 3.0
640  */

641 public ITypeHierarchy loadTypeHierachy(InputStream JavaDoc input, WorkingCopyOwner owner, IProgressMonitor monitor) throws JavaModelException {
642     // TODO monitor should be passed to TypeHierarchy.load(...)
643
return TypeHierarchy.load(this, input, owner);
644 }
645 /**
646  * @see IType
647  */

648 public ITypeHierarchy newSupertypeHierarchy(IProgressMonitor monitor) throws JavaModelException {
649     return this.newSupertypeHierarchy(DefaultWorkingCopyOwner.PRIMARY, monitor);
650 }
651 /*
652  * @see IType#newSupertypeHierarchy(ICompilationUnit[], IProgressMonitor)
653  */

654 public ITypeHierarchy newSupertypeHierarchy(
655     ICompilationUnit[] workingCopies,
656     IProgressMonitor monitor)
657     throws JavaModelException {
658
659     CreateTypeHierarchyOperation op= new CreateTypeHierarchyOperation(this, workingCopies, SearchEngine.createWorkspaceScope(), false);
660     op.runOperation(monitor);
661     return op.getResult();
662 }
663 /**
664  * @param workingCopies the working copies that take precedence over their original compilation units
665  * @param monitor the given progress monitor
666  * @return a type hierarchy for this type containing this type and all of its supertypes
667  * @exception JavaModelException if this element does not exist or if an
668  * exception occurs while accessing its corresponding resource.
669  *
670  * @see IType#newSupertypeHierarchy(IWorkingCopy[], IProgressMonitor)
671  * @deprecated
672  */

673 public ITypeHierarchy newSupertypeHierarchy(
674     IWorkingCopy[] workingCopies,
675     IProgressMonitor monitor)
676     throws JavaModelException {
677
678     ICompilationUnit[] copies;
679     if (workingCopies == null) {
680         copies = null;
681     } else {
682         int length = workingCopies.length;
683         System.arraycopy(workingCopies, 0, copies = new ICompilationUnit[length], 0, length);
684     }
685     return newSupertypeHierarchy(copies, monitor);
686 }
687 /**
688  * @see IType#newSupertypeHierarchy(WorkingCopyOwner, IProgressMonitor)
689  */

690 public ITypeHierarchy newSupertypeHierarchy(
691     WorkingCopyOwner owner,
692     IProgressMonitor monitor)
693     throws JavaModelException {
694
695     ICompilationUnit[] workingCopies = JavaModelManager.getJavaModelManager().getWorkingCopies(owner, true/*add primary working copies*/);
696     CreateTypeHierarchyOperation op= new CreateTypeHierarchyOperation(this, workingCopies, SearchEngine.createWorkspaceScope(), false);
697     op.runOperation(monitor);
698     return op.getResult();
699 }
700 /**
701  * @see IType
702  */

703 public ITypeHierarchy newTypeHierarchy(IJavaProject project, IProgressMonitor monitor) throws JavaModelException {
704     return newTypeHierarchy(project, DefaultWorkingCopyOwner.PRIMARY, monitor);
705 }
706 /**
707  * @see IType#newTypeHierarchy(IJavaProject, WorkingCopyOwner, IProgressMonitor)
708  */

709 public ITypeHierarchy newTypeHierarchy(IJavaProject project, WorkingCopyOwner owner, IProgressMonitor monitor) throws JavaModelException {
710     if (project == null) {
711         throw new IllegalArgumentException JavaDoc(Messages.hierarchy_nullProject);
712     }
713     ICompilationUnit[] workingCopies = JavaModelManager.getJavaModelManager().getWorkingCopies(owner, true/*add primary working copies*/);
714     ICompilationUnit[] projectWCs = null;
715     if (workingCopies != null) {
716         int length = workingCopies.length;
717         projectWCs = new ICompilationUnit[length];
718         int index = 0;
719         for (int i = 0; i < length; i++) {
720             ICompilationUnit wc = workingCopies[i];
721             if (project.equals(wc.getJavaProject())) {
722                 projectWCs[index++] = wc;
723             }
724         }
725         if (index != length) {
726             System.arraycopy(projectWCs, 0, projectWCs = new ICompilationUnit[index], 0, index);
727         }
728     }
729     CreateTypeHierarchyOperation op= new CreateTypeHierarchyOperation(
730         this,
731         projectWCs,
732         project,
733         true);
734     op.runOperation(monitor);
735     return op.getResult();
736 }
737 /**
738  * @see IType
739  */

740 public ITypeHierarchy newTypeHierarchy(IProgressMonitor monitor) throws JavaModelException {
741     CreateTypeHierarchyOperation op= new CreateTypeHierarchyOperation(this, null, SearchEngine.createWorkspaceScope(), true);
742     op.runOperation(monitor);
743     return op.getResult();
744 }
745 /*
746  * @see IType#newTypeHierarchy(ICompilationUnit[], IProgressMonitor)
747  */

748 public ITypeHierarchy newTypeHierarchy(
749     ICompilationUnit[] workingCopies,
750     IProgressMonitor monitor)
751     throws JavaModelException {
752         
753     CreateTypeHierarchyOperation op= new CreateTypeHierarchyOperation(this, workingCopies, SearchEngine.createWorkspaceScope(), true);
754     op.runOperation(monitor);
755     return op.getResult();
756 }
757 /**
758  * @see IType#newTypeHierarchy(IWorkingCopy[], IProgressMonitor)
759  * @deprecated
760  */

761 public ITypeHierarchy newTypeHierarchy(
762     IWorkingCopy[] workingCopies,
763     IProgressMonitor monitor)
764     throws JavaModelException {
765         
766     ICompilationUnit[] copies;
767     if (workingCopies == null) {
768         copies = null;
769     } else {
770         int length = workingCopies.length;
771         System.arraycopy(workingCopies, 0, copies = new ICompilationUnit[length], 0, length);
772     }
773     return newTypeHierarchy(copies, monitor);
774 }
775 /**
776  * @see IType#newTypeHierarchy(WorkingCopyOwner, IProgressMonitor)
777  */

778 public ITypeHierarchy newTypeHierarchy(
779     WorkingCopyOwner owner,
780     IProgressMonitor monitor)
781     throws JavaModelException {
782         
783     ICompilationUnit[] workingCopies = JavaModelManager.getJavaModelManager().getWorkingCopies(owner, true/*add primary working copies*/);
784     CreateTypeHierarchyOperation op= new CreateTypeHierarchyOperation(this, workingCopies, SearchEngine.createWorkspaceScope(), true);
785     op.runOperation(monitor);
786     return op.getResult();
787 }
788 public JavaElement resolved(Binding binding) {
789     SourceRefElement resolvedHandle = new ResolvedSourceType(this.parent, this.name, new String JavaDoc(binding.computeUniqueKey()));
790     resolvedHandle.occurrenceCount = this.occurrenceCount;
791     return resolvedHandle;
792 }
793 /**
794  * @see IType#resolveType(String)
795  */

796 public String JavaDoc[][] resolveType(String JavaDoc typeName) throws JavaModelException {
797     return resolveType(typeName, DefaultWorkingCopyOwner.PRIMARY);
798 }
799 /**
800  * @see IType#resolveType(String, WorkingCopyOwner)
801  */

802 public String JavaDoc[][] resolveType(String JavaDoc typeName, WorkingCopyOwner owner) throws JavaModelException {
803     ISourceType info = (ISourceType) getElementInfo();
804     JavaProject project = (JavaProject) getJavaProject();
805     SearchableEnvironment environment = project.newSearchableNameEnvironment(owner);
806
807     class TypeResolveRequestor implements ISelectionRequestor {
808         String JavaDoc[][] answers = null;
809         public void acceptType(char[] packageName, char[] tName, int modifiers, boolean isDeclaration, char[] uniqueKey, int start, int end) {
810             String JavaDoc[] answer = new String JavaDoc[] {new String JavaDoc(packageName), new String JavaDoc(tName) };
811             if (this.answers == null) {
812                 this.answers = new String JavaDoc[][]{ answer };
813             } else {
814                 // grow
815
int length = this.answers.length;
816                 System.arraycopy(this.answers, 0, this.answers = new String JavaDoc[length+1][], 0, length);
817                 this.answers[length] = answer;
818             }
819         }
820         public void acceptError(CategorizedProblem error) {
821             // ignore
822
}
823         public void acceptField(char[] declaringTypePackageName, char[] declaringTypeName, char[] fieldName, boolean isDeclaration, char[] uniqueKey, int start, int end) {
824             // ignore
825
}
826         public void acceptMethod(char[] declaringTypePackageName, char[] declaringTypeName, String JavaDoc enclosingDeclaringTypeSignature, char[] selector, char[][] parameterPackageNames, char[][] parameterTypeNames, String JavaDoc[] parameterSignatures, char[][] typeParameterNames, char[][][] typeParameterBoundNames, boolean isConstructor, boolean isDeclaration, char[] uniqueKey, int start, int end) {
827             // ignore
828
}
829         public void acceptPackage(char[] packageName){
830             // ignore
831
}
832         public void acceptTypeParameter(char[] declaringTypePackageName, char[] declaringTypeName, char[] typeParameterName, boolean isDeclaration, int start, int end) {
833             // ignore
834
}
835         public void acceptMethodTypeParameter(char[] declaringTypePackageName, char[] declaringTypeName, char[] selector, int selectorStart, int selcetorEnd, char[] typeParameterName, boolean isDeclaration, int start, int end) {
836             // ignore
837
}
838
839     }
840     TypeResolveRequestor requestor = new TypeResolveRequestor();
841     SelectionEngine engine =
842         new SelectionEngine(environment, requestor, project.getOptions(true));
843         
844     IType[] topLevelTypes = getCompilationUnit().getTypes();
845     int length = topLevelTypes.length;
846     SourceTypeElementInfo[] topLevelInfos = new SourceTypeElementInfo[length];
847     for (int i = 0; i < length; i++) {
848         topLevelInfos[i] = (SourceTypeElementInfo) ((SourceType)topLevelTypes[i]).getElementInfo();
849     }
850         
851     engine.selectType(info, typeName.toCharArray(), topLevelInfos, false);
852     if (NameLookup.VERBOSE) {
853         System.out.println(Thread.currentThread() + " TIME SPENT in NameLoopkup#seekTypesInSourcePackage: " + environment.nameLookup.timeSpentInSeekTypesInSourcePackage + "ms"); //$NON-NLS-1$ //$NON-NLS-2$
854
System.out.println(Thread.currentThread() + " TIME SPENT in NameLoopkup#seekTypesInBinaryPackage: " + environment.nameLookup.timeSpentInSeekTypesInBinaryPackage + "ms"); //$NON-NLS-1$ //$NON-NLS-2$
855
}
856     return requestor.answers;
857 }
858 /**
859  * @private Debugging purposes
860  */

861 protected void toStringInfo(int tab, StringBuffer JavaDoc buffer, Object JavaDoc info, boolean showResolvedInfo) {
862     buffer.append(tabString(tab));
863     if (info == null) {
864         String JavaDoc elementName = getElementName();
865         if (elementName.length() == 0) {
866             buffer.append("<anonymous #"); //$NON-NLS-1$
867
buffer.append(this.occurrenceCount);
868             buffer.append(">"); //$NON-NLS-1$
869
} else {
870             toStringName(buffer);
871         }
872         buffer.append(" (not open)"); //$NON-NLS-1$
873
} else if (info == NO_INFO) {
874         String JavaDoc elementName = getElementName();
875         if (elementName.length() == 0) {
876             buffer.append("<anonymous #"); //$NON-NLS-1$
877
buffer.append(this.occurrenceCount);
878             buffer.append(">"); //$NON-NLS-1$
879
} else {
880             toStringName(buffer);
881         }
882     } else {
883         try {
884             if (this.isEnum()) {
885                 buffer.append("enum "); //$NON-NLS-1$
886
} else if (this.isAnnotation()) {
887                 buffer.append("@interface "); //$NON-NLS-1$
888
} else if (this.isInterface()) {
889                 buffer.append("interface "); //$NON-NLS-1$
890
} else {
891                 buffer.append("class "); //$NON-NLS-1$
892
}
893             String JavaDoc elementName = getElementName();
894             if (elementName.length() == 0) {
895                 buffer.append("<anonymous #"); //$NON-NLS-1$
896
buffer.append(this.occurrenceCount);
897                 buffer.append(">"); //$NON-NLS-1$
898
} else {
899                 toStringName(buffer);
900             }
901         } catch (JavaModelException e) {
902             buffer.append("<JavaModelException in toString of " + getElementName()); //$NON-NLS-1$
903
}
904     }
905 }
906 }
907
Popular Tags