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 net.sourceforge.phpeclipse.PHPeclipsePlugin;
7 import org.eclipse.jface.resource.ImageDescriptor;
9 import java.util.ArrayList;
10 import java.util.Enumeration;
14 * This class is my ClassDeclaration declaration for php.
15 * It is similar to org.eclipse.jdt.internal.compiler.ast.TypeDeclaration
16 * It directly extends AstNode because a class cannot appear anywhere in php
17 * @author Matthieu Casanova
19 public class ClassDeclaration extends Statement implements OutlineableWithChildren {
21 /** The name of the class. */
23 /** The superclass. */
24 public char[] superclass;
26 public int declarationSourceStart;
27 public int declarationSourceEnd;
30 /** The methods of the class. */
31 private final ArrayList methods = new ArrayList();
32 /** The constructor of the class. */
33 public MethodDeclaration constructor;
34 /** The fields of the class. */
35 private ArrayList fields = new ArrayList();
37 private Object parent;
38 /** The outlineable children (those will be in the node array too. */
39 private ArrayList children = new ArrayList();
42 * Create a class giving starting and ending offset
43 * @param sourceStart starting offset
44 * @param sourceEnd ending offset
46 public ClassDeclaration(Object parent,
51 super(sourceStart, sourceEnd);
54 this.superclass = superclass;
58 * Create a class giving starting and ending offset
59 * @param sourceStart starting offset
60 * @param sourceEnd ending offset
62 public ClassDeclaration(Object parent,
66 super(sourceStart, sourceEnd);
71 public void addMethod(MethodDeclaration method) {
74 if (method.name.equals(name)) {
79 public void addVariable(FieldDeclaration var) {
80 for (int i = 0; i < var.vars.length; i++) {
81 VariableDeclaration c = var.vars[i];
88 * Tell if the class has a constructor.
91 public boolean hasConstructor() {
92 return constructor != null;
96 * Return the class as String.
97 * @param tab how many tabs before the class
98 * @return the code of this class into String
100 public String toString(int tab) {
101 return tabString(tab) + toStringHeader() + toStringBody(tab);
105 * Return the body of the class as String
106 * @param tab how many tabs before the body of the class
107 * @return the body as String
109 public String toStringBody(int tab) {
110 final StringBuffer buff = new StringBuffer(" {");//$NON-NLS-1$
111 if (fields != null) {
112 for (int i = 0; i < fields.size(); i++) {
113 FieldDeclaration field = (FieldDeclaration) fields.get(i);
114 buff.append("\n"); //$NON-NLS-1$
115 buff.append(field.toString(tab + 1));
116 buff.append(";");//$NON-NLS-1$
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));
124 buff.append("\n").append(tabString(tab)).append("}"); //$NON-NLS-2$ //$NON-NLS-1$
125 return buff.toString();
129 * Return the header of the class as String.
130 * @return the header of the class
132 public String toStringHeader() {
133 final StringBuffer buff = new StringBuffer("class ").append(name);//$NON-NLS-1$
134 if (superclass != null) {
135 buff.append(" extends "); //$NON-NLS-1$
136 buff.append(superclass);
138 return buff.toString();
142 * Get the image of a class.
143 * @return the image that represents a php class
145 public ImageDescriptor getImage() {
146 return PHPUiImages.DESC_CLASS;
149 public Object getParent() {
153 public boolean add(Outlineable o) {
154 return children.add(o);
157 public Outlineable get(int index) {
158 return (Outlineable) children.get(index);
162 return children.size();