1 package net.sourceforge.phpdt.internal.compiler.parser;
3 import java.util.Enumeration;
4 import java.util.Hashtable;
6 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
8 import org.eclipse.jface.resource.ImageDescriptor;
11 * A function declaration.
14 public class PHPFunctionDeclaration extends PHPSegmentWithChildren {
16 /** The parameters of the function. */
17 private final Hashtable parameters;
19 /** The parameters of the function. */
20 private final Hashtable declaredVariables;
23 * A String representation of the function (it's generated once because it needs to iterate
24 * a hashtable == long.
26 private String stringRepresentation;
29 * Create a function declaration.
30 * @param parent the parent object (it should be a php class)
31 * @param name the name of the function
32 * @param index where the function is in the file
34 public PHPFunctionDeclaration(Object parent, String name, int index) {
35 super(parent, name, index);
37 declaredVariables = null;
41 * Create a function declaration.
42 * @param parent the parent object (it should be a php class)
43 * @param name the name of the function
44 * @param index where the function is in the file
45 * @param parameters the list of parameters (it should contains only PHPVar)
47 public PHPFunctionDeclaration(Object parent, String name, int index, Hashtable parameters) {
48 super(parent, name, index);
49 this.parameters = parameters;
50 declaredVariables = (Hashtable) parameters.clone();
55 * Get the image of a class.
56 * @return the image that represents a php class
58 public ImageDescriptor getImage() {
59 return PHPUiImages.DESC_FUN;
63 * Return the string representation of the function.
64 * @return the string representation of the function
66 public String toString() {
67 if (parameters == null) {
68 return super.toString();
70 return stringRepresentation;
74 * Create the String representation of the function.
76 private void createStringView() {
77 StringBuffer buff = new StringBuffer(name).append("(");
78 Enumeration vars = parameters.elements();
79 boolean first = false;
80 while (vars.hasMoreElements()) {
81 PHPVarDeclaration o = (PHPVarDeclaration) vars.nextElement();
87 buff.append(o.toString());
90 stringRepresentation = buff.toString();
94 * Return a parameter of the function
95 * @param parameterName the name of the parameter
96 * @return it will return the PHPVarDeclaration of the parameter asked, or null
98 public PHPVarDeclaration getParameter(String parameterName) {
99 return (PHPVarDeclaration) parameters.get(parameterName);
103 * Return all parameters of the function.
104 * @return a hashtable containing all PHPVarDeclaration
106 public Hashtable getParameters() {
111 * Return a variable of the function
112 * @param variableName the name of the parameter
113 * @return it will return the PHPVarDeclaration of the parameter asked, or null
115 public PHPVarDeclaration getVariable(String variableName) {
116 return (PHPVarDeclaration) declaredVariables.get(variableName);
120 * Return all declared variables of the function.
121 * @return a hashtable containing all PHPVarDeclaration
123 public Hashtable getVariables() {
124 return declaredVariables;
127 public Object addVariable(PHPVarDeclaration var) {
128 return declaredVariables.put(var.getVariable().getName(),var);