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 String 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;
44 * Create a class giving starting and ending offset
45 * @param sourceStart starting offset
46 * @param sourceEnd ending offset
48 public ClassDeclaration(final Object parent,
50 final String superclass,
51 final int sourceStart,
52 final int sourceEnd) {
53 super(sourceStart, sourceEnd);
56 this.superclass = superclass;
57 position = new Position(sourceStart, name.length());
61 * Create a class giving starting and ending offset
62 * @param sourceStart starting offset
63 * @param sourceEnd ending offset
65 public ClassDeclaration(final Object parent,
67 final int sourceStart,
68 final int sourceEnd) {
69 super(sourceStart, sourceEnd);
72 position = new Position(sourceStart, name.length());
76 * Add a method to the class.
77 * @param method the method declaration
79 public void addMethod(final MethodDeclaration method) {
82 if (method.name.equals(name)) {
87 public void addField(final FieldDeclaration var) {
88 for (int i = 0; i < var.vars.length; i++) {
89 final VariableDeclaration c = var.vars[i];
95 public boolean add(final Outlineable o) {
96 return children.add(o);
100 * Tell if the class has a constructor.
103 public boolean hasConstructor() {
104 return constructor != null;
108 * Return the class as String.
109 * @param tab how many tabs before the class
110 * @return the code of this class into String
112 public String toString(final int tab) {
113 return tabString(tab) + toStringHeader() + toStringBody(tab);
117 * Return the body of the class as String.
118 * @param tab how many tabs before the body of the class
119 * @return the body as String
121 public String toStringBody(final int tab) {
122 final StringBuffer buff = new StringBuffer(" {");//$NON-NLS-1$
123 if (fields != null) {
124 for (int i = 0; i < fields.size(); i++) {
125 final FieldDeclaration field = (FieldDeclaration) fields.get(i);
126 buff.append("\n"); //$NON-NLS-1$
127 buff.append(field.toString(tab + 1));
128 buff.append(";");//$NON-NLS-1$
131 for (int i = 0; i < methods.size(); i++) {
132 final MethodDeclaration o = (MethodDeclaration) methods.get(i);
133 buff.append("\n");//$NON-NLS-1$
134 buff.append(o.toString(tab + 1));
136 buff.append("\n").append(tabString(tab)).append("}"); //$NON-NLS-2$ //$NON-NLS-1$
137 return buff.toString();
141 * Return the header of the class as String.
142 * @return the header of the class
144 public String toStringHeader() {
145 final StringBuffer buff = new StringBuffer("class ").append(name);//$NON-NLS-1$
146 if (superclass != null) {
147 buff.append(" extends "); //$NON-NLS-1$
148 buff.append(superclass);
150 return buff.toString();
154 * Get the image of a class.
155 * @return the image that represents a php class
157 public ImageDescriptor getImage() {
158 return PHPUiImages.DESC_CLASS;
161 public Object getParent() {
165 public Outlineable get(final int index) {
166 return (Outlineable) children.get(index);
170 return children.size();
173 public String toString() {
174 final StringBuffer buff = new StringBuffer(name);
175 if (superclass != null) {
176 buff.append(":"); //$NON-NLS-1$
177 buff.append(superclass);
179 return buff.toString();
182 public Position getPosition() {
186 public List getList() {
191 * Get the variables from outside (parameters, globals ...)
193 public void getOutsideVariable(final List list) {
197 * get the modified variables.
199 public void getModifiedVariable(final List list) {
203 * Get the variables used.
205 public void getUsedVariable(final List list) {