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

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

#
Java | 148 lines | 68 code | 16 blank | 64 comment | 10 complexity | 94e77b4a05ed88735ff3ece0cbb7818d 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. public int numDefinedDims;
  43. public int numUndefinedDims;
  44. /**
  45. The Length in each defined dimension. This value set by the eval()
  46. Since the values can come from Expressions we should be re-eval()d each
  47. time.
  48. */
  49. public int [] definedDimensions;
  50. BSHArrayDimensions(int id) { super(id); }
  51. public void addDefinedDimension() { numDefinedDims++; }
  52. public void addUndefinedDimension() { numUndefinedDims++; }
  53. public Object eval(
  54. Class type, CallStack callstack, Interpreter interpreter )
  55. throws EvalError
  56. {
  57. if ( Interpreter.DEBUG ) Interpreter.debug("array base type = "+type);
  58. baseType = type;
  59. return eval( callstack, interpreter );
  60. }
  61. /**
  62. Evaluate the structure of the array in one of two ways:
  63. a) an initializer exists, evaluate it and return
  64. the fully constructed array object, also record the dimensions
  65. of that array
  66. b) evaluate and record the lengths in each dimension and
  67. return void.
  68. The structure of the array dims is maintained in dimensions.
  69. */
  70. public Object eval( CallStack callstack, Interpreter interpreter )
  71. throws EvalError
  72. {
  73. SimpleNode child = (SimpleNode)jjtGetChild(0);
  74. /*
  75. Child is array initializer. Evaluate it and fill in the
  76. dimensions it returns. Initialized arrays are always fully defined
  77. (no undefined dimensions to worry about).
  78. The syntax uses the undefinedDimension count.
  79. e.g. int [][] { 1, 2 };
  80. */
  81. if (child instanceof BSHArrayInitializer)
  82. {
  83. if ( baseType == null )
  84. throw new EvalError(
  85. "Internal Array Eval err: unknown base type",
  86. this, callstack );
  87. Object initValue = ((BSHArrayInitializer)child).eval(
  88. baseType, numUndefinedDims, callstack, interpreter);
  89. Class arrayClass = initValue.getClass();
  90. int actualDimensions = Reflect.getArrayDimensions(arrayClass);
  91. definedDimensions = new int[ actualDimensions ];
  92. // Compare with number of dimensions actually created with the
  93. // number specified (syntax uses the undefined ones here)
  94. if ( definedDimensions.length != numUndefinedDims )
  95. throw new EvalError(
  96. "Incompatible initializer. Allocation calls for a " +
  97. numUndefinedDims+ " dimensional array, but initializer is a " +
  98. actualDimensions + " dimensional array", this, callstack );
  99. // fill in definedDimensions [] lengths
  100. Object arraySlice = initValue;
  101. for ( int i = 0; i < definedDimensions.length; i++ ) {
  102. definedDimensions[i] = Array.getLength( arraySlice );
  103. if ( definedDimensions[i] > 0 )
  104. arraySlice = Array.get(arraySlice, 0);
  105. }
  106. return initValue;
  107. }
  108. else
  109. // Evaluate the defined dimensions of the array
  110. {
  111. definedDimensions = new int[ numDefinedDims ];
  112. for(int i = 0; i < numDefinedDims; i++)
  113. {
  114. try {
  115. Object length = ((SimpleNode)jjtGetChild(i)).eval(
  116. callstack, interpreter);
  117. definedDimensions[i] = ((Primitive)length).intValue();
  118. }
  119. catch(Exception e)
  120. {
  121. throw new EvalError(
  122. "Array index: " + i +
  123. " does not evaluate to an integer", this, callstack );
  124. }
  125. }
  126. }
  127. return Primitive.VOID;
  128. }
  129. }