KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.net.URL JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.HashMap JavaDoc;
17
18 import org.eclipse.core.runtime.Assert;
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.core.runtime.OperationCanceledException;
21 import org.eclipse.jdt.core.*;
22 import org.eclipse.jdt.core.compiler.CharOperation;
23 import org.eclipse.jdt.core.search.SearchEngine;
24 import org.eclipse.jdt.internal.codeassist.CompletionEngine;
25 import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
26 import org.eclipse.jdt.internal.compiler.env.IBinaryType;
27 import org.eclipse.jdt.internal.compiler.lookup.Binding;
28 import org.eclipse.jdt.internal.compiler.util.SuffixConstants;
29 import org.eclipse.jdt.internal.core.JavaModelManager.PerProjectInfo;
30 import org.eclipse.jdt.internal.core.hierarchy.TypeHierarchy;
31 import org.eclipse.jdt.internal.core.util.MementoTokenizer;
32 import org.eclipse.jdt.internal.core.util.Messages;
33 import org.eclipse.jdt.internal.core.util.Util;
34
35 /**
36  * Parent is an IClassFile.
37  *
38  * @see IType
39  */

40
41 public class BinaryType extends BinaryMember implements IType, SuffixConstants {
42     
43     private static final IField[] NO_FIELDS = new IField[0];
44     private static final IMethod[] NO_METHODS = new IMethod[0];
45     private static final IType[] NO_TYPES = new IType[0];
46     private static final IInitializer[] NO_INITIALIZERS = new IInitializer[0];
47     public static final String JavaDoc EMPTY_JAVADOC = org.eclipse.jdt.internal.compiler.util.Util.EMPTY_STRING;
48     
49 protected BinaryType(JavaElement parent, String JavaDoc name) {
50     super(parent, name);
51 }
52 /*
53  * Remove my cached children from the Java Model
54  */

55 protected void closing(Object JavaDoc info) throws JavaModelException {
56     ClassFileInfo cfi = getClassFileInfo();
57     cfi.removeBinaryChildren();
58 }
59
60 /**
61  * @see IType#codeComplete(char[], int, int, char[][], char[][], int[], boolean, ICompletionRequestor)
62  * @deprecated
63  */

64 public void codeComplete(char[] snippet,int insertion,int position,char[][] localVariableTypeNames,char[][] localVariableNames,int[] localVariableModifiers,boolean isStatic,ICompletionRequestor requestor) throws JavaModelException {
65     codeComplete(snippet, insertion, position, localVariableTypeNames, localVariableNames, localVariableModifiers, isStatic, requestor, DefaultWorkingCopyOwner.PRIMARY);
66 }
67
68 /**
69  * @see IType#codeComplete(char[], int, int, char[][], char[][], int[], boolean, ICompletionRequestor, WorkingCopyOwner)
70  * @deprecated
71  */

72 public void codeComplete(char[] snippet,int insertion,int position,char[][] localVariableTypeNames,char[][] localVariableNames,int[] localVariableModifiers,boolean isStatic,ICompletionRequestor requestor, WorkingCopyOwner owner) throws JavaModelException {
73     if (requestor == null) {
74         throw new IllegalArgumentException JavaDoc("Completion requestor cannot be null"); //$NON-NLS-1$
75
}
76     codeComplete(snippet, insertion, position, localVariableTypeNames, localVariableNames, localVariableModifiers, isStatic, new org.eclipse.jdt.internal.codeassist.CompletionRequestorWrapper(requestor), owner);
77 }
78 /*
79  * @see IType#codeComplete(char[], int, int, char[][], char[][], int[], boolean, ICompletionRequestor)
80  */

81 public void codeComplete(char[] snippet,int insertion,int position,char[][] localVariableTypeNames,char[][] localVariableNames,int[] localVariableModifiers,boolean isStatic,CompletionRequestor requestor) throws JavaModelException {
82     codeComplete(snippet, insertion, position, localVariableTypeNames, localVariableNames, localVariableModifiers, isStatic, requestor, DefaultWorkingCopyOwner.PRIMARY);
83 }
84
85 /*
86  * @see IType#codeComplete(char[], int, int, char[][], char[][], int[], boolean, ICompletionRequestor, WorkingCopyOwner)
87  */

88 public void codeComplete(char[] snippet,int insertion,int position,char[][] localVariableTypeNames,char[][] localVariableNames,int[] localVariableModifiers,boolean isStatic,CompletionRequestor requestor, WorkingCopyOwner owner) throws JavaModelException {
89     if (requestor == null) {
90         throw new IllegalArgumentException JavaDoc("Completion requestor cannot be null"); //$NON-NLS-1$
91
}
92     JavaProject project = (JavaProject) getJavaProject();
93     SearchableEnvironment environment = project.newSearchableNameEnvironment(owner);
94     CompletionEngine engine = new CompletionEngine(environment, requestor, project.getOptions(true), project);
95
96     String JavaDoc source = getClassFile().getSource();
97     if (source != null && insertion > -1 && insertion < source.length()) {
98         // code complete
99

100         char[] prefix = CharOperation.concat(source.substring(0, insertion).toCharArray(), new char[]{'{'});
101         char[] suffix = CharOperation.concat(new char[]{'}'}, source.substring(insertion).toCharArray());
102         char[] fakeSource = CharOperation.concat(prefix, snippet, suffix);
103         
104         BasicCompilationUnit cu =
105             new BasicCompilationUnit(
106                 fakeSource,
107                 null,
108                 getElementName(),
109                 project); // use project to retrieve corresponding .java IFile
110

111         engine.complete(cu, prefix.length + position, prefix.length);
112     } else {
113         engine.complete(this, snippet, position, localVariableTypeNames, localVariableNames, localVariableModifiers, isStatic);
114     }
115     if (NameLookup.VERBOSE) {
116         System.out.println(Thread.currentThread() + " TIME SPENT in NameLoopkup#seekTypesInSourcePackage: " + environment.nameLookup.timeSpentInSeekTypesInSourcePackage + "ms"); //$NON-NLS-1$ //$NON-NLS-2$
117
System.out.println(Thread.currentThread() + " TIME SPENT in NameLoopkup#seekTypesInBinaryPackage: " + environment.nameLookup.timeSpentInSeekTypesInBinaryPackage + "ms"); //$NON-NLS-1$ //$NON-NLS-2$
118
}
119 }
120
121 /*
122  * @see IType#createField(String, IJavaElement, boolean, IProgressMonitor)
123  */

124 public IField createField(String JavaDoc contents, IJavaElement sibling, boolean force, IProgressMonitor monitor) throws JavaModelException {
125     throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.READ_ONLY, this));
126 }
127 /*
128  * @see IType#createInitializer(String, IJavaElement, IProgressMonitor)
129  */

