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.ast;
19:
20: import static org.mvel.MVEL.eval;
21: import org.mvel.compiler.ExecutableStatement;
22: import org.mvel.compiler.AbstractParser;
23: import org.mvel.integration.VariableResolverFactory;
24: import static org.mvel.util.ParseTools.*;
25: import static org.mvel.util.PropertyTools.find;
26:
27: /**
28: * @author Christopher Brock
29: */
30: public class TypedVarNode extends ASTNode implements Assignment {
31: private String name;
32: private char[] stmt;
33:
34: private transient ExecutableStatement statement;
35:
36: public TypedVarNode(char[] expr, int fields, Class type) {
37: this .egressType = type;
38: this .fields = fields;
39:
40: int assignStart;
41: if ((assignStart = find(super .name = expr, '=')) != -1) {
42: checkNameSafety(name = new String(expr, 0, assignStart)
43: .trim());
44:
45: if (((fields |= ASSIGN) & COMPILE_IMMEDIATE) != 0) {
46: statement = (ExecutableStatement) subCompileExpression(stmt = subset(
47: expr, assignStart + 1));
48: } else {
49: stmt = subset(expr, assignStart + 1);
50: }
51: } else {
52: checkNameSafety(name = new String(expr));
53:
54: }
55:
56: if ((fields & COMPILE_IMMEDIATE) != 0) {
57: AbstractParser.getCurrentThreadParserContext().addVariable(
58: name, egressType, true);
59: }
60: }
61:
62: public Object getReducedValueAccelerated(Object ctx,
63: Object this Value, VariableResolverFactory factory) {
64: if (statement == null)
65: statement = (ExecutableStatement) subCompileExpression(stmt);
66: factory.createVariable(name, ctx = statement.getValue(ctx,
67: this Value, factory), egressType);
68: return ctx;
69: }
70:
71: public Object getReducedValue(Object ctx, Object this Value,
72: VariableResolverFactory factory) {
73: factory.createVariable(name, ctx = eval(stmt, this Value,
74: factory), egressType);
75: return ctx;
76: }
77:
78: public String getName() {
79: return name;
80: }
81:
82: public String getAssignmentVar() {
83: return name;
84: }
85:
86: public char[] getExpression() {
87: return stmt;
88: }
89:
90: public boolean isNewDeclaration() {
91: return true;
92: }
93: }
|