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;
8 import java.util.Hashtable;
9 import java.util.Enumeration;
10 import java.util.ArrayList;
13 * A Method declaration.
14 * @author Matthieu Casanova
16 public class MethodDeclaration extends Statement implements OutlineableWithChildren {
18 /** The name of the method. */
20 public Hashtable arguments;
23 public Statement[] statements;
25 public int bodyEnd = -1;
26 /** Tell if the method is a class constructor. */
27 public boolean isConstructor;
29 /** The parent object. */
30 private Object parent;
31 /** The outlineable children (those will be in the node array too. */
32 private ArrayList children = new ArrayList();
34 /** Tell if the method returns a reference. */
35 public boolean reference;
37 public MethodDeclaration(Object parent,
43 super(sourceStart, sourceEnd);
45 this.arguments = arguments;
47 this.reference = reference;
51 * Return method into String, with a number of tabs
52 * @param tab the number of tabs
53 * @return the String containing the method
55 public String toString(int tab) {
56 StringBuffer buff = new StringBuffer(tabString(tab));
57 buff.append(toStringHeader());
58 buff.append(toStringStatements(tab + 1));
59 return buff.toString();
62 public String toStringHeader() {
63 StringBuffer buff = new StringBuffer();
64 buff.append("function ");//$NON-NLS-1$
66 buff.append('&');//$NON-NLS-1$
68 buff.append(name).append("(");//$NON-NLS-1$
70 if (arguments != null) {
71 Enumeration values = arguments.elements();
73 while (values.hasMoreElements()) {
74 VariableDeclaration o = (VariableDeclaration) values.nextElement();
75 buff.append(o.toStringExpression());
76 if (i != (arguments.size() - 1)) {
77 buff.append(", "); //$NON-NLS-1$
82 buff.append(")"); //$NON-NLS-1$
83 return buff.toString();
87 * Return the statements of the method into Strings
88 * @param tab the number of tabs
89 * @return the String containing the statements
91 public String toStringStatements(int tab) {
92 StringBuffer buff = new StringBuffer(" {"); //$NON-NLS-1$
93 if (statements != null) {
94 for (int i = 0; i < statements.length; i++) {
95 buff.append("\n").append(statements[i].toString(tab)); //$NON-NLS-1$
96 if (!(statements[i] instanceof Block)) {
97 buff.append(";"); //$NON-NLS-1$
101 buff.append("\n").append(tabString(tab == 0 ? 0 : tab - 1)).append("}"); //$NON-NLS-2$ //$NON-NLS-1$
102 return buff.toString();
106 * Get the image of a class.
107 * @return the image that represents a php class
109 public ImageDescriptor getImage() {
110 return PHPUiImages.DESC_FUN;
113 public void setParent(Object parent) {
114 this.parent = parent;
117 public Object getParent() {
121 public boolean add(Outlineable o) {
122 return children.add(o);
125 public Outlineable get(int index) {
126 return (Outlineable) children.get(index);
130 return children.size();
133 public String toString() {
134 return toStringHeader();