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