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