PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-3-pre5/bsh/BSHArrayInitializer.java

#
Java | 161 lines | 82 code | 18 blank | 61 comment | 8 complexity | 95e1e5a59940c20e939d172b88b6020b 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. 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 =
  61. Array.newInstance( baseType, dima );
  62. // Evaluate the initializers
  63. for (int i = 0; i < numInitializers; i++)
  64. {
  65. SimpleNode node = (SimpleNode)jjtGetChild(i);
  66. Object currentInitializer;
  67. if ( node instanceof BSHArrayInitializer ) {
  68. if ( dimensions < 2 )
  69. throw new EvalError(
  70. "Invalid Location for Intializer, position: "+i,
  71. this, callstack );
  72. currentInitializer =
  73. ((BSHArrayInitializer)node).eval(
  74. baseType, dimensions-1, callstack, interpreter);
  75. } else
  76. currentInitializer = node.eval( callstack, interpreter);
  77. if ( currentInitializer == Primitive.VOID )
  78. throw new EvalError(
  79. "Void in array initializer, position"+i, this, callstack );
  80. // unwrap primitive to the wrapper type
  81. Object value;
  82. if ( currentInitializer instanceof Primitive )
  83. {
  84. Primitive primValue = (Primitive)currentInitializer;
  85. /*
  86. TODO:
  87. to get cast and boxing working e.g.
  88. e.g. Byte [] ia = { 1, 2 }
  89. If the baseType is a wrapper type then we need to get the
  90. primitive TYPE class for the base type here in order for
  91. the cast to allow it... Then boxing will happen naturally in
  92. the Array.set().
  93. e.g. Integer [] ia = { 1, 2 }
  94. */
  95. /*
  96. if ( Primitive.isWrapperType( baseType ) )
  97. baseType = Primitive.getPrimitiveTypeForType( baseType );
  98. */
  99. // don't deal with object types here... unless above
  100. if ( baseType.isPrimitive() )
  101. try {
  102. primValue = primValue.castToType(
  103. baseType, Types.CAST );
  104. } catch ( UtilEvalError e ) {
  105. e.printStackTrace();
  106. Interpreter.debug("error:"+e);
  107. throwTypeError( baseType, primValue, i, callstack );
  108. }
  109. value = primValue.getValue();
  110. }
  111. else
  112. value = currentInitializer;
  113. // store the value in the array
  114. try {
  115. Array.set(initializers, i, value);
  116. } catch( IllegalArgumentException e ) {
  117. Interpreter.debug("illegal arg"+e);
  118. throwTypeError( baseType, currentInitializer, i, callstack );
  119. } catch( ArrayStoreException e ) { // I think this can happen
  120. Interpreter.debug("arraystore"+e);
  121. throwTypeError( baseType, currentInitializer, i, callstack );
  122. }
  123. }
  124. return initializers;
  125. }
  126. private void throwTypeError(
  127. Class baseType, Object initializer, int argNum, CallStack callstack )
  128. throws EvalError
  129. {
  130. String rhsType;
  131. if (initializer instanceof Primitive)
  132. rhsType =
  133. ((Primitive)initializer).getType().getName();
  134. else
  135. rhsType = Reflect.normalizeClassName(
  136. initializer.getClass());
  137. throw new EvalError ( "Incompatible type: " + rhsType
  138. +" in initializer of array type: "+ baseType
  139. +" at position: "+argNum, this, callstack );
  140. }
  141. }