KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > polyglot > ext > jl > ast > AbstractBlock_c


1 package polyglot.ext.jl.ast;
2
3 import polyglot.ast.*;
4 import polyglot.types.*;
5 import polyglot.util.*;
6 import polyglot.visit.*;
7 import java.util.*;
8
9 /**
10  * A <code>Block</code> represents a Java block statement -- an immutable
11  * sequence of statements.
12  */

13 public abstract class AbstractBlock_c extends Stmt_c implements Block
14 {
15     protected List statements;
16
17     public AbstractBlock_c(Position pos, List statements) {
18     super(pos);
19     this.statements = TypedList.copyAndCheck(statements, Stmt.class, true);
20     }
21
22     /** Get the statements of the block. */
23     public List statements() {
24     return this.statements;
25     }
26
27     /** Set the statements of the block. */
28     public Block statements(List statements) {
29     AbstractBlock_c n = (AbstractBlock_c) copy();
30     n.statements = TypedList.copyAndCheck(statements, Stmt.class, true);
31     return n;
32     }
33
34     /** Append a statement to the block. */
35     public Block append(Stmt stmt) {
36     List l = new ArrayList(statements.size()+1);
37     l.addAll(statements);
38     l.add(stmt);
39     return statements(l);
40     }
41
42     /** Prepend a statement to the block. */
43     public Block prepend(Stmt stmt) {
44         List l = new ArrayList(statements.size()+1);
45         l.add(stmt);
46         l.addAll(statements);
47         return statements(l);
48     }
49
50     /** Reconstruct the block. */
51     protected AbstractBlock_c reconstruct(List statements) {
52     if (! CollectionUtil.equals(statements, this.statements)) {
53         AbstractBlock_c n = (AbstractBlock_c) copy();
54         n.statements = TypedList.copyAndCheck(statements, Stmt.class, true);
55         return n;
56     }
57
58     return this;
59     }
60
61     /** Visit the children of the block. */
62     public Node visitChildren(NodeVisitor v) {
63         List statements = visitList(this.statements, v);
64     return reconstruct(statements);
65     }
66
67     public Context enterScope(Context c) {
68     return c.pushBlock();
69     }
70
71     /** Write the block to an output file. */
72     public void prettyPrint(CodeWriter w, PrettyPrinter tr) {
73     w.begin(0);
74
75     for (Iterator i = statements.iterator(); i.hasNext(); ) {
76         Stmt n = (Stmt) i.next();
77         printBlock(n, w, tr);
78
79         if (i.hasNext()) {
80         w.newline(0);
81         }
82     }
83
84     w.end();
85     }
86
87     public Term entry() {
88         return listEntry(statements, this);
89     }
90
91     public List acceptCFG(CFGBuilder v, List succs) {
92         v.visitCFGList(statements, this);
93         return succs;
94     }
95
96     public String JavaDoc toString() {
97         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
98         sb.append("{");
99
100         int count = 0;
101
102         for (Iterator i = statements.iterator(); i.hasNext(); ) {
103             if (count++ > 2) {
104                 sb.append(" ...");
105                 break;
106             }
107
108             Stmt n = (Stmt) i.next();
109             sb.append(" ");
110             sb.append(n.toString());
111         }
112
113         sb.append(" }");
114         return sb.toString();
115     }
116 }
117
Popular Tags