130 public IInitializer createInitializer(String JavaDoc contents, IJavaElement sibling, IProgressMonitor monitor) throws JavaModelException {
131     throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.READ_ONLY, this));
132 }
133 /*
134  * @see IType#createMethod(String, IJavaElement, boolean, IProgressMonitor)
135  */

136 public IMethod createMethod(String JavaDoc contents, IJavaElement sibling, boolean force, IProgressMonitor monitor) throws JavaModelException {
137     throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.READ_ONLY, this));
138 }
139 /*
140  * @see IType#createType(String, IJavaElement, boolean, IProgressMonitor)
141  */

142 public IType createType(String JavaDoc contents, IJavaElement sibling, boolean force, IProgressMonitor monitor) throws JavaModelException {
143     throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.READ_ONLY, this));
144 }
145 public boolean equals(Object JavaDoc o) {
146     if (!(o instanceof BinaryType)) return false;
147     return super.equals(o);
148 }
149 /*
150  * @see IType#findMethods(IMethod)
151  */

152 public IMethod[] findMethods(IMethod method) {
153     try {
154         return findMethods(method, getMethods());
155     } catch (JavaModelException e) {
156         // if type doesn't exist, no matching method can exist
157
return null;
158     }
159 }
160 /*
161  * @see IParent#getChildren()
162  */

163 public IJavaElement[] getChildren() throws JavaModelException {
164     ClassFileInfo cfi = getClassFileInfo();
165     return cfi.binaryChildren;
166 }
167 public IJavaElement[] getChildrenForCategory(String JavaDoc category) throws JavaModelException {
168     IJavaElement[] children = getChildren();
169     int length = children.length;
170     if (length == 0) return children;
171     SourceMapper mapper= getSourceMapper();
172     if (mapper != null) {
173         // ensure the class file's buffer is open so that categories are computed
174
((ClassFile)getClassFile()).getBuffer();
175         
176         HashMap JavaDoc categories = mapper.categories;
177         IJavaElement[] result = new IJavaElement[length];
178         int index = 0;
179         if (categories != null) {
180             for (int i = 0; i < length; i++) {
181                 IJavaElement child = children[i];
182                 String JavaDoc[] cats = (String JavaDoc[]) categories.get(child);
183                 if (cats != null) {
184                     for (int j = 0, length2 = cats.length; j < length2; j++) {
185                         if (cats[j].equals(category)) {
186                             result[index++] = child;
187                             break;
188                         }
189                     }
190                 }
191             }
192         }
193         if (index < length)
194             System.arraycopy(result, 0, result = new IJavaElement[index], 0, index);
195         return result;
196     }
197     return NO_ELEMENTS;
198 }
199 protected ClassFileInfo getClassFileInfo() throws JavaModelException {
200     ClassFile cf = (ClassFile)this.parent;
201     return (ClassFileInfo) cf.getElementInfo();
202 }
203 /*
204  * @see IMember#getDeclaringType()
205  */

206 public IType getDeclaringType() {
207     IClassFile classFile = this.getClassFile();
208     if (classFile.isOpen()) {
209         try {
210             char[] enclosingTypeName = ((IBinaryType) getElementInfo()).getEnclosingTypeName();
211             if (enclosingTypeName == null) {
212                 return null;
213             }
214             enclosingTypeName = ClassFile.unqualifiedName(enclosingTypeName);
215             
216             // workaround problem with class files compiled with javac 1.1.*
217
// that return a non-null enclosing type name for local types defined in anonymous (e.g. A$1$B)
218
if (classFile.getElementName().length() > enclosingTypeName.length+1
219                     && Character.isDigit(classFile.getElementName().charAt(enclosingTypeName.length+1))) {
220                 return null;
221             }
222             
223             return getPackageFragment().getClassFile(new String JavaDoc(enclosingTypeName) + SUFFIX_STRING_class).getType();
224         } catch (JavaModelException npe) {
225             return null;
226         }
227     } else {
228         // cannot access .class file without opening it
229
// and getDeclaringType() is supposed to be a handle-only method,
230
// so default to assuming $ is an enclosing type separator
231
String JavaDoc classFileName = classFile.getElementName();
232         int lastDollar = -1;
233         for (int i = 0, length = classFileName.length(); i < length; i++) {
234             char c = classFileName.charAt(i);
235             if (Character.isDigit(c) && lastDollar == i-1) {
236                 // anonymous or local type
237
return null;
238             } else if (c == '$') {
239                 lastDollar = i;
240             }
241         }
242         if (lastDollar == -1) {
243             return null;
244         } else {
245             String JavaDoc enclosingName = classFileName.substring(0, lastDollar);
246             String JavaDoc enclosingClassFileName = enclosingName + SUFFIX_STRING_class;
247             return
248                 new BinaryType(
249                     (JavaElement)this.getPackageFragment().getClassFile(enclosingClassFileName),
250                     Util.localTypeName(enclosingName, enclosingName.lastIndexOf('$'), enclosingName.length()));
251         }
252     }
253 }
254 public Object JavaDoc getElementInfo(IProgressMonitor monitor) throws JavaModelException {
255     JavaModelManager manager = JavaModelManager.getJavaModelManager();
256     Object JavaDoc info = manager.getInfo(this);
257     if (info != null && info != JavaModelCache.NON_EXISTING_JAR_TYPE_INFO) return info;
258     return openWhenClosed(createElementInfo(), monitor);
259 }
260 /*
261  * @see IJavaElement
262  */

