*** 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.ast.Block;
4 import net.sourceforge.phpdt.internal.compiler.ast.ArgumentDeclaration;
5 import net.sourceforge.phpdt.internal.compiler.parser.OutlineableWithChildren;
6 import net.sourceforge.phpdt.internal.compiler.parser.Outlineable;
7 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
8 import org.eclipse.jface.resource.ImageDescriptor;
9
10 import java.util.Hashtable;
11 import java.util.Enumeration;
12 import java.util.ArrayList;
13
14 /**
15  * A Method declaration.
16  * @author Matthieu Casanova
17  */
18 public class MethodDeclaration extends Statement implements OutlineableWithChildren {
19
20   /** The name of the method. */
21   public char[] name;
22   public Hashtable arguments;
23
24
25   public Statement[] statements;
26   public int bodyStart;
27   public int bodyEnd = -1;
28   /** Tell if the method is a class constructor. */
29   public boolean isConstructor;
30   private Object parent;
31   /** The outlineable children (those will be in the node array too. */
32   private ArrayList children = new ArrayList();
33
34   public boolean reference;
35
36   public MethodDeclaration(Object parent,
37                            char[] name,
38                            Hashtable arguments,
39                            boolean reference,
40                            int sourceStart,
41                            int sourceEnd) {
42     super(sourceStart, sourceEnd);
43     this.name = name;
44     this.arguments = arguments;
45     this.parent = parent;
46     this.reference = reference;
47   }
48
49   /**
50    * Return method into String, with a number of tabs
51    * @param tab the number of tabs
52    * @return the String containing the method
53    */
54   public String toString(int tab) {
55     StringBuffer buff = new StringBuffer(tabString(tab));
56     buff.append("function ");//$NON-NLS-1$
57     if (reference) {
58       buff.append('&');//$NON-NLS-1$
59     }
60     buff.append(name).append("(");//$NON-NLS-1$
61
62     if (arguments != null) {
63       Enumeration values = arguments.elements();
64       int i = 0;
65       while (values.hasMoreElements()) {
66         ArgumentDeclaration o = (ArgumentDeclaration) values.nextElement();
67         buff.append(o.toString(0));
68         if (i != (arguments.size() - 1)) {
69           buff.append(", "); //$NON-NLS-1$
70         }
71         i++;
72       }
73     }
74     buff.append(")"); //$NON-NLS-1$
75
76    buff.append(toStringStatements(tab + 1));
77     return buff.toString();
78   }
79
80   /**
81    * Return the statements of the method into Strings
82    * @param tab the number of tabs
83    * @return the String containing the statements
84    */
85   public String toStringStatements(int tab) {
86     StringBuffer buff = new StringBuffer(" {"); //$NON-NLS-1$
87     if (statements != null) {
88       for (int i = 0; i < statements.length; i++) {
89         buff.append("\n").append(statements[i].toString(tab)); //$NON-NLS-1$
90         if (!(statements[i] instanceof Block)) {
91           buff.append(";"); //$NON-NLS-1$
92         }
93       }
94     }
95     buff.append("\n").append(tabString(tab == 0 ? 0 : tab - 1)).append("}"); //$NON-NLS-2$ //$NON-NLS-1$
96     return buff.toString();
97   }
98
99   /**
100    * Get the image of a class.
101    * @return the image that represents a php class
102    */
103   public ImageDescriptor getImage() {
104     return PHPUiImages.DESC_FUN;
105   }
106
107   public Object getParent() {
108     return parent;
109   }
110
111   public boolean add(Outlineable o) {
112     return children.add(o);
113   }
114
115   public Outlineable get(int index) {
116     return (Outlineable) children.get(index);
117   }
118
119   public int size() {
120     return children.size();
121   }
122 }