1 package net.sourceforge.phpdt.internal.compiler.ast;
3 import net.sourceforge.phpdt.internal.compiler.parser.Outlineable;
4 import net.sourceforge.phpdt.internal.compiler.parser.OutlineableWithChildren;
5 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
6 import org.eclipse.jface.resource.ImageDescriptor;
8 import java.util.ArrayList;
9 import java.util.Enumeration;
13 * This class is my ClassDeclaration declaration for php.
14 * It is similar to org.eclipse.jdt.internal.compiler.ast.TypeDeclaration
15 * It directly extends AstNode because a class cannot appear anywhere in php
16 * @author Matthieu Casanova
18 public class ClassDeclaration extends Statement implements OutlineableWithChildren {
20 /** The name of the class. */
22 /** The superclass. */
23 public char[] superclass;
25 public int declarationSourceStart;
26 public int declarationSourceEnd;
29 /** The methods of the class. */
30 private ArrayList methods = new ArrayList();
31 /** The constructor of the class. */
32 public MethodDeclaration constructor;
33 /** The fields of the class. */
34 private ArrayList fields = new ArrayList();
36 private Object parent;
37 /** The outlineable children (those will be in the node array too. */
38 private ArrayList children = new ArrayList();
41 * Create a class giving starting and ending offset
42 * @param sourceStart starting offset
43 * @param sourceEnd ending offset
45 public ClassDeclaration(Object parent,
50 super(sourceStart, sourceEnd);
53 this.superclass = superclass;
57 * Create a class giving starting and ending offset
58 * @param sourceStart starting offset
59 * @param sourceEnd ending offset
61 public ClassDeclaration(Object parent,
65 super(sourceStart, sourceEnd);
70 public void addMethod(MethodDeclaration method) {
73 if (method.name.equals(name)) {
78 public void addVariable(FieldDeclaration var) {
79 for (int i = 0; i < var.vars.length; i++) {
80 VariableDeclaration c = var.vars[i];
87 * Tell if the class has a constructor.
90 public boolean hasConstructor() {
91 return constructor != null;
95 * Return the class as String.
96 * @param tab how many tabs before the class
97 * @return the code of this class into String
99 public String toString(int tab) {
100 return tabString(tab) + toStringHeader() + toStringBody(tab);
104 * Return the body of the class as String
105 * @param tab how many tabs before the body of the class
106 * @return the body as String
108 public String toStringBody(int tab) {
109 final StringBuffer buff = new StringBuffer(" {");//$NON-NLS-1$
110 if (fields != null) {
111 for (int i = 0; i < fields.size(); i++) {
112 FieldDeclaration field = (FieldDeclaration) fields.get(i);
113 buff.append("\n"); //$NON-NLS-1$
114 buff.append(field.toString(tab + 1));
115 buff.append(";");//$NON-NLS-1$
118 if (methods != null) {
119 for (int i = 0; i < methods.size(); i++) {
120 MethodDeclaration o = (MethodDeclaration) methods.get(i);
121 buff.append("\n");//$NON-NLS-1$
122 buff.append(o.toString(tab + 1));
125 buff.append("\n").append(tabString(tab)).append("}"); //$NON-NLS-2$ //$NON-NLS-1$
126 return buff.toString();
130 * Return the header of the class as String.
131 * @return the header of the class
133 public String toStringHeader() {
134 final StringBuffer buff = new StringBuffer("class").append(name);//$NON-NLS-1$
135 if (superclass != null) {
136 buff.append(" extends "); //$NON-NLS-1$
137 buff.append(superclass);
139 return buff.toString();
143 * Get the image of a class.
144 * @return the image that represents a php class
146 public ImageDescriptor getImage() {
147 return PHPUiImages.DESC_CLASS;
150 public Object getParent() {
154 public boolean add(Outlineable o) {
155 return children.add(o);
158 public Outlineable get(int index) {
159 return (Outlineable) children.get(index);
163 return children.size();