PageRenderTime 55ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/bsh/This.java

#
Java | 336 lines | 130 code | 29 blank | 177 comment | 29 complexity | 9c4e312b3287932b7c6c6876532db514 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 org.gjt.sp.jedit.bsh;
  34. /**
  35. 'This' is the type of bsh scripted objects.
  36. A 'This' object is a bsh scripted object context. It holds a namespace
  37. reference and implements event listeners and various other interfaces.
  38. This holds a reference to the declaring interpreter for callbacks from
  39. outside of bsh.
  40. */
  41. public class This implements java.io.Serializable, Runnable
  42. {
  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. getThis() is a factory for bsh.This type references. The capabilities
  56. of ".this" references in bsh are version dependent up until jdk1.3.
  57. The version dependence was to support different default interface
  58. implementations. i.e. different sets of listener interfaces which
  59. scripted objects were capable of implementing. In jdk1.3 the
  60. reflection proxy mechanism was introduced which allowed us to
  61. implement arbitrary interfaces. This is fantastic.
  62. A This object is a thin layer over a namespace, comprising a bsh object
  63. context. We create it here only if needed for the namespace.
  64. Note: this method could be considered slow because of the way it
  65. dynamically factories objects. However I've also done tests where
  66. I hard-code the factory to return JThis and see no change in the
  67. rough test suite time. This references are also cached in NameSpace.
  68. */
  69. static This getThis(
  70. NameSpace namespace, Interpreter declaringInterpreter )
  71. {
  72. try {
  73. Class c;
  74. if ( Capabilities.canGenerateInterfaces() )
  75. c = Class.forName( "org.gjt.sp.jedit.bsh.XThis" );
  76. else if ( Capabilities.haveSwing() )
  77. c = Class.forName( "org.gjt.sp.jedit.bsh.JThis" );
  78. else
  79. return new This( namespace, declaringInterpreter );
  80. return (This)Reflect.constructObject( c,
  81. new Object [] { namespace, declaringInterpreter } );
  82. } catch ( Exception e ) {
  83. throw new InterpreterError("internal error 1 in This: "+e);
  84. }
  85. }
  86. /**
  87. Get a version of this scripted object implementing the specified
  88. interface.
  89. */
  90. /*
  91. If this type of This implements it directly return this,
  92. else try complain that we don't have the proxy mechanism.
  93. */
  94. public Object getInterface( Class clas )
  95. throws UtilEvalError
  96. {
  97. if ( clas.isInstance( this ) )
  98. return this;
  99. else
  100. throw new UtilEvalError( "Dynamic proxy mechanism not available. "
  101. + "Cannot construct interface type: "+clas );
  102. }
  103. /**
  104. Get a version of this scripted object implementing the specified
  105. interfaces.
  106. */
  107. public Object getInterface( Class [] ca )
  108. throws UtilEvalError
  109. {
  110. for(int i=0; i<ca.length; i++)
  111. if ( !(ca[i].isInstance( this )) )
  112. throw new UtilEvalError(
  113. "Dynamic proxy mechanism not available. "
  114. + "Cannot construct interface type: "+ca[i] );
  115. return this;
  116. }
  117. /*
  118. I wish protected access were limited to children and not also
  119. package scope... I want this to be a singleton implemented by various
  120. children.
  121. */
  122. protected This( NameSpace namespace, Interpreter declaringInterpreter ) {
  123. this.namespace = namespace;
  124. this.declaringInterpreter = declaringInterpreter;
  125. //initCallStack( namespace );
  126. }
  127. public NameSpace getNameSpace() {
  128. return namespace;
  129. }
  130. public String toString() {
  131. return "'this' reference to Bsh object: " + namespace;
  132. }
  133. public void run() {
  134. try {
  135. invokeMethod( "run", new Object[0] );
  136. } catch( EvalError e ) {
  137. declaringInterpreter.error(
  138. "Exception in runnable:" + e );
  139. }
  140. }
  141. /**
  142. Invoke specified method as from outside java code, using the
  143. declaring interpreter and current namespace.
  144. The call stack will indicate that the method is being invoked from
  145. outside of bsh in native java code.
  146. Note: you must still wrap/unwrap args/return values using
  147. Primitive/Primitive.unwrap() for use outside of BeanShell.
  148. @see org.gjt.sp.jedit.bsh.Primitive
  149. */
  150. public Object invokeMethod( String name, Object [] args )
  151. throws EvalError
  152. {
  153. // null callstack, one will be created for us
  154. return invokeMethod(
  155. name, args, null/*declaringInterpreter*/, null, null,
  156. false/*declaredOnly*/ );
  157. }
  158. /**
  159. Invoke a method in this namespace with the specified args,
  160. interpreter reference, callstack, and caller info.
  161. <p>
  162. Note: If you use this method outside of the bsh package and wish to
  163. use variables with primitive values you will have to wrap them using
  164. bsh.Primitive. Consider using This getInterface() to make a true Java
  165. interface for invoking your scripted methods.
  166. <p>
  167. This method also implements the default object protocol of toString(),
  168. hashCode() and equals() and the invoke() meta-method handling as a
  169. last resort.
  170. <p>
  171. Note: The invoke() meta-method will not catch the Object protocol
  172. methods (toString(), hashCode()...). If you want to override them you
  173. have to script them directly.
  174. <p>
  175. @see org.gjt.sp.jedit.bsh.This#invokeMethod(String methodName, Object [] args, Interpreter interpreter, CallStack callstack, SimpleNode callerInfo, boolean declaredOnly ) invokeMethod
  176. @param callstack if callStack is null a new CallStack will be created and
  177. initialized with this namespace.
  178. @param declaredOnly if true then only methods declared directly in the
  179. namespace will be visible - no inherited or imported methods will
  180. be visible.
  181. @see org.gjt.sp.jedit.bsh.Primitive Primitive
  182. */
  183. /*
  184. invokeMethod() here is generally used by outside code to callback
  185. into the bsh interpreter. e.g. when we are acting as an interface
  186. for a scripted listener, etc. In this case there is no real call stack
  187. so we make a default one starting with the special JAVACODE namespace
  188. and our namespace as the next.
  189. */
  190. public Object invokeMethod(
  191. String methodName, Object [] args,
  192. Interpreter interpreter, CallStack callstack, SimpleNode callerInfo,
  193. boolean declaredOnly )
  194. throws EvalError
  195. {
  196. /*
  197. Wrap nulls.
  198. This is a bit of a cludge to address a deficiency in the class
  199. generator whereby it does not wrap nulls on method delegate. See
  200. Class Generator.java. If we fix that then we can remove this.
  201. (just have to generate the code there.)
  202. */
  203. if ( args != null )
  204. {
  205. Object [] oa = new Object [args.length];
  206. for(int i=0; i<args.length; i++)
  207. oa[i] = ( args[i] == null ? Primitive.NULL : args[i] );
  208. args = oa;
  209. }
  210. if ( interpreter == null )
  211. interpreter = declaringInterpreter;
  212. if ( callstack == null )
  213. callstack = new CallStack( namespace );
  214. if ( callerInfo == null )
  215. callerInfo = SimpleNode.JAVACODE;
  216. // Find the bsh method
  217. Class [] types = Types.getTypes( args );
  218. BshMethod bshMethod = null;
  219. try {
  220. bshMethod = namespace.getMethod( methodName, types, declaredOnly );
  221. } catch ( UtilEvalError e ) {
  222. // leave null
  223. }
  224. if ( bshMethod != null )
  225. return bshMethod.invoke( args, interpreter, callstack, callerInfo );
  226. /*
  227. No scripted method of that name.
  228. Implement the required part of the Object protocol:
  229. public int hashCode();
  230. public boolean equals(java.lang.Object);
  231. public java.lang.String toString();
  232. if these were not handled by scripted methods we must provide
  233. a default impl.
  234. */
  235. // a default toString() that shows the interfaces we implement
  236. if ( methodName.equals("toString" ) )
  237. return toString();
  238. // a default hashCode()
  239. if ( methodName.equals("hashCode" ) )
  240. return new Integer(this.hashCode());
  241. // a default equals() testing for equality with the This reference
  242. if ( methodName.equals("equals" ) ) {
  243. Object obj = args[0];
  244. return new Boolean( this == obj );
  245. }
  246. // Look for a default invoke() handler method in the namespace
  247. // Note: this code duplicates that in NameSpace getCommand()
  248. // is that ok?
  249. try {
  250. bshMethod = namespace.getMethod(
  251. "invoke", new Class [] { null, null } );
  252. } catch ( UtilEvalError e ) { /*leave null*/ }
  253. // Call script "invoke( String methodName, Object [] args );
  254. if ( bshMethod != null )
  255. return bshMethod.invoke( new Object [] { methodName, args },
  256. interpreter, callstack, callerInfo );
  257. throw new EvalError("Method " +
  258. StringUtil.methodString( methodName, types ) +
  259. " not found in bsh scripted object: "+ namespace.getName(),
  260. callerInfo, callstack );
  261. }
  262. /**
  263. Bind a This reference to a parent's namespace with the specified
  264. declaring interpreter. Also re-init the callstack. It's necessary
  265. to bind a This reference before it can be used after deserialization.
  266. This is used by the bsh load() command.
  267. <p>
  268. This is a static utility method because it's used by a bsh command
  269. bind() and the interpreter doesn't currently allow access to direct
  270. methods of This objects (small hack)
  271. */
  272. public static void bind(
  273. This ths, NameSpace namespace, Interpreter declaringInterpreter )
  274. {
  275. ths.namespace.setParent( namespace );
  276. ths.declaringInterpreter = declaringInterpreter;
  277. }
  278. /**
  279. Allow invocations of these method names on This type objects.
  280. Don't give bsh.This a chance to override their behavior.
  281. <p>
  282. If the method is passed here the invocation will actually happen on
  283. the bsh.This object via the regular reflective method invocation
  284. mechanism. If not, then the method is evaluated by bsh.This itself
  285. as a scripted method call.
  286. */
  287. static boolean isExposedThisMethod( String name )
  288. {
  289. return
  290. name.equals("getClass")
  291. || name.equals("invokeMethod")
  292. || name.equals("getInterface")
  293. // These are necessary to let us test synchronization from scripts
  294. || name.equals("wait")
  295. || name.equals("notify")
  296. || name.equals("notifyAll");
  297. }
  298. }