KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > scriptengine > core > nodes > AssignmentStatementNode


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, is permitted provided that the following conditions are met: -
6  * Redistributions of source code must retain the above copyright notice, this
7  * list of conditions and the following disclaimer. - Redistributions in binary
8  * form must reproduce the above copyright notice, this list of conditions and
9  * the following disclaimer in the documentation and/or other materials
10  * provided with the distribution. - All advertising materials mentioning
11  * features or use of this software must display the following acknowledgment:
12  * "This product includes Djeneric." - Products derived from this software may
13  * not be called "Djeneric" nor may "Djeneric" appear in their names without
14  * prior written permission of Genimen BV. - Redistributions of any form
15  * whatsoever must retain the following acknowledgment: "This product includes
16  * Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG, OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.tools.scriptengine.core.nodes;
31
32 import com.genimen.djeneric.language.Messages;
33 import com.genimen.djeneric.repository.DjExtent;
34 import com.genimen.djeneric.repository.exceptions.ObjectNotDefinedException;
35 import com.genimen.djeneric.tools.scriptengine.core.DjScriptParserEngine;
36 import com.genimen.djeneric.tools.scriptengine.core.SimpleNode;
37 import com.genimen.djeneric.tools.scriptengine.core.util.DjScriptCompileTimeScope;
38 import com.genimen.djeneric.tools.scriptengine.core.util.DjScriptExecutionException;
39 import com.genimen.djeneric.tools.scriptengine.core.util.DjScriptExecutionTimeScope;
40
41 public class AssignmentStatementNode extends SimpleNode
42 {
43
44   public AssignmentStatementNode(int i)
45   {
46     super(i);
47   }
48
49   public AssignmentStatementNode(DjScriptParserEngine p, int i)
50   {
51     super(p, i);
52   }
53
54   public String JavaDoc getName()
55   {
56     return "assignment";
57   }
58
59   public String JavaDoc toString()
60   {
61     return getName();
62   }
63
64   public void execute(DjScriptExecutionTimeScope context) throws DjScriptExecutionException
65   {
66     PropertyPathNode propNode = (PropertyPathNode) getChild(PropertyPathNode.class);
67     ValueExpression valueExpr = (ValueExpression) getChild(1);
68
69     context.setPropertyValue(propNode.getPath(), valueExpr.getValue(context), this);
70   }
71
72   public String JavaDoc getValidatedTypeName(DjScriptCompileTimeScope context) throws DjScriptExecutionException
73   {
74     PropertyPathNode propNode = (PropertyPathNode) getChild(PropertyPathNode.class);
75     ValueExpression valueExpr = (ValueExpression) getChild(1);
76
77     String JavaDoc leftType = propNode.getValidatedTypeName(context);
78     String JavaDoc rightType = valueExpr.getValidatedTypeName(context);
79
80     DjExtent le = null;
81     DjExtent re = null;
82
83     try
84     {
85       le = context.getPersistenceManager().getExtentByObjectType(leftType);
86     }
87     catch (ObjectNotDefinedException onde)
88     {
89       // It's not a model object
90
}
91
92     try
93     {
94       re = context.getPersistenceManager().getExtentByObjectType(rightType);
95     }
96     catch (ObjectNotDefinedException onde)
97     {
98       // It's not a model object
99
}
100
101     if (le != null && re != null)
102     {
103       // Also support implicit casting i.e. also le.isInstanceof(re)
104
if (!re.isInstanceof(le) && !le.isInstanceof(re)) throw new DjScriptExecutionException(Messages
105           .getString("Variable.CannotAssign", re.getObjectType(), le.getObjectType()), this);
106       return re.getQualifiedObjectType();
107     }
108
109     // If the left type is valid; and the right type is function determined, assume the
110
// the type of object will be ok
111
if (le != null && PropertyOrFunctionNode.FUNCTION_DETERMINED.equals(rightType))
112     {
113       return leftType;
114     }
115
116     if (le != null || re != null)
117     {
118       throw new DjScriptExecutionException(Messages.getString("Variable.CannotAssign", rightType, leftType), this);
119     }
120     return rightType;
121   }
122
123   public void validateScript(DjScriptCompileTimeScope ctxt) throws DjScriptExecutionException
124   {
125     getValidatedTypeName(ctxt);
126   }
127
128 }
Popular Tags