PageRenderTime 61ms CodeModel.GetById 54ms app.highlight 5ms 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
  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
 35package bsh;
 36
 37import java.lang.reflect.*;
 38import java.lang.reflect.InvocationHandler;
 39import java.io.*;
 40import java.util.Hashtable;
 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*/
 60class XThis extends This 
 61	{
 62	/**
 63		A cache of proxy interface handlers.
 64		Currently just one per interface.
 65	*/
 66	Hashtable interfaces;
 67
 68	InvocationHandler invocationHandler = new Handler();
 69
 70	XThis( NameSpace namespace, Interpreter declaringInterp ) { 
 71		super( namespace, declaringInterp ); 
 72	}
 73
 74	public String toString() {
 75		return "'this' reference (XThis) to Bsh object: " + namespace.name;
 76	}
 77
 78	String toStringShowInts( Class [] ints ) {
 79		StringBuffer sb = new StringBuffer( toString() + "\nimplements:" );
 80		for(int i=0; i<ints.length; i++)
 81			sb.append( " "+ ints[i].getName() + ((ints.length > 1)?",":"") );
 82		return sb.toString();
 83	}
 84
 85	/**
 86		Get dynamic proxy for interface, caching those it creates.
 87	*/
 88	public Object getInterface( Class clas ) {
 89		if ( interfaces == null )
 90			interfaces = new Hashtable();
 91
 92		Object interf = interfaces.get( clas );
 93		if ( interf == null ) {
 94			interf = Proxy.newProxyInstance( clas.getClassLoader(), 
 95				new Class[] { clas }, invocationHandler );
 96			interfaces.put( clas, interf );
 97		}
 98		return interf;
 99	}
100
101	/**
102		Get a proxy interface for the specified XThis reference.
103		This is a static utility method because the interpreter doesn't 
104		currently allow access to direct methods of This objects.
105		
106	public static Object getInterface( XThis ths, Class interf ) { 
107		return ths.getInterface( interf ); 
108	}
109	*/
110
111	/**
112		Inner class for the invocation handler seems to shield this unavailable
113		interface from JDK1.2 VM...  
114		
115		I don't understand this.  JThis works just fine even if those
116		classes aren't there (doesn't it?)  This class shouldn't be loaded
117		if an XThis isn't instantiated in NameSpace.java, should it?
118	*/
119	class Handler implements InvocationHandler, java.io.Serializable {
120
121		public Object invoke( Object proxy, Method method, Object[] args ) 
122			throws EvalError 
123		{
124			Class [] sig = Reflect.getTypes( args );
125			BshMethod bmethod = 
126				namespace.getMethod( method.getName(), sig );
127
128			if ( bmethod != null )
129				return Primitive.unwrap( 
130					bmethod.invokeDeclaredMethod( 
131					args, declaringInterpreter, callstack, null ) );
132
133			// Look for the default handler
134			bmethod = namespace.getMethod( "invoke", 
135				new Class [] { null, null } );
136
137			// Call script "invoke( String methodName, Object [] args );
138			if ( bmethod != null )
139				return Primitive.unwrap( 
140					bmethod.invokeDeclaredMethod( 
141					new Object [] { method.getName(), args }, 
142					declaringInterpreter, callstack, null ) );
143
144			/*
145				implement the required part of the Object protocol:
146					public int hashCode();
147					public boolean equals(java.lang.Object);
148					public java.lang.String toString();
149				if these were not handled by scripted methods we must provide
150				a default impl.
151			*/
152			// a default toString() that shows the interfaces we implement
153			if ( method.getName().equals("toString" ) )
154				return toStringShowInts( proxy.getClass().getInterfaces());
155
156			// a default hashCode()
157			if ( method.getName().equals("hashCode" ) )
158				return new Integer(this.hashCode());
159
160			// a default equals()
161			if ( method.getName().equals("equals" ) ) {
162				Object obj = args[0];
163				return new Boolean( proxy == obj );
164			}
165
166			throw new EvalError("Bsh script method: "+ method.getName()
167				+ " not found in namespace: "+ namespace.name );
168		}
169	};
170
171	/**
172		For serialization.
173		Note: this is copied from superclass... 
174		It must be private, but we can probably add an accessor to allow
175		us to call the super method explicitly.
176		Just testing to see if this is causing a problem.
177	*/
178    private synchronized void writeObject(ObjectOutputStream s)
179        throws IOException {
180
181		// Temporarily prune the namespace.
182
183		NameSpace parent = namespace.getParent();
184		// Bind would set the interpreter, but it's possible that the parent
185		// is null (it's the root).  So save it...
186		Interpreter interpreter = declaringInterpreter;
187		namespace.prune();
188		s.defaultWriteObject();
189		// put it back
190		namespace.setParent( parent );
191		declaringInterpreter = interpreter;
192	}
193}
194
195
196