KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15
16 import org.eclipse.jdt.core.*;
17 import org.eclipse.jdt.core.IClassFile;
18 import org.eclipse.jdt.core.IField;
19 import org.eclipse.jdt.core.IJavaElement;
20 import org.eclipse.jdt.core.IPackageFragment;
21 import org.eclipse.jdt.core.IType;
22 import org.eclipse.jdt.core.JavaModelException;
23 import org.eclipse.jdt.core.Signature;
24 import org.eclipse.jdt.core.compiler.CharOperation;
25 import org.eclipse.jdt.internal.compiler.env.IBinaryField;
26 import org.eclipse.jdt.internal.compiler.env.IBinaryMethod;
27 import org.eclipse.jdt.internal.compiler.env.IBinaryNestedType;
28 import org.eclipse.jdt.internal.compiler.env.IBinaryType;
29 import org.eclipse.jdt.internal.compiler.util.SuffixConstants;
30
31 /**
32  * Element info for <code>ClassFile</code> handles.
33  */

34  
35 /* package */ class ClassFileInfo extends OpenableElementInfo implements SuffixConstants {
36     /**
37      * The children of the <code>BinaryType</code> corresponding to our
38      * <code>ClassFile</code>. These are kept here because we don't have
39      * access to the <code>BinaryType</code> info (<code>ClassFileReader</code>).
40      */

41     protected JavaElement[] binaryChildren = null;
42     /*
43      * The type parameters in this class file.
44      */

45     protected ITypeParameter[] typeParameters;
46     
47 /**
48  * Creates the handles and infos for the fields of the given binary type.
49  * Adds new handles to the given vector.
50  */

51 private void generateFieldInfos(IType type, IBinaryType typeInfo, HashMap JavaDoc newElements, ArrayList JavaDoc childrenHandles) {
52     // Make the fields
53
IBinaryField[] fields = typeInfo.getFields();
54     if (fields == null) {
55         return;
56     }
57     JavaModelManager manager = JavaModelManager.getJavaModelManager();
58     for (int i = 0, fieldCount = fields.length; i < fieldCount; i++) {
59         IBinaryField fieldInfo = fields[i];
60         IField field = new BinaryField((JavaElement)type, manager.intern(new String JavaDoc(fieldInfo.getName())));
61         newElements.put(field, fieldInfo);
62         childrenHandles.add(field);
63     }
64 }
65 /**
66  * Creates the handles for the inner types of the given binary type.
67  * Adds new handles to the given vector.
68  */

69 private void generateInnerClassHandles(IType type, IBinaryType typeInfo, ArrayList JavaDoc childrenHandles) {
70     // Add inner types
71
// If the current type is an inner type, innerClasses returns
72
// an extra entry for the current type. This entry must be removed.
73
// Can also return an entry for the enclosing type of an inner type.
74
IBinaryNestedType[] innerTypes = typeInfo.getMemberTypes();
75     if (innerTypes != null) {
76         IPackageFragment pkg = (IPackageFragment) type.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
77         for (int i = 0, typeCount = innerTypes.length; i < typeCount; i++) {
78             IBinaryNestedType binaryType = innerTypes[i];
79             IClassFile parentClassFile= pkg.getClassFile(new String JavaDoc(ClassFile.unqualifiedName(binaryType.getName())) + SUFFIX_STRING_class);
80             IType innerType = new BinaryType((JavaElement) parentClassFile, ClassFile.simpleName(binaryType.getName()));
81             childrenHandles.add(innerType);
82         }
83     }
84 }
85 /**
86  * Creates the handles and infos for the methods of the given binary type.
87  * Adds new handles to the given vector.
88  */

89 private void generateMethodInfos(IType type, IBinaryType typeInfo, HashMap JavaDoc newElements, ArrayList JavaDoc childrenHandles, ArrayList JavaDoc typeParameterHandles) {
90     IBinaryMethod[] methods = typeInfo.getMethods();
91     if (methods == null) {
92         return;
93     }
94     for (int i = 0, methodCount = methods.length; i < methodCount; i++) {
95         IBinaryMethod methodInfo = methods[i];
96         // TODO (jerome) filter out synthetic members
97
// indexer should not index them as well
98
// if ((methodInfo.getModifiers() & IConstants.AccSynthetic) != 0) continue; // skip synthetic
99
char[] signature = methodInfo.getGenericSignature();
100         if (signature == null) signature = methodInfo.getMethodDescriptor();
101         String JavaDoc[] pNames = null;
102         try {
103             pNames = Signature.getParameterTypes(new String JavaDoc(signature));
104         } catch (IllegalArgumentException JavaDoc e) {
105             // protect against malformed .class file (e.g. com/sun/crypto/provider/SunJCE_b.class has a 'a' generic signature)
106
signature = methodInfo.getMethodDescriptor();
107             pNames = Signature.getParameterTypes(new String JavaDoc(signature));
108         }
109         char[][] paramNames= new char[pNames.length][];
110         for (int j= 0; j < pNames.length; j++) {
111             paramNames[j]= pNames[j].toCharArray();
112         }
113         char[][] parameterTypes = ClassFile.translatedNames(paramNames);
114         JavaModelManager manager = JavaModelManager.getJavaModelManager();
115         String JavaDoc selector = new String JavaDoc(methodInfo.getSelector());
116         if (methodInfo.isConstructor()) {
117             selector =type.getElementName();
118         }
119         selector = manager.intern(selector);
120         for (int j= 0; j < pNames.length; j++) {
121             pNames[j]= manager.intern(new String JavaDoc(parameterTypes[j]));
122         }
123         BinaryMethod method = new BinaryMethod((JavaElement)type, selector, pNames);
124         childrenHandles.add(method);
125         
126         // ensure that 2 binary methods with the same signature but with different return types have different occurence counts.
127
// (case of bridge methods in 1.5)
128
while (newElements.containsKey(method))
129             method.occurrenceCount++;
130         
131         newElements.put(method, methodInfo);
132         
133         generateTypeParameterInfos(method, signature, newElements, typeParameterHandles);
134     }
135 }
136 /**
137  * Creates the handles and infos for the type parameter of the given binary member.
138  * Adds new handles to the given vector.
139  */

140 private void generateTypeParameterInfos(BinaryMember parent, char[] signature, HashMap JavaDoc newElements, ArrayList JavaDoc typeParameterHandles) {
141     if (signature == null) return;
142     char[][] typeParameterSignatures = Signature.getTypeParameters(signature);
143     for (int i = 0, typeParameterCount = typeParameterSignatures.length; i < typeParameterCount; i++) {
144         char[] typeParameterSignature = typeParameterSignatures[i];
145         char[] typeParameterName = Signature.getTypeVariable(typeParameterSignature);
146         char[][] typeParameterBoundSignatures = Signature.getTypeParameterBounds(typeParameterSignature);
147         int boundLength = typeParameterBoundSignatures.length;
148         char[][] typeParameterBounds = new char[boundLength][];
149         for (int j = 0; j < boundLength; j++) {
150             typeParameterBounds[j] = Signature.toCharArray(typeParameterBoundSignatures[j]);
151             CharOperation.replace(typeParameterBounds[j], '/', '.');
152         }
153         TypeParameter typeParameter = new TypeParameter(parent, new String JavaDoc(typeParameterName));
154         TypeParameterElementInfo info = new TypeParameterElementInfo();
155         info.bounds = typeParameterBounds;
156         typeParameterHandles.add(typeParameter);
157         
158         // ensure that 2 binary methods with the same signature but with different return types have different occurence counts.
159
// (case of bridge methods in 1.5)
160
while (newElements.containsKey(typeParameter))
161             typeParameter.occurrenceCount++;
162         
163         newElements.put(typeParameter, info);
164     }
165 }
166 /**
167  * Returns true iff the <code>readBinaryChildren</code> has already
168  * been called.
169  */

170 boolean hasReadBinaryChildren() {
171     return this.binaryChildren != null;
172 }
173 /**
174  * Creates the handles for <code>BinaryMember</code>s defined in this
175  * <code>ClassFile</code> and adds them to the
176  * <code>JavaModelManager</code>'s cache.
177  */

178 protected void readBinaryChildren(ClassFile classFile, HashMap JavaDoc newElements, IBinaryType typeInfo) {
179     ArrayList JavaDoc childrenHandles = new ArrayList JavaDoc();
180     BinaryType type = (BinaryType) classFile.getType();
181     ArrayList JavaDoc typeParameterHandles = new ArrayList JavaDoc();
182     if (typeInfo != null) { //may not be a valid class file
183
generateTypeParameterInfos(type, typeInfo.getGenericSignature(), newElements, typeParameterHandles);
184         generateFieldInfos(type, typeInfo, newElements, childrenHandles);
185         generateMethodInfos(type, typeInfo, newElements, childrenHandles, typeParameterHandles);
186         generateInnerClassHandles(type, typeInfo, childrenHandles); // Note inner class are separate openables that are not opened here: no need to pass in newElements
187
}
188     
189     this.binaryChildren = new JavaElement[childrenHandles.size()];
190     childrenHandles.toArray(this.binaryChildren);
191     int typeParameterHandleSize = typeParameterHandles.size();
192     if (typeParameterHandleSize == 0) {
193         this.typeParameters = TypeParameter.NO_TYPE_PARAMETERS;
194     } else {
195         this.typeParameters = new ITypeParameter[typeParameterHandleSize];
196         typeParameterHandles.toArray(this.typeParameters);
197     }
198 }
199 /**
200  * Removes the binary children handles and remove their infos from
201  * the <code>JavaModelManager</code>'s cache.
202  */

203 void removeBinaryChildren() throws JavaModelException {
204     if (this.binaryChildren != null) {
205         JavaModelManager manager = JavaModelManager.getJavaModelManager();
206         for (int i = 0; i <this.binaryChildren.length; i++) {
207             JavaElement child = this.binaryChildren[i];
208             if (child instanceof BinaryType) {
209                 manager.removeInfoAndChildren((JavaElement)child.getParent());
210             } else {
211                 manager.removeInfoAndChildren(child);
212             }
213         }
214         this.binaryChildren = JavaElement.NO_ELEMENTS;
215     }
216     if (this.typeParameters != null) {
217         JavaModelManager manager = JavaModelManager.getJavaModelManager();
218         for (int i = 0; i <this.typeParameters.length; i++) {
219             TypeParameter typeParameter = (TypeParameter) this.typeParameters[i];
220             manager.removeInfoAndChildren(typeParameter);
221         }
222         this.typeParameters = TypeParameter.NO_TYPE_PARAMETERS;
223     }
224 }
225 }
226
Popular Tags