View Javadoc
Minimize
Table

1   /*
2    * Copyright 2003-2010 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.expr;
17  
18  import org.codehaus.groovy.ast.*;
19  import org.codehaus.groovy.ast.stmt.Statement;
20  import org.codehaus.groovy.runtime.InvokerHelper;
21  
22  /**
23   * Represents a closure expression such as { statement }
24   * or { i -> statement } or { i, x, String y ->  statement }
25   * 
26   * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
27   * @author Hamlet D'Arcy
28   * @version $Revision: 21894 $
29   */
30  public class ClosureExpression extends Expression {
31      
32      private Parameter[] parameters;
33      private Statement code;
34      private VariableScope variableScope;
35      
36      public ClosureExpression(Parameter[] parameters, Statement code) {
37          this.parameters = parameters;
38          this.code = code;
39          super.setType(ClassHelper.CLOSURE_TYPE);
40      }
41      
42      public void visit(GroovyCodeVisitor visitor) {
43          visitor.visitClosureExpression(this);
44      }
45  
46      public Expression transformExpression(ExpressionTransformer transformer) {
47          return this;
48      }
49      
50      public String toString() {
51          return super.toString() + InvokerHelper.toString(parameters) + "{ " + code + " }";
52      }
53  
54      /**
55       * This gets the code statement of the closure. You can read this method to find out what actions
56       * the closure is going to perform.
57       *
58       * @return the code statement of the closure
59       */
60      public Statement getCode() {
61          return code;
62      }
63  
64      /**
65       * This sets the code statement of the closure. You can use this method in order to add more actions
66       * during the closure execution.
67       *
68       * @param code the new Statement
69       */
70      public void setCode(Statement code) {
71          this.code = code;
72      }
73  
74      public Parameter[] getParameters() {
75          return parameters;
76      }
77  
78      public boolean isParameterSpecified() {
79          return parameters != null && parameters.length > 0;
80      }
81      
82      public VariableScope getVariableScope() {
83          return variableScope;
84      }
85  
86      public void setVariableScope(VariableScope variableScope) {
87          this.variableScope = variableScope;
88      }
89  
90      @Override
91      public String getText() {
92          String paramText = AstToTextHelper.getParametersText(parameters);
93          if (paramText.length() > 0) {
94              return "{ " + paramText + " -> ... }";
95          } else {
96              return "{ -> ... }";
97          }
98      }
99  }