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