PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre5/bsh/LHS.java

#
Java | 205 lines | 132 code | 23 blank | 50 comment | 31 complexity | 10587853b7d5de544de6c85e813ca4ee 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. The left hand side in an assignment
  38. This is probably the most debatable design issue in bsh...
  39. Because of the way things started, the grammar splits on whether
  40. something is an LHS or not... The alternative would be to wrap all
  41. objects in bsh, rather than just carrying types that might be used in
  42. an assignment. Wrapping all objects, say, in a generalized BshObject
  43. type, would also provide a nice place to put all the reflection stuff,
  44. which is now static in bsh.Reflect
  45. Note: moving some stuff from Reflect to a BshObject, but not going
  46. as far as the above yet...
  47. */
  48. class LHS implements ParserConstants, java.io.Serializable
  49. {
  50. NameSpace nameSpace;
  51. static final int
  52. VARIABLE = 0,
  53. FIELD = 1,
  54. PROPERTY = 2,
  55. INDEX = 3;
  56. int type;
  57. String varName;
  58. String propName;
  59. Field field;
  60. Object object;
  61. int index;
  62. LHS(NameSpace nameSpace, String varName)
  63. {
  64. type = VARIABLE;
  65. this.varName = varName;
  66. this.nameSpace = nameSpace;
  67. }
  68. // Static field
  69. LHS(Field field)
  70. {
  71. type = FIELD;
  72. this.object = null;
  73. this.field = field;
  74. }
  75. // Object field
  76. LHS(Object object, Field field)
  77. {
  78. if(object == null)
  79. throw new NullPointerException("constructed empty LHS");
  80. type = FIELD;
  81. this.object = object;
  82. this.field = field;
  83. }
  84. // Object property
  85. LHS(Object object, String propName)
  86. {
  87. if(object == null)
  88. throw new NullPointerException("constructed empty LHS");
  89. type = PROPERTY;
  90. this.object = object;
  91. this.propName = propName;
  92. }
  93. // Array index
  94. LHS(Object array, int index)
  95. {
  96. if(array == null)
  97. throw new NullPointerException("constructed empty LHS");
  98. type = INDEX;
  99. this.object = array;
  100. this.index = index;
  101. }
  102. public Object getValue() throws EvalError
  103. {
  104. if(type == VARIABLE)
  105. return nameSpace.getVariable(varName);
  106. else if (type == FIELD)
  107. try {
  108. return field.get(object);
  109. }
  110. catch(IllegalAccessException e2) {
  111. throw new EvalError("Can't read field: " + field);
  112. }
  113. else if(type == PROPERTY)
  114. try {
  115. return Reflect.getObjectProperty(object, propName);
  116. }
  117. catch(ReflectError e) {
  118. Interpreter.debug(e.getMessage());
  119. throw new EvalError("No such property: " + propName);
  120. }
  121. else if(type == INDEX)
  122. try
  123. {
  124. return Reflect.getIndex(object, index);
  125. }
  126. catch(Exception e)
  127. {
  128. throw new EvalError("Array access: " + e);
  129. }
  130. throw new InterpreterError("LHS type");
  131. }
  132. public Object assign( Object val ) throws EvalError
  133. {
  134. if ( type == VARIABLE )
  135. nameSpace.setVariable(varName, val);
  136. else
  137. if ( type == FIELD )
  138. try {
  139. if(val instanceof Primitive)
  140. val = ((Primitive)val).getValue();
  141. field.set(object, val);
  142. return val;
  143. }
  144. catch( NullPointerException e) {
  145. throw new EvalError(
  146. "LHS ("+field.getName()+") not a static field.");
  147. }
  148. catch( IllegalAccessException e2) {
  149. throw new EvalError(
  150. "LHS ("+field.getName()+") can't access field.");
  151. }
  152. catch( IllegalArgumentException e3) {
  153. throw new EvalError(
  154. "Argument type mismatch. "
  155. + (val == null ? "null" : val.getClass().getName())
  156. + " not assignable to field "+field.getName());
  157. }
  158. else if(type == PROPERTY)
  159. if ( object instanceof Hashtable )
  160. ((Hashtable)object).put(propName, val);
  161. else
  162. try {
  163. Reflect.setObjectProperty(object, propName, val);
  164. }
  165. catch(ReflectError e) {
  166. Interpreter.debug("Assignment: " + e.getMessage());
  167. throw new EvalError("No such property: " + propName);
  168. }
  169. else if(type == INDEX)
  170. try {
  171. Reflect.setIndex(object, index, val);
  172. } catch(TargetError e1) { // pass along target error
  173. throw e1;
  174. } catch(Exception e) {
  175. throw new EvalError("Assignment: " + e.getMessage());
  176. }
  177. return val;
  178. }
  179. public String toString() { return "LHS"; }
  180. }