KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > compiler > base > ast > Decs


1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * This file is part of the compiler and core tools for the AspectJ(tm)
4  * programming language; see http://aspectj.org
5  *
6  * The contents of this file are subject to the Mozilla Public License
7  * Version 1.1 (the "License"); you may not use this file except in
8  * compliance with the License. You may obtain a copy of the License at
9  * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is AspectJ.
17  *
18  * The Initial Developer of the Original Code is Xerox Corporation. Portions
19  * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
20  * All Rights Reserved.
21  *
22  * Contributor(s):
23  */

24
25 package org.aspectj.compiler.base.ast;
26
27 import org.aspectj.compiler.base.*;
28 import org.aspectj.compiler.base.JavaCompiler;
29 import org.aspectj.compiler.base.CodeWriter;
30 import org.aspectj.compiler.base.cst.*;
31
32 import java.util.*;
33
34 import org.aspectj.compiler.base.bcg.CodeBuilder;
35 import org.aspectj.compiler.base.bcg.Label;
36
37 /**
38   * @grammar dec*
39   * @children Dec dec
40   */

41
42 //!!! this should extend ASTObject...
43
public class Decs extends Stmt {
44
45     public List getList() {
46         return Arrays.asList(children);
47     }
48
49     public ListIterator iterator() {
50         return new ListIterator() {
51                 private int i = -1;
52                 private boolean isRemovable = false;
53
54                 public int previousIndex() {
55                     return i == -1 ? size - 1 : i - 1;
56                 }
57                 public boolean hasPrevious() {
58                     int i = previousIndex();
59                     return i >= 0 && i < size;
60                 }
61                 public Object JavaDoc previous() {
62                     if (! hasPrevious()) {
63                         throw new NoSuchElementException();
64                     }
65                     isRemovable = true;
66                     return children[--i];
67                 }
68
69                 public int nextIndex() {
70                     return i == -1 ? 0 : i + 1;
71                 }
72                 public boolean hasNext() {
73                     int i = nextIndex();
74                     return i >= 0 && i < size;
75                 }
76                 public Object JavaDoc next() {
77                     if (! hasNext()) {
78                         throw new NoSuchElementException();
79                     }
80                     isRemovable = true;
81                     return children[++i];
82                 }
83
84                 public void remove() {
85                     if (! isRemovable) {
86                         throw new IllegalStateException JavaDoc();
87                     }
88                     isRemovable = false;
89                     Decs.this.remove(i--);
90                 }
91                 public void set(Object JavaDoc o) {
92                     if (! isRemovable) {
93                         throw new IllegalStateException JavaDoc();
94                     }
95                     Decs.this.set(i, (Dec)o);
96                 }
97                 public void add(Object JavaDoc o) {
98                     if (i == -1) i = 0;
99                     isRemovable = false;
100                     Decs.this.add(i++, (Dec)o);
101                 }
102             };
103     }
104
105     //!!! should go away
106
public void append(Decs decs) {
107         for(int i=0; i<decs.size(); i++) {
108             append(decs.get(i));
109         }
110     }
111
112     public void append(Dec dec) {
113         add(dec);
114     }
115
116     public void prepend(Dec dec) {
117         add(0, dec);
118     }
119
120     public void unparse(CodeWriter writer) {
121         final int N = size;
122         for (int i = 0; i < N; i++) {
123             if (i > 0) writer.newLine();
124             writer.write(children[i]);
125         }
126     }
127
128     public void remove(Dec dec) {
129         int i = indexOf(dec);
130         if (i != -1) remove(i);
131     }
132
133     public void cleanup() {
134         for(int i=0; i < size; i++) {
135             children[i].cleanup();
136         }
137         super.cleanup();
138     }
139
140     // ------------------------------
141
// bcg
142
// should only be a list of varDecs here...
143
protected void cgStmt(CodeBuilder cb) {
144         for (int i = 0, len = size; i < len; i++) {
145             children[i].cgTop(cb);
146         }
147     }
148
149     //BEGIN: Generated from @child and @property
150
protected int size;
151     public Dec[] children;
152
153     public Decs(SourceLocation location, Dec[] _children) {
154         super(location);
155         for(int i=0; i<_children.length; i++) {
156             if (_children[i] != null) _children[i].setParent(this);
157         }
158         children = _children;
159         size = _children.length;
160     }
161
162     public Decs(SourceLocation location) {
163         this(location, new Dec[] {});
164     }
165
166     public Decs(SourceLocation location, Dec child1) {
167         this(location, new Dec[] {child1});
168     }
169
170     public Decs(SourceLocation location, Dec child1, Dec child2) {
171         this(location, new Dec[] {child1, child2});
172     }
173
174     public Decs(SourceLocation location, Dec child1, Dec child2, Dec child3) {
175         this(location, new Dec[] {child1, child2, child3});
176     }
177
178     public ASTObject copyWalk(CopyWalker walker) {
179         final int N = size;
180         Dec[] copiedChildren = new Dec[N];
181         int newIndex = 0;
182         for(int oldIndex=0; oldIndex<N; oldIndex++) {
183             Dec newChild = (Dec)walker.process(children[oldIndex]);
184             if (newChild != null) copiedChildren[newIndex++] = newChild;
185         }
186         Decs ret = new Decs(getSourceLocation(),copiedChildren);
187         ret.size = newIndex;
188         ret.setSource(this);
189         return ret;
190     }
191
192     public ASTObject getChildAt(int childIndex) { return get(childIndex); }
193     public void setChildAt(int childIndex, ASTObject child) { set(childIndex, (Dec)child); }
194     public String JavaDoc getChildNameAt(int childIndex) { return "dec"+childIndex; }
195
196     public int getChildCount() { return size; }
197     public int size() { return size; }
198
199     public Dec get(int index) {
200         if (index >= size) throw new ArrayIndexOutOfBoundsException JavaDoc();
201         return children[index];
202     }
203
204     public void set(int index, Dec child) {
205         if (index >= size) throw new ArrayIndexOutOfBoundsException JavaDoc();
206         children[index] = child;
207         child.setParent(this);
208     }
209
210     public void resize(int newSize) {
211         if (newSize > children.length) {
212             Dec[] newChildren = new Dec[children.length*2 + 1];
213             System.arraycopy(children, 0, newChildren, 0, children.length);
214             children = newChildren;
215         }
216         size = newSize;
217     }
218
219     public void addAll(Decs collection) {
220         addAll(size, collection);
221     }
222
223     public void addAll(int index, Decs collection) {
224         for(int i=0; i<collection.size(); i++) {
225             add(index+i, collection.get(i));
226         }
227     }
228
229     public void add(Dec child) {
230         add(size, child);
231     }
232
233     public void add(int index, Dec child) {
234         if (child == null) return;
235
236         if (index < 0 || index > size) throw new ArrayIndexOutOfBoundsException JavaDoc();
237
238         resize(size+1);
239
240         for(int moveIndex = size-1; moveIndex > index; moveIndex--) {
241             children[moveIndex] = children[moveIndex-1];
242         }
243
244         children[index] = child;
245         child.setParent(this);
246     }
247
248     public void remove(int index) {
249         if (index < 0 || index > size) throw new ArrayIndexOutOfBoundsException JavaDoc();
250
251         size -= 1;
252
253         for(int moveIndex = index; moveIndex < size; moveIndex++) {
254             children[moveIndex] = children[moveIndex+1];
255         }
256     }
257
258     public String JavaDoc getDefaultDisplayName() {
259         return "Decs()";
260     }
261
262     //END: Generated from @child and @property
263
}
264
Popular Tags