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

/jEdit/tags/jedit-4-2-pre14/bsh/BSHType.java

#
Java | 218 lines | 112 code | 24 blank | 82 comment | 37 complexity | 60b65f2b85ef64f7f16bda6a26b90231 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 BSHType extends SimpleNode
  36. implements BshClassManager.Listener
  37. {
  38. /**
  39. baseType is used during evaluation of full type and retained for the
  40. case where we are an array type.
  41. In the case where we are not an array this will be the same as type.
  42. */
  43. private Class baseType;
  44. /**
  45. If we are an array type this will be non zero and indicate the
  46. dimensionality of the array. e.g. 2 for String[][];
  47. */
  48. private int arrayDims;
  49. /**
  50. Internal cache of the type. Cleared on classloader change.
  51. */
  52. private Class type;
  53. String descriptor;
  54. BSHType(int id) {
  55. super(id);
  56. }
  57. /**
  58. Used by the grammar to indicate dimensions of array types
  59. during parsing.
  60. */
  61. public void addArrayDimension() {
  62. arrayDims++;
  63. }
  64. SimpleNode getTypeNode() {
  65. return (SimpleNode)jjtGetChild(0);
  66. }
  67. /**
  68. Returns a class descriptor for this type.
  69. If the type is an ambiguous name (object type) evaluation is
  70. attempted through the namespace in order to resolve imports.
  71. If it is not found and the name is non-compound we assume the default
  72. package for the name.
  73. */
  74. public String getTypeDescriptor(
  75. CallStack callstack, Interpreter interpreter, String defaultPackage )
  76. {
  77. // return cached type if available
  78. if ( descriptor != null )
  79. return descriptor;
  80. String descriptor;
  81. // first node will either be PrimitiveType or AmbiguousName
  82. SimpleNode node = getTypeNode();
  83. if ( node instanceof BSHPrimitiveType )
  84. descriptor = getTypeDescriptor( ((BSHPrimitiveType)node).type );
  85. else
  86. {
  87. String clasName = ((BSHAmbiguousName)node).text;
  88. BshClassManager bcm = interpreter.getClassManager();
  89. // Note: incorrect here - we are using the hack in bsh class
  90. // manager that allows lookup by base name. We need to eliminate
  91. // this limitation by working through imports. See notes in class
  92. // manager.
  93. String definingClass = bcm.getClassBeingDefined( clasName );
  94. Class clas = null;
  95. if ( definingClass == null )
  96. {
  97. try {
  98. clas = ((BSHAmbiguousName)node).toClass(
  99. callstack, interpreter );
  100. } catch ( EvalError e ) {
  101. //throw new InterpreterError("unable to resolve type: "+e);
  102. // ignore and try default package
  103. //System.out.println("BSHType: "+node+" class not found");
  104. }
  105. } else
  106. clasName = definingClass;
  107. if ( clas != null )
  108. {
  109. //System.out.println("found clas: "+clas);
  110. descriptor = getTypeDescriptor( clas );
  111. }else
  112. {
  113. if ( defaultPackage == null || Name.isCompound( clasName ) )
  114. descriptor = "L" + clasName.replace('.','/') + ";";
  115. else
  116. descriptor =
  117. "L"+defaultPackage.replace('.','/')+"/"+clasName + ";";
  118. }
  119. }
  120. for(int i=0; i<arrayDims; i++)
  121. descriptor = "["+descriptor;
  122. this.descriptor = descriptor;
  123. //System.out.println("BSHType: returning descriptor: "+descriptor);
  124. return descriptor;
  125. }
  126. public Class getType( CallStack callstack, Interpreter interpreter )
  127. throws EvalError
  128. {
  129. // return cached type if available
  130. if ( type != null )
  131. return type;
  132. // first node will either be PrimitiveType or AmbiguousName
  133. SimpleNode node = getTypeNode();
  134. if ( node instanceof BSHPrimitiveType )
  135. baseType = ((BSHPrimitiveType)node).getType();
  136. else
  137. baseType = ((BSHAmbiguousName)node).toClass(
  138. callstack, interpreter );
  139. if ( arrayDims > 0 ) {
  140. try {
  141. // Get the type by constructing a prototype array with
  142. // arbitrary (zero) length in each dimension.
  143. int[] dims = new int[arrayDims]; // int array default zeros
  144. Object obj = Array.newInstance(baseType, dims);
  145. type = obj.getClass();
  146. } catch(Exception e) {
  147. throw new EvalError("Couldn't construct array type",
  148. this, callstack );
  149. }
  150. } else
  151. type = baseType;
  152. // hack... sticking to first interpreter that resolves this
  153. // see comments on type instance variable
  154. interpreter.getClassManager().addListener(this);
  155. return type;
  156. }
  157. /**
  158. baseType is used during evaluation of full type and retained for the
  159. case where we are an array type.
  160. In the case where we are not an array this will be the same as type.
  161. */
  162. public Class getBaseType() {
  163. return baseType;
  164. }
  165. /**
  166. If we are an array type this will be non zero and indicate the
  167. dimensionality of the array. e.g. 2 for String[][];
  168. */
  169. public int getArrayDims() {
  170. return arrayDims;
  171. }
  172. public void classLoaderChanged() {
  173. type = null;
  174. baseType = null;
  175. }
  176. public static String getTypeDescriptor( Class clas )
  177. {
  178. if ( clas == Boolean.TYPE ) return "Z";
  179. if ( clas == Character.TYPE ) return "C";
  180. if ( clas == Byte.TYPE ) return "B";
  181. if ( clas == Short.TYPE ) return "S";
  182. if ( clas == Integer.TYPE ) return "I";
  183. if ( clas == Long.TYPE ) return "J";
  184. if ( clas == Float.TYPE ) return "F";
  185. if ( clas == Double.TYPE ) return "D";
  186. if ( clas == Void.TYPE ) return "V";
  187. // Is getName() ok? test with 1.1
  188. String name = clas.getName().replace('.','/');
  189. if ( name.startsWith("[") || name.endsWith(";") )
  190. return name;
  191. else
  192. return "L"+ name.replace('.','/') +";";
  193. }
  194. }