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

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/bsh/BSHMethodDeclaration.java

#
Java | 179 lines | 95 code | 24 blank | 60 comment | 16 complexity | 605ac5c3379041cc74b75dc2758877da 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 org.gjt.sp.jedit.bsh;
  34. class BSHMethodDeclaration extends SimpleNode
  35. {
  36. public String name;
  37. // Begin Child node structure evaluated by insureNodesParsed
  38. BSHReturnType returnTypeNode;
  39. BSHFormalParameters paramsNode;
  40. BSHBlock blockNode;
  41. // index of the first throws clause child node
  42. int firstThrowsClause;
  43. // End Child node structure evaluated by insureNodesParsed
  44. public Modifiers modifiers;
  45. // Unsafe caching of type here.
  46. Class returnType; // null (none), Void.TYPE, or a Class
  47. int numThrows = 0;
  48. BSHMethodDeclaration(int id) { super(id); }
  49. /**
  50. Set the returnTypeNode, paramsNode, and blockNode based on child
  51. node structure. No evaluation is done here.
  52. */
  53. synchronized void insureNodesParsed()
  54. {
  55. if ( paramsNode != null ) // there is always a paramsNode
  56. return;
  57. Object firstNode = jjtGetChild(0);
  58. firstThrowsClause = 1;
  59. if ( firstNode instanceof BSHReturnType )
  60. {
  61. returnTypeNode = (BSHReturnType)firstNode;
  62. paramsNode = (BSHFormalParameters)jjtGetChild(1);
  63. if ( jjtGetNumChildren() > 2+numThrows )
  64. blockNode = (BSHBlock)jjtGetChild(2+numThrows); // skip throws
  65. ++firstThrowsClause;
  66. }
  67. else
  68. {
  69. paramsNode = (BSHFormalParameters)jjtGetChild(0);
  70. blockNode = (BSHBlock)jjtGetChild(1+numThrows); // skip throws
  71. }
  72. }
  73. /**
  74. Evaluate the return type node.
  75. @return the type or null indicating loosely typed return
  76. */
  77. Class evalReturnType( CallStack callstack, Interpreter interpreter )
  78. throws EvalError
  79. {
  80. insureNodesParsed();
  81. if ( returnTypeNode != null )
  82. return returnTypeNode.evalReturnType( callstack, interpreter );
  83. else
  84. return null;
  85. }
  86. String getReturnTypeDescriptor(
  87. CallStack callstack, Interpreter interpreter, String defaultPackage )
  88. {
  89. insureNodesParsed();
  90. if ( returnTypeNode == null )
  91. return null;
  92. else
  93. return returnTypeNode.getTypeDescriptor(
  94. callstack, interpreter, defaultPackage );
  95. }
  96. BSHReturnType getReturnTypeNode() {
  97. insureNodesParsed();
  98. return returnTypeNode;
  99. }
  100. /**
  101. Evaluate the declaration of the method. That is, determine the
  102. structure of the method and install it into the caller's namespace.
  103. */
  104. public Object eval( CallStack callstack, Interpreter interpreter )
  105. throws EvalError
  106. {
  107. returnType = evalReturnType( callstack, interpreter );
  108. evalNodes( callstack, interpreter );
  109. // Install an *instance* of this method in the namespace.
  110. // See notes in BshMethod
  111. // This is not good...
  112. // need a way to update eval without re-installing...
  113. // so that we can re-eval params, etc. when classloader changes
  114. // look into this
  115. NameSpace namespace = callstack.top();
  116. BshMethod bshMethod = new BshMethod( this, namespace, modifiers );
  117. try {
  118. namespace.setMethod( name, bshMethod );
  119. } catch ( UtilEvalError e ) {
  120. throw e.toEvalError(this,callstack);
  121. }
  122. return Primitive.VOID;
  123. }
  124. private void evalNodes( CallStack callstack, Interpreter interpreter )
  125. throws EvalError
  126. {
  127. insureNodesParsed();
  128. // validate that the throws names are class names
  129. for(int i=firstThrowsClause; i<numThrows+firstThrowsClause; i++)
  130. ((BSHAmbiguousName)jjtGetChild(i)).toClass(
  131. callstack, interpreter );
  132. paramsNode.eval( callstack, interpreter );
  133. // if strictJava mode, check for loose parameters and return type
  134. if ( interpreter.getStrictJava() )
  135. {
  136. for(int i=0; i<paramsNode.paramTypes.length; i++)
  137. if ( paramsNode.paramTypes[i] == null )
  138. // Warning: Null callstack here. Don't think we need
  139. // a stack trace to indicate how we sourced the method.
  140. throw new EvalError(
  141. "(Strict Java Mode) Undeclared argument type, parameter: " +
  142. paramsNode.getParamNames()[i] + " in method: "
  143. + name, this, null );
  144. if ( returnType == null )
  145. // Warning: Null callstack here. Don't think we need
  146. // a stack trace to indicate how we sourced the method.
  147. throw new EvalError(
  148. "(Strict Java Mode) Undeclared return type for method: "
  149. + name, this, null );
  150. }
  151. }
  152. public String toString() {
  153. return "MethodDeclaration: "+name;
  154. }
  155. }