1 33 34 35 package bsh; 36 37 import java.io.IOException ; 38 39 47 public class This implements java.io.Serializable , Runnable 48 { 49 52 NameSpace namespace; 53 54 60 transient Interpreter declaringInterpreter; 61 62 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 103 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 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 138 protected This( NameSpace namespace, Interpreter declaringInterpreter ) { 139 this.namespace = namespace; 140 this.declaringInterpreter = declaringInterpreter; 141 } 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 170 public Object invokeMethod( String name, Object [] args ) 171 throws EvalError 172 { 173 return invokeMethod( 175 name, args, null, null, null, 176 false ); 177 } 178 179 210 217 public Object invokeMethod( 218 String methodName, Object [] args, 219 Interpreter interpreter, CallStack callstack, SimpleNode callerInfo, 220 boolean declaredOnly ) 221 throws EvalError 222 { 223 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 Class [] types = Types.getTypes( args ); 247 BshMethod bshMethod = null; 248 try { 249 bshMethod = namespace.getMethod( methodName, types, declaredOnly ); 250 } catch ( UtilEvalError e ) { 251 } 253 254 if ( bshMethod != null ) 255 return bshMethod.invoke( args, interpreter, callstack, callerInfo ); 256 257 266 if ( methodName.equals("toString" ) ) 268 return toString(); 269 270 if ( methodName.equals("hashCode" ) ) 272 return new Integer (this.hashCode()); 273 274 if ( methodName.equals("equals" ) ) { 276 Object obj = args[0]; 277 return new Boolean ( this == obj ); 278 } 279 280 try { 284 bshMethod = namespace.getMethod( 285 "invoke", new Class [] { null, null } ); 286 } catch ( UtilEvalError e ) { } 287 288 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 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 327 static boolean isExposedThisMethod( String name ) 328 { 329 return 330 name.equals("getClass") 331 || name.equals("invokeMethod") 332 || name.equals("getInterface") 333 || name.equals("wait") 335 || name.equals("notify") 336 || name.equals("notifyAll"); 337 } 338 339 } 340 341 | Popular Tags |