263 public int getElementType() {
264     return TYPE;
265 }
266 /*
267  * @see IType#getField(String name)
268  */

269 public IField getField(String JavaDoc fieldName) {
270     return new BinaryField(this, fieldName);
271 }
272 /*
273  * @see IType#getFields()
274  */

275 public IField[] getFields() throws JavaModelException {
276     ArrayList JavaDoc list = getChildrenOfType(FIELD);
277     int size;
278     if ((size = list.size()) == 0) {
279         return NO_FIELDS;
280     } else {
281         IField[] array= new IField[size];
282         list.toArray(array);
283         return array;
284     }
285 }
286 /*
287  * @see IMember#getFlags()
288  */

289 public int getFlags() throws JavaModelException {
290     IBinaryType info = (IBinaryType) getElementInfo();
291     return info.getModifiers();
292 }
293 /*
294  * @see IType#getFullyQualifiedName()
295  */

296 public String JavaDoc getFullyQualifiedName() {
297     return this.getFullyQualifiedName('$');
298 }
299 /*
300  * @see IType#getFullyQualifiedName(char enclosingTypeSeparator)
301  */

302 public String JavaDoc getFullyQualifiedName(char enclosingTypeSeparator) {
303     try {
304         return getFullyQualifiedName(enclosingTypeSeparator, false/*don't show parameters*/);
305     } catch (JavaModelException e) {
306         // exception thrown only when showing parameters
307
return null;
308     }
309 }
310
311 /*
312  * @see IType#getFullyQualifiedParameterizedName()
313  */

314 public String JavaDoc getFullyQualifiedParameterizedName() throws JavaModelException {
315     return getFullyQualifiedName('.', true/*show parameters*/);
316 }
317
318 /*
319  * @see JavaElement
320  */

321 public IJavaElement getHandleFromMemento(String JavaDoc token, MementoTokenizer memento, WorkingCopyOwner workingCopyOwner) {
322     switch (token.charAt(0)) {
323         case JEM_COUNT:
324             return getHandleUpdatingCountFromMemento(memento, workingCopyOwner);
325         case JEM_FIELD:
326             if (!memento.hasMoreTokens()) return this;
327             String JavaDoc fieldName = memento.nextToken();
328             JavaElement field = (JavaElement)getField(fieldName);
329             return field.getHandleFromMemento(memento, workingCopyOwner);
330         case JEM_INITIALIZER:
331             if (!memento.hasMoreTokens()) return this;
332             String JavaDoc count = memento.nextToken();
333             JavaElement initializer = (JavaElement)getInitializer(Integer.parseInt(count));
334             return initializer.getHandleFromMemento(memento, workingCopyOwner);
335         case JEM_METHOD:
336             if (!memento.hasMoreTokens()) return this;
337             String JavaDoc selector = memento.nextToken();
338             ArrayList JavaDoc params = new ArrayList JavaDoc();
339             nextParam: while (memento.hasMoreTokens()) {
340                 token = memento.nextToken();
341                 switch (token.charAt(0)) {
342                     case JEM_TYPE:
343                     case JEM_TYPE_PARAMETER:
344                         break nextParam;
345                     case JEM_METHOD:
346                         if (!memento.hasMoreTokens()) return this;
347                         String JavaDoc param = memento.nextToken();
348                         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
349                         while (param.length() == 1 && Signature.C_ARRAY == param.charAt(0)) { // backward compatible with 3.0 mementos
350
buffer.append(Signature.C_ARRAY);
351                             if (!memento.hasMoreTokens()) return this;
352                             param = memento.nextToken();
353                         }
354                         params.add(buffer.toString() + param);
355                         break;
356                     default:
357                         break nextParam;
358                 }
359             }
360             String JavaDoc[] parameters = new String JavaDoc[params.size()];
361             params.toArray(parameters);
362             JavaElement method = (JavaElement)getMethod(selector, parameters);
363             switch (token.charAt(0)) {
364                 case JEM_TYPE:
365                 case JEM_TYPE_PARAMETER:
366                 case JEM_LOCALVARIABLE:
367                     return method.getHandleFromMemento(token, memento, workingCopyOwner);
368                 default:
369                     return method;
370             }
371         case JEM_TYPE:
372             String JavaDoc typeName;
373             if (memento.hasMoreTokens()) {
374                 typeName = memento.nextToken();
375                 char firstChar = typeName.charAt(0);
376                 if (firstChar == JEM_FIELD || firstChar == JEM_INITIALIZER || firstChar == JEM_METHOD || firstChar == JEM_TYPE || firstChar == JEM_COUNT) {
377                     token = typeName;
378                     typeName = ""; //$NON-NLS-1$
379
} else {
380                     token = null;
381                 }
382             } else {
383                 typeName = ""; //$NON-NLS-1$
384
token = null;
385             }
386             JavaElement type = (JavaElement)getType(typeName);
387             if (token == null) {
388                 return type.getHandleFromMemento(memento, workingCopyOwner);
389             } else {
390                 return type.getHandleFromMemento(token, memento, workingCopyOwner);
391             }
392         case JEM_TYPE_PARAMETER:
393             if (!memento.hasMoreTokens()) return this;
394             String JavaDoc typeParameterName = memento.nextToken();
395             JavaElement typeParameter = new TypeParameter(this, typeParameterName);
396             return typeParameter.getHandleFromMemento(memento, workingCopyOwner);
397     }
398     return null;
399 }
400 /*
401  * @see IType#getInitializer(int occurrenceCount)
402  */

