PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre3/bsh/BSHMethodDeclaration.java

#
Java | 125 lines | 51 code | 13 blank | 61 comment | 10 complexity | b631aeff1e6bfd14bccf5fb80f6b6355 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. Object returnType; // null (none), Primitive.VOID, or a Class
  48. BSHMethodDeclaration(int id)
  49. {
  50. super(id);
  51. }
  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. Since the structure of the method is only determined by type evaluation
  56. (through eval of BSHFormalParameters) we do not need the interpreter
  57. or callstack.
  58. */
  59. public Object eval( NameSpace namespace )
  60. throws EvalError
  61. {
  62. if ( block == null )
  63. {
  64. // We will allow methods to be re-written.
  65. /*
  66. if( namespace has method )
  67. throw new EvalError(
  68. "Method: " + name + " already defined in scope", this);
  69. */
  70. if(jjtGetNumChildren() == 3)
  71. {
  72. returnType =
  73. ((BSHReturnType)jjtGetChild(0)).getReturnType( namespace );
  74. params = (BSHFormalParameters)jjtGetChild(1);
  75. block = (BSHBlock)jjtGetChild(2);
  76. }
  77. else
  78. {
  79. params = (BSHFormalParameters)jjtGetChild(0);
  80. block = (BSHBlock)jjtGetChild(1);
  81. }
  82. params.eval( namespace );
  83. // if strictJava mode, check for loose parameters and return type
  84. if ( Interpreter.strictJava )
  85. {
  86. for(int i=0; i<params.argTypes.length; i++)
  87. if ( params.argTypes[i] == null )
  88. throw new EvalError(
  89. "(Strict Java Mode) Undeclared argument type, parameter: " +
  90. params.argNames[i] + " in method: "
  91. + name, this );
  92. if ( returnType == null )
  93. throw new EvalError(
  94. "(Strict Java Mode) Undeclared return type for method: "
  95. + name, this );
  96. }
  97. }
  98. // Install an *instance* of this method in the namespace.
  99. // See notes in BshMethod
  100. // This is not good...
  101. // need a way to update eval without re-installing...
  102. // so that we can re-eval params, etc. when classloader changes
  103. // look into this
  104. namespace.setMethod( name, new BshMethod( this, namespace ) );
  105. return Primitive.VOID;
  106. }
  107. public String toString() {
  108. return "MethodDeclaration: "+name;
  109. }
  110. }