PageRenderTime 58ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/bsh/BSHArrayInitializer.java

#
Java | 147 lines | 76 code | 15 blank | 56 comment | 8 complexity | 3830f66068abcb7235d39558de477166 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 org.gjt.sp.jedit.bsh;
  34. import java.lang.reflect.Array;
  35. class BSHArrayInitializer extends SimpleNode
  36. {
  37. BSHArrayInitializer(int id) { super(id); }
  38. public Object eval( CallStack callstack, Interpreter interpreter )
  39. throws EvalError
  40. {
  41. throw new EvalError( "Array initializer has no base type.",
  42. this, callstack );
  43. }
  44. /**
  45. Construct the array from the initializer syntax.
  46. @param baseType the base class type of the array (no dimensionality)
  47. @param dimensions the top number of dimensions of the array
  48. e.g. 2 for a String [][];
  49. */
  50. public Object eval( Class baseType, int dimensions,
  51. CallStack callstack, Interpreter interpreter )
  52. throws EvalError
  53. {
  54. int numInitializers = jjtGetNumChildren();
  55. // allocate the array to store the initializers
  56. int [] dima = new int [dimensions]; // description of the array
  57. // The other dimensions default to zero and are assigned when
  58. // the values are set.
  59. dima[0] = numInitializers;
  60. Object initializers = Array.newInstance( baseType, dima );
  61. // Evaluate the initializers
  62. for (int i = 0; i < numInitializers; i++)
  63. {
  64. SimpleNode node = (SimpleNode)jjtGetChild(i);
  65. Object currentInitializer;
  66. if ( node instanceof BSHArrayInitializer ) {
  67. if ( dimensions < 2 )
  68. throw new EvalError(
  69. "Invalid Location for Intializer, position: "+i,
  70. this, callstack );
  71. currentInitializer =
  72. ((BSHArrayInitializer)node).eval(
  73. baseType, dimensions-1, callstack, interpreter);
  74. } else
  75. currentInitializer = node.eval( callstack, interpreter);
  76. if ( currentInitializer == Primitive.VOID )
  77. throw new EvalError(
  78. "Void in array initializer, position"+i, this, callstack );
  79. // Determine if any conversion is necessary on the initializers.
  80. //
  81. // Quick test to see if conversions apply:
  82. // If the dimensionality of the array is 1 then the elements of
  83. // the initializer can be primitives or boxable types. If it is
  84. // greater then the values must be array (object) types and there
  85. // are currently no conversions that we do on those.
  86. // If we have conversions on those in the future then we need to
  87. // get the real base type here instead of the dimensionless one.
  88. Object value = currentInitializer;
  89. if ( dimensions == 1 )
  90. {
  91. // We do a bsh cast here. strictJava should be able to affect
  92. // the cast there when we tighten control
  93. try {
  94. value = Types.castObject(
  95. currentInitializer, baseType, Types.CAST );
  96. } catch ( UtilEvalError e ) {
  97. throw e.toEvalError(
  98. "Error in array initializer", this, callstack );
  99. }
  100. // unwrap any primitive, map voids to null, etc.
  101. value = Primitive.unwrap( value );
  102. }
  103. // store the value in the array
  104. try {
  105. Array.set(initializers, i, value);
  106. } catch( IllegalArgumentException e ) {
  107. Interpreter.debug("illegal arg"+e);
  108. throwTypeError( baseType, currentInitializer, i, callstack );
  109. } catch( ArrayStoreException e ) { // I think this can happen
  110. Interpreter.debug("arraystore"+e);
  111. throwTypeError( baseType, currentInitializer, i, callstack );
  112. }
  113. }
  114. return initializers;
  115. }
  116. private void throwTypeError(
  117. Class baseType, Object initializer, int argNum, CallStack callstack )
  118. throws EvalError
  119. {
  120. String rhsType;
  121. if (initializer instanceof Primitive)
  122. rhsType =
  123. ((Primitive)initializer).getType().getName();
  124. else
  125. rhsType = Reflect.normalizeClassName(
  126. initializer.getClass());
  127. throw new EvalError ( "Incompatible type: " + rhsType
  128. +" in initializer of array type: "+ baseType
  129. +" at position: "+argNum, this, callstack );
  130. }
  131. }