PageRenderTime 274ms CodeModel.GetById 244ms app.highlight 22ms RepoModel.GetById 1ms app.codeStats 1ms

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

#
Java | 193 lines | 76 code | 25 blank | 92 comment | 13 complexity | b2064386e5379d46781ccf427cfea879 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		This is the invocation handler for the dynamic proxy.
113		<p>
114
115		Notes:
116		Inner class for the invocation handler seems to shield this unavailable
117		interface from JDK1.2 VM...  
118		
119		I don't understand this.  JThis works just fine even if those
120		classes aren't there (doesn't it?)  This class shouldn't be loaded
121		if an XThis isn't instantiated in NameSpace.java, should it?
122	*/
123	class Handler implements InvocationHandler, java.io.Serializable 
124	{
125		public Object invoke( Object proxy, Method method, Object[] args ) 
126			throws EvalError 
127		{
128			try { 
129				return invokeImpl( proxy, method, args );
130			} catch ( EvalError ee ) {
131				// Ease debugging...
132				Interpreter.debug( "EvalError in scripted interface: "
133					+ XThis.this.toString() + ": "+ ee );
134				throw ee;
135			}
136		}
137
138		public Object invokeImpl( Object proxy, Method method, Object[] args ) 
139			throws EvalError 
140		{
141			CallStack callstack = newCallStack();
142
143			Class [] sig = Reflect.getTypes( args );
144			BshMethod bmethod = 
145				namespace.getMethod( method.getName(), sig );
146
147			if ( bmethod != null )
148				return Primitive.unwrap( 
149					bmethod.invokeDeclaredMethod( 
150					args, declaringInterpreter, callstack, null ) );
151
152			// Look for the default handler
153			bmethod = namespace.getMethod( "invoke", 
154				new Class [] { null, null } );
155
156			// Call script "invoke( String methodName, Object [] args );
157			if ( bmethod != null )
158				return Primitive.unwrap( 
159					bmethod.invokeDeclaredMethod( 
160					new Object [] { method.getName(), args }, 
161					declaringInterpreter, callstack, null ) );
162
163			/*
164				implement the required part of the Object protocol:
165					public int hashCode();
166					public boolean equals(java.lang.Object);
167					public java.lang.String toString();
168				if these were not handled by scripted methods we must provide
169				a default impl.
170			*/
171			// a default toString() that shows the interfaces we implement
172			if ( method.getName().equals("toString" ) )
173				return toStringShowInts( proxy.getClass().getInterfaces());
174
175			// a default hashCode()
176			if ( method.getName().equals("hashCode" ) )
177				return new Integer(this.hashCode());
178
179			// a default equals()
180			if ( method.getName().equals("equals" ) ) {
181				Object obj = args[0];
182				return new Boolean( proxy == obj );
183			}
184
185			throw new EvalError("Bsh script method: "+ method.getName()
186				+ " not found in namespace: "+ namespace.name );
187		}
188	};
189
190}
191
192
193