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