PageRenderTime 166ms CodeModel.GetById 2ms RepoModel.GetById 2ms app.codeStats 0ms

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

#
Java | 224 lines | 82 code | 26 blank | 116 comment | 4 complexity | 657c7cdcbda18667df37937ac50264f7 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.io.IOException;
  35. /**
  36. 'This' is the type of bsh scripted objects.
  37. A 'This' object is a bsh scripted object context. It holds a namespace
  38. reference and implements event listeners and various other interfaces.
  39. This holds a reference to the declaring interpreter for callbacks from
  40. outside of bsh.
  41. */
  42. public class This implements java.io.Serializable, Runnable {
  43. /**
  44. The namespace that this This reference wraps.
  45. */
  46. NameSpace namespace;
  47. /**
  48. This is the interpreter running when the This ref was created.
  49. It's used as a default interpreter for callback through the This
  50. where there is no current interpreter instance
  51. e.g. interface proxy or event call backs from outside of bsh.
  52. */
  53. transient Interpreter declaringInterpreter;
  54. /**
  55. invokeMethod() here is generally used by outside code to callback
  56. into the bsh interpreter. e.g. when we are acting as an interface
  57. for a scripted listener, etc. In this case there is no real call stack
  58. so we make a default one starting with the special JAVACODE namespace
  59. and our namespace as the next.
  60. */
  61. transient CallStack callstack;
  62. /**
  63. getThis() is a factory for bsh.This type references. The capabilities
  64. of ".this" references in bsh are version dependent up until jdk1.3.
  65. The version dependence was to support different default interface
  66. implementations. i.e. different sets of listener interfaces which
  67. scripted objects were capable of implementing. In jdk1.3 the
  68. reflection proxy mechanism was introduced which allowed us to
  69. implement arbitrary interfaces. This is fantastic.
  70. A This object is a thin layer over a namespace, comprising a bsh object
  71. context. We create it here only if needed for the namespace.
  72. Note: this method could be considered slow because of the way it
  73. dynamically factories objects. However I've also done tests where
  74. I hard-code the factory to return JThis and see no change in the
  75. rough test suite time. This references are also cached in NameSpace.
  76. */
  77. static This getThis(
  78. NameSpace namespace, Interpreter declaringInterpreter )
  79. {
  80. try {
  81. if ( Capabilities.canGenerateInterfaces() )
  82. return (This)Reflect.constructObject( "bsh.XThis",
  83. new Object [] { namespace, declaringInterpreter } );
  84. else if ( Capabilities.haveSwing() )
  85. return (This)Reflect.constructObject( "bsh.JThis",
  86. new Object [] { namespace, declaringInterpreter } );
  87. else
  88. return new This( namespace, declaringInterpreter );
  89. } catch ( Exception e ) {
  90. throw new InterpreterError("internal error 1 in This: "+e);
  91. }
  92. }
  93. /**
  94. Get a version of the interface.
  95. If this type of This implements it directly return this,
  96. else try complain that we don't have the proxy mechanism.
  97. */
  98. public Object getInterface( Class clas )
  99. throws EvalError
  100. {
  101. if ( clas.isInstance( this ) )
  102. return this;
  103. else
  104. throw new EvalError( "Dynamic proxy mechanism not available. "
  105. + "Cannot construct interface type: "+clas );
  106. }
  107. /*
  108. I wish protected access were limited to children and not also
  109. package scope... I want this to be a singleton implemented by various
  110. children.
  111. */
  112. protected This( NameSpace namespace, Interpreter declaringInterpreter ) {
  113. this.namespace = namespace;
  114. this.declaringInterpreter = declaringInterpreter;
  115. initCallStack( namespace );
  116. }
  117. public NameSpace getNameSpace() {
  118. return namespace;
  119. }
  120. public String toString() {
  121. return "'this' reference to Bsh object: " + namespace.name;
  122. }
  123. public void run() {
  124. try {
  125. invokeMethod( "run", new Object[0] );
  126. } catch( EvalError e ) {
  127. declaringInterpreter.error(
  128. "Exception in runnable:" + e );
  129. }
  130. }
  131. /**
  132. Invoke specified method from outside java code, using the declaring
  133. interpreter and current namespace.
  134. The call stack will appear as if the method is being invoked from
  135. outside of bsh in native java code.
  136. */
  137. public Object invokeMethod( String name, Object [] args )
  138. throws EvalError
  139. {
  140. // null callstack, one will be created for us in namespace.invokMethod
  141. // null callerInfo is legal
  142. return invokeMethod( name, args, declaringInterpreter, null, null );
  143. }
  144. /**
  145. Invoke specified method with specified interpreter.
  146. This is simply a convenience method.
  147. */
  148. public Object invokeMethod(
  149. String name, Object [] args, Interpreter interpreter,
  150. CallStack callstack, SimpleNode callerInfo )
  151. throws EvalError
  152. {
  153. return namespace.invokeMethod(
  154. name, args, interpreter, callstack, callerInfo );
  155. }
  156. /**
  157. Bind a This reference to a parent's namespace with the specified
  158. declaring interpreter. Also re-init the callstack. It's necessary
  159. to bind a This reference before it can be used after deserialization.
  160. <p>
  161. This is a static utility method because it's used by a bsh command
  162. bind() and the interpreter doesn't currently allow access to direct
  163. methods of This objects (small hack)
  164. */
  165. public static void bind(
  166. This ths, NameSpace namespace, Interpreter declaringInterpreter )
  167. {
  168. ths.namespace.setParent( namespace );
  169. ths.declaringInterpreter = declaringInterpreter;
  170. ths.initCallStack( namespace );
  171. }
  172. /**
  173. For serialization.
  174. */
  175. private synchronized void writeObject(java.io.ObjectOutputStream s)
  176. throws IOException {
  177. // Temporarily prune the namespace.
  178. NameSpace parent = namespace.getParent();
  179. // Bind would set the interpreter, but it's possible that the parent
  180. // is null (it's the root). So save it...
  181. //?Interpreter interpreter = declaringInterpreter;
  182. namespace.prune();
  183. s.defaultWriteObject();
  184. // put it back
  185. namespace.setParent( parent );
  186. //?declaringInterpreter = interpreter;
  187. //?initCallStack( namespace );
  188. }
  189. private final void initCallStack( NameSpace namespace ) {
  190. callstack = new CallStack();
  191. callstack.push( namespace );
  192. }
  193. }