001: //--------------------------------------------------------------------------
002: // Copyright (c) 1998-2004, Drew Davidson and Luke Blanshard
003: // All rights reserved.
004: //
005: // Redistribution and use in source and binary forms, with or without
006: // modification, are permitted provided that the following conditions are
007: // met:
008: //
009: // Redistributions of source code must retain the above copyright notice,
010: // this list of conditions and the following disclaimer.
011: // Redistributions in binary form must reproduce the above copyright
012: // notice, this list of conditions and the following disclaimer in the
013: // documentation and/or other materials provided with the distribution.
014: // Neither the name of the Drew Davidson nor the names of its contributors
015: // may be used to endorse or promote products derived from this software
016: // without specific prior written permission.
017: //
018: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
019: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
020: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
021: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
022: // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
023: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
024: // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
025: // OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
026: // AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
027: // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
028: // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
029: // DAMAGE.
030: //--------------------------------------------------------------------------
031: package ognl;
032:
033: import java.lang.reflect.*;
034: import java.util.*;
035:
036: /**
037: * @author Luke Blanshard (blanshlu@netscape.net)
038: * @author Drew Davidson (drew@ognl.org)
039: */
040: class ASTCtor extends SimpleNode {
041: private String className;
042: private boolean isArray;
043:
044: public ASTCtor(int id) {
045: super (id);
046: }
047:
048: public ASTCtor(OgnlParser p, int id) {
049: super (p, id);
050: }
051:
052: /** Called from parser action. */
053: void setClassName(String className) {
054: this .className = className;
055: }
056:
057: void setArray(boolean value) {
058: isArray = value;
059: }
060:
061: protected Object getValueBody(OgnlContext context, Object source)
062: throws OgnlException {
063: Object result, root = context.getRoot();
064: int count = jjtGetNumChildren();
065: Object[] args = OgnlRuntime.getObjectArrayPool().create(count);
066:
067: try {
068: for (int i = 0; i < count; ++i) {
069: args[i] = children[i].getValue(context, root);
070: }
071: if (isArray) {
072: if (args.length == 1) {
073: try {
074: Class componentClass = OgnlRuntime
075: .classForName(context, className);
076: List sourceList = null;
077: int size;
078:
079: if (args[0] instanceof List) {
080: sourceList = (List) args[0];
081: size = sourceList.size();
082: } else {
083: size = (int) OgnlOps.longValue(args[0]);
084: }
085: result = Array
086: .newInstance(componentClass, size);
087: if (sourceList != null) {
088: TypeConverter converter = context
089: .getTypeConverter();
090:
091: for (int i = 0, icount = sourceList.size(); i < icount; i++) {
092: Object o = sourceList.get(i);
093:
094: if ((o == null)
095: || componentClass.isInstance(o)) {
096: Array.set(result, i, o);
097: } else {
098: Array.set(result, i, converter
099: .convertValue(context,
100: null, null, null,
101: o, componentClass));
102: }
103: }
104: }
105: } catch (ClassNotFoundException ex) {
106: throw new OgnlException(
107: "array component class '" + className
108: + "' not found", ex);
109: }
110: } else {
111: throw new OgnlException(
112: "only expect array size or fixed initializer list");
113: }
114: } else {
115: result = OgnlRuntime.callConstructor(context,
116: className, args);
117: }
118:
119: return result;
120: } finally {
121: OgnlRuntime.getObjectArrayPool().recycle(args);
122: }
123: }
124:
125: public String toString() {
126: String result = "new " + className;
127:
128: if (isArray) {
129: if (children[0] instanceof ASTConst) {
130: result = result + "[" + children[0] + "]";
131: } else {
132: result = result + "[] " + children[0];
133: }
134: } else {
135: result = result + "(";
136: if ((children != null) && (children.length > 0)) {
137: for (int i = 0; i < children.length; i++) {
138: if (i > 0) {
139: result = result + ", ";
140: }
141: result = result + children[i];
142: }
143: }
144: result = result + ")";
145: }
146: return result;
147: }
148: }
|