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

/jEdit/tags/jedit-4-2-pre4/bsh/XThis.java

#
Java | 199 lines | 86 code | 22 blank | 91 comment | 14 complexity | 55e681c2e1a3505053b80a3fca4397fe 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.lang.reflect.*;
  35. import java.lang.reflect.InvocationHandler;
  36. import java.io.*;
  37. import java.util.Hashtable;
  38. /**
  39. XThis is a dynamically loaded extension which extends This.java and adds
  40. support for the generalized interface proxy mechanism introduced in
  41. JDK1.3. XThis allows bsh scripted objects to implement arbitrary
  42. interfaces (be arbitrary event listener types).
  43. Note: This module relies on new features of JDK1.3 and will not compile
  44. with JDK1.2 or lower. For those environments simply do not compile this
  45. class.
  46. Eventually XThis should become simply This, but for backward compatability
  47. we will maintain This without requiring support for the proxy mechanism.
  48. XThis stands for "eXtended This" (I had to call it something).
  49. @see JThis See also JThis with explicit JFC support for compatability.
  50. @see This
  51. */
  52. class XThis extends This
  53. {
  54. /**
  55. A cache of proxy interface handlers.
  56. Currently just one per interface.
  57. */
  58. Hashtable interfaces;
  59. InvocationHandler invocationHandler = new Handler();
  60. XThis( NameSpace namespace, Interpreter declaringInterp ) {
  61. super( namespace, declaringInterp );
  62. }
  63. public String toString() {
  64. return "'this' reference (XThis) to Bsh object: " + namespace;
  65. }
  66. /**
  67. Get dynamic proxy for interface, caching those it creates.
  68. */
  69. public Object getInterface( Class clas )
  70. {
  71. return getInterface( new Class[] { clas } );
  72. }
  73. /**
  74. Get dynamic proxy for interface, caching those it creates.
  75. */
  76. public Object getInterface( Class [] ca )
  77. {
  78. if ( interfaces == null )
  79. interfaces = new Hashtable();
  80. // Make a hash of the interface hashcodes in order to cache them
  81. int hash = 21;
  82. for(int i=0; i<ca.length; i++)
  83. hash *= ca[i].hashCode() + 3;
  84. Object hashKey = new Integer(hash);
  85. Object interf = interfaces.get( hashKey );
  86. if ( interf == null )
  87. {
  88. ClassLoader classLoader = ca[0].getClassLoader(); // ?
  89. interf = Proxy.newProxyInstance(
  90. classLoader, ca, invocationHandler );
  91. interfaces.put( hashKey, interf );
  92. }
  93. return interf;
  94. }
  95. /**
  96. This is the invocation handler for the dynamic proxy.
  97. <p>
  98. Notes:
  99. Inner class for the invocation handler seems to shield this unavailable
  100. interface from JDK1.2 VM...
  101. I don't understand this. JThis works just fine even if those
  102. classes aren't there (doesn't it?) This class shouldn't be loaded
  103. if an XThis isn't instantiated in NameSpace.java, should it?
  104. */
  105. class Handler implements InvocationHandler, java.io.Serializable
  106. {
  107. public Object invoke( Object proxy, Method method, Object[] args )
  108. throws Throwable
  109. {
  110. try {
  111. return invokeImpl( proxy, method, args );
  112. } catch ( TargetError te ) {
  113. // Unwrap target exception. If the interface declares that
  114. // it throws the ex it will be delivered. If not it will be
  115. // wrapped in an UndeclaredThrowable
  116. throw te.getTarget();
  117. } catch ( EvalError ee ) {
  118. // Ease debugging...
  119. // XThis.this refers to the enclosing class instance
  120. if ( Interpreter.DEBUG )
  121. Interpreter.debug( "EvalError in scripted interface: "
  122. + XThis.this.toString() + ": "+ ee );
  123. throw ee;
  124. }
  125. }
  126. public Object invokeImpl( Object proxy, Method method, Object[] args )
  127. throws EvalError
  128. {
  129. String methodName = method.getName();
  130. CallStack callstack = new CallStack( namespace );
  131. /*
  132. If equals() is not explicitly defined we must override the
  133. default implemented by the This object protocol for scripted
  134. object. To support XThis equals() must test for equality with
  135. the generated proxy object, not the scripted bsh This object;
  136. otherwise callers from outside in Java will not see a the
  137. proxy object as equal to itself.
  138. */
  139. BshMethod equalsMethod = null;
  140. try {
  141. equalsMethod = namespace.getMethod(
  142. "equals", new Class [] { Object.class } );
  143. } catch ( UtilEvalError e ) {/*leave null*/ }
  144. if ( methodName.equals("equals" ) && equalsMethod == null ) {
  145. Object obj = args[0];
  146. return new Boolean( proxy == obj );
  147. }
  148. /*
  149. If toString() is not explicitly defined override the default
  150. to show the proxy interfaces.
  151. */
  152. BshMethod toStringMethod = null;
  153. try {
  154. toStringMethod =
  155. namespace.getMethod( "toString", new Class [] { } );
  156. } catch ( UtilEvalError e ) {/*leave null*/ }
  157. if ( methodName.equals("toString" ) && toStringMethod == null)
  158. {
  159. Class [] ints = proxy.getClass().getInterfaces();
  160. // XThis.this refers to the enclosing class instance
  161. StringBuffer sb = new StringBuffer(
  162. XThis.this.toString() + "\nimplements:" );
  163. for(int i=0; i<ints.length; i++)
  164. sb.append( " "+ ints[i].getName()
  165. + ((ints.length > 1)?",":"") );
  166. return sb.toString();
  167. }
  168. return Primitive.unwrap( invokeMethod( methodName, args ) );
  169. }
  170. };
  171. }