PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre14/bsh/LHS.java

#
Java | 270 lines | 159 code | 26 blank | 85 comment | 28 complexity | 0770b9fde8ca68723d6e20f8c07e245e MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  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. package bsh;
  34. import java.lang.reflect.Field;
  35. import java.util.Hashtable;
  36. /**
  37. An LHS is a wrapper for an variable, field, or property. It ordinarily
  38. holds the "left hand side" of an assignment and may be either resolved to
  39. a value or assigned a value.
  40. <p>
  41. There is one special case here termed METHOD_EVAL where the LHS is used
  42. in an intermediate evaluation of a chain of suffixes and wraps a method
  43. invocation. In this case it may only be resolved to a value and cannot be
  44. assigned. (You can't assign a value to the result of a method call e.g.
  45. "foo() = 5;").
  46. <p>
  47. */
  48. class LHS implements ParserConstants, java.io.Serializable
  49. {
  50. NameSpace nameSpace;
  51. /** The assignment should be to a local variable */
  52. boolean localVar;
  53. /**
  54. Identifiers for the various types of LHS.
  55. */
  56. static final int
  57. VARIABLE = 0,
  58. FIELD = 1,
  59. PROPERTY = 2,
  60. INDEX = 3,
  61. METHOD_EVAL = 4;
  62. int type;
  63. String varName;
  64. String propName;
  65. Field field;
  66. Object object;
  67. int index;
  68. /**
  69. Variable LHS constructor.
  70. */
  71. LHS( NameSpace nameSpace, String varName )
  72. {
  73. throw new Error("namespace lhs");
  74. /*
  75. type = VARIABLE;
  76. this.varName = varName;
  77. this.nameSpace = nameSpace;
  78. */
  79. }
  80. /**
  81. @param localVar if true the variable is set directly in the This
  82. reference's local scope. If false recursion to look for the variable
  83. definition in parent's scope is allowed. (e.g. the default case for
  84. undefined vars going to global).
  85. */
  86. LHS( NameSpace nameSpace, String varName, boolean localVar )
  87. {
  88. type = VARIABLE;
  89. this.localVar = localVar;
  90. this.varName = varName;
  91. this.nameSpace = nameSpace;
  92. }
  93. /**
  94. Static field LHS Constructor.
  95. This simply calls Object field constructor with null object.
  96. */
  97. LHS( Field field )
  98. {
  99. type = FIELD;
  100. this.object = null;
  101. this.field = field;
  102. }
  103. /**
  104. Object field LHS Constructor.
  105. */
  106. LHS( Object object, Field field )
  107. {
  108. if ( object == null)
  109. throw new NullPointerException("constructed empty LHS");
  110. type = FIELD;
  111. this.object = object;
  112. this.field = field;
  113. }
  114. /**
  115. Object property LHS Constructor.
  116. */
  117. LHS( Object object, String propName )
  118. {
  119. if(object == null)
  120. throw new NullPointerException("constructed empty LHS");
  121. type = PROPERTY;
  122. this.object = object;
  123. this.propName = propName;
  124. }
  125. /**
  126. Array index LHS Constructor.
  127. */
  128. LHS( Object array, int index )
  129. {
  130. if(array == null)
  131. throw new NullPointerException("constructed empty LHS");
  132. type = INDEX;
  133. this.object = array;
  134. this.index = index;
  135. }
  136. public Object getValue() throws UtilEvalError
  137. {
  138. if ( type == VARIABLE )
  139. return nameSpace.getVariable( varName );
  140. if (type == FIELD)
  141. try {
  142. Object o = field.get( object );
  143. return Primitive.wrap( o, field.getType() );
  144. } catch(IllegalAccessException e2) {
  145. throw new UtilEvalError("Can't read field: " + field);
  146. }
  147. if ( type == PROPERTY )
  148. try {
  149. return Reflect.getObjectProperty(object, propName);
  150. }
  151. catch(ReflectError e) {
  152. Interpreter.debug(e.getMessage());
  153. throw new UtilEvalError("No such property: " + propName);
  154. }
  155. if ( type == INDEX )
  156. try {
  157. return Reflect.getIndex(object, index);
  158. }
  159. catch(Exception e) {
  160. throw new UtilEvalError("Array access: " + e);
  161. }
  162. throw new InterpreterError("LHS type");
  163. }
  164. /**
  165. Assign a value to the LHS.
  166. */
  167. public Object assign( Object val, boolean strictJava )
  168. throws UtilEvalError
  169. {
  170. if ( type == VARIABLE )
  171. {
  172. // Set the variable in namespace according to localVar flag
  173. if ( localVar )
  174. nameSpace.setLocalVariable( varName, val, strictJava );
  175. else
  176. nameSpace.setVariable( varName, val, strictJava );
  177. } else
  178. if ( type == FIELD )
  179. {
  180. try {
  181. Object fieldVal = val instanceof Primitive ?
  182. ((Primitive)val).getValue() : val;
  183. // This should probably be in Reflect.java
  184. ReflectManager.RMSetAccessible( field );
  185. field.set( object, fieldVal );
  186. return val;
  187. }
  188. catch( NullPointerException e) {
  189. throw new UtilEvalError(
  190. "LHS ("+field.getName()+") not a static field.");
  191. }
  192. catch( IllegalAccessException e2) {
  193. throw new UtilEvalError(
  194. "LHS ("+field.getName()+") can't access field: "+e2);
  195. }
  196. catch( IllegalArgumentException e3)
  197. {
  198. String type = val instanceof Primitive ?
  199. ((Primitive)val).getType().getName()
  200. : val.getClass().getName();
  201. throw new UtilEvalError(
  202. "Argument type mismatch. " + (val == null ? "null" : type )
  203. + " not assignable to field "+field.getName());
  204. }
  205. }
  206. else
  207. if ( type == PROPERTY )
  208. {
  209. /*
  210. if ( object instanceof Hashtable )
  211. ((Hashtable)object).put(propName, val);
  212. */
  213. CollectionManager cm = CollectionManager.getCollectionManager();
  214. if ( cm.isMap( object ) )
  215. cm.putInMap( object/*map*/, propName, val );
  216. else
  217. try {
  218. Reflect.setObjectProperty(object, propName, val);
  219. }
  220. catch(ReflectError e) {
  221. Interpreter.debug("Assignment: " + e.getMessage());
  222. throw new UtilEvalError("No such property: " + propName);
  223. }
  224. } else
  225. if ( type == INDEX )
  226. try {
  227. Reflect.setIndex(object, index, val);
  228. } catch ( UtilTargetError e1 ) { // pass along target error
  229. throw e1;
  230. } catch ( Exception e ) {
  231. throw new UtilEvalError("Assignment: " + e.getMessage());
  232. }
  233. else
  234. throw new InterpreterError("unknown lhs");
  235. return val;
  236. }
  237. public String toString() {
  238. return "LHS: "
  239. +((field!=null)? "field = "+field.toString():"")
  240. +(varName!=null ? " varName = "+varName: "")
  241. +(nameSpace!=null ? " nameSpace = "+nameSpace.toString(): "");
  242. }
  243. }