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;
28 /* package */ class SourceMethod extends Member implements IMethod {
31 * The parameter type signatures of the method - stored locally
32 * to perform equality test. <code>null</code> indicates no
35 protected String[] fParameterTypes;
38 * An empty list of Strings
40 protected static final String[] fgEmptyList= new String[] {};
41 protected SourceMethod(JavaElement parent, String name, String[] parameterTypes) {
43 Assert.isTrue(name.indexOf('.') == -1);
44 if (parameterTypes == null) {
45 fParameterTypes= fgEmptyList;
47 fParameterTypes= parameterTypes;
50 public boolean equals(Object o) {
51 if (!(o instanceof SourceMethod)) return false;
52 return super.equals(o) && Util.equalArraysOrNull(fParameterTypes, ((SourceMethod)o).fParameterTypes);
55 * @see JavaElement#equalsDOMNode
57 protected boolean equalsDOMNode(IDOMNode node) throws JavaModelException {
58 if (node.getNodeType() == IDOMNode.METHOD) {
59 IDOMMethod m = (IDOMMethod)node;
60 if (isConstructor()) {
62 (m.isConstructor() || m.getName().equals(this.getElementName()) /* case of a constructor that is being renamed */)
63 && signatureEquals(m);
65 return super.equalsDOMNode(node) && signatureEquals(m);
75 public int getElementType() {
81 public String[] getExceptionTypes() throws JavaModelException {
82 SourceMethodElementInfo info = (SourceMethodElementInfo) getElementInfo();
83 char[][] exs= info.getExceptionTypeNames();
84 return CompilationUnitStructureRequestor.convertTypeNamesToSigs(exs);
87 * @see JavaElement#getHandleMemento()
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]);
97 return buff.toString();
100 * @see JavaElement#getHandleMemento()
102 protected char getHandleMementoDelimiter() {
103 return JavaElement.JEM_METHOD;
108 public int getNumberOfParameters() {
109 return fParameterTypes == null ? 0 : fParameterTypes.length;
114 public String[] getParameterNames() throws JavaModelException {
115 SourceMethodElementInfo info = (SourceMethodElementInfo) getElementInfo();
116 char[][] names= info.getArgumentNames();
117 if (names == null || names.length == 0) {
120 String[] strings= new String[names.length];
121 for (int i= 0; i < names.length; i++) {
122 strings[i]= new String(names[i]);
129 public String[] getParameterTypes() {
130 return fParameterTypes;
135 public String getReturnType() throws JavaModelException {
136 SourceMethodElementInfo info = (SourceMethodElementInfo) getElementInfo();
137 return Signature.createTypeSignature(info.getReturnTypeName(), false);
142 public String getSignature() throws JavaModelException {
143 SourceMethodElementInfo info = (SourceMethodElementInfo) getElementInfo();
144 return info.getSignature();
149 public boolean isConstructor() throws JavaModelException {
150 SourceMethodElementInfo info = (SourceMethodElementInfo) getElementInfo();
151 return info.isConstructor();
154 * @see IMethod#isMainMethod()
156 public boolean isMainMethod() throws JavaModelException {
157 return this.isMainMethod(this);
161 * @see IMethod#isSimilar(IMethod)
163 public boolean isSimilar(IMethod method) {
165 this.areSimilarMethods(
166 this.getElementName(), this.getParameterTypes(),
167 method.getElementName(), method.getParameterTypes(),
173 public String readableName() {
175 StringBuffer buffer = new StringBuffer(super.readableName());
177 String[] parameterTypes = this.getParameterTypes();
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$
188 return buffer.toString();
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>.
194 protected boolean signatureEquals(IDOMMethod method) throws JavaModelException {
195 String[] otherTypes= method.getParameterTypes();
196 String[] types= getParameterTypes();
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);
208 // ensure the parameter type signatures match
212 for (i= 0; i < types.length; i++) {
213 String otherType= Signature.createTypeSignature(otherTypes[i].toCharArray(), false);
214 if (!types[i].equals(otherType)) {
225 * @private Debugging purposes
227 protected void toStringInfo(int tab, StringBuffer buffer, Object info) {
228 buffer.append(this.tabString(tab));
230 buffer.append(getElementName());
231 buffer.append(" (not open)"); //$NON-NLS-1$
232 } else if (info == NO_INFO) {
233 buffer.append(getElementName());
236 if (Flags.isStatic(this.getFlags())) {
237 buffer.append("static "); //$NON-NLS-1$
239 if (!this.isConstructor()) {
240 buffer.append(Signature.toString(this.getReturnType()));
243 buffer.append(this.getElementName());
245 String[] parameterTypes = this.getParameterTypes();
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$
256 } catch (JavaModelException e) {
257 buffer.append("<JavaModelException in toString of " + getElementName()); //$NON-NLS-1$