A massive organize imports and formatting of the sources using default Eclipse code...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / core / SourceMethod.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core;
12
13 import net.sourceforge.phpdt.core.Flags;
14 import net.sourceforge.phpdt.core.IJavaElement;
15 import net.sourceforge.phpdt.core.IMethod;
16 import net.sourceforge.phpdt.core.JavaModelException;
17 import net.sourceforge.phpdt.core.Signature;
18 import net.sourceforge.phpdt.core.jdom.IDOMMethod;
19 import net.sourceforge.phpdt.core.jdom.IDOMNode;
20 import net.sourceforge.phpdt.internal.core.util.Util;
21 import net.sourceforge.phpdt.internal.corext.Assert;
22
23 /**
24  * @see IMethod
25  */
26
27 public class SourceMethod extends Member implements IMethod {
28
29         /**
30          * The parameter type signatures of the method - stored locally to perform
31          * equality test. <code>null</code> indicates no parameters.
32          */
33         protected String[] fParameterTypes;
34
35         /**
36          * An empty list of Strings
37          */
38         protected static final String[] fgEmptyList = new String[] {};
39
40         protected SourceMethod(JavaElement parent, String name,
41                         String[] parameterTypes) {
42                 super(parent, name);
43                 Assert.isTrue(name.indexOf('.') == -1);
44                 if (parameterTypes == null) {
45                         fParameterTypes = fgEmptyList;
46                 } else {
47                         fParameterTypes = parameterTypes;
48                 }
49         }
50
51         public boolean equals(Object o) {
52                 if (!(o instanceof SourceMethod))
53                         return false;
54                 return super.equals(o)
55                                 && Util.equalArraysOrNull(fParameterTypes,
56                                                 ((SourceMethod) o).fParameterTypes);
57         }
58
59         /**
60          * @see JavaElement#equalsDOMNode
61          */
62         protected boolean equalsDOMNode(IDOMNode node) throws JavaModelException {
63                 if (node.getNodeType() == IDOMNode.METHOD) {
64                         IDOMMethod m = (IDOMMethod) node;
65                         if (isConstructor()) {
66                                 return (m.isConstructor() || m.getName().equals(
67                                                 this.getElementName()) /*
68                                                                                                  * case of a constructor that is
69                                                                                                  * being renamed
70                                                                                                  */)
71                                                 && signatureEquals(m);
72                         } else {
73                                 return super.equalsDOMNode(node) && signatureEquals(m);
74                         }
75                 } else {
76                         return false;
77                 }
78
79         }
80
81         /**
82          * @see IJavaElement
83          */
84         public int getElementType() {
85                 return METHOD;
86         }
87
88         /**
89          * @see IMethod
90          */
91         public String[] getExceptionTypes() throws JavaModelException {
92                 SourceMethodElementInfo info = (SourceMethodElementInfo) getElementInfo();
93                 char[][] exs = info.getExceptionTypeNames();
94                 return CompilationUnitStructureRequestor.convertTypeNamesToSigs(exs);
95         }
96
97         /**
98          * @see JavaElement#getHandleMemento()
99          */
100         public String getHandleMemento() {
101                 StringBuffer buff = new StringBuffer(((JavaElement) getParent())
102                                 .getHandleMemento());
103                 buff.append(getHandleMementoDelimiter());
104                 buff.append(getElementName());
105                 for (int i = 0; i < fParameterTypes.length; i++) {
106                         buff.append(getHandleMementoDelimiter());
107                         buff.append(fParameterTypes[i]);
108                 }
109                 return buff.toString();
110         }
111
112         /**
113          * @see JavaElement#getHandleMemento()
114          */
115         protected char getHandleMementoDelimiter() {
116                 return JavaElement.JEM_METHOD;
117         }
118
119         /**
120          * @see IMethod
121          */
122         public int getNumberOfParameters() {
123                 return fParameterTypes == null ? 0 : fParameterTypes.length;
124         }
125
126         /**
127          * @see IMethod
128          */
129         public String[] getParameterNames() throws JavaModelException {
130                 SourceMethodElementInfo info = (SourceMethodElementInfo) getElementInfo();
131                 char[][] names = info.getArgumentNames();
132                 if (names == null || names.length == 0) {
133                         return fgEmptyList;
134                 }
135                 String[] strings = new String[names.length];
136                 for (int i = 0; i < names.length; i++) {
137                         strings[i] = new String(names[i]);
138                 }
139                 return strings;
140         }
141
142         /**
143          * @see IMethod
144          */
145         public String[] getParameterTypes() {
146                 return fParameterTypes;
147         }
148
149         /**
150          * @see IMethod
151          */
152         public String getReturnType() throws JavaModelException {
153                 SourceMethodElementInfo info = (SourceMethodElementInfo) getElementInfo();
154                 return Signature.createTypeSignature(info.getReturnTypeName(), false);
155         }
156
157         /**
158          * @see IMethod
159          */
160         public String getSignature() throws JavaModelException {
161                 SourceMethodElementInfo info = (SourceMethodElementInfo) getElementInfo();
162                 return info.getSignature();
163         }
164
165         /**
166          * @see IMethod
167          */
168         public boolean isConstructor() throws JavaModelException {
169                 SourceMethodElementInfo info = (SourceMethodElementInfo) getElementInfo();
170                 return info.isConstructor();
171         }
172
173         /**
174          * @see IMethod#isMainMethod()
175          */
176         public boolean isMainMethod() throws JavaModelException {
177                 return this.isMainMethod(this);
178         }
179
180         /**
181          * @see IMethod#isSimilar(IMethod)
182          */
183         public boolean isSimilar(IMethod method) {
184                 return this.areSimilarMethods(this.getElementName(), this
185                                 .getParameterTypes(), method.getElementName(), method
186                                 .getParameterTypes(), null);
187         }
188
189         /**
190          */
191         public String readableName() {
192
193                 StringBuffer buffer = new StringBuffer(super.readableName());
194                 buffer.append('(');
195                 String[] parameterTypes = this.getParameterTypes();
196                 int length;
197                 if (parameterTypes != null && (length = parameterTypes.length) > 0) {
198                         for (int i = 0; i < length; i++) {
199                                 buffer.append(Signature.toString(parameterTypes[i]));
200                                 if (i < length - 1) {
201                                         buffer.append(", "); //$NON-NLS-1$
202                                 }
203                         }
204                 }
205                 buffer.append(')');
206                 return buffer.toString();
207         }
208
209         /**
210          * Returns <code>true</code> if the signature of this
211          * <code>SourceMethod</code> matches that of the given
212          * <code>IDOMMethod</code>, otherwise <code>false</code>.
213          */
214         protected boolean signatureEquals(IDOMMethod method)
215                         throws JavaModelException {
216                 String[] otherTypes = method.getParameterTypes();
217                 String[] types = getParameterTypes();
218                 boolean ok = true;
219
220                 // ensure the number of parameters match
221                 if (otherTypes == null || otherTypes.length == 0) {
222                         ok = (types == null || types.length == 0);
223                 } else if (types != null) {
224                         ok = (otherTypes.length == types.length);
225                 } else {
226                         return false;
227                 }
228
229                 // ensure the parameter type signatures match
230                 if (ok) {
231                         if (types != null) {
232                                 int i;
233                                 for (i = 0; i < types.length; i++) {
234                                         String otherType = Signature.createTypeSignature(
235                                                         otherTypes[i].toCharArray(), false);
236                                         if (!types[i].equals(otherType)) {
237                                                 ok = false;
238                                                 break;
239                                         }
240                                 }
241                         }
242                 }
243
244                 return ok;
245         }
246
247         /**
248          * @private Debugging purposes
249          */
250         protected void toStringInfo(int tab, StringBuffer buffer, Object info) {
251                 buffer.append(this.tabString(tab));
252                 if (info == null) {
253                         buffer.append(getElementName());
254                         buffer.append(" (not open)"); //$NON-NLS-1$
255                 } else if (info == NO_INFO) {
256                         buffer.append(getElementName());
257                 } else {
258                         try {
259                                 if (Flags.isStatic(this.getFlags())) {
260                                         buffer.append("static "); //$NON-NLS-1$
261                                 }
262                                 if (!this.isConstructor()) {
263                                         buffer.append(Signature.toString(this.getReturnType()));
264                                         buffer.append(' ');
265                                 }
266                                 buffer.append(this.getElementName());
267                                 buffer.append('(');
268                                 String[] parameterTypes = this.getParameterTypes();
269                                 int length;
270                                 if (parameterTypes != null
271                                                 && (length = parameterTypes.length) > 0) {
272                                         for (int i = 0; i < length; i++) {
273                                                 buffer.append(Signature.toString(parameterTypes[i]));
274                                                 if (i < length - 1) {
275                                                         buffer.append(", "); //$NON-NLS-1$
276                                                 }
277                                         }
278                                 }
279                                 buffer.append(')');
280                         } catch (JavaModelException e) {
281                                 buffer
282                                                 .append("<JavaModelException in toString of " + getElementName()); //$NON-NLS-1$
283                         }
284                 }
285         }
286 }