1 package net.sourceforge.phpdt.internal.compiler.ast;
3 import java.util.ArrayList;
6 import net.sourceforge.phpdt.internal.compiler.parser.Outlineable;
7 import net.sourceforge.phpdt.internal.compiler.parser.OutlineableWithChildren;
8 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
10 import org.eclipse.jface.resource.ImageDescriptor;
11 import org.eclipse.jface.text.Position;
15 * This class is my ClassDeclaration declaration for php.
16 * It is similar to org.eclipse.jdt.internal.compiler.ast.TypeDeclaration
17 * It directly extends AstNode because a class cannot appear anywhere in php
18 * @author Matthieu Casanova
20 public class ClassDeclaration extends Statement implements OutlineableWithChildren {
22 /** The name of the class. */
24 /** The superclass. */
25 public String superclass;
27 public int declarationSourceStart;
28 public int declarationSourceEnd;
31 /** The methods of the class. */
32 private final ArrayList methods = new ArrayList();
33 /** The constructor of the class. */
34 public MethodDeclaration constructor;
35 /** The fields of the class. */
36 private ArrayList fields = new ArrayList();
38 private Object parent;
39 /** The outlineable children (those will be in the node array too. */
40 private ArrayList children = new ArrayList();
42 private Position position;
45 * Create a class giving starting and ending offset
46 * @param sourceStart starting offset
47 * @param sourceEnd ending offset
49 public ClassDeclaration(final Object parent,
51 final String superclass,
52 final int sourceStart,
53 final int sourceEnd) {
54 super(sourceStart, sourceEnd);
57 this.superclass = superclass;
58 position = new Position(sourceStart, name.length());
62 * Create a class giving starting and ending offset
63 * @param sourceStart starting offset
64 * @param sourceEnd ending offset
66 public ClassDeclaration(final Object parent,
68 final int sourceStart,
69 final int sourceEnd) {
70 super(sourceStart, sourceEnd);
73 position = new Position(sourceStart, name.length());
77 * Add a method to the class.
78 * @param method the method declaration
80 public void addMethod(final MethodDeclaration method) {
83 if (method.name.equals(name)) {
88 public void addField(final FieldDeclaration var) {
89 for (int i = 0; i < var.vars.length; i++) {
90 final VariableDeclaration c = var.vars[i];
96 public boolean add(final Outlineable o) {
97 return children.add(o);
101 * Tell if the class has a constructor.
104 public boolean hasConstructor() {
105 return constructor != null;
109 * Return the class as String.
110 * @param tab how many tabs before the class
111 * @return the code of this class into String
113 public String toString(final int tab) {
114 return tabString(tab) + toStringHeader() + toStringBody(tab);
118 * Return the body of the class as String.
119 * @param tab how many tabs before the body of the class
120 * @return the body as String
122 public String toStringBody(final int tab) {
123 final StringBuffer buff = new StringBuffer(" {");//$NON-NLS-1$
124 if (fields != null) {
125 for (int i = 0; i < fields.size(); i++) {
126 final FieldDeclaration field = (FieldDeclaration) fields.get(i);
127 buff.append("\n"); //$NON-NLS-1$
128 buff.append(field.toString(tab + 1));
129 buff.append(";");//$NON-NLS-1$
132 for (int i = 0; i < methods.size(); i++) {
133 final MethodDeclaration o = (MethodDeclaration) methods.get(i);
134 buff.append("\n");//$NON-NLS-1$
135 buff.append(o.toString(tab + 1));
137 buff.append("\n").append(tabString(tab)).append("}"); //$NON-NLS-2$ //$NON-NLS-1$
138 return buff.toString();
142 * Return the header of the class as String.
143 * @return the header of the class
145 public String toStringHeader() {
146 final StringBuffer buff = new StringBuffer("class ").append(name);//$NON-NLS-1$
147 if (superclass != null) {
148 buff.append(" extends "); //$NON-NLS-1$
149 buff.append(superclass);
151 return buff.toString();
155 * Get the image of a class.
156 * @return the image that represents a php class
158 public ImageDescriptor getImage() {
159 return PHPUiImages.DESC_CLASS;
162 public Object getParent() {
166 public Outlineable get(final int index) {
167 return (Outlineable) children.get(index);
171 return children.size();
174 public String toString() {
175 final StringBuffer buff = new StringBuffer(name);
176 if (superclass != null) {
177 buff.append(":"); //$NON-NLS-1$
178 buff.append(superclass);
180 return buff.toString();
183 public Position getPosition() {
187 public List getList() {
192 * Get the variables from outside (parameters, globals ...)
194 public void getOutsideVariable(final List list) {
198 * get the modified variables.
200 public void getModifiedVariable(final List list) {
204 * Get the variables used.
206 public void getUsedVariable(final List list) {