PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-1-pre5/bsh/This.java

#
Java | 223 lines | 73 code | 20 blank | 130 comment | 4 complexity | 8f46e97099eaf05eba35bc6943879cb2 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. // not thread safe... trying workaround
  62. //transient CallStack callstack;
  63. /**
  64. getThis() is a factory for bsh.This type references. The capabilities
  65. of ".this" references in bsh are version dependent up until jdk1.3.
  66. The version dependence was to support different default interface
  67. implementations. i.e. different sets of listener interfaces which
  68. scripted objects were capable of implementing. In jdk1.3 the
  69. reflection proxy mechanism was introduced which allowed us to
  70. implement arbitrary interfaces. This is fantastic.
  71. A This object is a thin layer over a namespace, comprising a bsh object
  72. context. We create it here only if needed for the namespace.
  73. Note: this method could be considered slow because of the way it
  74. dynamically factories objects. However I've also done tests where
  75. I hard-code the factory to return JThis and see no change in the
  76. rough test suite time. This references are also cached in NameSpace.
  77. */
  78. static This getThis(
  79. NameSpace namespace, Interpreter declaringInterpreter )
  80. {
  81. try {
  82. if ( Capabilities.canGenerateInterfaces() )
  83. return (This)Reflect.constructObject( "bsh.XThis",
  84. new Object [] { namespace, declaringInterpreter } );
  85. else if ( Capabilities.haveSwing() )
  86. return (This)Reflect.constructObject( "bsh.JThis",
  87. new Object [] { namespace, declaringInterpreter } );
  88. else
  89. return new This( namespace, declaringInterpreter );
  90. } catch ( Exception e ) {
  91. throw new InterpreterError("internal error 1 in This: "+e);
  92. }
  93. }
  94. /**
  95. Get a version of the interface.
  96. If this type of This implements it directly return this,
  97. else try complain that we don't have the proxy mechanism.
  98. */
  99. public Object getInterface( Class clas )
  100. throws EvalError
  101. {
  102. if ( clas.isInstance( this ) )
  103. return this;
  104. else
  105. throw new EvalError( "Dynamic proxy mechanism not available. "
  106. + "Cannot construct interface type: "+clas );
  107. }
  108. /*
  109. I wish protected access were limited to children and not also
  110. package scope... I want this to be a singleton implemented by various
  111. children.
  112. */
  113. protected This( NameSpace namespace, Interpreter declaringInterpreter ) {
  114. this.namespace = namespace;
  115. this.declaringInterpreter = declaringInterpreter;
  116. //initCallStack( namespace );
  117. }
  118. public NameSpace getNameSpace() {
  119. return namespace;
  120. }
  121. public String toString() {
  122. return "'this' reference to Bsh object: " + namespace.name;
  123. }
  124. public void run() {
  125. try {
  126. invokeMethod( "run", new Object[0] );
  127. } catch( EvalError e ) {
  128. declaringInterpreter.error(
  129. "Exception in runnable:" + e );
  130. }
  131. }
  132. /**
  133. Invoke specified method from outside java code, using the declaring
  134. interpreter and current namespace.
  135. The call stack will appear as if the method is being invoked from
  136. outside of bsh in native java code.
  137. */
  138. public Object invokeMethod( String name, Object [] args )
  139. throws EvalError
  140. {
  141. // null callstack, one will be created for us in namespace.invokMethod
  142. // null callerInfo is legal
  143. return invokeMethod( name, args, declaringInterpreter, null, null );
  144. }
  145. /**
  146. Invoke specified method with specified interpreter.
  147. This is simply a convenience method.
  148. */
  149. public Object invokeMethod(
  150. String name, Object [] args, Interpreter interpreter,
  151. CallStack callstack, SimpleNode callerInfo )
  152. throws EvalError
  153. {
  154. return namespace.invokeMethod(
  155. name, args, interpreter, callstack, callerInfo );
  156. }
  157. /**
  158. Bind a This reference to a parent's namespace with the specified
  159. declaring interpreter. Also re-init the callstack. It's necessary
  160. to bind a This reference before it can be used after deserialization.
  161. This is used by the bsh load() command.
  162. <p>
  163. This is a static utility method because it's used by a bsh command
  164. bind() and the interpreter doesn't currently allow access to direct
  165. methods of This objects (small hack)
  166. */
  167. public static void bind(
  168. This ths, NameSpace namespace, Interpreter declaringInterpreter )
  169. {
  170. ths.namespace.setParent( namespace );
  171. ths.declaringInterpreter = declaringInterpreter;
  172. //ths.initCallStack( namespace );
  173. }
  174. /**
  175. Remove a This reference from a parent's namespace.
  176. It's necessary to unbind a This reference before serialization if
  177. you don't want serialization to save the entire interpreter and all
  178. enclosing namespaces. This is used by the bsh save() command.
  179. <p>
  180. This is a static utility method because it's used by a bsh command
  181. bind() and the interpreter doesn't currently allow access to direct
  182. methods of This objects (small hack)
  183. public static void unbind( This ths ) {
  184. }
  185. */
  186. /*
  187. private final void initCallStack( NameSpace namespace ) {
  188. callstack = new CallStack();
  189. callstack.push( namespace );
  190. }
  191. */
  192. CallStack newCallStack() {
  193. CallStack callstack = new CallStack();
  194. callstack.push( namespace );
  195. return callstack;
  196. }
  197. }