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.internal.core.JavaElement;
15 import net.sourceforge.phpdt.core.IJavaElement;
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;
33 /* package */ class SourceMethod extends Member implements IMethod {
36 * The parameter type signatures of the method - stored locally
37 * to perform equality test. <code>null</code> indicates no
40 protected String[] fParameterTypes;
43 * An empty list of Strings
45 protected static final String[] fgEmptyList= new String[] {};
46 protected SourceMethod(JavaElement parent, String name, String[] parameterTypes) {
48 Assert.isTrue(name.indexOf('.') == -1);
49 if (parameterTypes == null) {
50 fParameterTypes= fgEmptyList;
52 fParameterTypes= parameterTypes;
55 public boolean equals(Object o) {
56 if (!(o instanceof SourceMethod)) return false;
57 return super.equals(o) && Util.equalArraysOrNull(fParameterTypes, ((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()) {
67 (m.isConstructor() || m.getName().equals(this.getElementName()) /* case of a constructor that is being renamed */)
68 && signatureEquals(m);
70 return super.equalsDOMNode(node) && signatureEquals(m);
80 public int getElementType() {
86 public String[] getExceptionTypes() throws JavaModelException {
87 SourceMethodElementInfo info = (SourceMethodElementInfo) getElementInfo();
88 char[][] exs= info.getExceptionTypeNames();
89 return CompilationUnitStructureRequestor.convertTypeNamesToSigs(exs);
92 * @see JavaElement#getHandleMemento()
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]);
102 return buff.toString();
105 * @see JavaElement#getHandleMemento()
107 protected char getHandleMementoDelimiter() {
108 return JavaElement.JEM_METHOD;
113 public int getNumberOfParameters() {
114 return fParameterTypes == null ? 0 : fParameterTypes.length;
119 public String[] getParameterNames() throws JavaModelException {
120 SourceMethodElementInfo info = (SourceMethodElementInfo) getElementInfo();
121 char[][] names= info.getArgumentNames();
122 if (names == null || names.length == 0) {
125 String[] strings= new String[names.length];
126 for (int i= 0; i < names.length; i++) {
127 strings[i]= new String(names[i]);
134 public String[] getParameterTypes() {
135 return fParameterTypes;
140 public String getReturnType() throws JavaModelException {
141 SourceMethodElementInfo info = (SourceMethodElementInfo) getElementInfo();
142 return Signature.createTypeSignature(info.getReturnTypeName(), false);
147 public String getSignature() throws JavaModelException {
148 SourceMethodElementInfo info = (SourceMethodElementInfo) getElementInfo();
149 return info.getSignature();
154 public boolean isConstructor() throws JavaModelException {
155 SourceMethodElementInfo info = (SourceMethodElementInfo) getElementInfo();
156 return info.isConstructor();
159 * @see IMethod#isMainMethod()
161 public boolean isMainMethod() throws JavaModelException {
162 return this.isMainMethod(this);
166 * @see IMethod#isSimilar(IMethod)
168 public boolean isSimilar(IMethod method) {
170 this.areSimilarMethods(
171 this.getElementName(), this.getParameterTypes(),
172 method.getElementName(), method.getParameterTypes(),
178 public String readableName() {
180 StringBuffer buffer = new StringBuffer(super.readableName());
182 String[] parameterTypes = this.getParameterTypes();
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$
193 return buffer.toString();
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>.
199 protected boolean signatureEquals(IDOMMethod method) throws JavaModelException {
200 String[] otherTypes= method.getParameterTypes();
201 String[] types= getParameterTypes();
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);
213 // ensure the parameter type signatures match
217 for (i= 0; i < types.length; i++) {
218 String otherType= Signature.createTypeSignature(otherTypes[i].toCharArray(), false);
219 if (!types[i].equals(otherType)) {
230 * @private Debugging purposes
232 protected void toStringInfo(int tab, StringBuffer buffer, Object info) {
233 buffer.append(this.tabString(tab));
235 buffer.append(getElementName());
236 buffer.append(" (not open)"); //$NON-NLS-1$
237 } else if (info == NO_INFO) {
238 buffer.append(getElementName());
241 if (Flags.isStatic(this.getFlags())) {
242 buffer.append("static "); //$NON-NLS-1$
244 if (!this.isConstructor()) {
245 buffer.append(Signature.toString(this.getReturnType()));
248 buffer.append(this.getElementName());
250 String[] parameterTypes = this.getParameterTypes();
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$
261 } catch (JavaModelException e) {
262 buffer.append("<JavaModelException in toString of " + getElementName()); //$NON-NLS-1$