PageRenderTime 22ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre3/bsh/BSHArrayDimensions.java

#
Java | 137 lines | 65 code | 16 blank | 56 comment | 9 complexity | d6a6a68998ef0e18cb4b532809290bbc 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.Array;
  35. /**
  36. The name of this class is somewhat misleading. This covers both the case
  37. where there is an array initializer and
  38. */
  39. class BSHArrayDimensions extends SimpleNode
  40. {
  41. public Class baseType;
  42. private int arrayDims;
  43. /** The Length in each dimension. This value set by the eval() */
  44. // is it ok to cache this here?
  45. // it can't change, right?
  46. /*transient?why?*/
  47. public int [] dimensions;
  48. BSHArrayDimensions(int id) { super(id); }
  49. public void addArrayDimension() { arrayDims++; }
  50. public Object eval(
  51. Class type, CallStack callstack, Interpreter interpreter )
  52. throws EvalError
  53. {
  54. Interpreter.debug("array base type = "+type);
  55. baseType = type;
  56. return eval( callstack, interpreter );
  57. }
  58. /**
  59. Evaluate the structure of the array in one of two ways:
  60. a) an initializer exists, evaluate it and return
  61. the fully constructed array object, also record the dimensions
  62. of that array
  63. b) evaluate and record the lengths in each dimension and
  64. return void.
  65. The structure of the array dims is maintained in dimensions.
  66. */
  67. public Object eval( CallStack callstack, Interpreter interpreter )
  68. throws EvalError
  69. {
  70. SimpleNode child = (SimpleNode)jjtGetChild(0);
  71. if (child instanceof BSHArrayInitializer)
  72. // evaluate the initializer and the dimensions it returns
  73. {
  74. if ( baseType == null )
  75. throw new EvalError(
  76. "Internal Array Eval err: unknown base type", this);
  77. Object initValue = ((BSHArrayInitializer)child).eval(
  78. baseType, arrayDims, callstack, interpreter);
  79. Class arrayClass = initValue.getClass();
  80. dimensions = new int[
  81. Reflect.getArrayDimensions(arrayClass) ];
  82. // compare with number of dimensions explicitly specified
  83. if (dimensions.length != arrayDims)
  84. throw new EvalError(
  85. "Incompatible initializer. Allocation calls for a " +
  86. arrayDims + " dimensional array, but initializer is a " +
  87. dimensions.length + " dimensional array", this);
  88. // fill in dimensions[] lengths
  89. Object arraySlice = initValue;
  90. for(int i = 0; i < dimensions.length; i++) {
  91. dimensions[i] = Array.getLength( arraySlice );
  92. if ( dimensions[i] > 0 )
  93. arraySlice = Array.get(arraySlice, 0);
  94. }
  95. return initValue;
  96. }
  97. else
  98. // evaluate the dimensions of the array
  99. {
  100. dimensions = new int[ jjtGetNumChildren() ];
  101. for(int i = 0; i < dimensions.length; i++)
  102. {
  103. try {
  104. Object length = ((SimpleNode)jjtGetChild(i)).eval(
  105. callstack, interpreter);
  106. dimensions[i] = ((Primitive)length).intValue();
  107. }
  108. catch(Exception e)
  109. {
  110. throw new EvalError(
  111. "Array index: " + i +
  112. " does not evaluate to an integer", this);
  113. }
  114. }
  115. }
  116. return Primitive.VOID;
  117. }
  118. }