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.Collections;
19  import java.util.List;
20  
21  import org.codehaus.groovy.ast.ClassNode;
22  import org.codehaus.groovy.ast.GroovyCodeVisitor;
23  
24  /**
25   * Represents an array object construction either using a fixed size
26   * or an initializer expression
27   *
28   * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
29   * @version $Revision: 20590 $
30   */
31  public class ArrayExpression extends Expression {
32      private List<Expression> expressions;
33      private List<Expression> sizeExpression;
34  
35      private ClassNode elementType;
36  
37      private static ClassNode makeArray(ClassNode base, List<Expression> sizeExpression) {
38          ClassNode ret = base.makeArray();
39          if (sizeExpression == null) return ret;
40          int size = sizeExpression.size();
41          for (int i = 1; i < size; i++) {
42              ret = ret.makeArray();
43          }
44          return ret;
45      }
46  
47      public ArrayExpression(ClassNode elementType, List<Expression> expressions, List<Expression> sizeExpression) {
48          //expect to get the elementType
49          super.setType(makeArray(elementType, sizeExpression));
50          if (expressions == null) expressions = Collections.emptyList();
51          this.elementType = elementType;
52          this.expressions = expressions;
53          this.sizeExpression = sizeExpression;
54  
55          for (Object item : expressions) {
56              if (item != null && !(item instanceof Expression)) {
57                  throw new ClassCastException("Item: " + item + " is not an Expression");
58              }
59          }
60          if (sizeExpression != null) {
61              for (Object item : sizeExpression) {
62                  if (!(item instanceof Expression)) {
63                      throw new ClassCastException("Item: " + item + " is not an Expression");
64                  }
65              }
66          }
67      }
68  
69  
70      /**
71       * Creates an array using an initializer expression
72       */
73      public ArrayExpression(ClassNode elementType, List<Expression> expressions) {
74          this(elementType, expressions, null);
75      }
76  
77      public void addExpression(Expression expression) {
78          expressions.add(expression);
79      }
80  
81      public List<Expression> getExpressions() {
82          return expressions;
83      }
84  
85      public void visit(GroovyCodeVisitor visitor) {
86          visitor.visitArrayExpression(this);
87      }
88  
89      public boolean isDynamic() {
90          return false;
91      }
92  
93      public Expression transformExpression(ExpressionTransformer transformer) {
94          List<Expression> exprList = transformExpressions(expressions, transformer);
95          List<Expression> sizes = null;
96          if (sizeExpression != null) sizes = transformExpressions(sizeExpression, transformer);
97          Expression ret = new ArrayExpression(elementType, exprList, sizes);
98          ret.setSourcePosition(this);
99          return ret;
100     }
101 
102     public Expression getExpression(int i) {
103         return expressions.get(i);
104     }
105 
106     public ClassNode getElementType() {
107         return elementType;
108     }
109 
110     public String getText() {
111         StringBuffer buffer = new StringBuffer("[");
112         boolean first = true;
113         for (Expression expression : expressions) {
114             if (first) {
115                 first = false;
116             } else {
117                 buffer.append(", ");
118             }
119 
120             buffer.append(expression.getText());
121         }
122         buffer.append("]");
123         return buffer.toString();
124     }
125 
126     public List<Expression> getSizeExpression() {
127         return sizeExpression;
128     }
129 
130     public String toString() {
131         return super.toString() + expressions;
132     }
133 }