/jEdit/tags/beanshell-1-3/bsh/This.java

# · Java · 267 lines · 92 code · 27 blank · 148 comment · 14 complexity · 53c6403766d6d154d164e1cd0d6d6683 MD5 · raw file

  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. /**
  62. getThis() is a factory for bsh.This type references. The capabilities
  63. of ".this" references in bsh are version dependent up until jdk1.3.
  64. The version dependence was to support different default interface
  65. implementations. i.e. different sets of listener interfaces which
  66. scripted objects were capable of implementing. In jdk1.3 the
  67. reflection proxy mechanism was introduced which allowed us to
  68. implement arbitrary interfaces. This is fantastic.
  69. A This object is a thin layer over a namespace, comprising a bsh object
  70. context. We create it here only if needed for the namespace.
  71. Note: this method could be considered slow because of the way it
  72. dynamically factories objects. However I've also done tests where
  73. I hard-code the factory to return JThis and see no change in the
  74. rough test suite time. This references are also cached in NameSpace.
  75. */
  76. static This getThis(
  77. NameSpace namespace, Interpreter declaringInterpreter )
  78. {
  79. try {
  80. Class c;
  81. if ( Capabilities.canGenerateInterfaces() )
  82. c = Class.forName( "bsh.XThis" );
  83. else if ( Capabilities.haveSwing() )
  84. c = Class.forName( "bsh.JThis" );
  85. else
  86. return new This( namespace, declaringInterpreter );
  87. return (This)Reflect.constructObject( c,
  88. new Object [] { 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 UtilEvalError
  100. {
  101. if ( clas.isInstance( this ) )
  102. return this;
  103. else
  104. throw new UtilEvalError( "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 as from outside java code, using the
  133. declaring interpreter and current namespace.
  134. The call stack will indicate that the method is being invoked from
  135. outside of bsh in native java code.
  136. Note: you must still wrap/unwrap args/return values using
  137. Primitive/Primitive.unwrap() for use outside of BeanShell.
  138. @see bsh.Primitive
  139. */
  140. public Object invokeMethod( String name, Object [] args )
  141. throws EvalError
  142. {
  143. // null callstack, one will be created for us
  144. return invokeMethod(
  145. name, args, declaringInterpreter, null, SimpleNode.JAVACODE );
  146. }
  147. /**
  148. Invoke a method in this namespace with the specified args,
  149. interpreter reference, callstack, and caller info.
  150. <p>
  151. Note: If you use this method outside of the bsh package and wish to
  152. use variables with primitive values you will have to wrap them using
  153. bsh.Primitive. Consider using This getInterface() to make a true Java
  154. interface for invoking your scripted methods.
  155. <p>
  156. This method also implements the default object protocol of toString(),
  157. hashCode() and equals() and the invoke() meta-method handling as a
  158. last resort.
  159. <p>
  160. Note: the invoke() method will not catch the object method
  161. (toString, ...). If you want to override them you have to script
  162. them directly.
  163. <p>
  164. @see bsh.This.invokeMethod(
  165. String methodName, Object [] args, Interpreter interpreter,
  166. CallStack callstack, SimpleNode callerInfo )
  167. @param if callStack is null a new CallStack will be created and
  168. initialized with this namespace.
  169. @see bsh.Primitive
  170. */
  171. public Object invokeMethod(
  172. String methodName, Object [] args, Interpreter interpreter,
  173. CallStack callstack, SimpleNode callerInfo )
  174. throws EvalError
  175. {
  176. if ( callstack == null )
  177. callstack = new CallStack( namespace );
  178. // Find the bsh method
  179. Class [] types = Reflect.getTypes( args );
  180. BshMethod bshMethod = namespace.getMethod( methodName, types );
  181. if ( bshMethod != null )
  182. return bshMethod.invoke(
  183. args, interpreter, callstack, callerInfo );
  184. /*
  185. No scripted method of that name.
  186. Implement the required part of the Object protocol:
  187. public int hashCode();
  188. public boolean equals(java.lang.Object);
  189. public java.lang.String toString();
  190. if these were not handled by scripted methods we must provide
  191. a default impl.
  192. */
  193. // a default toString() that shows the interfaces we implement
  194. if ( methodName.equals("toString" ) )
  195. return toString();
  196. // a default hashCode()
  197. if ( methodName.equals("hashCode" ) )
  198. return new Integer(this.hashCode());
  199. // a default equals() testing for equality with the This reference
  200. if ( methodName.equals("equals" ) ) {
  201. Object obj = args[0];
  202. return new Boolean( this == obj );
  203. }
  204. // Look for a default invoke() handler method in the namespace
  205. bshMethod = namespace.getMethod(
  206. "invoke", new Class [] { null, null } );
  207. // Call script "invoke( String methodName, Object [] args );
  208. if ( bshMethod != null )
  209. return bshMethod.invoke( new Object [] { methodName, args },
  210. interpreter, callstack, callerInfo );
  211. throw new EvalError("Method " +
  212. StringUtil.methodString( methodName, types ) +
  213. " not found in bsh scripted object: "+ namespace.getName(),
  214. callerInfo, callstack );
  215. }
  216. /**
  217. Bind a This reference to a parent's namespace with the specified
  218. declaring interpreter. Also re-init the callstack. It's necessary
  219. to bind a This reference before it can be used after deserialization.
  220. This is used by the bsh load() command.
  221. <p>
  222. This is a static utility method because it's used by a bsh command
  223. bind() and the interpreter doesn't currently allow access to direct
  224. methods of This objects (small hack)
  225. */
  226. public static void bind(
  227. This ths, NameSpace namespace, Interpreter declaringInterpreter )
  228. {
  229. ths.namespace.setParent( namespace );
  230. ths.declaringInterpreter = declaringInterpreter;
  231. }
  232. }