KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > bsh > XThis


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
34
35 package bsh;
36
37 import java.lang.reflect.*;
38 import java.lang.reflect.InvocationHandler JavaDoc;
39 import java.io.*;
40 import java.util.Hashtable JavaDoc;
41
42 /**
43     XThis is a dynamically loaded extension which extends This.java and adds
44     support for the generalized interface proxy mechanism introduced in
45     JDK1.3. XThis allows bsh scripted objects to implement arbitrary
46     interfaces (be arbitrary event listener types).
47
48     Note: This module relies on new features of JDK1.3 and will not compile
49     with JDK1.2 or lower. For those environments simply do not compile this
50     class.
51
52     Eventually XThis should become simply This, but for backward compatability
53     we will maintain This without requiring support for the proxy mechanism.
54
55     XThis stands for "eXtended This" (I had to call it something).
56     
57     @see JThis See also JThis with explicit JFC support for compatability.
58     @see This
59 */

60 public class XThis extends This
61     {
62     /**
63         A cache of proxy interface handlers.
64         Currently just one per interface.
65     */

66     Hashtable JavaDoc interfaces;
67
68     InvocationHandler JavaDoc invocationHandler = new Handler JavaDoc();
69
70     public XThis( NameSpace namespace, Interpreter declaringInterp ) {
71         super( namespace, declaringInterp );
72     }
73
74     public String JavaDoc toString() {
75         return "'this' reference (XThis) to Bsh object: " + namespace;
76     }
77
78     /**
79         Get dynamic proxy for interface, caching those it creates.
80     */

81     public Object JavaDoc getInterface( Class JavaDoc clas )
82     {
83         return getInterface( new Class JavaDoc[] { clas } );
84     }
85
86     /**
87         Get dynamic proxy for interface, caching those it creates.
88     */

89     public Object JavaDoc getInterface( Class JavaDoc [] ca )
90     {
91         if ( interfaces == null )
92             interfaces = new Hashtable JavaDoc();
93
94         // Make a hash of the interface hashcodes in order to cache them
95
int hash = 21;
96         for(int i=0; i<ca.length; i++)
97             hash *= ca[i].hashCode() + 3;
98         Object JavaDoc hashKey = new Integer JavaDoc(hash);
99
100         Object JavaDoc interf = interfaces.get( hashKey );
101
102         if ( interf == null )
103         {
104             ClassLoader JavaDoc classLoader = ca[0].getClassLoader(); // ?
105
interf = Proxy.newProxyInstance(
106                 classLoader, ca, invocationHandler );
107             interfaces.put( hashKey, interf );
108         }
109
110         return interf;
111     }
112
113     /**
114         This is the invocation handler for the dynamic proxy.
115         <p>
116
117         Notes:
118         Inner class for the invocation handler seems to shield this unavailable
119         interface from JDK1.2 VM...
120         
121         I don't understand this. JThis works just fine even if those
122         classes aren't there (doesn't it?) This class shouldn't be loaded
123         if an XThis isn't instantiated in NameSpace.java, should it?
124     */

125     class Handler implements InvocationHandler JavaDoc, java.io.Serializable JavaDoc
126     {
127         public Object JavaDoc invoke( Object JavaDoc proxy, Method method, Object JavaDoc[] args )
128             throws Throwable JavaDoc
129         {
130             try {
131                 return invokeImpl( proxy, method, args );
132             } catch ( TargetError te ) {
133                 // Unwrap target exception. If the interface declares that
134
// it throws the ex it will be delivered. If not it will be
135
// wrapped in an UndeclaredThrowable
136
throw te.getTarget();
137             } catch ( EvalError ee ) {
138                 // Ease debugging...
139
// XThis.this refers to the enclosing class instance
140
if ( Interpreter.DEBUG )
141                     Interpreter.debug( "EvalError in scripted interface: "
142                     + XThis.this.toString() + ": "+ ee );
143                 throw ee;
144             }
145         }
146
147         public Object JavaDoc invokeImpl( Object JavaDoc proxy, Method method, Object JavaDoc[] args )
148             throws EvalError
149         {
150             String JavaDoc methodName = method.getName();
151             CallStack callstack = new CallStack( namespace );
152
153             /*
154                 If equals() is not explicitly defined we must override the
155                 default implemented by the This object protocol for scripted
156                 object. To support XThis equals() must test for equality with
157                 the generated proxy object, not the scripted bsh This object;
158                 otherwise callers from outside in Java will not see a the
159                 proxy object as equal to itself.
160             */

161             BshMethod equalsMethod = null;
162             try {
163                 equalsMethod = namespace.getMethod(
164                     "equals", new Class JavaDoc [] { Object JavaDoc.class } );
165             } catch ( UtilEvalError e ) {/*leave null*/ }
166             if ( methodName.equals("equals" ) && equalsMethod == null ) {
167                 Object JavaDoc obj = args[0];
168                 return new Boolean JavaDoc( proxy == obj );
169             }
170
171             /*
172                 If toString() is not explicitly defined override the default
173                 to show the proxy interfaces.
174             */

175             BshMethod toStringMethod = null;
176             try {
177                 toStringMethod =
178                     namespace.getMethod( "toString", new Class JavaDoc [] { } );
179             } catch ( UtilEvalError e ) {/*leave null*/ }
180
181             if ( methodName.equals("toString" ) && toStringMethod == null)
182             {
183                 Class JavaDoc [] ints = proxy.getClass().getInterfaces();
184                 // XThis.this refers to the enclosing class instance
185
StringBuffer JavaDoc sb = new StringBuffer JavaDoc(
186                     XThis.this.toString() + "\nimplements:" );
187                 for(int i=0; i<ints.length; i++)
188                     sb.append( " "+ ints[i].getName()
189                         + ((ints.length > 1)?",":"") );
190                 return sb.toString();
191             }
192
193             Class JavaDoc [] paramTypes = method.getParameterTypes();
194             return Primitive.unwrap(
195                 invokeMethod( methodName, Primitive.wrap(args, paramTypes) ) );
196         }
197     };
198 }
199
200
201
202
Popular Tags