Home |
Back |
Contents |
Next |
eval("a=5;"); print( a ); // 5 |
// Declare methods foo() and bar( int, String ) foo() { ... } bar( int arg1, String arg2 ) { ... } // Invoke a no-args method foo() by its name using eval() name="foo"; // invoke foo() using eval() eval( name+"()"); // Invoke two arg method bar(arg1,arg2) by name using eval() name="bar"; arg1=5; arg2="stringy"; eval( name+"(arg1,arg2)"); |
// Print the methods defined in this namespace print( this.methods ); |
this.invokeMethod( "bar", new Object [] { new Integer(5), "stringy" } ); |
foo() { ... } foo( int a ) { ... } bar( int arg1, String arg2 ) { ... } print ( this.namespace.getMethods() ); // Array: [Lbsh.BshMethod;@291aff { // Bsh Method: bar // Bsh Method: foo // Bsh Method: foo // } |
name="bar"; signature = new Class [] { Integer.TYPE, String.class }; // Look up a method named bar with arg types int and String bshMethod = this.namespace.getMethod( name, signature ); print("Found method: "+bshMethod); |
Tip: The Java reflection API uses special class values to represent primitive types such as int, char, an boolean. These types are static fields in the respective primitive wrapper classes. e.g. Integer.TYPE, Character.TYPE, Boolean.TYPE. |
name = bshMethod.getName(); Class [] types = bshMethod.getArgumentTypes(); Class returnType = bshMethod.getReturnType(); |
// invoke the method with arg bshMethod.invoke( new Object [] { new Integer(1), "blah!" }, this.interpreter, this.callstack ); |
Home |
Back |
Contents |
Next |