PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 144 lines | 83 code | 18 blank | 43 comment | 8 complexity | 3a3404ad4c95d167499c1b4453f2973e 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. /*
  34. Warning: this is a hack... should be unified with BSHPrimarySuffix
  35. */
  36. package bsh;
  37. import java.util.Hashtable;
  38. import java.lang.reflect.InvocationTargetException;
  39. class BSHLHSPrimarySuffix extends SimpleNode
  40. {
  41. public static final int
  42. INDEX = 1,
  43. NAME = 2,
  44. PROPERTY = 3;
  45. public int operation; // field access or index
  46. Object index; // the index value if any
  47. /*
  48. If this is a simple field access field is the field
  49. If we're hopscotching a method to a field, method is the
  50. method name and field is the field of the resulting object
  51. */
  52. public String field;
  53. public String method;
  54. BSHLHSPrimarySuffix(int id) { super(id); }
  55. public LHS doLHSSuffix(
  56. Object obj, CallStack callstack, Interpreter interpreter)
  57. throws EvalError
  58. {
  59. try
  60. {
  61. switch(operation)
  62. {
  63. case INDEX:
  64. return doIndex(obj, callstack, interpreter);
  65. case NAME:
  66. return doName(obj, callstack, interpreter);
  67. case PROPERTY:
  68. return doProperty(obj, callstack, interpreter);
  69. default:
  70. throw new InterpreterError("LHS suffix");
  71. }
  72. }
  73. catch(ReflectError e)
  74. {
  75. throw new EvalError("reflection error: " + e, this);
  76. }
  77. catch(InvocationTargetException e)
  78. {
  79. throw new TargetError(
  80. "target exception", e.getTargetException(), this, true);
  81. }
  82. }
  83. private LHS doName(
  84. Object obj, CallStack callstack, Interpreter interpreter)
  85. throws EvalError, ReflectError, InvocationTargetException
  86. {
  87. if (jjtGetNumChildren() == 0)
  88. // simple field access
  89. return Reflect.getLHSObjectField(obj, field);
  90. else {
  91. // intermediate method invocation, and field access
  92. Object[] oa = ((BSHArguments)jjtGetChild(0)).getArguments(
  93. callstack, interpreter);
  94. try {
  95. obj = Reflect.invokeObjectMethod(interpreter, obj, method, oa, this);
  96. } catch ( EvalError ee ) {
  97. // catch and re-throw to get line number right
  98. throw new EvalError( ee.getMessage(), this );
  99. }
  100. return Reflect.getLHSObjectField(obj, field);
  101. }
  102. }
  103. private LHS doIndex(
  104. Object obj, CallStack callstack, Interpreter interpreter)
  105. throws EvalError, ReflectError
  106. {
  107. int index = BSHPrimarySuffix.getIndexAux(
  108. obj, callstack, interpreter, this );
  109. return new LHS(obj, index);
  110. }
  111. private LHS doProperty(
  112. Object obj, CallStack callstack, Interpreter interpreter)
  113. throws EvalError, ReflectError
  114. {
  115. if(obj == Primitive.VOID)
  116. throw new EvalError("Attempt to access property on a void type", this);
  117. else if(obj instanceof Primitive)
  118. throw new EvalError("Attempt to access property on a primitive", this);
  119. Object value = ((SimpleNode)jjtGetChild(0)).eval(
  120. callstack, interpreter);
  121. if(!(value instanceof String))
  122. throw new EvalError("Property expression must be a String or identifier.", this);
  123. Interpreter.debug("LHS property access: ");
  124. return new LHS(obj, (String)value);
  125. }
  126. }