001    /*****************************************************************************
002     *                                                                           *
003     *  This file is part of the BeanShell Java Scripting distribution.          *
004     *  Documentation and updates may be found at http://www.beanshell.org/      *
005     *                                                                           *
006     *  Sun Public License Notice:                                               *
007     *                                                                           *
008     *  The contents of this file are subject to the Sun Public License Version  *
009     *  1.0 (the "License"); you may not use this file except in compliance with *
010     *  the License. A copy of the License is available at http://www.sun.com    * 
011     *                                                                           *
012     *  The Original Code is BeanShell. The Initial Developer of the Original    *
013     *  Code is Pat Niemeyer. Portions created by Pat Niemeyer are Copyright     *
014     *  (C) 2000.  All Rights Reserved.                                          *
015     *                                                                           *
016     *  GNU Public License Notice:                                               *
017     *                                                                           *
018     *  Alternatively, the contents of this file may be used under the terms of  *
019     *  the GNU Lesser General Public License (the "LGPL"), in which case the    *
020     *  provisions of LGPL are applicable instead of those above. If you wish to *
021     *  allow use of your version of this file only under the  terms of the LGPL *
022     *  and not to allow others to use your version of this file under the SPL,  *
023     *  indicate your decision by deleting the provisions above and replace      *
024     *  them with the notice and other provisions required by the LGPL.  If you  *
025     *  do not delete the provisions above, a recipient may use your version of  *
026     *  this file under either the SPL or the LGPL.                              *
027     *                                                                           *
028     *  Patrick Niemeyer (pat@pat.net)                                           *
029     *  Author of Learning Java, O'Reilly & Associates                           *
030     *  http://www.pat.net/~pat/                                                 *
031     *                                                                           *
032     *****************************************************************************/
033    
034    
035    package bsh;
036    
037    import java.io.IOException;
038    
039    /**
040            'This' is the type of bsh scripted objects.
041            A 'This' object is a bsh scripted object context.  It holds a namespace 
042            reference and implements event listeners and various other interfaces.
043    
044            This holds a reference to the declaring interpreter for callbacks from
045            outside of bsh.
046    */
047    public class This implements java.io.Serializable, Runnable 
048    {
049            /**
050                    The namespace that this This reference wraps.
051            */
052            NameSpace namespace;
053    
054            /**
055                    This is the interpreter running when the This ref was created.
056                    It's used as a default interpreter for callback through the This
057                    where there is no current interpreter instance 
058                    e.g. interface proxy or event call backs from outside of bsh.
059            */
060            transient Interpreter declaringInterpreter;
061    
062            /**
063                    getThis() is a factory for bsh.This type references.  The capabilities
064                    of ".this" references in bsh are version dependent up until jdk1.3.
065                    The version dependence was to support different default interface
066                    implementations.  i.e. different sets of listener interfaces which
067                    scripted objects were capable of implementing.  In jdk1.3 the 
068                    reflection proxy mechanism was introduced which allowed us to 
069                    implement arbitrary interfaces.  This is fantastic.
070    
071                    A This object is a thin layer over a namespace, comprising a bsh object
072                    context.  We create it here only if needed for the namespace.
073    
074                    Note: this method could be considered slow because of the way it 
075                    dynamically factories objects.  However I've also done tests where 
076                    I hard-code the factory to return JThis and see no change in the 
077                    rough test suite time.  This references are also cached in NameSpace.  
078            */
079        static This getThis( 
080                    NameSpace namespace, Interpreter declaringInterpreter ) 
081            {
082                    try {
083                            Class c;
084                            if ( Capabilities.canGenerateInterfaces() )
085                                    c = Class.forName( "bsh.XThis" );
086                            else if ( Capabilities.haveSwing() )
087                                    c = Class.forName( "bsh.JThis" );
088                            else
089                                    return new This( namespace, declaringInterpreter );
090    
091                            return (This)Reflect.constructObject( c,
092                                    new Object [] { namespace, declaringInterpreter } );
093    
094                    } catch ( Exception e ) {
095                            throw new InterpreterError("internal error 1 in This: "+e);
096                    }
097        }
098    
099            /**
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