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

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

#
Java | 130 lines | 69 code | 16 blank | 45 comment | 7 complexity | 76ae7763880d7e04a9b51e604f7e604d 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. }
  43. /**
  44. Construct the array from the initializer syntax.
  45. @param baseType the base class type of the array (no dimensionality)
  46. @param dimensions the top number of dimensions of the array
  47. e.g. 2 for a String [][];
  48. */
  49. public Object eval( Class baseType, int dimensions,
  50. CallStack callstack, Interpreter interpreter )
  51. throws EvalError
  52. {
  53. int numInitializers = jjtGetNumChildren();
  54. // allocate the array to store the initializers
  55. int [] dima = new int [dimensions]; // description of the array
  56. // The other dimensions default to zero and are assigned when
  57. // the values are set.
  58. dima[0] = numInitializers;
  59. Object initializers =
  60. 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, this);
  70. currentInitializer =
  71. ((BSHArrayInitializer)node).eval(
  72. baseType, dimensions-1, callstack, interpreter);
  73. } else
  74. currentInitializer = node.eval( callstack, interpreter);
  75. if ( currentInitializer == Primitive.VOID )
  76. throw new EvalError(
  77. "Void in array initializer, position"+i, this);
  78. // unwrap primitive to the wrapper type
  79. Object value;
  80. if ( currentInitializer instanceof Primitive )
  81. value = ((Primitive)currentInitializer).getValue();
  82. else
  83. value = currentInitializer;
  84. // store the value in the array
  85. try {
  86. Array.set(initializers, i, value);
  87. } catch( IllegalArgumentException e ) {
  88. Interpreter.debug("illegal arg"+e);
  89. throwTypeError( baseType, currentInitializer, i );
  90. } catch( ArrayStoreException e ) { // I think this can happen
  91. Interpreter.debug("arraystore"+e);
  92. throwTypeError( baseType, currentInitializer, i );
  93. }
  94. }
  95. return initializers;
  96. }
  97. private void throwTypeError(
  98. Class baseType, Object initializer, int argNum )
  99. throws EvalError
  100. {
  101. String lhsType = Reflect.normalizeClassName(baseType);
  102. String rhsType;
  103. if (initializer instanceof Primitive)
  104. rhsType =
  105. ((Primitive)initializer).getType().getName();
  106. else
  107. rhsType = Reflect.normalizeClassName(
  108. initializer.getClass());
  109. throw new EvalError ( "Incompatible type: " + rhsType
  110. +" in initializer of array type: "+ baseType
  111. +" at position: "+argNum, this );
  112. }
  113. }