PageRenderTime 82ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre4/bsh/BSHLHSPrimarySuffix.java

#
Java | 154 lines | 84 code | 18 blank | 52 comment | 9 complexity | 62d99a8fe9c25a4fca8156832061bbc3 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, callstack );
  76. }
  77. catch(InvocationTargetException e)
  78. {
  79. throw new TargetError(
  80. "target exception", e.getTargetException(), this, callstack, true);
  81. }
  82. }
  83. private LHS doName(
  84. Object obj, CallStack callstack, Interpreter interpreter)
  85. throws EvalError, ReflectError, InvocationTargetException
  86. {
  87. try {
  88. if (jjtGetNumChildren() == 0)
  89. // simple field access
  90. return Reflect.getLHSObjectField(obj, field);
  91. else {
  92. throw new EvalError( "method invocation in suffix evaluation - broken", this, callstack );
  93. /*
  94. // intermediate method invocation, and field access
  95. //System.out.println("here: "+obj);
  96. Object[] oa = ((BSHArguments)jjtGetChild(0)).getArguments(
  97. callstack, interpreter);
  98. // replace obj with result of method invocation
  99. obj = Reflect.invokeObjectMethod(
  100. obj, method, oa, interpreter, callstack, this );
  101. //System.out.println("here2: "+obj);
  102. return Reflect.getLHSObjectField(obj, field);
  103. */
  104. }
  105. } catch ( UtilEvalError e ) {
  106. throw e.toEvalError( this, callstack );
  107. }
  108. }
  109. private LHS doIndex(
  110. Object obj, CallStack callstack, Interpreter interpreter)
  111. throws EvalError, ReflectError
  112. {
  113. int index = BSHPrimarySuffix.getIndexAux(
  114. obj, callstack, interpreter, this );
  115. return new LHS(obj, index);
  116. }
  117. private LHS doProperty(
  118. Object obj, CallStack callstack, Interpreter interpreter)
  119. throws EvalError, ReflectError
  120. {
  121. if(obj == Primitive.VOID)
  122. throw new EvalError("Attempt to access property on a void type",
  123. this, callstack );
  124. else if(obj instanceof Primitive)
  125. throw new EvalError("Attempt to access property on a primitive",
  126. this, callstack );
  127. Object value = ((SimpleNode)jjtGetChild(0)).eval(
  128. callstack, interpreter);
  129. if(!(value instanceof String))
  130. throw new EvalError(
  131. "Property expression must be a String or identifier.",
  132. this, callstack );
  133. if ( Interpreter.DEBUG ) Interpreter.debug("LHS property access: ");
  134. return new LHS(obj, (String)value);
  135. }
  136. }