PageRenderTime 66ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 156 lines | 80 code | 21 blank | 55 comment | 6 complexity | 1c06a598849385bab1fa099bd6f7714c 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. */
  36. class BSHClassDeclaration extends SimpleNode
  37. {
  38. /**
  39. The class instance initializer method name.
  40. A BshMethod by this name is installed by the class delcaration into
  41. the scripted class and it is called to initialize instances of the
  42. class.
  43. */
  44. static final String CLASSINITNAME = "_bshClassInit";
  45. String name;
  46. Modifiers modifiers;
  47. int numInterfaces;
  48. Class [] interfaces;
  49. boolean extend;
  50. BSHClassDeclaration(int id) { super(id); }
  51. /**
  52. */
  53. public Object eval( CallStack callstack, Interpreter interpreter )
  54. throws EvalError
  55. {
  56. int child = 0;
  57. NameSpace enclosingNameSpace = callstack.top();
  58. NameSpace superNameSpace;
  59. if ( extend )
  60. {
  61. BSHAmbiguousName superNode = (BSHAmbiguousName)jjtGetChild(child++);
  62. Object superClass = superNode.toObject(
  63. callstack, interpreter, false /*forceclass*/ );
  64. if ( superClass instanceof ClassIdentifier )
  65. throw new EvalError(
  66. "BeanShell Scripted Classes cannot currently extend "
  67. +"Java types.", this, callstack );
  68. if ( ClassNameSpace.isScriptedClass( superClass ) )
  69. superNameSpace = ((This)superClass).getNameSpace();
  70. else
  71. throw new EvalError(
  72. "Class cannot extend type: "
  73. +superClass.getClass(), this, callstack );
  74. }
  75. else
  76. superNameSpace = enclosingNameSpace;
  77. // Get interfaces
  78. interfaces = new Class[numInterfaces];
  79. for( int i=0; i<numInterfaces; i++) {
  80. BSHAmbiguousName node = (BSHAmbiguousName)jjtGetChild(child++);
  81. interfaces[i] = node.toClass(callstack, interpreter);
  82. if ( !interfaces[i].isInterface() )
  83. throw new EvalError(
  84. "Type: "+node.text+" is not an interface!",
  85. this, callstack );
  86. }
  87. BSHBlock block;
  88. if ( child < jjtGetNumChildren() )
  89. block = (BSHBlock)jjtGetChild(child);
  90. else
  91. block = new BSHBlock( ParserTreeConstants.JJTBLOCK );
  92. /*
  93. Install this class in the enclosingNameSpace.
  94. Make a scripted object representing the class and install it under
  95. the name of the class.
  96. */
  97. NameSpace classStaticNameSpace =
  98. new ClassNameSpace( superNameSpace, name, ClassNameSpace.CLASS );
  99. /*
  100. Evaluate the block in the classStaticNameSpace
  101. push it onto the stack and then call the block eval with the
  102. overrideNamespace option so that it uses ours. Then pop it.
  103. */
  104. callstack.push( classStaticNameSpace );
  105. // evaluate the static portion of the block in it
  106. block.eval( callstack, interpreter, true/*override*/ );
  107. // Add the block as a default constructor method
  108. BshMethod classInitializer =
  109. new BshMethod(
  110. CLASSINITNAME,
  111. null /*returnType*/,
  112. new String [0] /*argNames*/,
  113. new Class [0] /*argTypes*/,
  114. block,
  115. classStaticNameSpace/*declaringNameSpace*/,
  116. new Modifiers()
  117. );
  118. try {
  119. classStaticNameSpace.setMethod( CLASSINITNAME, classInitializer );
  120. } catch ( UtilEvalError e ) {
  121. throw e.toEvalError(this, callstack);
  122. }
  123. callstack.pop();
  124. try {
  125. enclosingNameSpace.setVariable(
  126. name, classStaticNameSpace.getThis( interpreter), false );
  127. } catch ( UtilEvalError e ) {
  128. throw e.toEvalError( this, callstack );
  129. }
  130. return Primitive.VOID;
  131. }
  132. public String toString() {
  133. return "ClassDeclaration: "+name;
  134. }
  135. }