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;
21 import net.sourceforge.phpdt.internal.corext.Assert;
27 public class SourceMethod extends Member implements IMethod {
30 * The parameter type signatures of the method - stored locally to perform
31 * equality test. <code>null</code> indicates no parameters.
33 protected String[] fParameterTypes;
36 * An empty list of Strings
38 protected static final String[] fgEmptyList = new String[] {};
40 protected SourceMethod(JavaElement parent, String name,
41 String[] parameterTypes) {
43 Assert.isTrue(name.indexOf('.') == -1);
44 if (parameterTypes == null) {
45 fParameterTypes = fgEmptyList;
47 fParameterTypes = parameterTypes;
51 public boolean equals(Object o) {
52 if (!(o instanceof SourceMethod))
54 return super.equals(o)
55 && Util.equalArraysOrNull(fParameterTypes,
56 ((SourceMethod) o).fParameterTypes);
60 * @see JavaElement#equalsDOMNode
62 protected boolean equalsDOMNode(IDOMNode node) throws JavaModelException {
63 if (node.getNodeType() == IDOMNode.METHOD) {
64 IDOMMethod m = (IDOMMethod) node;
65 if (isConstructor()) {
66 return (m.isConstructor() || m.getName().equals(
67 this.getElementName()) /*
68 * case of a constructor that is
71 && signatureEquals(m);
73 return super.equalsDOMNode(node) && signatureEquals(m);
84 public int getElementType() {
91 public String[] getExceptionTypes() throws JavaModelException {
92 SourceMethodElementInfo info = (SourceMethodElementInfo) getElementInfo();
93 char[][] exs = info.getExceptionTypeNames();
94 return CompilationUnitStructureRequestor.convertTypeNamesToSigs(exs);
98 * @see JavaElement#getHandleMemento()
100 public String getHandleMemento() {
101 StringBuffer buff = new StringBuffer(((JavaElement) getParent())
102 .getHandleMemento());
103 buff.append(getHandleMementoDelimiter());
104 buff.append(getElementName());
105 for (int i = 0; i < fParameterTypes.length; i++) {
106 buff.append(getHandleMementoDelimiter());
107 buff.append(fParameterTypes[i]);
109 return buff.toString();
113 * @see JavaElement#getHandleMemento()
115 protected char getHandleMementoDelimiter() {
116 return JavaElement.JEM_METHOD;
122 public int getNumberOfParameters() {
123 return fParameterTypes == null ? 0 : fParameterTypes.length;
129 public String[] getParameterNames() throws JavaModelException {
130 SourceMethodElementInfo info = (SourceMethodElementInfo) getElementInfo();
131 char[][] names = info.getArgumentNames();
132 if (names == null || names.length == 0) {
135 String[] strings = new String[names.length];
136 for (int i = 0; i < names.length; i++) {
137 strings[i] = new String(names[i]);
145 public String[] getParameterTypes() {
146 return fParameterTypes;
152 public String getReturnType() throws JavaModelException {
153 SourceMethodElementInfo info = (SourceMethodElementInfo) getElementInfo();
154 return Signature.createTypeSignature(info.getReturnTypeName(), false);
160 public String getSignature() throws JavaModelException {
161 SourceMethodElementInfo info = (SourceMethodElementInfo) getElementInfo();
162 return info.getSignature();
168 public boolean isConstructor() throws JavaModelException {
169 SourceMethodElementInfo info = (SourceMethodElementInfo) getElementInfo();
170 return info.isConstructor();
174 * @see IMethod#isMainMethod()
176 public boolean isMainMethod() throws JavaModelException {
177 return this.isMainMethod(this);
181 * @see IMethod#isSimilar(IMethod)
183 public boolean isSimilar(IMethod method) {
184 return this.areSimilarMethods(this.getElementName(), this
185 .getParameterTypes(), method.getElementName(), method
186 .getParameterTypes(), null);
191 public String readableName() {
193 StringBuffer buffer = new StringBuffer(super.readableName());
195 String[] parameterTypes = this.getParameterTypes();
197 if (parameterTypes != null && (length = parameterTypes.length) > 0) {
198 for (int i = 0; i < length; i++) {
199 buffer.append(Signature.toString(parameterTypes[i]));
200 if (i < length - 1) {
201 buffer.append(", "); //$NON-NLS-1$
206 return buffer.toString();
210 * Returns <code>true</code> if the signature of this
211 * <code>SourceMethod</code> matches that of the given
212 * <code>IDOMMethod</code>, otherwise <code>false</code>.
214 protected boolean signatureEquals(IDOMMethod method)
215 throws JavaModelException {
216 String[] otherTypes = method.getParameterTypes();
217 String[] types = getParameterTypes();
220 // ensure the number of parameters match
221 if (otherTypes == null || otherTypes.length == 0) {
222 ok = (types == null || types.length == 0);
223 } else if (types != null) {
224 ok = (otherTypes.length == types.length);
229 // ensure the parameter type signatures match
233 for (i = 0; i < types.length; i++) {
234 String otherType = Signature.createTypeSignature(
235 otherTypes[i].toCharArray(), false);
236 if (!types[i].equals(otherType)) {
248 * @private Debugging purposes
250 protected void toStringInfo(int tab, StringBuffer buffer, Object info) {
251 buffer.append(this.tabString(tab));
253 buffer.append(getElementName());
254 buffer.append(" (not open)"); //$NON-NLS-1$
255 } else if (info == NO_INFO) {
256 buffer.append(getElementName());
259 if (Flags.isStatic(this.getFlags())) {
260 buffer.append("static "); //$NON-NLS-1$
262 if (!this.isConstructor()) {
263 buffer.append(Signature.toString(this.getReturnType()));
266 buffer.append(this.getElementName());
268 String[] parameterTypes = this.getParameterTypes();
270 if (parameterTypes != null
271 && (length = parameterTypes.length) > 0) {
272 for (int i = 0; i < length; i++) {
273 buffer.append(Signature.toString(parameterTypes[i]));
274 if (i < length - 1) {
275 buffer.append(", "); //$NON-NLS-1$
280 } catch (JavaModelException e) {
282 .append("<JavaModelException in toString of " + getElementName()); //$NON-NLS-1$