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;
7 import org.eclipse.jface.text.Position;
9 import java.util.ArrayList;
10 import java.util.List;
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();
41 private Position position;
43 * Create a class giving starting and ending offset
44 * @param sourceStart starting offset
45 * @param sourceEnd ending offset
47 public ClassDeclaration(Object parent,
52 super(sourceStart, sourceEnd);
55 this.superclass = superclass;
56 position = new Position(sourceStart, name.length);
60 * Create a class giving starting and ending offset
61 * @param sourceStart starting offset
62 * @param sourceEnd ending offset
64 public ClassDeclaration(Object parent,
68 super(sourceStart, sourceEnd);
71 position = new Position(sourceStart, name.length);
74 public void addMethod(MethodDeclaration method) {
77 if (method.name.equals(name)) {
82 public void addField(FieldDeclaration var) {
83 for (int i = 0; i < var.vars.length; i++) {
84 VariableDeclaration c = var.vars[i];
90 public boolean add(Outlineable o) {
91 return children.add(o);
95 * Tell if the class has a constructor.
98 public boolean hasConstructor() {
99 return constructor != null;
103 * Return the class as String.
104 * @param tab how many tabs before the class
105 * @return the code of this class into String
107 public String toString(int tab) {
108 return tabString(tab) + toStringHeader() + toStringBody(tab);
112 * Return the body of the class as String.
113 * @param tab how many tabs before the body of the class
114 * @return the body as String
116 public String toStringBody(int tab) {
117 final StringBuffer buff = new StringBuffer(" {");//$NON-NLS-1$
118 if (fields != null) {
119 for (int i = 0; i < fields.size(); i++) {
120 FieldDeclaration field = (FieldDeclaration) fields.get(i);
121 buff.append("\n"); //$NON-NLS-1$
122 buff.append(field.toString(tab + 1));
123 buff.append(";");//$NON-NLS-1$
126 for (int i = 0; i < methods.size(); i++) {
127 MethodDeclaration o = (MethodDeclaration) methods.get(i);
128 buff.append("\n");//$NON-NLS-1$
129 buff.append(o.toString(tab + 1));
131 buff.append("\n").append(tabString(tab)).append("}"); //$NON-NLS-2$ //$NON-NLS-1$
132 return buff.toString();
136 * Return the header of the class as String.
137 * @return the header of the class
139 public String toStringHeader() {
140 final StringBuffer buff = new StringBuffer("class ").append(name);//$NON-NLS-1$
141 if (superclass != null) {
142 buff.append(" extends "); //$NON-NLS-1$
143 buff.append(superclass);
145 return buff.toString();
149 * Get the image of a class.
150 * @return the image that represents a php class
152 public ImageDescriptor getImage() {
153 return PHPUiImages.DESC_CLASS;
156 public Object getParent() {
160 public Outlineable get(int index) {
161 return (Outlineable) children.get(index);
165 return children.size();
168 public String toString() {
169 final StringBuffer buff = new StringBuffer(new String(name));
170 if (superclass != null) {
171 buff.append(":"); //$NON-NLS-1$
172 buff.append(superclass);
174 return buff.toString();
177 public Position getPosition() {
181 public List getList() {