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.ICompilationUnit;
15 import net.sourceforge.phpdt.core.IMethod;
16 import net.sourceforge.phpdt.core.IType;
17 import net.sourceforge.phpdt.core.JavaModelException;
18 import net.sourceforge.phpdt.core.Signature;
19 import net.sourceforge.phpdt.core.jdom.IDOMMethod;
20 import net.sourceforge.phpdt.core.jdom.IDOMNode;
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(IType parent, String name, String[] parameterTypes) {
42 super(METHOD, parent, name);
43 Assert.isTrue(name.indexOf('.') == -1);
44 if (parameterTypes == null) {
45 fParameterTypes= fgEmptyList;
47 fParameterTypes= parameterTypes;
50 protected SourceMethod(ICompilationUnit parent, String name, String[] parameterTypes) {
51 super(METHOD, parent, name);
52 Assert.isTrue(name.indexOf('.') == -1);
53 if (parameterTypes == null) {
54 fParameterTypes= fgEmptyList;
56 fParameterTypes= parameterTypes;
59 public boolean equals(Object o) {
60 return super.equals(o) && Util.equalArraysOrNull(fParameterTypes, ((SourceMethod)o).fParameterTypes);
63 * @see JavaElement#equalsDOMNode
65 protected boolean equalsDOMNode(IDOMNode node) throws JavaModelException {
66 if (node.getNodeType() == IDOMNode.METHOD) {
67 IDOMMethod m = (IDOMMethod)node;
68 if (isConstructor()) {
70 (m.isConstructor() || m.getName().equals(this.getElementName()) /* case of a constructor that is being renamed */)
71 && signatureEquals(m);
73 return super.equalsDOMNode(node) && signatureEquals(m);
83 public String[] getExceptionTypes() throws JavaModelException {
84 SourceMethodElementInfo info = (SourceMethodElementInfo) getElementInfo();
85 char[][] exs= info.getExceptionTypeNames();
86 return CompilationUnitStructureRequestor.convertTypeNamesToSigs(exs);
89 * @see JavaElement#getHandleMemento()
91 public String getHandleMemento() {
92 StringBuffer buff = new StringBuffer(((JavaElement) getParent()).getHandleMemento());
93 buff.append(getHandleMementoDelimiter());
94 buff.append(getElementName());
95 for (int i = 0; i < fParameterTypes.length; i++) {
96 buff.append(getHandleMementoDelimiter());
97 buff.append(fParameterTypes[i]);
99 return buff.toString();
102 * @see JavaElement#getHandleMemento()
104 protected char getHandleMementoDelimiter() {
105 return JavaElement.JEM_METHOD;
110 public int getNumberOfParameters() {
111 return fParameterTypes == null ? 0 : fParameterTypes.length;
116 public String[] getParameterNames() throws JavaModelException {
117 SourceMethodElementInfo info = (SourceMethodElementInfo) getElementInfo();
118 char[][] names= info.getArgumentNames();
119 if (names == null || names.length == 0) {
122 String[] strings= new String[names.length];
123 for (int i= 0; i < names.length; i++) {
124 strings[i]= new String(names[i]);
131 public String[] getParameterTypes() {
132 return fParameterTypes;
137 public String getReturnType() throws JavaModelException {
138 SourceMethodElementInfo info = (SourceMethodElementInfo) getElementInfo();
139 return Signature.createTypeSignature(info.getReturnTypeName(), false);
144 public String getSignature() throws JavaModelException {
145 SourceMethodElementInfo info = (SourceMethodElementInfo) getElementInfo();
146 return info.getSignature();
151 public boolean isConstructor() throws JavaModelException {
152 SourceMethodElementInfo info = (SourceMethodElementInfo) getElementInfo();
153 return info.isConstructor();
156 * @see IMethod#isMainMethod()
158 public boolean isMainMethod() throws JavaModelException {
159 return this.isMainMethod(this);
163 * @see IMethod#isSimilar(IMethod)
165 public boolean isSimilar(IMethod method) {
167 this.areSimilarMethods(
168 this.getElementName(), this.getParameterTypes(),
169 method.getElementName(), method.getParameterTypes(),
175 public String readableName() {
177 StringBuffer buffer = new StringBuffer(super.readableName());
179 String[] parameterTypes = this.getParameterTypes();
181 if (parameterTypes != null && (length = parameterTypes.length) > 0) {
182 for (int i = 0; i < length; i++) {
183 buffer.append(Signature.toString(parameterTypes[i]));
184 if (i < length - 1) {
185 buffer.append(", "); //$NON-NLS-1$
190 return buffer.toString();
193 * Returns <code>true</code> if the signature of this <code>SourceMethod</code> matches that of the given
194 * <code>IDOMMethod</code>, otherwise <code>false</code>.
196 protected boolean signatureEquals(IDOMMethod method) throws JavaModelException {
197 String[] otherTypes= method.getParameterTypes();
198 String[] types= getParameterTypes();
201 // ensure the number of parameters match
202 if (otherTypes == null || otherTypes.length == 0) {
203 ok= (types == null || types.length == 0);
204 } else if (types != null) {
205 ok= (otherTypes.length == types.length);
210 // ensure the parameter type signatures match
214 for (i= 0; i < types.length; i++) {
215 String otherType= Signature.createTypeSignature(otherTypes[i].toCharArray(), false);
216 if (!types[i].equals(otherType)) {
227 * @private Debugging purposes
229 protected void toStringInfo(int tab, StringBuffer buffer, Object info) {
230 buffer.append(this.tabString(tab));
232 buffer.append(getElementName());
233 buffer.append(" (not open)"); //$NON-NLS-1$
234 } else if (info == NO_INFO) {
235 buffer.append(getElementName());
238 if (Flags.isStatic(this.getFlags())) {
239 buffer.append("static "); //$NON-NLS-1$
241 if (!this.isConstructor()) {
242 buffer.append(Signature.toString(this.getReturnType()));
245 buffer.append(this.getElementName());
247 String[] parameterTypes = this.getParameterTypes();
249 if (parameterTypes != null && (length = parameterTypes.length) > 0) {
250 for (int i = 0; i < length; i++) {
251 buffer.append(Signature.toString(parameterTypes[i]));
252 if (i < length - 1) {
253 buffer.append(", "); //$NON-NLS-1$
258 } catch (JavaModelException e) {
259 buffer.append("<JavaModelException in toString of " + getElementName()); //$NON-NLS-1$