KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > bsh > LHS


1 /*****************************************************************************
2  * *
3  * This file is part of the BeanShell Java Scripting distribution. *
4  * Documentation and updates may be found at http://www.beanshell.org/ *
5  * *
6  * Sun Public License Notice: *
7  * *
8  * The contents of this file are subject to the Sun Public License Version *
9  * 1.0 (the "License"); you may not use this file except in compliance with *
10  * the License. A copy of the License is available at http://www.sun.com *
11  * *
12  * The Original Code is BeanShell. The Initial Developer of the Original *
13  * Code is Pat Niemeyer. Portions created by Pat Niemeyer are Copyright *
14  * (C) 2000. All Rights Reserved. *
15  * *
16  * GNU Public License Notice: *
17  * *
18  * Alternatively, the contents of this file may be used under the terms of *
19  * the GNU Lesser General Public License (the "LGPL"), in which case the *
20  * provisions of LGPL are applicable instead of those above. If you wish to *
21  * allow use of your version of this file only under the terms of the LGPL *
22  * and not to allow others to use your version of this file under the SPL, *
23  * indicate your decision by deleting the provisions above and replace *
24  * them with the notice and other provisions required by the LGPL. If you *
25  * do not delete the provisions above, a recipient may use your version of *
26  * this file under either the SPL or the LGPL. *
27  * *
28  * Patrick Niemeyer (pat@pat.net) *
29  * Author of Learning Java, O'Reilly & Associates *
30  * http://www.pat.net/~pat/ *
31  * *
32  *****************************************************************************/

33
34
35 package bsh;
36
37 import java.lang.reflect.Field JavaDoc;
38 import java.util.Hashtable JavaDoc;
39
40 /**
41     An LHS is a wrapper for an variable, field, or property. It ordinarily
42     holds the "left hand side" of an assignment and may be either resolved to
43     a value or assigned a value.
44     <p>
45     
46     There is one special case here termed METHOD_EVAL where the LHS is used
47     in an intermediate evaluation of a chain of suffixes and wraps a method
48     invocation. In this case it may only be resolved to a value and cannot be
49     assigned. (You can't assign a value to the result of a method call e.g.
50     "foo() = 5;").
51     <p>
52 */

