PageRenderTime 53ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 202 lines | 132 code | 23 blank | 47 comment | 31 complexity | 7ab57ec6da62b5c18f37bc31db55ce68 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. */
  46. class LHS implements ParserConstants, java.io.Serializable
  47. {
  48. NameSpace nameSpace;
  49. static final int
  50. VARIABLE = 0,
  51. FIELD = 1,
  52. PROPERTY = 2,
  53. INDEX = 3;
  54. int type;
  55. String varName;
  56. String propName;
  57. Field field;
  58. Object object;
  59. int index;
  60. LHS(NameSpace nameSpace, String varName)
  61. {
  62. type = VARIABLE;
  63. this.varName = varName;
  64. this.nameSpace = nameSpace;
  65. }
  66. // Static field
  67. LHS(Field field)
  68. {
  69. type = FIELD;
  70. this.object = null;
  71. this.field = field;
  72. }
  73. // Object field
  74. LHS(Object object, Field field)
  75. {
  76. if(object == null)
  77. throw new NullPointerException("constructed empty LHS");
  78. type = FIELD;
  79. this.object = object;
  80. this.field = field;
  81. }
  82. // Object property
  83. LHS(Object object, String propName)
  84. {
  85. if(object == null)
  86. throw new NullPointerException("constructed empty LHS");
  87. type = PROPERTY;
  88. this.object = object;
  89. this.propName = propName;
  90. }
  91. // Array index
  92. LHS(Object array, int index)
  93. {
  94. if(array == null)
  95. throw new NullPointerException("constructed empty LHS");
  96. type = INDEX;
  97. this.object = array;
  98. this.index = index;
  99. }
  100. public Object getValue() throws EvalError
  101. {
  102. if(type == VARIABLE)
  103. return nameSpace.getVariable(varName);
  104. else if (type == FIELD)
  105. try {
  106. return field.get(object);
  107. }
  108. catch(IllegalAccessException e2) {
  109. throw new EvalError("Can't read field: " + field);
  110. }
  111. else if(type == PROPERTY)
  112. try {
  113. return Reflect.getObjectProperty(object, propName);
  114. }
  115. catch(ReflectError e) {
  116. Interpreter.debug(e.getMessage());
  117. throw new EvalError("No such property: " + propName);
  118. }
  119. else if(type == INDEX)
  120. try
  121. {
  122. return Reflect.getIndex(object, index);
  123. }
  124. catch(Exception e)
  125. {
  126. throw new EvalError("Array access: " + e);
  127. }
  128. throw new InterpreterError("LHS type");
  129. }
  130. public Object assign( Object val ) throws EvalError
  131. {
  132. if ( type == VARIABLE )
  133. nameSpace.setVariable(varName, val);
  134. else
  135. if ( type == FIELD )
  136. try {
  137. if(val instanceof Primitive)
  138. val = ((Primitive)val).getValue();
  139. field.set(object, val);
  140. return val;
  141. }
  142. catch( NullPointerException e) {
  143. throw new EvalError(
  144. "LHS ("+field.getName()+") not a static field.");
  145. }
  146. catch( IllegalAccessException e2) {
  147. throw new EvalError(
  148. "LHS ("+field.getName()+") can't access field.");
  149. }
  150. catch( IllegalArgumentException e3) {
  151. throw new EvalError(
  152. "Argument type mismatch. "
  153. + (val == null ? "null" : val.getClass().getName())
  154. + " not assignable to field "+field.getName());
  155. }
  156. else if(type == PROPERTY)
  157. if ( object instanceof Hashtable )
  158. ((Hashtable)object).put(propName, val);
  159. else
  160. try {
  161. Reflect.setObjectProperty(object, propName, val);
  162. }
  163. catch(ReflectError e) {
  164. Interpreter.debug("Assignment: " + e.getMessage());
  165. throw new EvalError("No such property: " + propName);
  166. }
  167. else if(type == INDEX)
  168. try {
  169. Reflect.setIndex(object, index, val);
  170. } catch(TargetError e1) { // pass along target error
  171. throw e1;
  172. } catch(Exception e) {
  173. throw new EvalError("Assignment: " + e.getMessage());
  174. }
  175. return val;
  176. }
  177. public String toString() { return "LHS"; }
  178. }