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;
12 import java.util.List;
15 * A Method declaration.
16 * @author Matthieu Casanova
18 public class MethodDeclaration extends Statement implements OutlineableWithChildren {
20 /** The name of the method. */
22 public Hashtable arguments;
25 public Statement[] statements;
27 public int bodyEnd = -1;
28 /** Tell if the method is a class constructor. */
29 public boolean isConstructor;
31 /** The parent object. */
32 private Object parent;
33 /** The outlineable children (those will be in the node array too. */
34 private ArrayList children = new ArrayList();
36 /** Tell if the method returns a reference. */
37 public boolean reference;
39 private Position position;
41 public MethodDeclaration(Object parent,
47 super(sourceStart, sourceEnd);
49 this.arguments = arguments;
51 this.reference = reference;
52 position = new Position(sourceStart, sourceEnd);
56 * Return method into String, with a number of tabs
57 * @param tab the number of tabs
58 * @return the String containing the method
60 public String toString(int tab) {
61 StringBuffer buff = new StringBuffer(tabString(tab));
62 buff.append(toStringHeader());
63 buff.append(toStringStatements(tab + 1));
64 return buff.toString();
67 public String toStringHeader() {
68 return "function " + toString();
72 * Return the statements of the method into Strings
73 * @param tab the number of tabs
74 * @return the String containing the statements
76 public String toStringStatements(int tab) {
77 StringBuffer buff = new StringBuffer(" {"); //$NON-NLS-1$
78 if (statements != null) {
79 for (int i = 0; i < statements.length; i++) {
80 buff.append("\n").append(statements[i].toString(tab)); //$NON-NLS-1$
81 if (!(statements[i] instanceof Block)) {
82 buff.append(";"); //$NON-NLS-1$
86 buff.append("\n").append(tabString(tab == 0 ? 0 : tab - 1)).append("}"); //$NON-NLS-2$ //$NON-NLS-1$
87 return buff.toString();
91 * Get the image of a class.
92 * @return the image that represents a php class
94 public ImageDescriptor getImage() {
95 return PHPUiImages.DESC_FUN;
98 public void setParent(Object parent) {
102 public Object getParent() {
106 public boolean add(Outlineable o) {
107 return children.add(o);
110 public Outlineable get(int index) {
111 return (Outlineable) children.get(index);
115 return children.size();
118 public String toString() {
119 StringBuffer buff = new StringBuffer();
121 buff.append("&");//$NON-NLS-1$
123 buff.append(name).append("(");//$NON-NLS-1$
125 if (arguments != null) {
126 Enumeration values = arguments.elements();
128 while (values.hasMoreElements()) {
129 VariableDeclaration o = (VariableDeclaration) values.nextElement();
130 buff.append(o.toStringExpression());
131 if (i != (arguments.size() - 1)) {
132 buff.append(", "); //$NON-NLS-1$
137 buff.append(")"); //$NON-NLS-1$
138 return buff.toString();
141 public Position getPosition() {
145 public List getList() {