View Javadoc
Minimize
Table

1   /*
2    * Copyright 2003-2007 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.codehaus.groovy.ast.stmt;
17  
18  import java.util.ArrayList;
19  import java.util.Arrays;
20  import java.util.List;
21  
22  import org.codehaus.groovy.ast.GroovyCodeVisitor;
23  import org.codehaus.groovy.ast.VariableScope;
24  
25  /**
26   * A list of statements and a scope. 
27   * 
28   * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
29   * @version $Revision: 21288 $
30   */
31  public class BlockStatement extends Statement {
32  
33      private List<Statement> statements = new ArrayList<Statement>();
34      private VariableScope scope;
35      
36      public BlockStatement() {
37          this(new ArrayList<Statement>(), new VariableScope());
38      }
39  
40      /**
41       * Creates a BlockStatement with a scope and children statements.
42       * @param statements
43       *      the statements. Do not pass null. If you do, no exception will occur,
44       *      but a NullPointerException will eventually occur later. Also, a reference
45       *      to the list is kept, so modifying the List later does effect this class.
46       * @param scope
47       *      the scope
48       */
49      public BlockStatement(List<Statement> statements, VariableScope scope) {
50          this.statements = statements;
51          this.scope = scope;
52      }
53      
54      /**
55       * Creates a BlockStatement with a scope and children statements.
56       * @param statements
57       *      the statements, which cannot be null or an exception occurs. No reference
58       *      to the array is held, so modifying the array later has no effect on this
59       *      class.
60       * @param scope
61       *      the scope
62       */
63      public BlockStatement(Statement[] statements, VariableScope scope) {
64          this.statements.addAll(Arrays.asList(statements));
65          this.scope = scope;
66      }
67  
68      public void visit(GroovyCodeVisitor visitor) {
69          visitor.visitBlockStatement(this);
70      }
71  
72      public List<Statement> getStatements() {
73          return statements;
74      }
75  
76      public void addStatement(Statement statement) {
77          statements.add(statement);
78      }
79  
80      public void addStatements(List<Statement> listOfStatements) {
81          statements.addAll(listOfStatements);
82      }
83  
84      public String toString() {
85          return super.toString() + statements;
86      }
87  
88      public String getText() {
89          StringBuffer buffer = new StringBuffer("{ ");
90          boolean first = true;
91          for (Statement statement : statements) {
92              if (first) {
93                  first = false;
94              }
95              else {
96                  buffer.append("; ");
97              }
98              buffer.append(statement.getText());
99          }
100         buffer.append(" }");
101         return buffer.toString();
102     }
103 
104     public boolean isEmpty() {
105         return statements.isEmpty();
106     }
107 
108     public void setVariableScope(VariableScope scope) {
109         this.scope = scope;
110     }
111     
112     public VariableScope getVariableScope() {
113         return scope;
114     }
115 }