1 46 package org.codehaus.groovy.ast.expr; 47 48 import java.util.Collections ; 49 import java.util.Iterator ; 50 import java.util.List ; 51 52 import org.codehaus.groovy.ast.GroovyCodeVisitor; 53 import org.codehaus.groovy.classgen.BytecodeHelper; 54 import org.codehaus.groovy.classgen.AsmClassGenerator2; 55 56 63 public class ArrayExpression extends Expression { 64 private List expressions; 65 private Expression sizeExpression; 66 67 private String elementType; 68 71 public ArrayExpression(String type, List expressions) { 72 String boxedType = BytecodeHelper.getObjectTypeForPrimitive(type); 74 this.elementType = boxedType; 75 setSuperType(boxedType); 76 this.expressions = expressions; 77 78 for (Iterator iter = expressions.iterator(); iter.hasNext();) { 79 Object item = iter.next(); 80 if (!(item instanceof Expression)) { 81 throw new ClassCastException ("Item: " + item + " is not an Expression"); 82 } 83 } 84 } 85 86 private void setSuperType(String type) { 87 if (type == null) System.out.println("setSuperType: null"); 88 if (type.endsWith("[]")) { 89 } 90 else { 91 type += "[]"; 92 } 93 super.setType(type); } 95 96 99 public ArrayExpression(String type, Expression sizeExpression) { 100 String boxedType = BytecodeHelper.getObjectTypeForPrimitive(type); 101 this.elementType = boxedType; 102 setSuperType(boxedType); 103 this.sizeExpression = sizeExpression; 104 this.expressions = Collections.EMPTY_LIST; 105 } 106 107 public void addExpression(Expression expression) { 108 expressions.add(expression); 109 } 110 111 public List getExpressions() { 112 return expressions; 113 } 114 115 public void visit(GroovyCodeVisitor visitor) { 116 visitor.visitArrayExpression(this); 117 } 118 119 public boolean isDynamic() { 120 return false; 121 } 122 123 public Expression transformExpression(ExpressionTransformer transformer) { 124 return new ArrayExpression(type, transformExpressions(expressions, transformer)); 125 } 126 127 public Expression getExpression(int i) { 128 Object object = expressions.get(i); 129 return (Expression) object; 130 } 131 132 public String getElementType() { 133 if (BytecodeHelper.isPrimitiveType(elementType)) { 134 return BytecodeHelper.getObjectTypeForPrimitive(elementType); 135 } 136 return elementType; 137 } 138 139 public String getText() { 140 StringBuffer buffer = new StringBuffer ("["); 141 boolean first = true; 142 for (Iterator iter = expressions.iterator(); iter.hasNext();) { 143 if (first) { 144 first = false; 145 } 146 else { 147 buffer.append(", "); 148 } 149 150 buffer.append(((Expression) iter.next()).getText()); 151 } 152 buffer.append("]"); 153 return buffer.toString(); 154 } 155 156 public Expression getSizeExpression() { 157 return sizeExpression; 158 } 159 160 public String toString() { 161 return super.toString() + expressions; 162 } 163 164 protected void resolveType(AsmClassGenerator2 resolver) { 165 } 167 } 168 | Popular Tags |