403 public IInitializer getInitializer(int count) {
404     return new Initializer(this, count);
405 }
406 /*
407  * @see IType#getInitializers()
408  */

409 public IInitializer[] getInitializers() {
410     return NO_INITIALIZERS;
411 }
412 public String JavaDoc getKey(boolean forceOpen) throws JavaModelException {
413     return getKey(this, forceOpen);
414 }
415 /*
416  * @see IType#getMethod(String name, String[] parameterTypeSignatures)
417  */

418 public IMethod getMethod(String JavaDoc selector, String JavaDoc[] parameterTypeSignatures) {
419     return new BinaryMethod(this, selector, parameterTypeSignatures);
420 }
421 /*
422  * @see IType#getMethods()
423  */

424 public IMethod[] getMethods() throws JavaModelException {
425     ArrayList JavaDoc list = getChildrenOfType(METHOD);
426     int size;
427     if ((size = list.size()) == 0) {
428         return NO_METHODS;
429     } else {
430         IMethod[] array= new IMethod[size];
431         list.toArray(array);
432         return array;
433     }
434 }
435 /*
436  * @see IType#getPackageFragment()
437  */

438 public IPackageFragment getPackageFragment() {
439     IJavaElement parentElement = this.parent;
440     while (parentElement != null) {
441         if (parentElement.getElementType() == IJavaElement.PACKAGE_FRAGMENT) {
442             return (IPackageFragment)parentElement;
443         }
444         else {
445             parentElement = parentElement.getParent();
446         }
447     }
448     Assert.isTrue(false); // should not happen
449
return null;
450 }
451
452 /**
453  * @see IType#getSuperclassTypeSignature()
454  * @since 3.0
455  */

456 public String JavaDoc getSuperclassTypeSignature() throws JavaModelException {
457     IBinaryType info = (IBinaryType) getElementInfo();
458     char[] genericSignature = info.getGenericSignature();
459     if (genericSignature != null) {
460         int signatureLength = genericSignature.length;
461         // skip type parameters
462
int index = 0;
463         if (genericSignature[0] == '<') {
464             int count = 1;
465             while (count > 0 && ++index < signatureLength) {
466                 switch (genericSignature[index]) {
467                     case '<':
468                         count++;
469                         break;
470                     case '>':
471                         count--;
472                         break;
473                 }
474             }
475             index++;
476         }
477         int start = index;
478         index = Util.scanClassTypeSignature(genericSignature, start) + 1;
479         char[] superclassSig = CharOperation.subarray(genericSignature, start, index);
480         return new String JavaDoc(ClassFile.translatedName(superclassSig));
481     } else {
482         char[] superclassName = info.getSuperclassName();
483         if (superclassName == null) {
484             return null;
485         }
486         return new String JavaDoc(Signature.createTypeSignature(ClassFile.translatedName(superclassName), true));
487     }
488 }
489
490 public String JavaDoc getSourceFileName(IBinaryType info) {
491     if (info == null) {
492         try {
493             info = (IBinaryType) getElementInfo();
494         } catch (JavaModelException e) {
495             // default to using the outer most declaring type name
496
IType type = this;
497             IType enclosingType = getDeclaringType();
498             while (enclosingType != null) {
499                 type = enclosingType;
500                 enclosingType = type.getDeclaringType();
501             }
502             return type.getElementName() + Util.defaultJavaExtension();
503         }
504     }
505     return sourceFileName(info);
506 }
507
508 /*
509  * @see IType#getSuperclassName()
510  */

511 public String JavaDoc getSuperclassName() throws JavaModelException {
512     IBinaryType info = (IBinaryType) getElementInfo();
513     char[] superclassName = info.getSuperclassName();
514     if (superclassName == null) {
515         return null;
516     }
517     return new String JavaDoc(ClassFile.translatedName(superclassName));
518 }
519 /*
520  * @see IType#getSuperInterfaceNames()
521  */

522 public String JavaDoc[] getSuperInterfaceNames() throws JavaModelException {
523     IBinaryType info = (IBinaryType) getElementInfo();
524     char[][] names= info.getInterfaceNames();
525     int length;
526     if (names == null || (length = names.length) == 0) {
527         return CharOperation.NO_STRINGS;
528     }
529     names= ClassFile.translatedNames(names);
530     String JavaDoc[] strings= new String JavaDoc[length];
531     for (int i= 0; i < length; i++) {
532         strings[i]= new String JavaDoc(names[i]);
533     }
534     return strings;
535 }
536
537 /**
538  * @see IType#getSuperInterfaceTypeSignatures()
539  * @since 3.0
540  */

