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.compiler.ast.declarations.VariableUsage;
 
   6 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
 
   7 import org.eclipse.jface.resource.ImageDescriptor;
 
   8 import org.eclipse.jface.text.Position;
 
  10 import java.util.ArrayList;
 
  11 import java.util.List;
 
  12 import java.util.Enumeration;
 
  16  * This class is my ClassDeclaration declaration for php.
 
  17  * It is similar to org.eclipse.jdt.internal.compiler.ast.TypeDeclaration
 
  18  * It directly extends AstNode because a class cannot appear anywhere in php
 
  19  * @author Matthieu Casanova
 
  21 public class ClassDeclaration extends Statement implements OutlineableWithChildren {
 
  23   /** The name of the class. */
 
  25   /** The superclass. */
 
  26   public char[] superclass;
 
  28   public int declarationSourceStart;
 
  29   public int declarationSourceEnd;
 
  32   /** The methods of the class. */
 
  33   private final ArrayList methods = new ArrayList();
 
  34   /** The constructor of the class. */
 
  35   public MethodDeclaration constructor;
 
  36   /** The fields of the class. */
 
  37   private ArrayList fields = new ArrayList();
 
  39   private Object parent;
 
  40   /** The outlineable children (those will be in the node array too. */
 
  41   private ArrayList children = new ArrayList();
 
  43   private Position position;
 
  46    * Create a class giving starting and ending offset
 
  47    * @param sourceStart starting offset
 
  48    * @param sourceEnd ending offset
 
  50   public ClassDeclaration(final Object parent,
 
  52                           final char[] superclass,
 
  53                           final int sourceStart,
 
  54                           final int sourceEnd) {
 
  55     super(sourceStart, sourceEnd);
 
  58     this.superclass = superclass;
 
  59     position = new Position(sourceStart, name.length);
 
  63    * Create a class giving starting and ending offset
 
  64    * @param sourceStart starting offset
 
  65    * @param sourceEnd ending offset
 
  67   public ClassDeclaration(final Object parent,
 
  69                           final int sourceStart,
 
  70                           final int sourceEnd) {
 
  71     super(sourceStart, sourceEnd);
 
  74     position = new Position(sourceStart, name.length);
 
  77   public void addMethod(final MethodDeclaration method) {
 
  80     if (method.name.equals(name)) {
 
  85   public void addField(final FieldDeclaration var) {
 
  86     for (int i = 0; i < var.vars.length; i++) {
 
  87       final VariableDeclaration c = var.vars[i];
 
  93   public boolean add(final Outlineable o) {
 
  94     return children.add(o);
 
  98    * Tell if the class has a constructor.
 
 101   public boolean hasConstructor() {
 
 102     return constructor != null;
 
 106    * Return the class as String.
 
 107    * @param tab how many tabs before the class
 
 108    * @return the code of this class into String
 
 110   public String toString(final int tab) {
 
 111     return tabString(tab) + toStringHeader() + toStringBody(tab);
 
 115    * Return the body of the class as String.
 
 116    * @param tab how many tabs before the body of the class
 
 117    * @return the body as String
 
 119   public String toStringBody(final int tab) {
 
 120     final StringBuffer buff = new StringBuffer(" {");//$NON-NLS-1$
 
 121     if (fields != null) {
 
 122       for (int i = 0; i < fields.size(); i++) {
 
 123         final FieldDeclaration field = (FieldDeclaration) fields.get(i);
 
 124         buff.append("\n"); //$NON-NLS-1$
 
 125         buff.append(field.toString(tab + 1));
 
 126         buff.append(";");//$NON-NLS-1$
 
 129     for (int i = 0; i < methods.size(); i++) {
 
 130       final MethodDeclaration o = (MethodDeclaration) methods.get(i);
 
 131       buff.append("\n");//$NON-NLS-1$
 
 132       buff.append(o.toString(tab + 1));
 
 134     buff.append("\n").append(tabString(tab)).append("}"); //$NON-NLS-2$ //$NON-NLS-1$
 
 135     return buff.toString();
 
 139    * Return the header of the class as String.
 
 140    * @return the header of the class
 
 142   public String toStringHeader() {
 
 143     final StringBuffer buff = new StringBuffer("class ").append(name);//$NON-NLS-1$
 
 144     if (superclass != null) {
 
 145       buff.append(" extends "); //$NON-NLS-1$
 
 146       buff.append(superclass);
 
 148     return buff.toString();
 
 152    * Get the image of a class.
 
 153    * @return the image that represents a php class
 
 155   public ImageDescriptor getImage() {
 
 156     return PHPUiImages.DESC_CLASS;
 
 159   public Object getParent() {
 
 163   public Outlineable get(final int index) {
 
 164     return (Outlineable) children.get(index);
 
 168     return children.size();
 
 171   public String toString() {
 
 172     final StringBuffer buff = new StringBuffer(new String(name));
 
 173     if (superclass != null) {
 
 174       buff.append(":"); //$NON-NLS-1$
 
 175       buff.append(superclass);
 
 177     return buff.toString();
 
 180   public Position getPosition() {
 
 184   public List getList() {
 
 189    * Get the variables from outside (parameters, globals ...)
 
 190    * @return the variables from outside
 
 192   public List getOutsideVariable() {
 
 193     return new ArrayList();
 
 197    * get the modified variables.
 
 198    * @return the variables from we change value
 
 200   public List getModifiedVariable() {
 
 201     return new ArrayList();
 
 205    * Get the variables used.
 
 206    * @return the variables used
 
 208   public List getUsedVariable() {
 
 209     return new ArrayList();