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.expr;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import org.codehaus.groovy.ast.ClassHelper;
22  import org.codehaus.groovy.ast.ClassNode;
23  import org.codehaus.groovy.ast.AnnotatedNode;
24  import org.codehaus.groovy.GroovyBugError;
25  
26  /**
27   * Represents a base class for expressions which evaluate as an object
28   * 
29   * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
30   * @version $Revision: 17114 $
31   */
32  public abstract class Expression extends AnnotatedNode {
33  
34      private ClassNode type=ClassHelper.DYNAMIC_TYPE;
35      
36      /**
37       * Return a copy of the expression calling the transformer on any nested expressions 
38       * @param transformer
39       */
40      public abstract Expression transformExpression(ExpressionTransformer transformer);
41  
42      /**
43       * Transforms the list of expressions
44       * @return a new list of transformed expressions
45       */
46      protected List<Expression> transformExpressions(List<? extends Expression> expressions, ExpressionTransformer transformer) {
47          List<Expression> list = new ArrayList<Expression>(expressions.size());
48          for (Expression expr : expressions ) {
49              list.add(transformer.transform(expr));
50          }
51          return list;
52      }
53  
54      /**
55       * Transforms the list of expressions, and checks that all transformed expressions have the given type.
56       *
57       * @return a new list of transformed expressions
58       */
59      protected <T extends Expression> List<T> transformExpressions(List<? extends Expression> expressions,
60              ExpressionTransformer transformer, Class<T> transformedType) {
61          List<T> list = new ArrayList<T>(expressions.size());
62          for (Expression expr : expressions) {
63              Expression transformed = transformer.transform(expr);
64              if (!transformedType.isInstance(transformed))
65                  throw new GroovyBugError(String.format("Transformed expression should have type %s but has type %s",
66                      transformedType, transformed.getClass()));
67              list.add(transformedType.cast(transformed));
68          }
69          return list;
70      }
71      
72      public ClassNode getType() {
73          return type;
74      }
75      
76      public void setType(ClassNode t) {
77          type=t;
78      }
79  }