PageRenderTime 37ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre5/bsh/BSHType.java

#
Java | 127 lines | 48 code | 14 blank | 65 comment | 5 complexity | b01f953a3dfa8a51b22fb79c3db4be83 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 fully expressed type.
  51. i.e. primtive, class, or array. Cleared on classloader change.
  52. */
  53. private Class type;
  54. BSHType(int id) {
  55. super(id);
  56. BshClassManager.addCMListener(this);
  57. }
  58. /**
  59. Used by the grammar to indicate dimensions of array types
  60. during parsing.
  61. */
  62. public void addArrayDimension() {
  63. arrayDims++;
  64. }
  65. /**
  66. Returns a class for the type
  67. */
  68. public Class getType( NameSpace namespace )
  69. throws EvalError
  70. {
  71. // return cached type if available
  72. if (type != null)
  73. return type;
  74. // first node will either be PrimitiveType or AmbiguousName
  75. SimpleNode node = (SimpleNode)jjtGetChild(0);
  76. if(node instanceof BSHPrimitiveType)
  77. baseType = ((BSHPrimitiveType)node).getType();
  78. else
  79. baseType = ((BSHAmbiguousName)node).toClass( namespace );
  80. if(arrayDims > 0) {
  81. try {
  82. // Get the type by constructing a prototype array with
  83. // arbitrary (zero) length in each dimension.
  84. int[] dims = new int[arrayDims]; // int array default zeros
  85. Object obj = Array.newInstance(baseType, dims);
  86. type = obj.getClass();
  87. } catch(Exception e) {
  88. throw new EvalError("Couldn't construct array type", this);
  89. }
  90. } else
  91. type = baseType;
  92. return type;
  93. }
  94. /**
  95. baseType is used during evaluation of full type and retained for the
  96. case where we are an array type.
  97. In the case where we are not an array this will be the same as type.
  98. */
  99. public Class getBaseType() {
  100. return baseType;
  101. }
  102. /**
  103. If we are an array type this will be non zero and indicate the
  104. dimensionality of the array. e.g. 2 for String[][];
  105. */
  106. public int getArrayDims() {
  107. return arrayDims;
  108. }
  109. public void classLoaderChanged() {
  110. type = null;
  111. baseType = null;
  112. }
  113. }