541 public String JavaDoc[] getSuperInterfaceTypeSignatures() throws JavaModelException {
542     IBinaryType info = (IBinaryType) getElementInfo();
543     char[] genericSignature = info.getGenericSignature();
544     if (genericSignature != null) {
545         ArrayList JavaDoc interfaces = new ArrayList JavaDoc();
546         int signatureLength = genericSignature.length;
547         // skip type parameters
548
int index = 0;
549         if (genericSignature[0] == '<') {
550             int count = 1;
551             while (count > 0 && ++index < signatureLength) {
552                 switch (genericSignature[index]) {
553                     case '<':
554                         count++;
555                         break;
556                     case '>':
557                         count--;
558                         break;
559                 }
560             }
561             index++;
562         }
563         // skip superclass
564
index = Util.scanClassTypeSignature(genericSignature, index) + 1;
565         while (index < signatureLength) {
566             int start = index;
567             index = Util.scanClassTypeSignature(genericSignature, start) + 1;
568             char[] interfaceSig = CharOperation.subarray(genericSignature, start, index);
569             interfaces.add(new String JavaDoc(ClassFile.translatedName(interfaceSig)));
570         }
571         int size = interfaces.size();
572         String JavaDoc[] result = new String JavaDoc[size];
573         interfaces.toArray(result);
574         return result;
575     } else {
576         char[][] names= info.getInterfaceNames();
577         int length;
578         if (names == null || (length = names.length) == 0) {
579             return CharOperation.NO_STRINGS;
580         }
581         names= ClassFile.translatedNames(names);
582         String JavaDoc[] strings= new String JavaDoc[length];
583         for (int i= 0; i < length; i++) {
584             strings[i]= new String JavaDoc(Signature.createTypeSignature(names[i], true));
585         }
586         return strings;
587     }
588 }
589
590 public ITypeParameter[] getTypeParameters() throws JavaModelException {
591     String JavaDoc[] typeParameterSignatures = getTypeParameterSignatures();
592     int length = typeParameterSignatures.length;
593     if (length == 0) return TypeParameter.NO_TYPE_PARAMETERS;
594     ITypeParameter[] typeParameters = new ITypeParameter[length];
595     for (int i = 0; i < typeParameterSignatures.length; i++) {
596         String JavaDoc typeParameterName = Signature.getTypeVariable(typeParameterSignatures[i]);
597         typeParameters[i] = new TypeParameter(this, typeParameterName);
598     }
599     return typeParameters;
600 }
601
602 /**
603  * @see IType#getTypeParameterSignatures()
604  * @since 3.0
605  */

606 public String JavaDoc[] getTypeParameterSignatures() throws JavaModelException {
607     IBinaryType info = (IBinaryType) getElementInfo();
608     char[] genericSignature = info.getGenericSignature();
609     if (genericSignature == null)
610         return CharOperation.NO_STRINGS;
611     
612     char[] dotBaseSignature = CharOperation.replaceOnCopy(genericSignature, '/', '.');
613     char[][] typeParams = Signature.getTypeParameters(dotBaseSignature);
614     return CharOperation.toStrings(typeParams);
615 }
616
617 /*
618  * @see IType#getType(String)
619  */

620 public IType getType(String JavaDoc typeName) {
621     IClassFile classFile= getPackageFragment().getClassFile(getTypeQualifiedName() + "$" + typeName + SUFFIX_STRING_class); //$NON-NLS-1$
622
return new BinaryType((JavaElement)classFile, typeName);
623 }
624 public ITypeParameter getTypeParameter(String JavaDoc typeParameterName) {
625     return new TypeParameter(this, typeParameterName);
626 }
627 /*
628  * @see IType#getTypeQualifiedName()
629  */

630 public String JavaDoc getTypeQualifiedName() {
631     return this.getTypeQualifiedName('$');
632 }
633 /*
634  * @see IType#getTypeQualifiedName(char)
635  */

636 public String JavaDoc getTypeQualifiedName(char enclosingTypeSeparator) {
637     try {
638         return getTypeQualifiedName(enclosingTypeSeparator, false/*don't show parameters*/);
639     } catch (JavaModelException e) {
640         // exception thrown only when showing parameters
641
return null;
642     }
643 }
644 /*
645  * @see IType#getTypes()
646  */

647 public IType[] getTypes() throws JavaModelException {
648     ArrayList JavaDoc list = getChildrenOfType(TYPE);
649     int size;
650     if ((size = list.size()) == 0) {
651         return NO_TYPES;
652     } else {
653         IType[] array= new IType[size];
654         list.toArray(array);
655         return array;
656     }
657 }
658
659 /*
660  * @see IType#isAnonymous()
661  */

662 public boolean isAnonymous() throws JavaModelException {
663     IBinaryType info = (IBinaryType) getElementInfo();
664     return info.isAnonymous();
665 }
666 /*
667  * @see IType#isClass()
668  */

669 public boolean isClass() throws JavaModelException {
670     IBinaryType info = (IBinaryType) getElementInfo();
671     return TypeDeclaration.kind(info.getModifiers()) == TypeDeclaration.CLASS_DECL;
672
673 }
674
675 /**
676  * @see IType#isEnum()
677  * @since 3.0
678  */

679 public boolean isEnum() throws JavaModelException {
680     IBinaryType info = (IBinaryType) getElementInfo();
681     return TypeDeclaration.kind(info.getModifiers()) == TypeDeclaration.ENUM_DECL;
682 }
683
684 /*
685  * @see IType#isInterface()
686  */

