*** 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 import org.eclipse.jface.text.Position;
8
9 import java.util.Hashtable;
10 import java.util.Enumeration;
11 import java.util.ArrayList;
12
13 /**
14  * A Method declaration.
15  * @author Matthieu Casanova
16  */
17 public class MethodDeclaration extends Statement implements OutlineableWithChildren {
18
19   /** The name of the method. */
20   public char[] name;
21   public Hashtable arguments;
22
23
24   public Statement[] statements;
25   public int bodyStart;
26   public int bodyEnd = -1;
27   /** Tell if the method is a class constructor. */
28   public boolean isConstructor;
29
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();
34
35   /** Tell if the method returns a reference. */
36   public boolean reference;
37
38   private Position position;
39
40   public MethodDeclaration(Object parent,
41                            char[] name,
42                            Hashtable arguments,
43                            boolean reference,
44                            int sourceStart,
45                            int sourceEnd) {
46     super(sourceStart, sourceEnd);
47     this.name = name;
48     this.arguments = arguments;
49     this.parent = parent;
50     this.reference = reference;
51     position = new Position(sourceStart, sourceEnd);
52   }
53
54   /**
55    * Return method into String, with a number of tabs
56    * @param tab the number of tabs
57    * @return the String containing the method
58    */
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();
64   }
65
66   public String toStringHeader() {
67     StringBuffer buff = new StringBuffer();
68     buff.append("function ");//$NON-NLS-1$
69     if (reference) {
70       buff.append('&');//$NON-NLS-1$
71     }
72     buff.append(name).append("(");//$NON-NLS-1$
73
74     if (arguments != null) {
75       Enumeration values = arguments.elements();
76       int i = 0;
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$
82         }
83         i++;
84       }
85     }
86     buff.append(")"); //$NON-NLS-1$
87     return buff.toString();
88   }
89
90   /**
91    * Return the statements of the method into Strings
92    * @param tab the number of tabs
93    * @return the String containing the statements
94    */
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$
102         }
103       }
104     }
105     buff.append("\n").append(tabString(tab == 0 ? 0 : tab - 1)).append("}"); //$NON-NLS-2$ //$NON-NLS-1$
106     return buff.toString();
107   }
108
109   /**
110    * Get the image of a class.
111    * @return the image that represents a php class
112    */
113   public ImageDescriptor getImage() {
114     return PHPUiImages.DESC_FUN;
115   }
116
117   public void setParent(Object parent) {
118     this.parent = parent;
119   }
120
121   public Object getParent() {
122     return parent;
123   }
124
125   public boolean add(Outlineable o) {
126     return children.add(o);
127   }
128
129   public Outlineable get(int index) {
130     return (Outlineable) children.get(index);
131   }
132
133   public int size() {
134     return children.size();
135   }
136
137   public String toString() {
138     return toStringHeader();
139   }
140
141   public Position getPosition() {
142     return position;
143   }
144 }