KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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 org.eclipse.jdt.core.*;
14 import org.eclipse.jdt.core.Flags;
15 import org.eclipse.jdt.core.IMethod;
16 import org.eclipse.jdt.core.IType;
17 import org.eclipse.jdt.core.JavaModelException;
18 import org.eclipse.jdt.core.Signature;
19 import org.eclipse.jdt.core.compiler.CharOperation;
20 import org.eclipse.jdt.internal.compiler.SourceElementRequestorAdapter;
21 import org.eclipse.jdt.internal.compiler.env.IBinaryMethod;
22 import org.eclipse.jdt.internal.compiler.lookup.Binding;
23 import org.eclipse.jdt.internal.core.util.Util;
24
25 /**
26  * @see IMethod
27  */

28
29 /* package */ class BinaryMethod extends BinaryMember implements IMethod {
30     
31     class DecodeParametersNames extends SourceElementRequestorAdapter {
32             String[] parametersNames;
33         
34             public void enterMethod(MethodInfo methodInfo) {
35                     if (methodInfo.parameterNames != null) {
36                         int length = methodInfo.parameterNames.length;
37                         this.parametersNames = new String[length];
38                         for (int i = 0; i < length; i++) {
39                             this.parametersNames[i] = new String(methodInfo.parameterNames[i]);
40                         }
41                     }
42                 }
43                 
44             public void enterConstructor(MethodInfo methodInfo) {
45                     if (methodInfo.parameterNames != null) {
46                         int length = methodInfo.parameterNames.length;
47                         this.parametersNames = new String[length];
48                         for (int i = 0; i < length; i++) {
49                             this.parametersNames[i] = new String(methodInfo.parameterNames[i]);
50                         }
51                     }
52                 }
53                 
54                 public String[] getParametersNames() {
55                     return this.parametersNames;
56                 }
57     }
58
59     /**
60      * The parameter type signatures of the method - stored locally
61      * to perform equality test. <code>null</code> indicates no
62      * parameters.
63      */

64     protected String[] parameterTypes;
65     /**
66      * The parameter names for the method.
67      */

68     protected String[] parameterNames;
69
70     /**
71      * An empty list of Strings
72      */

73     protected static final String[] NO_TYPES= new String[] {};
74     protected String[] exceptionTypes;
75     protected String returnType;
76 protected BinaryMethod(JavaElement parent, String name, String[] paramTypes) {
77     super(parent, name);
78     Assert.isTrue(name.indexOf('.') == -1);
79     if (paramTypes == null) {
80         this.parameterTypes= NO_TYPES;
81     } else {
82         this.parameterTypes= paramTypes;
83     }
84 }
85 public boolean equals(Object o) {
86     if (!(o instanceof BinaryMethod)) return false;
87     return super.equals(o) && Util.equalArraysOrNull(this.parameterTypes, ((BinaryMethod)o).parameterTypes);
88 }
89 /*
90  * @see IMethod
91  */

92 public String[] getExceptionTypes() throws JavaModelException {
93     if (this.exceptionTypes == null) {
94         IBinaryMethod info = (IBinaryMethod) getElementInfo();
95         char[] genericSignature = info.getGenericSignature();
96         if (genericSignature != null) {
97             char[] dotBasedSignature = CharOperation.replaceOnCopy(genericSignature, '/', '.');
98             this.exceptionTypes = Signature.getThrownExceptionTypes(new String(dotBasedSignature));
99         }
100         if (this.exceptionTypes == null || this.exceptionTypes.length == 0) {
101             char[][] eTypeNames = info.getExceptionTypeNames();
102             if (eTypeNames == null || eTypeNames.length == 0) {
103                 this.exceptionTypes = NO_TYPES;
104             } else {
105                 eTypeNames = ClassFile.translatedNames(eTypeNames);
106                 this.exceptionTypes = new String[eTypeNames.length];
107                 for (int j = 0, length = eTypeNames.length; j < length; j++) {
108                     // 1G01HRY: ITPJCORE:WINNT - method.getExceptionType not in correct format
109
int nameLength = eTypeNames[j].length;
110                     char[] convertedName = new char[nameLength + 2];
111                     System.arraycopy(eTypeNames[j], 0, convertedName, 1, nameLength);
112                     convertedName[0] = 'L';
113                     convertedName[nameLength + 1] = ';';
114                     this.exceptionTypes[j] = new String(convertedName);
115                 }
116             }
117         }
118     }
119     return this.exceptionTypes;
120 }
121 /*
122  * @see IJavaElement
123  */

124 public int getElementType() {
125     return METHOD;
126 }
127 /*
128  * @see IMember
129  */

130 public int getFlags() throws JavaModelException {
131     IBinaryMethod info = (IBinaryMethod) getElementInfo();
132     return info.getModifiers();
133 }
134 /*
135  * @see JavaElement#getHandleMemento(StringBuffer)
136  */

137 protected void getHandleMemento(StringBuffer buff) {
138     ((JavaElement) getParent()).getHandleMemento(buff);
139     char delimiter = getHandleMementoDelimiter();
140     buff.append(delimiter);
141     escapeMementoName(buff, getElementName());
142     for (int i = 0; i < this.parameterTypes.length; i++) {
143         buff.append(delimiter);
144         escapeMementoName(buff, this.parameterTypes[i]);
145     }
146     if (this.occurrenceCount > 1) {
147         buff.append(JEM_COUNT);
148         buff.append(this.occurrenceCount);
149     }
150 }
151 /*
152  * @see JavaElement#getHandleMemento()
153  */

154 protected char getHandleMementoDelimiter() {
155     return JavaElement.JEM_METHOD;
156 }
157 public String getKey(boolean forceOpen) throws JavaModelException {
158     return getKey(this, forceOpen);
159 }
160 /*
161  * @see IMethod
162  */

163 public int getNumberOfParameters() {
164     return this.parameterTypes == null ? 0 : this.parameterTypes.length;
165 }
166 /*
167  * @see IMethod
168  * Look for source attachment information to retrieve the actual parameter names as stated in source.
169  */

170 public String[] getParameterNames() throws JavaModelException {
171     if (this.parameterNames == null) {
172
173         // force source mapping if not already done
174
IType type = (IType) getParent();
175         SourceMapper mapper = getSourceMapper();
176         if (mapper != null) {
177             char[][] paramNames = mapper.getMethodParameterNames(this);
178             
179             // map source and try to find parameter names
180
if(paramNames == null) {
181                 char[] source = mapper.findSource(type);
182                 if (source != null){
183                     mapper.mapSource(type, source);
184                 }
185                 paramNames = mapper.getMethodParameterNames(this);
186             }
187             
188             // if parameter names exist, convert parameter names to String array
189
if(paramNames != null) {
190                 this.parameterNames = new String[paramNames.length];
191                 for (int i = 0; i < paramNames.length; i++) {
192                     this.parameterNames[i] = new String(paramNames[i]);
193                 }
194             }
195         }
196         // if still no parameter names, produce fake ones
197
if (this.parameterNames == null) {
198             IBinaryMethod info = (IBinaryMethod) getElementInfo();
199             int paramCount = Signature.getParameterCount(new String(info.getMethodDescriptor()));
200             this.parameterNames = new String[paramCount];
201             for (int i = 0; i < paramCount; i++) {
202                 this.parameterNames[i] = "arg" + i; //$NON-NLS-1$
203
}
204         }
205     }
206     return this.parameterNames;
207 }
208 /*
209  * @see IMethod
210  */

211 public String[] getParameterTypes() {
212     return this.parameterTypes;
213 }
214
215 public ITypeParameter getTypeParameter(String typeParameterName) {
216     return new TypeParameter(this, typeParameterName);
217 }
218
219 public ITypeParameter[] getTypeParameters() throws JavaModelException {
220     String[] typeParameterSignatures = getTypeParameterSignatures();
221     int length = typeParameterSignatures.length;
222     if (length == 0) return TypeParameter.NO_TYPE_PARAMETERS;
223     ITypeParameter[] typeParameters = new ITypeParameter[length];
224     for (int i = 0; i < typeParameterSignatures.length; i++) {
225         String typeParameterName = Signature.getTypeVariable(typeParameterSignatures[i]);
226         typeParameters[i] = new TypeParameter(this, typeParameterName);
227     }
228     return typeParameters;
229 }
230
231 /**
232  * @see IMethod#getTypeParameterSignatures()
233  * @since 3.0
234  * @deprecated
235  */

236 public String[] getTypeParameterSignatures() throws JavaModelException {
237     IBinaryMethod info = (IBinaryMethod) getElementInfo();
238     char[] genericSignature = info.getGenericSignature();
239     if (genericSignature == null)
240         return CharOperation.NO_STRINGS;
241     char[] dotBasedSignature = CharOperation.replaceOnCopy(genericSignature, '/', '.');
242     char[][] typeParams = Signature.getTypeParameters(dotBasedSignature);
243     return CharOperation.toStrings(typeParams);
244 }
245
246 /*
247  * @see IMethod
248  */

249 public String getReturnType() throws JavaModelException {
250     if (this.returnType == null) {
251         IBinaryMethod info = (IBinaryMethod) getElementInfo();
252         this.returnType = getReturnType(info);
253     }
254     return this.returnType;
255 }
256 private String getReturnType(IBinaryMethod info) {
257     char[] genericSignature = info.getGenericSignature();
258     char[] signature = genericSignature == null ? info.getMethodDescriptor() : genericSignature;
259     char[] dotBasedSignature = CharOperation.replaceOnCopy(signature, '/', '.');
260     String returnTypeName= Signature.getReturnType(new String(dotBasedSignature));
261     return new String(ClassFile.translatedName(returnTypeName.toCharArray()));
262 }
263 /*
264  * @see IMethod
265  */

266 public String getSignature() throws JavaModelException {
267     IBinaryMethod info = (IBinaryMethod) getElementInfo();
268     return new String(info.getMethodDescriptor());
269 }
270 /**
271  * @see org.eclipse.jdt.internal.core.JavaElement#hashCode()
272  */

273 public int hashCode() {
274    int hash = super.hashCode();
275     for (int i = 0, length = parameterTypes.length; i < length; i++) {
276         hash = Util.combineHashCodes(hash, parameterTypes[i].hashCode());
277     }
278     return hash;
279 }
280 /*
281  * @see IMethod
282  */

283 public boolean isConstructor() throws JavaModelException {
284     IBinaryMethod info = (IBinaryMethod) getElementInfo();
285     return info.isConstructor();
286 }
287 /*
288  * @see IMethod#isMainMethod()
289  */

290 public boolean isMainMethod() throws JavaModelException {
291     return this.isMainMethod(this);
292 }
293 /* (non-Javadoc)
294  * @see org.eclipse.jdt.core.IMethod#isResolved()
295  */

296 public boolean isResolved() {
297     return false;
298 }
299 /*
300  * @see IMethod#isSimilar(IMethod)
301  */

302 public boolean isSimilar(IMethod method) {
303     return
304         areSimilarMethods(
305             this.getElementName(), this.getParameterTypes(),
306             method.getElementName(), method.getParameterTypes(),
307             null);
308 }
309
310 public String readableName() {
311
312     StringBuffer buffer = new StringBuffer(super.readableName());
313     buffer.append("("); //$NON-NLS-1$
314
String[] paramTypes = this.parameterTypes;
315     int length;
316     if (paramTypes != null && (length = paramTypes.length) > 0) {
317         for (int i = 0; i < length; i++) {
318             buffer.append(Signature.toString(paramTypes[i]));
319             if (i < length - 1) {
320                 buffer.append(", "); //$NON-NLS-1$
321
}
322         }
323     }
324     buffer.append(")"); //$NON-NLS-1$
325
return buffer.toString();
326 }
327 public JavaElement resolved(Binding binding) {
328     SourceRefElement resolvedHandle = new ResolvedBinaryMethod(this.parent, this.name, this.parameterTypes, new String(binding.computeUniqueKey()));
329     resolvedHandle.occurrenceCount = this.occurrenceCount;
330     return resolvedHandle;
331 }/*
332  * @private Debugging purposes
333  */

334 protected void toStringInfo(int tab, StringBuffer buffer, Object info, boolean showResolvedInfo) {
335     buffer.append(tabString(tab));
336     if (info == null) {
337         toStringName(buffer);
338         buffer.append(" (not open)"); //$NON-NLS-1$
339
} else if (info == NO_INFO) {
340         toStringName(buffer);
341     } else {
342         IBinaryMethod methodInfo = (IBinaryMethod) info;
343         int flags = methodInfo.getModifiers();
344         if (Flags.isStatic(flags)) {
345             buffer.append("static "); //$NON-NLS-1$
346
}
347         if (!methodInfo.isConstructor()) {
348             buffer.append(Signature.toString(getReturnType(methodInfo)));
349             buffer.append(' ');
350         }
351         toStringName(buffer, flags);
352     }
353 }
354 protected void toStringName(StringBuffer buffer) {
355     toStringName(buffer, 0);
356 }
357 protected void toStringName(StringBuffer buffer, int flags) {
358     buffer.append(getElementName());
359     buffer.append('(');
360     String[] parameters = getParameterTypes();
361     int length;
362     if (parameters != null && (length = parameters.length) > 0) {
363         boolean isVarargs = Flags.isVarargs(flags);
364         for (int i = 0; i < length; i++) {
365             try {
366                 if (i < length - 1) {
367                     buffer.append(Signature.toString(parameters[i]));
368                     buffer.append(", "); //$NON-NLS-1$
369
} else if (isVarargs) {
370                     // remove array from signature
371
String parameter = parameters[i].substring(1);
372                     buffer.append(Signature.toString(parameter));
373                     buffer.append(" ..."); //$NON-NLS-1$
374
} else {
375                     buffer.append(Signature.toString(parameters[i]));
376                 }
377             } catch (IllegalArgumentException e) {
378                 // parameter signature is malformed
379
buffer.append("*** invalid signature: "); //$NON-NLS-1$
380
buffer.append(parameters[i]);
381             }
382         }
383     }
384     buffer.append(')');
385     if (this.occurrenceCount > 1) {
386         buffer.append("#"); //$NON-NLS-1$
387
buffer.append(this.occurrenceCount);
388     }
389 }
390 }
391
Popular Tags