687 public boolean isInterface() throws JavaModelException {
688     IBinaryType info = (IBinaryType) getElementInfo();
689     switch (TypeDeclaration.kind(info.getModifiers())) {
690         case TypeDeclaration.INTERFACE_DECL:
691         case TypeDeclaration.ANNOTATION_TYPE_DECL: // annotation is interface too
692
return true;
693     }
694     return false;
695 }
696 /**
697  * @see IType#isAnnotation()
698  * @since 3.0
699  */

700 public boolean isAnnotation() throws JavaModelException {
701     IBinaryType info = (IBinaryType) getElementInfo();
702     return TypeDeclaration.kind(info.getModifiers()) == TypeDeclaration.ANNOTATION_TYPE_DECL;
703 }
704
705 /*
706  * @see IType#isLocal()
707  */

708 public boolean isLocal() throws JavaModelException {
709     IBinaryType info = (IBinaryType) getElementInfo();
710     return info.isLocal();
711 }
712 /*
713  * @see IType#isMember()
714  */

715 public boolean isMember() throws JavaModelException {
716     IBinaryType info = (IBinaryType) getElementInfo();
717     return info.isMember();
718 }
719 /* (non-Javadoc)
720  * @see org.eclipse.jdt.core.IType#isResolved()
721  */

722 public boolean isResolved() {
723     return false;
724 }
725 /*
726  * @see IType
727  */

728 public ITypeHierarchy loadTypeHierachy(InputStream JavaDoc input, IProgressMonitor monitor) throws JavaModelException {
729     return loadTypeHierachy(input, DefaultWorkingCopyOwner.PRIMARY, monitor);
730 }
731 /*
732  * @see IType
733  */

734 public ITypeHierarchy loadTypeHierachy(InputStream JavaDoc input, WorkingCopyOwner owner, IProgressMonitor monitor) throws JavaModelException {
735     return TypeHierarchy.load(this, input, owner);
736 }
737 /*
738  * @see IType#newSupertypeHierarchy(IProgressMonitor monitor)
739  */

740 public ITypeHierarchy newSupertypeHierarchy(IProgressMonitor monitor) throws JavaModelException {
741     return this.newSupertypeHierarchy(DefaultWorkingCopyOwner.PRIMARY, monitor);
742 }
743 /*
744  *@see IType#newSupertypeHierarchy(ICompilationUnit[], IProgressMonitor monitor)
745  */

746 public ITypeHierarchy newSupertypeHierarchy(
747     ICompilationUnit[] workingCopies,
748     IProgressMonitor monitor)
749     throws JavaModelException {
750     
751     CreateTypeHierarchyOperation op= new CreateTypeHierarchyOperation(this, workingCopies, SearchEngine.createWorkspaceScope(), false);
752     op.runOperation(monitor);
753     return op.getResult();
754 }
755 /**
756  * @param workingCopies the working copies that take precedence over their original compilation units
757  * @param monitor the given progress monitor
758  * @return a type hierarchy for this type containing this type and all of its supertypes
759  * @exception JavaModelException if this element does not exist or if an
760  * exception occurs while accessing its corresponding resource.
761  *
762  * @see IType#newSupertypeHierarchy(IWorkingCopy[], IProgressMonitor)
763  * @deprecated
764  */

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

782 public ITypeHierarchy newSupertypeHierarchy(
783     WorkingCopyOwner owner,
784     IProgressMonitor monitor)
785     throws JavaModelException {
786
787     ICompilationUnit[] workingCopies = JavaModelManager.getJavaModelManager().getWorkingCopies(owner, true/*add primary working copies*/);
788     CreateTypeHierarchyOperation op= new CreateTypeHierarchyOperation(this, workingCopies, SearchEngine.createWorkspaceScope(), false);
789     op.runOperation(monitor);
790     return op.getResult();
791 }
792 /*
793  * @see IType#newTypeHierarchy(IJavaProject, IProgressMonitor)
794  */

795 public ITypeHierarchy newTypeHierarchy(IJavaProject project, IProgressMonitor monitor) throws JavaModelException {
796     return newTypeHierarchy(project, DefaultWorkingCopyOwner.PRIMARY, monitor);
797 }
798 /*
799  * @see IType#newTypeHierarchy(IJavaProject, WorkingCopyOwner, IProgressMonitor)
800  */

801 public ITypeHierarchy newTypeHierarchy(IJavaProject project, WorkingCopyOwner owner, IProgressMonitor monitor) throws JavaModelException {
802     if (project == null) {
803         throw new IllegalArgumentException JavaDoc(Messages.hierarchy_nullProject);
804     }
805     ICompilationUnit[] workingCopies = JavaModelManager.getJavaModelManager().getWorkingCopies(owner, true/*add primary working copies*/);
806     ICompilationUnit[] projectWCs = null;
807     if (workingCopies != null) {
808         int length = workingCopies.length;
809         projectWCs = new ICompilationUnit[length];
810         int index = 0;
811         for (int i = 0; i < length; i++) {
812             ICompilationUnit wc = workingCopies[i];
813             if (project.equals(wc.getJavaProject())) {
814                 projectWCs[index++] = wc;
815             }
816         }
817         if (index != length) {
818             System.arraycopy(projectWCs, 0, projectWCs = new ICompilationUnit[index], 0, index);
819         }
820     }
821     CreateTypeHierarchyOperation op= new CreateTypeHierarchyOperation(
822         this,
823         projectWCs,
824         project,
825         true);
826     op.runOperation(monitor);
827     return op.getResult();
828 }
829 /**
830  * @param monitor the given progress monitor
831  * @exception JavaModelException if this element does not exist or if an
832  * exception occurs while accessing its corresponding resource.
833  * @return a type hierarchy for this type containing
834  *
835  * @see IType#newTypeHierarchy(IProgressMonitor monitor)
836  * @deprecated
837  */

