PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-1-pre5/bsh/BSHAllocationExpression.java

#
Java | 187 lines | 116 code | 22 blank | 49 comment | 12 complexity | 039efced481790c5ea3f36eaa5b504c4 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. import java.lang.reflect.InvocationTargetException;
  36. /**
  37. New object, new array, or inner class style allocation with body.
  38. */
  39. class BSHAllocationExpression extends SimpleNode
  40. {
  41. BSHAllocationExpression(int id) { super(id); }
  42. public Object eval( CallStack callstack, Interpreter interpreter)
  43. throws EvalError
  44. {
  45. // type is either a class name or a primitive type
  46. SimpleNode type = (SimpleNode)jjtGetChild(0);
  47. // args is either constructor arguments or array dimensions
  48. SimpleNode args = (SimpleNode)jjtGetChild(1);
  49. if ( type instanceof BSHAmbiguousName )
  50. {
  51. BSHAmbiguousName name = (BSHAmbiguousName)type;
  52. if(args instanceof BSHArguments)
  53. return objectAllocation(name, (BSHArguments)args,
  54. callstack, interpreter );
  55. else
  56. return objectArrayAllocation(name, (BSHArrayDimensions)args,
  57. callstack, interpreter );
  58. }
  59. else
  60. return primitiveArrayAllocation((BSHPrimitiveType)type,
  61. (BSHArrayDimensions)args, callstack, interpreter );
  62. }
  63. private Object objectAllocation(
  64. BSHAmbiguousName nameNode, BSHArguments argumentsNode,
  65. CallStack callstack, Interpreter interpreter
  66. )
  67. throws EvalError
  68. {
  69. NameSpace namespace = callstack.top();
  70. Class type = nameNode.toClass(namespace);
  71. /* toClass throws this
  72. if (type == null)
  73. throw new EvalError(
  74. "Class " + nameNode.getName(namespace) + " not found.", this);
  75. */
  76. Object[] args = argumentsNode.getArguments(callstack, interpreter);
  77. if(args == null)
  78. throw new EvalError("Trying to new a class...?", this);
  79. // Is an inner class style object allocation
  80. boolean hasBody = jjtGetNumChildren() > 2;
  81. if ( hasBody ) {
  82. BSHBlock body = (BSHBlock)jjtGetChild(2);
  83. return constructWithBody(
  84. type, args, body, callstack, interpreter );
  85. } else
  86. return constructObject( type, args );
  87. }
  88. private Object constructObject( Class type, Object[] args )
  89. throws EvalError
  90. {
  91. try {
  92. return Reflect.constructObject(type, args);
  93. } catch(ReflectError e) {
  94. throw new EvalError("Constructor error: " + e.getMessage(), this);
  95. } catch(InvocationTargetException e) {
  96. Interpreter.debug("The constructor threw an exception:\n\t" +
  97. e.getTargetException());
  98. throw new TargetError(
  99. "Object constructor", e.getTargetException(), this, true);
  100. }
  101. }
  102. private Object constructWithBody(
  103. Class type, Object[] args, BSHBlock body,
  104. CallStack callstack, Interpreter interpreter )
  105. throws EvalError
  106. {
  107. if ( ! type.isInterface() )
  108. throw new EvalError(
  109. "BeanShell cannot extend class types: "+ type );
  110. NameSpace namespace = callstack.top();
  111. // Maybe we should swap in local namespace for the top?
  112. // who is the caller?
  113. NameSpace local = new NameSpace(namespace, "anonymous block object");
  114. callstack.push(local);
  115. body.eval( callstack, interpreter, true );
  116. callstack.pop();
  117. return local.getThis(interpreter).getInterface( type );
  118. }
  119. // combine part of this with primitiveArrayAllocation
  120. private Object objectArrayAllocation(
  121. BSHAmbiguousName nameNode, BSHArrayDimensions dimensionsNode,
  122. CallStack callstack, Interpreter interpreter
  123. )
  124. throws EvalError
  125. {
  126. NameSpace namespace = callstack.top();
  127. Class type = nameNode.toClass(namespace);
  128. if(type == null)
  129. throw new EvalError(
  130. "Class " + nameNode.getName(namespace) + " not found.", this);
  131. // dimensionsNode can return either an intialized version or none.
  132. Object result = dimensionsNode.eval( type, callstack, interpreter );
  133. if(result != Primitive.VOID)
  134. return result;
  135. else
  136. return arrayNewInstance( type, dimensionsNode );
  137. }
  138. // combine part of this with objectArrayAllocation
  139. private Object primitiveArrayAllocation(
  140. BSHPrimitiveType typeNode, BSHArrayDimensions dimensionsNode,
  141. CallStack callstack, Interpreter interpreter
  142. )
  143. throws EvalError
  144. {
  145. Class type = typeNode.getType();
  146. // dimensionsNode can return either an intialized version or none.
  147. Object result = dimensionsNode.eval( type, callstack, interpreter );
  148. if (result != Primitive.VOID)
  149. return result;
  150. return arrayNewInstance( type, dimensionsNode );
  151. }
  152. private Object arrayNewInstance(
  153. Class type, BSHArrayDimensions dimensionsNode )
  154. throws EvalError
  155. {
  156. try {
  157. return Array.newInstance(type, dimensionsNode.dimensions);
  158. } catch( NegativeArraySizeException e1) {
  159. throw new TargetError("Negative Array Size", e1);
  160. } catch(Exception e) {
  161. throw new EvalError("Can't construct primitive array: " +
  162. e.getMessage(), this);
  163. }
  164. }
  165. }