*** 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 import java.util.List;
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
31   /** The parent object. */
32   private Object parent;
33   /** The outlineable children (those will be in the node array too. */
34   private ArrayList children = new ArrayList();
35
36   /** Tell if the method returns a reference. */
37   public boolean reference;
38
39   private Position position;
40
41   public MethodDeclaration(Object parent,
42                            char[] name,
43                            Hashtable arguments,
44                            boolean reference,
45                            int sourceStart,
46                            int sourceEnd) {
47     super(sourceStart, sourceEnd);
48     this.name = name;
49     this.arguments = arguments;
50     this.parent = parent;
51     this.reference = reference;
52     position = new Position(sourceStart, sourceEnd);
53   }
54
55   /**
56    * Return method into String, with a number of tabs
57    * @param tab the number of tabs
58    * @return the String containing the method
59    */
60   public String toString(int tab) {
61     StringBuffer buff = new StringBuffer(tabString(tab));
62     buff.append(toStringHeader());
63     buff.append(toStringStatements(tab + 1));
64     return buff.toString();
65   }
66
67   public String toStringHeader() {
68     return "function " + toString();
69   }
70
71   /**
72    * Return the statements of the method into Strings
73    * @param tab the number of tabs
74    * @return the String containing the statements
75    */
76   public String toStringStatements(int tab) {
77     StringBuffer buff = new StringBuffer(" {"); //$NON-NLS-1$
78     if (statements != null) {
79       for (int i = 0; i < statements.length; i++) {
80         buff.append("\n").append(statements[i].toString(tab)); //$NON-NLS-1$
81         if (!(statements[i] instanceof Block)) {
82           buff.append(";"); //$NON-NLS-1$
83         }
84       }
85     }
86     buff.append("\n").append(tabString(tab == 0 ? 0 : tab - 1)).append("}"); //$NON-NLS-2$ //$NON-NLS-1$
87     return buff.toString();
88   }
89
90   /**
91    * Get the image of a class.
92    * @return the image that represents a php class
93    */
94   public ImageDescriptor getImage() {
95     return PHPUiImages.DESC_FUN;
96   }
97
98   public void setParent(Object parent) {
99     this.parent = parent;
100   }
101
102   public Object getParent() {
103     return parent;
104   }
105
106   public boolean add(Outlineable o) {
107     return children.add(o);
108   }
109
110   public Outlineable get(int index) {
111     return (Outlineable) children.get(index);
112   }
113
114   public int size() {
115     return children.size();
116   }
117
118   public String toString() {
119     StringBuffer buff = new StringBuffer();
120     if (reference) {
121       buff.append("&");//$NON-NLS-1$
122     }
123     buff.append(name).append("(");//$NON-NLS-1$
124
125     if (arguments != null) {
126       Enumeration values = arguments.elements();
127       int i = 0;
128       while (values.hasMoreElements()) {
129         VariableDeclaration o = (VariableDeclaration) values.nextElement();
130         buff.append(o.toStringExpression());
131         if (i != (arguments.size() - 1)) {
132           buff.append(", "); //$NON-NLS-1$
133         }
134         i++;
135       }
136     }
137     buff.append(")"); //$NON-NLS-1$
138     return buff.toString();
139   }
140
141   public Position getPosition() {
142     return position;
143   }
144
145   public List getList() {
146     return children;
147   }
148 }