838 public ITypeHierarchy newTypeHierarchy(IProgressMonitor monitor) throws JavaModelException {
839     return newTypeHierarchy((IWorkingCopy[])null, monitor);
840 }
841 /*
842  * @see IType#newTypeHierarchy(ICompilationUnit[], IProgressMonitor)
843  */

844 public ITypeHierarchy newTypeHierarchy(
845     ICompilationUnit[] workingCopies,
846     IProgressMonitor monitor)
847     throws JavaModelException {
848
849     CreateTypeHierarchyOperation op= new CreateTypeHierarchyOperation(this, workingCopies, SearchEngine.createWorkspaceScope(), true);
850     op.runOperation(monitor);
851     return op.getResult();
852 }
853 /**
854  * @see IType#newTypeHierarchy(IWorkingCopy[], IProgressMonitor)
855  * @deprecated
856  */

857 public ITypeHierarchy newTypeHierarchy(
858     IWorkingCopy[] workingCopies,
859     IProgressMonitor monitor)
860     throws JavaModelException {
861
862     ICompilationUnit[] copies;
863     if (workingCopies == null) {
864         copies = null;
865     } else {
866         int length = workingCopies.length;
867         System.arraycopy(workingCopies, 0, copies = new ICompilationUnit[length], 0, length);
868     }
869     return newTypeHierarchy(copies, monitor);
870 }
871 /*
872  * @see IType#newTypeHierarchy(WorkingCopyOwner, IProgressMonitor)
873  */

874 public ITypeHierarchy newTypeHierarchy(
875     WorkingCopyOwner owner,
876     IProgressMonitor monitor)
877     throws JavaModelException {
878         
879     ICompilationUnit[] workingCopies = JavaModelManager.getJavaModelManager().getWorkingCopies(owner, true/*add primary working copies*/);
880     CreateTypeHierarchyOperation op= new CreateTypeHierarchyOperation(this, workingCopies, SearchEngine.createWorkspaceScope(), true);
881     op.runOperation(monitor);
882     return op.getResult();
883 }
884 public JavaElement resolved(Binding binding) {
885     SourceRefElement resolvedHandle = new ResolvedBinaryType(this.parent, this.name, new String JavaDoc(binding.computeUniqueKey()));
886     resolvedHandle.occurrenceCount = this.occurrenceCount;
887     return resolvedHandle;
888 }
889 /*
890  * @see IType#resolveType(String)
891  */

892 public String JavaDoc[][] resolveType(String JavaDoc typeName) {
893     // not implemented for binary types
894
return null;
895 }
896 /*
897  * @see IType#resolveType(String, WorkingCopyOwner)
898  */

899 public String JavaDoc[][] resolveType(String JavaDoc typeName, WorkingCopyOwner owner) {
900     // not implemented for binary types
901
return null;
902 }
903 /*
904  * Returns the source file name as defined in the given info.
905  * If not present in the info, infers it from this type.
906  */

907 public String JavaDoc sourceFileName(IBinaryType info) {
908     char[] sourceFileName = info.sourceFileName();
909     if (sourceFileName == null) {
910         /*
911          * We assume that this type has been compiled from a file with its name
912          * For example, A.class comes from A.java and p.A.class comes from a file A.java
913          * in the folder p.
914          */

915         if (info.isMember()) {
916             IType enclosingType = getDeclaringType();
917             if (enclosingType == null) return null; // play it safe
918
while (enclosingType.getDeclaringType() != null) {
919                 enclosingType = enclosingType.getDeclaringType();
920             }
921             return enclosingType.getElementName() + Util.defaultJavaExtension();
922         } else if (info.isLocal() || info.isAnonymous()){
923             String JavaDoc typeQualifiedName = getTypeQualifiedName();
924             int dollar = typeQualifiedName.indexOf('$');
925             if (dollar == -1) {
926                 // malformed inner type: name doesn't contain a dollar
927
return getElementName() + Util.defaultJavaExtension();
928             }
929             return typeQualifiedName.substring(0, dollar) + Util.defaultJavaExtension();
930         } else {
931             return getElementName() + Util.defaultJavaExtension();
932         }
933     } else {
934         int index = CharOperation.lastIndexOf('/', sourceFileName);
935         return new String JavaDoc(sourceFileName, index + 1, sourceFileName.length - index - 1);
936     }
937 }
938 /*
939  * @private Debugging purposes
940  */

