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