PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 146 lines | 65 code | 18 blank | 63 comment | 10 complexity | d79311864dd42335cf6deda1c24c05a4 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.util.Vector;
  35. /*
  36. This shouldn't have to be public.
  37. We should add to bsh.This allowing us to invoke a method.
  38. If we do that we should probably use it in Reflect.java
  39. Note: caching of the structure is done in BshMethod
  40. no caching need be done here or in formal param, etc.
  41. */
  42. class BSHMethodDeclaration extends SimpleNode
  43. {
  44. String name;
  45. BSHFormalParameters params;
  46. BSHBlock block;
  47. // Unsafe caching of type here.
  48. Object returnType; // null (none), Primitive.VOID, or a Class
  49. Modifiers modifiers;
  50. int numThrows = 0;
  51. BSHMethodDeclaration(int id) { super(id); }
  52. /**
  53. Evaluate the declaration of the method. That is, determine the
  54. structure of the method and install it into the caller's namespace.
  55. */
  56. public Object eval( CallStack callstack, Interpreter interpreter )
  57. throws EvalError
  58. {
  59. if ( block == null )
  60. evalNodes( callstack, interpreter );
  61. // Install an *instance* of this method in the namespace.
  62. // See notes in BshMethod
  63. // This is not good...
  64. // need a way to update eval without re-installing...
  65. // so that we can re-eval params, etc. when classloader changes
  66. // look into this
  67. NameSpace namespace = callstack.top();
  68. BshMethod bshMethod = new BshMethod( this, namespace, modifiers );
  69. try {
  70. namespace.setMethod( name, bshMethod );
  71. } catch ( UtilEvalError e ) {
  72. throw e.toEvalError(this,callstack);
  73. }
  74. return Primitive.VOID;
  75. }
  76. private void evalNodes( CallStack callstack, Interpreter interpreter )
  77. throws EvalError
  78. {
  79. // We will allow methods to be re-written.
  80. /*
  81. if( namespace has method )
  82. throw new EvalError(
  83. "Method: " + name + " already defined in scope", this);
  84. */
  85. Object firstNode = jjtGetChild(0);
  86. int firstThrows = 1;
  87. if ( firstNode instanceof BSHReturnType )
  88. {
  89. returnType = ((BSHReturnType)firstNode).getReturnType(
  90. callstack, interpreter );
  91. params = (BSHFormalParameters)jjtGetChild(1);
  92. block = (BSHBlock)jjtGetChild(2+numThrows); // skip throws
  93. ++firstThrows;
  94. }
  95. else
  96. {
  97. params = (BSHFormalParameters)jjtGetChild(0);
  98. block = (BSHBlock)jjtGetChild(1+numThrows); // skip throws
  99. }
  100. // validate that the throws names are class names
  101. for(int i=firstThrows; i<numThrows+firstThrows; i++)
  102. ((BSHAmbiguousName)jjtGetChild(i)).toClass(
  103. callstack, interpreter );
  104. params.eval( callstack, interpreter );
  105. // if strictJava mode, check for loose parameters and return type
  106. if ( interpreter.getStrictJava() )
  107. {
  108. for(int i=0; i<params.argTypes.length; i++)
  109. if ( params.argTypes[i] == null )
  110. // Warning: Null callstack here. Don't think we need
  111. // a stack trace to indicate how we sourced the method.
  112. throw new EvalError(
  113. "(Strict Java Mode) Undeclared argument type, parameter: " +
  114. params.argNames[i] + " in method: "
  115. + name, this, null );
  116. if ( returnType == null )
  117. // Warning: Null callstack here. Don't think we need
  118. // a stack trace to indicate how we sourced the method.
  119. throw new EvalError(
  120. "(Strict Java Mode) Undeclared return type for method: "
  121. + name, this, null );
  122. }
  123. }
  124. public String toString() {
  125. return "MethodDeclaration: "+name;
  126. }
  127. }