941 protected void toStringInfo(int tab, StringBuffer JavaDoc buffer, Object JavaDoc info, boolean showResolvedInfo) {
942     buffer.append(this.tabString(tab));
943     if (info == null) {
944         toStringName(buffer);
945         buffer.append(" (not open)"); //$NON-NLS-1$
946
} else if (info == NO_INFO) {
947         toStringName(buffer);
948     } else {
949         try {
950             if (this.isAnnotation()) {
951                 buffer.append("@interface "); //$NON-NLS-1$
952
} else if (this.isEnum()) {
953                 buffer.append("enum "); //$NON-NLS-1$
954
} else if (this.isInterface()) {
955                 buffer.append("interface "); //$NON-NLS-1$
956
} else {
957                 buffer.append("class "); //$NON-NLS-1$
958
}
959             toStringName(buffer);
960         } catch (JavaModelException e) {
961             buffer.append("<JavaModelException in toString of " + getElementName()); //$NON-NLS-1$
962
}
963     }
964 }
965 protected void toStringName(StringBuffer JavaDoc buffer) {
966     if (getElementName().length() > 0)
967         super.toStringName(buffer);
968     else
969         buffer.append("<anonymous>"); //$NON-NLS-1$
970
}
971 public String JavaDoc getAttachedJavadoc(IProgressMonitor monitor) throws JavaModelException {
972     final String JavaDoc contents = getJavadocContents(monitor);
973     if (contents == null) return null;
974     final int indexOfStartOfClassData = contents.indexOf(JavadocConstants.START_OF_CLASS_DATA);
975     if (indexOfStartOfClassData == -1) throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.UNKNOWN_JAVADOC_FORMAT, this));
976     int indexOfNextSummary = contents.indexOf(JavadocConstants.NESTED_CLASS_SUMMARY);
977     if (this.isEnum() && indexOfNextSummary == -1) {
978         // try to find enum constant summary start
979
indexOfNextSummary = contents.indexOf(JavadocConstants.ENUM_CONSTANT_SUMMARY);
980     }
981     if (this.isAnnotation() && indexOfNextSummary == -1) {
982         // try to find required enum constant summary start
983
indexOfNextSummary = contents.indexOf(JavadocConstants.ANNOTATION_TYPE_REQUIRED_MEMBER_SUMMARY);
984         if (indexOfNextSummary == -1) {
985             // try to find optional enum constant summary start
986
indexOfNextSummary = contents.indexOf(JavadocConstants.ANNOTATION_TYPE_OPTIONAL_MEMBER_SUMMARY);
987         }
988     }
989     if (indexOfNextSummary == -1) {
990         // try to find field summary start
991
indexOfNextSummary = contents.indexOf(JavadocConstants.FIELD_SUMMARY);
992     }
993     if (indexOfNextSummary == -1) {
994         // try to find constructor summary start
995
indexOfNextSummary = contents.indexOf(JavadocConstants.CONSTRUCTOR_SUMMARY);
996     }
997     if (indexOfNextSummary == -1) {
998         // try to find method summary start
999
indexOfNextSummary = contents.indexOf(JavadocConstants.METHOD_SUMMARY);
1000    }
1001    if (indexOfNextSummary == -1) {
1002        // we take the end of class data
1003
indexOfNextSummary = contents.indexOf(JavadocConstants.END_OF_CLASS_DATA);
1004    }
1005    if (indexOfNextSummary == -1) {
1006        throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.UNKNOWN_JAVADOC_FORMAT, this));
1007    }
1008    /*
1009     * Check out to cut off the hierarchy see 119844
1010     * We remove what the contents between the start of class data and the first <P>
1011     */

1012    int start = indexOfStartOfClassData + JavadocConstants.START_OF_CLASS_DATA_LENGTH;
1013    int indexOfFirstParagraph = contents.indexOf("<P>", start); //$NON-NLS-1$
1014
if (indexOfFirstParagraph == -1) {
1015        indexOfFirstParagraph = contents.indexOf("<p>", start); //$NON-NLS-1$
1016
}
1017    if (indexOfFirstParagraph != -1 && indexOfFirstParagraph < indexOfNextSummary) {
1018        start = indexOfFirstParagraph;
1019    }
1020    return contents.substring(start, indexOfNextSummary);
1021}
1022public String JavaDoc getJavadocContents(IProgressMonitor monitor) throws JavaModelException {
1023    PerProjectInfo projectInfo = JavaModelManager.getJavaModelManager().getPerProjectInfoCheckExistence(this.getJavaProject().getProject());
1024    String JavaDoc cachedJavadoc = null;
1025    synchronized (projectInfo.javadocCache) {
1026        cachedJavadoc = (String JavaDoc) projectInfo.javadocCache.get(this);
1027    }
1028    if (cachedJavadoc != null && cachedJavadoc != EMPTY_JAVADOC) {
1029        return cachedJavadoc;
1030    }
1031    URL JavaDoc baseLocation= getJavadocBaseLocation();
1032    if (baseLocation == null) {
1033        return null;
1034    }
1035    StringBuffer JavaDoc pathBuffer = new StringBuffer JavaDoc(baseLocation.toExternalForm());
1036
1037    if (!(pathBuffer.charAt(pathBuffer.length() - 1) == '/')) {
1038        pathBuffer.append('/');
1039    }
1040    IPackageFragment pack= this.getPackageFragment();
1041    String JavaDoc typeQualifiedName = null;
1042    if (this.isMember()) {
1043        IType currentType = this;
1044        StringBuffer JavaDoc typeName = new StringBuffer JavaDoc();
1045        while (currentType != null) {
1046            typeName.insert(0, currentType.getElementName());
1047            currentType = currentType.getDeclaringType();
1048            if (currentType != null) {
1049                typeName.insert(0, '.');
1050            }
1051        }
1052        typeQualifiedName = new String JavaDoc(typeName.toString());
1053    } else {
1054        typeQualifiedName = this.getElementName();
1055    }
1056    
1057    pathBuffer.append(pack.getElementName().replace('.', '/')).append('/').append(typeQualifiedName).append(JavadocConstants.HTML_EXTENSION);
1058    
1059    if (monitor != null && monitor.isCanceled()) throw new OperationCanceledException();
1060    final String JavaDoc contents = getURLContents(String.valueOf(pathBuffer));
1061    synchronized (projectInfo.javadocCache) {
1062        projectInfo.javadocCache.put(this, contents);
1063    }
1064    return contents;
1065}
1066}
1067
Popular Tags