001: /* ====================================================================
002: * Tea - Copyright (c) 1997-2000 Walt Disney Internet Group
003: * ====================================================================
004: * The Tea Software License, Version 1.1
005: *
006: * Copyright (c) 2000 Walt Disney Internet Group. All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * 1. Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * 2. Redistributions in binary form must reproduce the above copyright
016: * notice, this list of conditions and the following disclaimer in
017: * the documentation and/or other materials provided with the
018: * distribution.
019: *
020: * 3. The end-user documentation included with the redistribution,
021: * if any, must include the following acknowledgment:
022: * "This product includes software developed by the
023: * Walt Disney Internet Group (http://opensource.go.com/)."
024: * Alternately, this acknowledgment may appear in the software itself,
025: * if and wherever such third-party acknowledgments normally appear.
026: *
027: * 4. The names "Tea", "TeaServlet", "Kettle", "Trove" and "BeanDoc" must
028: * not be used to endorse or promote products derived from this
029: * software without prior written permission. For written
030: * permission, please contact opensource@dig.com.
031: *
032: * 5. Products derived from this software may not be called "Tea",
033: * "TeaServlet", "Kettle" or "Trove", nor may "Tea", "TeaServlet",
034: * "Kettle", "Trove" or "BeanDoc" appear in their name, without prior
035: * written permission of the Walt Disney Internet Group.
036: *
037: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
038: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
039: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
040: * DISCLAIMED. IN NO EVENT SHALL THE WALT DISNEY INTERNET GROUP OR ITS
041: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
042: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
043: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
044: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
045: * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
046: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
047: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
048: * ====================================================================
049: *
050: * For more information about Tea, please see http://opensource.go.com/.
051: */
052:
053: package com.go.tea.parsetree;
054:
055: import java.beans.IntrospectionException;
056: import com.go.tea.compiler.SourceInfo;
057: import com.go.tea.compiler.Type;
058:
059: /******************************************************************************
060: * An expression that evaluates to a new array or Map of values.
061: *
062: * @author Brian S O'Neill
063: * @version
064: * <!--$$Revision:--> 21 <!-- $-->, <!--$$JustDate:--> 9/07/00 <!-- $-->
065: */
066: public class NewArrayExpression extends Expression {
067: private ExpressionList mList;
068: private boolean mAssociative;
069:
070: public NewArrayExpression(SourceInfo info, ExpressionList list,
071: boolean associative) {
072: super (info);
073:
074: mList = list;
075: mAssociative = associative;
076: }
077:
078: public Object clone() {
079: NewArrayExpression nae = (NewArrayExpression) super .clone();
080: nae.mList = (ExpressionList) mList.clone();
081: return nae;
082: }
083:
084: public boolean isExceptionPossible() {
085: if (mList != null) {
086: Expression[] exprs = mList.getExpressions();
087: for (int i = 0; i < exprs.length; i++) {
088: if (exprs[i].isExceptionPossible()) {
089: return true;
090: }
091: }
092: }
093: return false;
094: }
095:
096: public Object accept(NodeVisitor visitor) {
097: return visitor.visit(this );
098: }
099:
100: public void convertTo(Type toType, boolean preferCast) {
101: super .convertTo(toType, preferCast);
102:
103: // If converting to a different array element type, convert all the
104: // expressions in the list.
105:
106: if (String.class.isAssignableFrom(toType.getNaturalClass())) {
107: // Special case to prevent String conversion from setting all
108: // internal elements to chars.
109: return;
110: }
111:
112: Type elementType;
113: try {
114: elementType = toType.getArrayElementType();
115: } catch (IntrospectionException e) {
116: throw new RuntimeException(e.toString());
117: }
118:
119: if (elementType != null) {
120: super .setType(toType);
121:
122: Expression[] exprs = getExpressionList().getExpressions();
123:
124: int index, increment;
125: if (isAssociative()) {
126: index = 1;
127: increment = 2;
128: } else {
129: index = 0;
130: increment = 1;
131: }
132:
133: for (; index < exprs.length; index += increment) {
134: if (exprs[index].getType() != Type.NULL_TYPE) {
135: exprs[index].convertTo(elementType, preferCast);
136: }
137: }
138: }
139: }
140:
141: public void setType(Type type) {
142: super .setType(null);
143: if (type != null) {
144: // NewArrayExpressions never evaluate to null.
145: // Call the overridden convertTo method in order for elements to
146: // be converted to the correct type.
147: this .convertTo(type.toNonNull(), false);
148: }
149: }
150:
151: public ExpressionList getExpressionList() {
152: return mList;
153: }
154:
155: public boolean isAssociative() {
156: return mAssociative;
157: }
158:
159: public void setExpressionList(ExpressionList list) {
160: mList = list;
161: }
162:
163: /**
164: * @return true if this array is composed entirely of constants.
165: */
166: public boolean isAllConstant() {
167: Expression[] exprs = mList.getExpressions();
168:
169: int i;
170: for (i = 0; i < exprs.length; i++) {
171: Expression expr = exprs[i];
172:
173: if (expr instanceof NewArrayExpression) {
174: NewArrayExpression nae = (NewArrayExpression) expr;
175: if (!nae.isAllConstant()) {
176: return false;
177: }
178: } else if (!expr.isValueKnown()) {
179: return false;
180: }
181: }
182:
183: return true;
184: }
185: }
|