1 package net.sourceforge.phpdt.internal.compiler.ast;
3 import net.sourceforge.phpdt.internal.compiler.parser.OutlineableWithChildren;
4 import net.sourceforge.phpdt.internal.compiler.parser.Outlineable;
5 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
6 import org.eclipse.jface.resource.ImageDescriptor;
7 import org.eclipse.jface.text.Position;
9 import java.util.Hashtable;
10 import java.util.Enumeration;
11 import java.util.ArrayList;
14 * A Method declaration.
15 * @author Matthieu Casanova
17 public class MethodDeclaration extends Statement implements OutlineableWithChildren {
19 /** The name of the method. */
21 public Hashtable arguments;
24 public Statement[] statements;
26 public int bodyEnd = -1;
27 /** Tell if the method is a class constructor. */
28 public boolean isConstructor;
30 /** The parent object. */
31 private Object parent;
32 /** The outlineable children (those will be in the node array too. */
33 private ArrayList children = new ArrayList();
35 /** Tell if the method returns a reference. */
36 public boolean reference;
38 private Position position;
40 public MethodDeclaration(Object parent,
46 super(sourceStart, sourceEnd);
48 this.arguments = arguments;
50 this.reference = reference;
51 position = new Position(sourceStart, sourceEnd);
55 * Return method into String, with a number of tabs
56 * @param tab the number of tabs
57 * @return the String containing the method
59 public String toString(int tab) {
60 StringBuffer buff = new StringBuffer(tabString(tab));
61 buff.append(toStringHeader());
62 buff.append(toStringStatements(tab + 1));
63 return buff.toString();
66 public String toStringHeader() {
67 StringBuffer buff = new StringBuffer();
68 buff.append("function ");//$NON-NLS-1$
70 buff.append('&');//$NON-NLS-1$
72 buff.append(name).append("(");//$NON-NLS-1$
74 if (arguments != null) {
75 Enumeration values = arguments.elements();
77 while (values.hasMoreElements()) {
78 VariableDeclaration o = (VariableDeclaration) values.nextElement();
79 buff.append(o.toStringExpression());
80 if (i != (arguments.size() - 1)) {
81 buff.append(", "); //$NON-NLS-1$
86 buff.append(")"); //$NON-NLS-1$
87 return buff.toString();
91 * Return the statements of the method into Strings
92 * @param tab the number of tabs
93 * @return the String containing the statements
95 public String toStringStatements(int tab) {
96 StringBuffer buff = new StringBuffer(" {"); //$NON-NLS-1$
97 if (statements != null) {
98 for (int i = 0; i < statements.length; i++) {
99 buff.append("\n").append(statements[i].toString(tab)); //$NON-NLS-1$
100 if (!(statements[i] instanceof Block)) {
101 buff.append(";"); //$NON-NLS-1$
105 buff.append("\n").append(tabString(tab == 0 ? 0 : tab - 1)).append("}"); //$NON-NLS-2$ //$NON-NLS-1$
106 return buff.toString();
110 * Get the image of a class.
111 * @return the image that represents a php class
113 public ImageDescriptor getImage() {
114 return PHPUiImages.DESC_FUN;
117 public void setParent(Object parent) {
118 this.parent = parent;
121 public Object getParent() {
125 public boolean add(Outlineable o) {
126 return children.add(o);
129 public Outlineable get(int index) {
130 return (Outlineable) children.get(index);
134 return children.size();
137 public String toString() {
138 return toStringHeader();
141 public Position getPosition() {