PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 196 lines | 72 code | 26 blank | 98 comment | 13 complexity | b5fe5a735ed87ad3d458b5d233da0663 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. Inner class for the invocation handler seems to shield this unavailable
  96. interface from JDK1.2 VM...
  97. I don't understand this. JThis works just fine even if those
  98. classes aren't there (doesn't it?) This class shouldn't be loaded
  99. if an XThis isn't instantiated in NameSpace.java, should it?
  100. */
  101. class Handler implements InvocationHandler, java.io.Serializable {
  102. public Object invoke( Object proxy, Method method, Object[] args )
  103. throws EvalError
  104. {
  105. Class [] sig = Reflect.getTypes( args );
  106. BshMethod bmethod =
  107. namespace.getMethod( method.getName(), sig );
  108. if ( bmethod != null )
  109. return Primitive.unwrap(
  110. bmethod.invokeDeclaredMethod(
  111. args, declaringInterpreter, callstack, null ) );
  112. // Look for the default handler
  113. bmethod = namespace.getMethod( "invoke",
  114. new Class [] { null, null } );
  115. // Call script "invoke( String methodName, Object [] args );
  116. if ( bmethod != null )
  117. return Primitive.unwrap(
  118. bmethod.invokeDeclaredMethod(
  119. new Object [] { method.getName(), args },
  120. declaringInterpreter, callstack, null ) );
  121. /*
  122. implement the required part of the Object protocol:
  123. public int hashCode();
  124. public boolean equals(java.lang.Object);
  125. public java.lang.String toString();
  126. if these were not handled by scripted methods we must provide
  127. a default impl.
  128. */
  129. // a default toString() that shows the interfaces we implement
  130. if ( method.getName().equals("toString" ) )
  131. return toStringShowInts( proxy.getClass().getInterfaces());
  132. // a default hashCode()
  133. if ( method.getName().equals("hashCode" ) )
  134. return new Integer(this.hashCode());
  135. // a default equals()
  136. if ( method.getName().equals("equals" ) ) {
  137. Object obj = args[0];
  138. return new Boolean( proxy == obj );
  139. }
  140. throw new EvalError("Bsh script method: "+ method.getName()
  141. + " not found in namespace: "+ namespace.name );
  142. }
  143. };
  144. /**
  145. For serialization.
  146. Note: this is copied from superclass...
  147. It must be private, but we can probably add an accessor to allow
  148. us to call the super method explicitly.
  149. Just testing to see if this is causing a problem.
  150. */
  151. private synchronized void writeObject(ObjectOutputStream s)
  152. throws IOException {
  153. // Temporarily prune the namespace.
  154. NameSpace parent = namespace.getParent();
  155. // Bind would set the interpreter, but it's possible that the parent
  156. // is null (it's the root). So save it...
  157. Interpreter interpreter = declaringInterpreter;
  158. namespace.prune();
  159. s.defaultWriteObject();
  160. // put it back
  161. namespace.setParent( parent );
  162. declaringInterpreter = interpreter;
  163. }
  164. }