PageRenderTime 90ms CodeModel.GetById 78ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-3-pre5/bsh/This.java

#
Java | 340 lines | 131 code | 30 blank | 179 comment | 29 complexity | dded4a069dbe12495eedc4fcd0f12d29 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.io.IOException;
 38
 39/**
 40	'This' is the type of bsh scripted objects.
 41	A 'This' object is a bsh scripted object context.  It holds a namespace 
 42	reference and implements event listeners and various other interfaces.
 43
 44	This holds a reference to the declaring interpreter for callbacks from
 45	outside of bsh.
 46*/
 47public class This implements java.io.Serializable, Runnable 
 48{
 49	/**
 50		The namespace that this This reference wraps.
 51	*/
 52	NameSpace namespace;
 53
 54	/**
 55		This is the interpreter running when the This ref was created.
 56		It's used as a default interpreter for callback through the This
 57		where there is no current interpreter instance 
 58		e.g. interface proxy or event call backs from outside of bsh.
 59	*/
 60	transient Interpreter declaringInterpreter;
 61
 62	/**
 63		getThis() is a factory for bsh.This type references.  The capabilities
 64		of ".this" references in bsh are version dependent up until jdk1.3.
 65		The version dependence was to support different default interface
 66		implementations.  i.e. different sets of listener interfaces which
 67		scripted objects were capable of implementing.  In jdk1.3 the 
 68		reflection proxy mechanism was introduced which allowed us to 
 69		implement arbitrary interfaces.  This is fantastic.
 70
 71		A This object is a thin layer over a namespace, comprising a bsh object
 72		context.  We create it here only if needed for the namespace.
 73
 74		Note: this method could be considered slow because of the way it 
 75		dynamically factories objects.  However I've also done tests where 
 76		I hard-code the factory to return JThis and see no change in the 
 77		rough test suite time.  This references are also cached in NameSpace.  
 78	*/
 79    static This getThis( 
 80		NameSpace namespace, Interpreter declaringInterpreter ) 
 81	{
 82		try {
 83			Class c;
 84			if ( Capabilities.canGenerateInterfaces() )
 85				c = Class.forName( "bsh.XThis" );
 86			else if ( Capabilities.haveSwing() )
 87				c = Class.forName( "bsh.JThis" );
 88			else
 89				return new This( namespace, declaringInterpreter );
 90
 91			return (This)Reflect.constructObject( c,
 92				new Object [] { namespace, declaringInterpreter } );
 93
 94		} catch ( Exception e ) {
 95			throw new InterpreterError("internal error 1 in This: "+e);
 96		}
 97    }
 98
 99	/**
100		Get a version of this scripted object implementing the specified 
101		interface.
102	*/
103	/*
104		If this type of This implements it directly return this,
105		else try complain that we don't have the proxy mechanism.
106	*/
107	public Object getInterface( Class clas ) 
108		throws UtilEvalError
109	{
110		if ( clas.isInstance( this ) )
111			return this;
112		else
113			throw new UtilEvalError( "Dynamic proxy mechanism not available. "
114			+ "Cannot construct interface type: "+clas );
115	}
116
117	/**
118		Get a version of this scripted object implementing the specified
119		interfaces.
120	*/
121	public Object getInterface( Class [] ca ) 
122		throws UtilEvalError
123	{
124		for(int i=0; i<ca.length; i++)
125			if ( !(ca[i].isInstance( this )) )
126				throw new UtilEvalError( 
127					"Dynamic proxy mechanism not available. " 
128					+ "Cannot construct interface type: "+ca[i] );
129
130		return this;
131	}
132
133	/*
134		I wish protected access were limited to children and not also 
135		package scope... I want this to be a singleton implemented by various
136		children.  
137	*/
138	protected This( NameSpace namespace, Interpreter declaringInterpreter ) { 
139		this.namespace = namespace; 
140		this.declaringInterpreter = declaringInterpreter;
141		//initCallStack( namespace );
142	}
143
144	public NameSpace getNameSpace() {
145		return namespace;
146	}
147
148	public String toString() {
149		return "'this' reference to Bsh object: " + namespace;
150	}
151
152	public void run() {
153		try {
154			invokeMethod( "run", new Object[0] );
155		} catch( EvalError e ) {
156			declaringInterpreter.error(
157				"Exception in runnable:" + e );
158		}
159	}
160
161	/**
162		Invoke specified method as from outside java code, using the 
163		declaring interpreter and current namespace.
164		The call stack will indicate that the method is being invoked from
165		outside of bsh in native java code.
166		Note: you must still wrap/unwrap args/return values using 
167		Primitive/Primitive.unwrap() for use outside of BeanShell.
168		@see bsh.Primitive
169	*/
170	public Object invokeMethod( String name, Object [] args ) 
171		throws EvalError
172	{
173		// null callstack, one will be created for us 
174		return invokeMethod( 
175			name, args, null/*declaringInterpreter*/, null, null, 
176			false/*declaredOnly*/ );
177	}
178
179	/**
180		Invoke a method in this namespace with the specified args,
181		interpreter reference, callstack, and caller info.
182		<p>
183
184		Note: If you use this method outside of the bsh package and wish to 
185		use variables with primitive values you will have to wrap them using 
186		bsh.Primitive.  Consider using This getInterface() to make a true Java
187		interface for invoking your scripted methods.
188		<p>
189
190		This method also implements the default object protocol of toString(), 
191		hashCode() and equals() and the invoke() meta-method handling as a 
192		last resort.
193		<p>
194
195		Note: The invoke() meta-method will not catch the Object protocol
196		methods (toString(), hashCode()...).  If you want to override them you 
197		have to script them directly.
198		<p>
199
200		@see bsh.This.invokeMethod( 
201			String methodName, Object [] args, Interpreter interpreter, 
202			CallStack callstack, SimpleNode callerInfo )
203		@param if callStack is null a new CallStack will be created and
204			initialized with this namespace.
205		@param declaredOnly if true then only methods declared directly in the
206			namespace will be visible - no inherited or imported methods will
207			be visible.
208		@see bsh.Primitive
209	*/
210	/*
211		invokeMethod() here is generally used by outside code to callback
212		into the bsh interpreter. e.g. when we are acting as an interface
213		for a scripted listener, etc.  In this case there is no real call stack
214		so we make a default one starting with the special JAVACODE namespace
215		and our namespace as the next.
216	*/
217	public Object invokeMethod( 
218		String methodName, Object [] args, 
219		Interpreter interpreter, CallStack callstack, SimpleNode callerInfo, 
220		boolean declaredOnly  ) 
221		throws EvalError
222	{
223		/*
224			Wrap nulls.
225			This is a bit of a cludge to address a deficiency in the class
226			generator whereby it does not wrap nulls on method delegate.  See
227			Class Generator.java.  If we fix that then we can remove this.
228			(just have to generate the code there.)
229		*/
230		if ( args != null )
231		{
232			Object [] oa = new Object [args.length];
233			for(int i=0; i<args.length; i++)
234				oa[i] = ( args[i] == null ? Primitive.NULL : args[i] );
235			args = oa;
236		}
237
238		if ( interpreter == null )
239			interpreter = declaringInterpreter;
240		if ( callstack == null )
241			callstack = new CallStack( namespace );
242		if ( callerInfo == null )
243			callerInfo = SimpleNode.JAVACODE;
244
245		// Find the bsh method
246		Class [] types = Types.getTypes( args );
247		BshMethod bshMethod = null;
248		try {
249			bshMethod = namespace.getMethod( methodName, types, declaredOnly );
250		} catch ( UtilEvalError e ) {
251			// leave null
252		}
253
254		if ( bshMethod != null )
255			return bshMethod.invoke( args, interpreter, callstack, callerInfo );
256
257		/*
258			No scripted method of that name.
259			Implement the required part of the Object protocol:
260				public int hashCode();
261				public boolean equals(java.lang.Object);
262				public java.lang.String toString();
263			if these were not handled by scripted methods we must provide
264			a default impl.
265		*/
266		// a default toString() that shows the interfaces we implement
267		if ( methodName.equals("toString" ) )
268			return toString();
269
270		// a default hashCode()
271		if ( methodName.equals("hashCode" ) )
272			return new Integer(this.hashCode());
273
274		// a default equals() testing for equality with the This reference
275		if ( methodName.equals("equals" ) ) {
276			Object obj = args[0];
277			return new Boolean( this == obj );
278		}
279
280		// Look for a default invoke() handler method in the namespace
281		// Note: this code duplicates that in NameSpace getCommand()
282		// is that ok?
283		try {
284			bshMethod = namespace.getMethod( 
285				"invoke", new Class [] { null, null } );
286		} catch ( UtilEvalError e ) { /*leave null*/ }
287
288		// Call script "invoke( String methodName, Object [] args );
289		if ( bshMethod != null )
290			return bshMethod.invoke( new Object [] { methodName, args }, 
291				interpreter, callstack, callerInfo );
292
293		throw new EvalError("Method " + 
294			StringUtil.methodString( methodName, types ) +
295			" not found in bsh scripted object: "+ namespace.getName(), 
296			callerInfo, callstack );
297	}
298
299	/**
300		Bind a This reference to a parent's namespace with the specified
301		declaring interpreter.  Also re-init the callstack.  It's necessary 
302		to bind a This reference before it can be used after deserialization.
303		This is used by the bsh load() command.
304		<p>
305
306		This is a static utility method because it's used by a bsh command
307		bind() and the interpreter doesn't currently allow access to direct 
308		methods of This objects (small hack)
309	*/
310	public static void bind( 
311		This ths, NameSpace namespace, Interpreter declaringInterpreter ) 
312	{ 
313		ths.namespace.setParent( namespace ); 
314		ths.declaringInterpreter = declaringInterpreter;
315	}
316
317	/**
318		Allow invocations of these method names on This type objects.
319		Don't give bsh.This a chance to override their behavior.
320		<p>
321
322		If the method is passed here the invocation will actually happen on
323		the bsh.This object via the regular reflective method invocation 
324		mechanism.  If not, then the method is evaluated by bsh.This itself
325		as a scripted method call.
326	*/
327	static boolean isExposedThisMethod( String name ) 
328	{
329		return 
330			name.equals("getClass") 
331			|| name.equals("invokeMethod")
332			|| name.equals("getInterface")
333			// These are necessary to let us test synchronization from scripts
334			|| name.equals("wait") 
335			|| name.equals("notify")
336			|| name.equals("notifyAll");
337	}
338
339}
340