01: /**
02: * MVEL (The MVFLEX Expression Language)
03: *
04: * Copyright (C) 2007 Christopher Brock, MVFLEX/Valhalla Project and the Codehaus
05: *
06: * Licensed under the Apache License, Version 2.0 (the "License");
07: * you may not use this file except in compliance with the License.
08: * You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: *
18: */package org.mvel.optimizers.impl.refl;
19:
20: import static org.mvel.DataConversion.convert;
21: import org.mvel.compiler.AccessorNode;
22: import org.mvel.compiler.ExecutableStatement;
23: import org.mvel.integration.VariableResolverFactory;
24: import org.mvel.util.ParseTools;
25: import static org.mvel.util.PropertyTools.getBaseComponentType;
26:
27: import java.lang.reflect.Array;
28:
29: public class ArrayAccessorNest implements AccessorNode {
30: private AccessorNode nextNode;
31: private ExecutableStatement index;
32:
33: private Class baseComponentType;
34: private boolean requireConversion;
35:
36: public ArrayAccessorNest() {
37: }
38:
39: public ArrayAccessorNest(String index) {
40: this .index = (ExecutableStatement) ParseTools
41: .subCompileExpression(index);
42: }
43:
44: public ArrayAccessorNest(ExecutableStatement stmt) {
45: this .index = stmt;
46: }
47:
48: public Object getValue(Object ctx, Object elCtx,
49: VariableResolverFactory vars) {
50: if (nextNode != null) {
51: return nextNode.getValue(((Object[]) ctx)[(Integer) index
52: .getValue(ctx, elCtx, vars)], elCtx, vars);
53: } else {
54: return ((Object[]) ctx)[(Integer) index.getValue(ctx,
55: elCtx, vars)];
56: }
57: }
58:
59: public Object setValue(Object ctx, Object elCtx,
60: VariableResolverFactory variableFactory, Object value) {
61: if (baseComponentType == null) {
62: baseComponentType = getBaseComponentType(ctx.getClass());
63: requireConversion = baseComponentType != value.getClass()
64: && !baseComponentType.isAssignableFrom(value
65: .getClass());
66: }
67:
68: if (requireConversion) {
69: Object o = convert(value, baseComponentType);
70: Array.set(ctx, (Integer) index.getValue(ctx, elCtx,
71: variableFactory), o);
72: return o;
73: } else {
74: Array.set(ctx, (Integer) index.getValue(ctx, elCtx,
75: variableFactory), value);
76: return value;
77: }
78: }
79:
80: public ExecutableStatement getIndex() {
81: return index;
82: }
83:
84: public void setIndex(ExecutableStatement index) {
85: this .index = index;
86: }
87:
88: public AccessorNode getNextNode() {
89: return nextNode;
90: }
91:
92: public AccessorNode setNextNode(AccessorNode nextNode) {
93: return this .nextNode = nextNode;
94: }
95:
96: public String toString() {
97: return "Array Accessor -> [" + index + "]";
98: }
99: }
|