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

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

#
Java | 203 lines | 99 code | 24 blank | 80 comment | 42 complexity | c80e0570bd5683239c74c9f26c929738 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. /**
  35. Implement casts.
  36. I think it should be possible to simplify some of the code here by
  37. using the NameSpace.getAssignableForm() method, but I haven't looked
  38. into it.
  39. */
  40. class BSHCastExpression extends SimpleNode {
  41. public BSHCastExpression(int id) { super(id); }
  42. /**
  43. @return the result of the cast.
  44. */
  45. public Object eval(
  46. CallStack callstack, Interpreter interpreter ) throws EvalError
  47. {
  48. NameSpace namespace = callstack.top();
  49. Class toType = ((BSHType)jjtGetChild(0)).getType(
  50. callstack, interpreter );
  51. SimpleNode expression = (SimpleNode)jjtGetChild(1);
  52. // evaluate the expression
  53. Object fromValue = expression.eval(callstack, interpreter);
  54. Class fromType = fromValue.getClass();
  55. try {
  56. return castObject( fromValue, toType );
  57. } catch ( UtilEvalError e ) {
  58. throw e.toEvalError( this, callstack );
  59. }
  60. }
  61. /**
  62. Cast an object to a new type.
  63. This method can handle bsh.Primitive types (representing primitive
  64. casts) as well as arbitrary object casts.
  65. @param fromValue an Object or bsh.Primitive primitive value
  66. @param toType the class type of the cast result, which may include
  67. primitive types, e.g. Byte.TYPE
  68. */
  69. public static Object castObject( Object fromValue, Class toType )
  70. throws UtilEvalError
  71. {
  72. Class fromType = fromValue.getClass();
  73. // The compiler isn't smart enough to allow me to leave this unassigned
  74. // even though it is clearly assigned in all cases below.
  75. Object result = null;
  76. // Going to a primitive type
  77. if ( toType.isPrimitive() )
  78. if ( fromValue instanceof Primitive )
  79. result = castPrimitive( (Primitive)fromValue, toType );
  80. else
  81. // cannot convert from object to primitive
  82. castError(fromValue.getClass(), toType);
  83. else
  84. // Going to an object type
  85. if ( fromValue instanceof Primitive )
  86. // let castPrimitive handle trivial but legit case of NULL
  87. result = castPrimitive( (Primitive)fromValue, toType );
  88. else
  89. // Can we use the proxy mechanism to cast a bsh.This to
  90. // the correct interface?
  91. if ( Capabilities.canGenerateInterfaces() &&
  92. (fromValue instanceof bsh.This) && toType.isInterface() )
  93. result = ((bsh.This)fromValue).getInterface( toType );
  94. else
  95. // Could probably add getAssignableForm here to allow
  96. // special bsh widening converions... wrappers to wrappers
  97. if ( toType.isInstance(fromValue ) )
  98. result = fromValue;
  99. else
  100. castError(fromType, toType);
  101. if ( result == null )
  102. throw new InternalError("bad construct somewhere...");
  103. return result;
  104. }
  105. /**
  106. Wrap up the ClassCastException in a TargetError so that it can
  107. be caught...
  108. Node user should catch and add the node
  109. */
  110. public static void castError(Class from, Class to) throws UtilEvalError {
  111. castError(
  112. Reflect.normalizeClassName(from), Reflect.normalizeClassName(to) );
  113. }
  114. public static void castError(String from, String to) throws UtilEvalError
  115. {
  116. Exception cce = new ClassCastException("Illegal cast. Cannot cast " +
  117. from + " to " + to );
  118. throw new UtilTargetError( cce );
  119. }
  120. /**
  121. Cast the bsh.Primitive value to a new bsh.Primitive value
  122. This is usually a numeric type cast. Other cases include:
  123. boolean can be cast to boolen
  124. null can be cast to any object type
  125. void cannot be cast to anything
  126. */
  127. public static Primitive castPrimitive( Primitive primValue, Class toType )
  128. throws UtilEvalError
  129. {
  130. // can't cast void to anything
  131. if ( primValue == Primitive.VOID )
  132. castError( "void value", Reflect.normalizeClassName(toType) );
  133. // unwrap, etc.
  134. Object value = primValue.getValue();
  135. Class fromType = primValue.getType();
  136. // Trying to cast primitive to an object type?
  137. // only works for Primitive.NULL
  138. if ( !toType.isPrimitive() )
  139. if ( primValue != Primitive.NULL )
  140. castError("primitive value", "object type:" + toType);
  141. else
  142. return primValue;
  143. // can only cast boolean to boolean
  144. if ( fromType == Boolean.TYPE )
  145. {
  146. if ( toType != Boolean.TYPE )
  147. castError(fromType, toType);
  148. else
  149. return primValue;
  150. }
  151. // trying to do numeric promotion
  152. // first promote char to Number type to avoid duplicating code
  153. if (value instanceof Character)
  154. value = new Integer(((Character)value).charValue());
  155. if (value instanceof Number)
  156. {
  157. Number number = (Number)value;
  158. if (toType == Byte.TYPE)
  159. value = new Primitive(number.byteValue());
  160. else if(toType == Short.TYPE)
  161. value = new Primitive(number.shortValue());
  162. else if(toType == Character.TYPE)
  163. value = new Primitive((char)number.intValue());
  164. else if(toType == Integer.TYPE)
  165. value = new Primitive(number.intValue());
  166. else if(toType == Long.TYPE)
  167. value = new Primitive(number.longValue());
  168. else if(toType == Float.TYPE)
  169. value = new Primitive(number.floatValue());
  170. else if(toType == Double.TYPE)
  171. value = new Primitive(number.doubleValue());
  172. else
  173. castError(fromType, toType);
  174. return (Primitive)value;
  175. }
  176. throw new UtilEvalError("unknown type in cast");
  177. }
  178. }