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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core;
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;
22 //import net.sourceforge.phpdt.internal.corext.Assert;
23 import org.eclipse.core.runtime.Assert;
29 public class SourceMethod extends Member implements IMethod {
32 * The parameter type signatures of the method - stored locally to perform
33 * equality test. <code>null</code> indicates no parameters.
35 protected String[] fParameterTypes;
38 * An empty list of Strings
40 protected static final String[] fgEmptyList = new String[] {};
42 protected SourceMethod(JavaElement parent, String name,
43 String[] parameterTypes) {
45 Assert.isTrue(name.indexOf('.') == -1);
46 if (parameterTypes == null) {
47 fParameterTypes = fgEmptyList;
49 fParameterTypes = parameterTypes;
53 public boolean equals(Object o) {
54 if (!(o instanceof SourceMethod))
56 return super.equals(o)
57 && Util.equalArraysOrNull(fParameterTypes,
58 ((SourceMethod) o).fParameterTypes);
62 * @see JavaElement#equalsDOMNode
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
73 && signatureEquals(m);
75 return super.equalsDOMNode(node) && signatureEquals(m);
86 public int getElementType() {
93 public String[] getExceptionTypes() throws JavaModelException {
94 SourceMethodElementInfo info = (SourceMethodElementInfo) getElementInfo();
95 char[][] exs = info.getExceptionTypeNames();
96 return CompilationUnitStructureRequestor.convertTypeNamesToSigs(exs);
100 * @see JavaElement#getHandleMemento()
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]);
111 return buff.toString();
115 * @see JavaElement#getHandleMemento()
117 protected char getHandleMementoDelimiter() {
118 return JavaElement.JEM_METHOD;
124 public int getNumberOfParameters() {
125 return fParameterTypes == null ? 0 : fParameterTypes.length;
131 public String[] getParameterNames() throws JavaModelException {
132 SourceMethodElementInfo info = (SourceMethodElementInfo) getElementInfo();
133 char[][] names = info.getArgumentNames();
134 if (names == null || names.length == 0) {
137 String[] strings = new String[names.length];
138 for (int i = 0; i < names.length; i++) {
139 strings[i] = new String(names[i]);
147 public String[] getParameterTypes() {
148 return fParameterTypes;
154 public String getReturnType() throws JavaModelException {
155 SourceMethodElementInfo info = (SourceMethodElementInfo) getElementInfo();
156 return Signature.createTypeSignature(info.getReturnTypeName(), false);
162 public String getSignature() throws JavaModelException {
163 SourceMethodElementInfo info = (SourceMethodElementInfo) getElementInfo();
164 return info.getSignature();
170 public boolean isConstructor() throws JavaModelException {
171 SourceMethodElementInfo info = (SourceMethodElementInfo) getElementInfo();
172 return info.isConstructor();
176 * @see IMethod#isMainMethod()
178 public boolean isMainMethod() throws JavaModelException {
179 return this.isMainMethod(this);
183 * @see IMethod#isSimilar(IMethod)
185 public boolean isSimilar(IMethod method) {
186 return this.areSimilarMethods(this.getElementName(), this
187 .getParameterTypes(), method.getElementName(), method
188 .getParameterTypes(), null);
193 public String readableName() {
195 StringBuffer buffer = new StringBuffer(super.readableName());
197 String[] parameterTypes = this.getParameterTypes();
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$
208 return buffer.toString();
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>.
216 protected boolean signatureEquals(IDOMMethod method)
217 throws JavaModelException {
218 String[] otherTypes = method.getParameterTypes();
219 String[] types = getParameterTypes();
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);
231 // ensure the parameter type signatures match
235 for (i = 0; i < types.length; i++) {
236 String otherType = Signature.createTypeSignature(
237 otherTypes[i].toCharArray(), false);
238 if (!types[i].equals(otherType)) {
250 * @private Debugging purposes
252 protected void toStringInfo(int tab, StringBuffer buffer, Object info) {
253 buffer.append(this.tabString(tab));
255 buffer.append(getElementName());
256 buffer.append(" (not open)"); //$NON-NLS-1$
257 } else if (info == NO_INFO) {
258 buffer.append(getElementName());
261 if (Flags.isStatic(this.getFlags())) {
262 buffer.append("static "); //$NON-NLS-1$
264 if (!this.isConstructor()) {
265 buffer.append(Signature.toString(this.getReturnType()));
268 buffer.append(this.getElementName());
270 String[] parameterTypes = this.getParameterTypes();
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$
282 } catch (JavaModelException e) {
284 .append("<JavaModelException in toString of " + getElementName()); //$NON-NLS-1$