interpreter import static method

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

interpreter import static method

bhomass
I have a class DateUtil with all static methods.

I want to call interpreter.eval(...), where the expression contains DateUtil.staticmethod().

but interpreter.set("instancename", object) only works for object instances. is there a way to set the class instead in order to use the static methods?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: interpreter import static method

alexander_a_sokolov
Hi,
You don't need to anything specific.

Just be sure that your class in beanshell classpath.
You can add you class to classpath by addClassPath("path/your.jar");


bhomass wrote
I have a class DateUtil with all static methods.

I want to call interpreter.eval(...), where the expression contains DateUtil.staticmethod().

but interpreter.set("instancename", object) only works for object instances. is there a way to set the class instead in order to use the static methods?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: interpreter import static method

bhomass
well I tried

                try {
                        Interpreter i = new Interpreter();
                        i.set("DateUtil", DateUtil.class);
                        String evalString = "DateUtil.hour(new Date())";
                        Object result = i.eval(evalString);
                } catch (EvalError ex){
                        ex.printStackTrace();
                }

and it does not work. the error says
Error in method invocation: Method hour( java.util.Date ) not found in class'java.lang.Class

how to fix this?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: interpreter import static method

alexander_a_sokolov
Look for this example:
               
import java.util.Date;

import bsh.EvalError;
import bsh.Interpreter;

public class BshSample {

        /**
         * @param args
         */
        public static void main(String[] args) {
                // TODO Auto-generated method stub
               
                try {
                        Interpreter i = new Interpreter();
                        //i.set("DateUtil", DateUtil.class);
                        //String evalString = "org.apache.commons.lang.time.DateUtils.hour(new Date())";
                        String evalString = "import  org.apache.commons.lang.time.DateUtils;"+
                                "DateUtils.addHours(new Date(), 0);";
                        //Object result = i.eval(evalString);
                        Date result =
                                (Date)i.eval(evalString);
                        System.out.println(result);
                       
                } catch (EvalError ex){
                        ex.printStackTrace();
                }

        }

}
Note that used classes must be in CLASSPATH.


bhomass wrote
well I tried

                try {
                        Interpreter i = new Interpreter();
                        i.set("DateUtil", DateUtil.class);
                        String evalString = "DateUtil.hour(new Date())";
                        Object result = i.eval(evalString);
                } catch (EvalError ex){
                        ex.printStackTrace();
                }

and it does not work. the error says
Error in method invocation: Method hour( java.util.Date ) not found in class'java.lang.Class

how to fix this?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: interpreter import static method

bhomass
Thank you Sir,

works like a charm.

Bruce


Loading...