*** empty log message ***
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / ast / MethodDeclaration.java
1 package net.sourceforge.phpdt.internal.compiler.ast;
2
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
8 import java.util.Hashtable;
9 import java.util.Enumeration;
10 import java.util.ArrayList;
11
12 /**
13  * A Method declaration.
14  * @author Matthieu Casanova
15  */
16 public class MethodDeclaration extends Statement implements OutlineableWithChildren {
17
18   /** The name of the method. */
19   public char[] name;
20   public Hashtable arguments;
21
22
23   public Statement[] statements;
24   public int bodyStart;
25   public int bodyEnd = -1;
26   /** Tell if the method is a class constructor. */
27   public boolean isConstructor;
28
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();
33
34   /** Tell if the method returns a reference. */
35   public boolean reference;
36
37   public MethodDeclaration(Object parent,
38                            char[] name,
39                            Hashtable arguments,
40                            boolean reference,
41                            int sourceStart,
42                            int sourceEnd) {
43     super(sourceStart, sourceEnd);
44     this.name = name;
45     this.arguments = arguments;
46     this.parent = parent;
47     this.reference = reference;
48   }
49
50   /**
51    * Return method into String, with a number of tabs
52    * @param tab the number of tabs
53    * @return the String containing the method
54    */
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();
60   }
61
62   public String toStringHeader() {
63     StringBuffer buff = new StringBuffer();
64     buff.append("function ");//$NON-NLS-1$
65     if (reference) {
66       buff.append('&');//$NON-NLS-1$
67     }
68     buff.append(name).append("(");//$NON-NLS-1$
69
70     if (arguments != null) {
71       Enumeration values = arguments.elements();
72       int i = 0;
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$
78         }
79         i++;
80       }
81     }
82     buff.append(")"); //$NON-NLS-1$
83     return buff.toString();
84   }
85
86   /**
87    * Return the statements of the method into Strings
88    * @param tab the number of tabs
89    * @return the String containing the statements
90    */
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$
98         }
99       }
100     }
101     buff.append("\n").append(tabString(tab == 0 ? 0 : tab - 1)).append("}"); //$NON-NLS-2$ //$NON-NLS-1$
102     return buff.toString();
103   }
104
105   /**
106    * Get the image of a class.
107    * @return the image that represents a php class
108    */
109   public ImageDescriptor getImage() {
110     return PHPUiImages.DESC_FUN;
111   }
112
113   public void setParent(Object parent) {
114     this.parent = parent;
115   }
116
117   public Object getParent() {
118     return parent;
119   }
120
121   public boolean add(Outlineable o) {
122     return children.add(o);
123   }
124
125   public Outlineable get(int index) {
126     return (Outlineable) children.get(index);
127   }
128
129   public int size() {
130     return children.size();
131   }
132
133   public String toString() {
134     return toStringHeader();
135   }
136 }