53 class LHS implements ParserConstants, java.io.Serializable JavaDoc
54 {
55     NameSpace nameSpace;
56     /** The assignment should be to a local variable */
57     boolean localVar;
58
59     /**
60         Identifiers for the various types of LHS.
61     */

62     static final int
63         VARIABLE = 0,
64         FIELD = 1,
65         PROPERTY = 2,
66         INDEX = 3,
67         METHOD_EVAL = 4;
68
69     int type;
70
71     String JavaDoc varName;
72     String JavaDoc propName;
73     Field JavaDoc field;
74     Object JavaDoc object;
75     int index;
76
77 /**
78         Variable LHS constructor.
79 */

80     LHS( NameSpace nameSpace, String JavaDoc varName )
81     {
82 throw new Error JavaDoc("namespace lhs");
83 /*
84         type = VARIABLE;
85         this.varName = varName;
86         this.nameSpace = nameSpace;
87 */

88     }
89
90     /**
91         @param localVar if true the variable is set directly in the This
92         reference's local scope. If false recursion to look for the variable
93         definition in parent's scope is allowed. (e.g. the default case for
94         undefined vars going to global).
95     */

96     LHS( NameSpace nameSpace, String JavaDoc varName, boolean localVar )
97     {
98         type = VARIABLE;
99         this.localVar = localVar;
100         this.varName = varName;
101         this.nameSpace = nameSpace;
102     }
103
104     /**
105         Static field LHS Constructor.
106         This simply calls Object field constructor with null object.
107     */

108     LHS( Field JavaDoc field )
109     {
110         type = FIELD;
111         this.object = null;
112         this.field = field;
113     }
114
115     /**
116         Object field LHS Constructor.
117     */

118     LHS( Object JavaDoc object, Field JavaDoc field )
119     {
120         if ( object == null)
121             throw new NullPointerException JavaDoc("constructed empty LHS");
122
123         type = FIELD;
124         this.object = object;
125         this.field = field;
126     }
127
128     /**
129         Object property LHS Constructor.
130     */

131     LHS( Object JavaDoc object, String JavaDoc propName )
132     {
133         if(object == null)
134             throw new NullPointerException JavaDoc("constructed empty LHS");
135
136         type = PROPERTY;
137         this.object = object;
138         this.propName = propName;
139     }
140
141     /**
142         Array index LHS Constructor.
143     */

144     LHS( Object JavaDoc array, int index )
145     {
146         if(array == null)
147             throw new NullPointerException JavaDoc("constructed empty LHS");
148
149         type = INDEX;
150         this.object = array;
151         this.index = index;
152     }
153
154     public Object JavaDoc getValue() throws UtilEvalError
155     {
156         if ( type == VARIABLE )
157             return nameSpace.getVariable( varName );
158
159         if (type == FIELD)
160             try {
161                 Object JavaDoc o = field.get( object );
162                 return Primitive.wrap( o, field.getType() );
163             } catch(IllegalAccessException JavaDoc e2) {
164                 throw new UtilEvalError("Can't read field: " + field);
165             }
166
167         if ( type == PROPERTY )
168             try {
169                 return Reflect.getObjectProperty(object, propName);
170             }
171             catch(ReflectError e) {
172                 Interpreter.debug(e.getMessage());
173                 throw new UtilEvalError("No such property: " + propName);
174             }
175
176         if ( type == INDEX )
177             try {
178                 return Reflect.getIndex(object, index);
179             }
180             catch(Exception JavaDoc e) {
181                 throw new UtilEvalError("Array access: " + e);
182             }
183
184         throw new InterpreterError("LHS type");
185     }
186
187     /**
188         Assign a value to the LHS.
189     */

190     public Object JavaDoc assign( Object JavaDoc val, boolean strictJava )
191         throws UtilEvalError
192     {
193         if ( type == VARIABLE )
194         {
195             // Set the variable in namespace according to localVar flag
196
if ( localVar )
197                 nameSpace.setLocalVariable( varName, val, strictJava );
198             else
199                 nameSpace.setVariable( varName, val, strictJava );
200         } else
201         if ( type == FIELD )
202         {
203             try {
204                 Object JavaDoc fieldVal = val instanceof Primitive ?
205                     ((Primitive)val).getValue() : val;
206
207                 // This should probably be in Reflect.java
208
ReflectManager.RMSetAccessible( field );
209                 field.set( object, fieldVal );
210                 return val;
211             }
212             catch( NullPointerException JavaDoc e) {
213                 throw new UtilEvalError(
214                     "LHS ("+field.getName()+") not a static field.");
215             }
216             catch( IllegalAccessException JavaDoc e2) {
217                 throw new UtilEvalError(
218                     "LHS ("+field.getName()+") can't access field: "+e2);
219             }
220             catch( IllegalArgumentException JavaDoc e3)
221             {
222                 String JavaDoc type = val instanceof Primitive ?
223                     ((Primitive)val).getType().getName()
224                     : val.getClass().getName();
225                 throw new UtilEvalError(
226                     "Argument type mismatch. " + (val == null ? "null" : type )
227                     + " not assignable to field "+field.getName());
228             }
229         }
230         else
231         if ( type == PROPERTY )
232         {
233             /*
234             if ( object instanceof Hashtable )
235                 ((Hashtable)object).put(propName, val);
236             */

237             CollectionManager cm = CollectionManager.getCollectionManager();
238             if ( cm.isMap( object ) )
239                 cm.putInMap( object/*map*/, propName, val );
240             else
241                 try {
242                     Reflect.setObjectProperty(object, propName, val);
243                 }
244                 catch(ReflectError e) {
245                     Interpreter.debug("Assignment: " + e.getMessage());
246                     throw new UtilEvalError("No such property: " + propName);
247                 }
248         } else
249         if ( type == INDEX )
250             try {
251                 Reflect.setIndex(object, index, val);
252             } catch ( UtilTargetError e1 ) { // pass along target error
253
throw e1;
254             } catch ( Exception JavaDoc e ) {
255                 throw new UtilEvalError("Assignment: " + e.getMessage());
256             }
257         else
258             throw new InterpreterError("unknown lhs");
259
260         return val;
261     }
262
263     public String JavaDoc toString() {
264         return "LHS: "
265             +((field!=null)? "field = "+field.toString():"")
266             +(varName!=null ? " varName = "+varName: "")
267             +(nameSpace!=null ? " nameSpace = "+nameSpace.toString(): "");
268     }
269 }
270
271
Popular Tags