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

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

#
Java | 193 lines | 76 code | 25 blank | 92 comment | 13 complexity | b2064386e5379d46781ccf427cfea879 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.name;
  65. }
  66. String toStringShowInts( Class [] ints ) {
  67. StringBuffer sb = new StringBuffer( toString() + "\nimplements:" );
  68. for(int i=0; i<ints.length; i++)
  69. sb.append( " "+ ints[i].getName() + ((ints.length > 1)?",":"") );
  70. return sb.toString();
  71. }
  72. /**
  73. Get dynamic proxy for interface, caching those it creates.
  74. */
  75. public Object getInterface( Class clas ) {
  76. if ( interfaces == null )
  77. interfaces = new Hashtable();
  78. Object interf = interfaces.get( clas );
  79. if ( interf == null ) {
  80. interf = Proxy.newProxyInstance( clas.getClassLoader(),
  81. new Class[] { clas }, invocationHandler );
  82. interfaces.put( clas, interf );
  83. }
  84. return interf;
  85. }
  86. /**
  87. Get a proxy interface for the specified XThis reference.
  88. This is a static utility method because the interpreter doesn't
  89. currently allow access to direct methods of This objects.
  90. public static Object getInterface( XThis ths, Class interf ) {
  91. return ths.getInterface( interf );
  92. }
  93. */
  94. /**
  95. This is the invocation handler for the dynamic proxy.
  96. <p>
  97. Notes:
  98. Inner class for the invocation handler seems to shield this unavailable
  99. interface from JDK1.2 VM...
  100. I don't understand this. JThis works just fine even if those
  101. classes aren't there (doesn't it?) This class shouldn't be loaded
  102. if an XThis isn't instantiated in NameSpace.java, should it?
  103. */
  104. class Handler implements InvocationHandler, java.io.Serializable
  105. {
  106. public Object invoke( Object proxy, Method method, Object[] args )
  107. throws EvalError
  108. {
  109. try {
  110. return invokeImpl( proxy, method, args );
  111. } catch ( EvalError ee ) {
  112. // Ease debugging...
  113. Interpreter.debug( "EvalError in scripted interface: "
  114. + XThis.this.toString() + ": "+ ee );
  115. throw ee;
  116. }
  117. }
  118. public Object invokeImpl( Object proxy, Method method, Object[] args )
  119. throws EvalError
  120. {
  121. CallStack callstack = newCallStack();
  122. Class [] sig = Reflect.getTypes( args );
  123. BshMethod bmethod =
  124. namespace.getMethod( method.getName(), sig );
  125. if ( bmethod != null )
  126. return Primitive.unwrap(
  127. bmethod.invokeDeclaredMethod(
  128. args, declaringInterpreter, callstack, null ) );
  129. // Look for the default handler
  130. bmethod = namespace.getMethod( "invoke",
  131. new Class [] { null, null } );
  132. // Call script "invoke( String methodName, Object [] args );
  133. if ( bmethod != null )
  134. return Primitive.unwrap(
  135. bmethod.invokeDeclaredMethod(
  136. new Object [] { method.getName(), args },
  137. declaringInterpreter, callstack, null ) );
  138. /*
  139. implement the required part of the Object protocol:
  140. public int hashCode();
  141. public boolean equals(java.lang.Object);
  142. public java.lang.String toString();
  143. if these were not handled by scripted methods we must provide
  144. a default impl.
  145. */
  146. // a default toString() that shows the interfaces we implement
  147. if ( method.getName().equals("toString" ) )
  148. return toStringShowInts( proxy.getClass().getInterfaces());
  149. // a default hashCode()
  150. if ( method.getName().equals("hashCode" ) )
  151. return new Integer(this.hashCode());
  152. // a default equals()
  153. if ( method.getName().equals("equals" ) ) {
  154. Object obj = args[0];
  155. return new Boolean( proxy == obj );
  156. }
  157. throw new EvalError("Bsh script method: "+ method.getName()
  158. + " not found in namespace: "+ namespace.name );
  159. }
  160. };
  161. }