import sysExt.* ; import testPkg.* ; // // Example driver for methodRef usage. // Shows how to take a reference to a method, // dynamically at runtime, and then make // an object perform that referenced method... // class classLoadTest { public static void main(String args[]) { try { simpleObject s2 = new simpleObject (2) ; //----------------------------------------------------- // Create a reference to sayHello() method. // Class and package information is explicitly // specified.. // methodRef mRef2 = methodRef.newRef("testPkg", "simpleObject", "sayGoodBye") ; //----------------------------------------------------- // set the target to s2 and make it perform // the sayGoodBye() method... // mRef2.setTarget(s2) ; mRef2.doAction() ; simpleObject s3 = new simpleObject (3) ; //----------------------------------------------------- // Now set the target to s3 and make it perform // the sayGoodBye() method... // mRef2.on( s3 ).doAction(); foobar f = new foobar (4) ; //----------------------------------------------------- // Create a reference to foobar class sayHello() method. // Note that foobar has no package (null package) // methodRef mRef4 = methodRef.newRef(null, "foobar", "sayHello") ; //----------------------------------------------------- // Now set the target to f and make it perform // the sayHello() method... // mRef4.on( f ).doAction(); //----------------------------------------------------- // Now create a new foobar object and make it sayGoodBye // foobar f2 = new foobar (5) ; methodRef mRef5 = methodRef.newRef(f2, "sayGoodBye" ) ; mRef5.doAction(); //----------------------------------------------------- // Now create a new simpleObject.... // simpleObject s1 = new simpleObject (1); String aString = new String( "Hello World"); //----------------------------------------------------- // ...and create a reference to its sayAnything() method. // Class and package information is extracted from s1 // Target is set to s1 in newRef() call. // Since sayAnything() takes a string as its parameter, // pass the string's class to newRef() // methodRef mRef = methodRef.newRef(s1, "sayAnything", aString.getClass() ) ; // // dispatch sayAnything() to s1 object.. // mRef.doAction( aString ); } catch ( Exception e ) { System.out.println( "Exception thrown!" ) ; e.printStackTrace() ; } catch ( NoSuchMethodError er ) { System.out.println( "NoSuchMethodError thrown!" ) ; er.printStackTrace() ; } } }