KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.eclipse.jdt.core.*;
14 import org.eclipse.jdt.core.compiler.CharOperation;
15 import org.eclipse.jdt.internal.compiler.lookup.Binding;
16 import org.eclipse.jdt.internal.core.util.Util;
17
18 /**
19  * @see IMethod
20  */

21
22 public class SourceMethod extends NamedMember implements IMethod {
23
24     /**
25      * The parameter type signatures of the method - stored locally
26      * to perform equality test. <code>null</code> indicates no
27      * parameters.
28      */

29     protected String JavaDoc[] parameterTypes;
30
31 protected SourceMethod(JavaElement parent, String JavaDoc name, String JavaDoc[] parameterTypes) {
32     super(parent, name);
33     // Assertion disabled since bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=179011
34
// Assert.isTrue(name.indexOf('.') == -1);
35
if (parameterTypes == null) {
36         this.parameterTypes= CharOperation.NO_STRINGS;
37     } else {
38         this.parameterTypes= parameterTypes;
39     }
40 }
41 protected void closing(Object JavaDoc info) throws JavaModelException {
42     super.closing(info);
43     SourceMethodElementInfo elementInfo = (SourceMethodElementInfo) info;
44     ITypeParameter[] typeParameters = elementInfo.typeParameters;
45     for (int i = 0, length = typeParameters.length; i < length; i++) {
46         ((TypeParameter) typeParameters[i]).close();
47     }
48 }
49 public boolean equals(Object JavaDoc o) {
50     if (!(o instanceof SourceMethod)) return false;
51     return super.equals(o) && Util.equalArraysOrNull(this.parameterTypes, ((SourceMethod)o).parameterTypes);
52 }
53 /**
54  * @see IJavaElement
55  */

56 public int getElementType() {
57     return METHOD;
58 }
59 /**
60  * @see IMethod
61  */

62 public String JavaDoc[] getExceptionTypes() throws JavaModelException {
63     SourceMethodElementInfo info = (SourceMethodElementInfo) getElementInfo();
64     char[][] exs= info.getExceptionTypeNames();
65     return CompilationUnitStructureRequestor.convertTypeNamesToSigs(exs);
66 }
67 /**
68  * @see JavaElement#getHandleMemento(StringBuffer)
69  */

70 protected void getHandleMemento(StringBuffer JavaDoc buff) {
71     ((JavaElement) getParent()).getHandleMemento(buff);
72     char delimiter = getHandleMementoDelimiter();
73     buff.append(delimiter);
74     escapeMementoName(buff, getElementName());
75     for (int i = 0; i < this.parameterTypes.length; i++) {
76         buff.append(delimiter);
77         escapeMementoName(buff, this.parameterTypes[i]);
78     }
79     if (this.occurrenceCount > 1) {
80         buff.append(JEM_COUNT);
81         buff.append(this.occurrenceCount);
82     }
83 }
84 /**
85  * @see JavaElement#getHandleMemento()
86  */

87 protected char getHandleMementoDelimiter() {
88     return JavaElement.JEM_METHOD;
89 }
90 /* (non-Javadoc)
91  * @see org.eclipse.jdt.core.IMethod#getKey()
92  */

93 public String JavaDoc getKey() {
94     try {
95         return getKey(this, false/*don't open*/);
96     } catch (JavaModelException e) {
97         // happen only if force open is true
98
return null;
99     }
100 }
101 /**
102  * @see IMethod
103  */

104 public int getNumberOfParameters() {
105     return this.parameterTypes == null ? 0 : this.parameterTypes.length;
106 }
107 /**
108  * @see IMethod
109  */

110 public String JavaDoc[] getParameterNames() throws JavaModelException {
111     SourceMethodElementInfo info = (SourceMethodElementInfo) getElementInfo();
112     char[][] names= info.getArgumentNames();
113     return CharOperation.toStrings(names);
114 }
115 /**
116  * @see IMethod
117  */

118 public String JavaDoc[] getParameterTypes() {
119     return this.parameterTypes;
120 }
121
122 public ITypeParameter getTypeParameter(String JavaDoc typeParameterName) {
123     return new TypeParameter(this, typeParameterName);
124 }
125
126 public ITypeParameter[] getTypeParameters() throws JavaModelException {
127     SourceMethodElementInfo info = (SourceMethodElementInfo) getElementInfo();
128     return info.typeParameters;
129 }
130
131 /**
132  * @see IMethod#getTypeParameterSignatures()
133  * @since 3.0
134  * @deprecated
135  */

136 public String JavaDoc[] getTypeParameterSignatures() throws JavaModelException {
137     ITypeParameter[] typeParameters = getTypeParameters();
138     int length = typeParameters.length;
139     String JavaDoc[] typeParameterSignatures = new String JavaDoc[length];
140     for (int i = 0; i < length; i++) {
141         TypeParameter typeParameter = (TypeParameter) typeParameters[i];
142         TypeParameterElementInfo info = (TypeParameterElementInfo) typeParameter.getElementInfo();
143         char[][] bounds = info.bounds;
144         if (bounds == null) {
145             typeParameterSignatures[i] = Signature.createTypeParameterSignature(typeParameter.getElementName(), CharOperation.NO_STRINGS);
146         } else {
147             int boundsLength = bounds.length;
148             char[][] boundSignatures = new char[boundsLength][];
149             for (int j = 0; j < boundsLength; j++) {
150                 boundSignatures[j] = Signature.createCharArrayTypeSignature(bounds[j], false);
151             }
152             typeParameterSignatures[i] = new String JavaDoc(Signature.createTypeParameterSignature(typeParameter.getElementName().toCharArray(), boundSignatures));
153         }
154     }
155     return typeParameterSignatures;
156 }
157
158 /*
159  * @see JavaElement#getPrimaryElement(boolean)
160  */

161 public IJavaElement getPrimaryElement(boolean checkOwner) {
162     if (checkOwner) {
163         CompilationUnit cu = (CompilationUnit)getAncestor(COMPILATION_UNIT);
164         if (cu.isPrimary()) return this;
165     }
166     IJavaElement primaryParent = this.parent.getPrimaryElement(false);
167     return ((IType)primaryParent).getMethod(this.name, this.parameterTypes);
168 }
169 public String JavaDoc[] getRawParameterNames() throws JavaModelException {
170     return getParameterNames();
171 }
172 /**
173  * @see IMethod
174  */

175 public String JavaDoc getReturnType() throws JavaModelException {
176     SourceMethodElementInfo info = (SourceMethodElementInfo) getElementInfo();
177     return Signature.createTypeSignature(info.getReturnTypeName(), false);
178 }
179 /**
180  * @see IMethod
181  */

182 public String JavaDoc getSignature() throws JavaModelException {
183     SourceMethodElementInfo info = (SourceMethodElementInfo) getElementInfo();
184     return Signature.createMethodSignature(this.parameterTypes, Signature.createTypeSignature(info.getReturnTypeName(), false));
185 }
186 /**
187  * @see org.eclipse.jdt.internal.core.JavaElement#hashCode()
188  */

189 public int hashCode() {
190    int hash = super.hashCode();
191     for (int i = 0, length = this.parameterTypes.length; i < length; i++) {
192         hash = Util.combineHashCodes(hash, this.parameterTypes[i].hashCode());
193     }
194     return hash;
195 }
196 /**
197  * @see IMethod
198  */

199 public boolean isConstructor() throws JavaModelException {
200     if (!this.getElementName().equals(this.parent.getElementName())) {
201         // faster than reaching the info
202
return false;
203     }
204     SourceMethodElementInfo info = (SourceMethodElementInfo) getElementInfo();
205     return info.isConstructor();
206 }
207 /**
208  * @see IMethod#isMainMethod()
209  */

210 public boolean isMainMethod() throws JavaModelException {
211     return this.isMainMethod(this);
212 }
213 /* (non-Javadoc)
214  * @see org.eclipse.jdt.core.IMethod#isResolved()
215  */

216 public boolean isResolved() {
217     return false;
218 }
219 /**
220  * @see IMethod#isSimilar(IMethod)
221  */

222 public boolean isSimilar(IMethod method) {
223     return
224         areSimilarMethods(
225             this.getElementName(), this.getParameterTypes(),
226             method.getElementName(), method.getParameterTypes(),
227             null);
228 }
229
230 /**
231  */

232 public String JavaDoc readableName() {
233
234     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(super.readableName());
235     buffer.append('(');
236     int length;
237     if (this.parameterTypes != null && (length = this.parameterTypes.length) > 0) {
238         for (int i = 0; i < length; i++) {
239             buffer.append(Signature.toString(this.parameterTypes[i]));
240             if (i < length - 1) {
241                 buffer.append(", "); //$NON-NLS-1$
242
}
243         }
244     }
245     buffer.append(')');
246     return buffer.toString();
247 }
248 public JavaElement resolved(Binding binding) {
249     SourceRefElement resolvedHandle = new ResolvedSourceMethod(this.parent, this.name, this.parameterTypes, new String JavaDoc(binding.computeUniqueKey()));
250     resolvedHandle.occurrenceCount = this.occurrenceCount;
251     return resolvedHandle;
252 }
253 /**
254  * @private Debugging purposes
255  */

256 protected void toStringInfo(int tab, StringBuffer JavaDoc buffer, Object JavaDoc info, boolean showResolvedInfo) {
257     buffer.append(tabString(tab));
258     if (info == null) {
259         toStringName(buffer);
260         buffer.append(" (not open)"); //$NON-NLS-1$
261
} else if (info == NO_INFO) {
262         toStringName(buffer);
263     } else {
264         SourceMethodElementInfo methodInfo = (SourceMethodElementInfo) info;
265         int flags = methodInfo.getModifiers();
266         if (Flags.isStatic(flags)) {
267             buffer.append("static "); //$NON-NLS-1$
268
}
269         if (!methodInfo.isConstructor()) {
270             buffer.append(methodInfo.getReturnTypeName());
271             buffer.append(' ');
272         }
273         toStringName(buffer, flags);
274     }
275 }
276 protected void toStringName(StringBuffer JavaDoc buffer) {
277     toStringName(buffer, 0);
278 }
279 protected void toStringName(StringBuffer JavaDoc buffer, int flags) {
280     buffer.append(getElementName());
281     buffer.append('(');
282     String JavaDoc[] parameters = getParameterTypes();
283     int length;
284     if (parameters != null && (length = parameters.length) > 0) {
285         boolean isVarargs = Flags.isVarargs(flags);
286         for (int i = 0; i < length; i++) {
287             try {
288                 if (i < length - 1) {
289                     buffer.append(Signature.toString(parameters[i]));
290                     buffer.append(", "); //$NON-NLS-1$
291
} else if (isVarargs) {
292                     // remove array from signature
293
String JavaDoc parameter = parameters[i].substring(1);
294                     buffer.append(Signature.toString(parameter));
295                     buffer.append(" ..."); //$NON-NLS-1$
296
} else {
297                     buffer.append(Signature.toString(parameters[i]));
298                 }
299             } catch (IllegalArgumentException JavaDoc e) {
300                 // parameter signature is malformed
301
buffer.append("*** invalid signature: "); //$NON-NLS-1$
302
buffer.append(parameters[i]);
303             }
304         }
305     }
306     buffer.append(')');
307     if (this.occurrenceCount > 1) {
308         buffer.append("#"); //$NON-NLS-1$
309
buffer.append(this.occurrenceCount);
310     }
311 }
312 }
313
Popular Tags