Using the Reflection API to dynamically modify methods

Discussion in 'Java' started by Serge, Feb 13, 2004.

  1. Serge

    Serge Guest

    Hello,

    I am wondering if I could use the Java reflection API to dynamically
    modify the methods of a class. For example, I need a function that will
    evaluate any f(x) at some x. Say I have something like:

    static double eval_f(double val){
    double result = (val)**2 + 2*(val) + 2;
    return result;
    }

    The above eval_f() will evaluate f(x) = x^2 + 2x + 2 at any x. However, in
    my program I need to be able to modify f(x) to whatever the user wants
    (the user enters the function he wants and it is copied to a string; my
    program simply finds roots of these functions using different numerical
    methods). Thus, if the user wants f(x) = x^11 + 2 I want to be able to
    dynamically modify the first line in the eval_f() function to:

    double result = (val)**11 + 2;

    It seems to me as if there might be a way to accomplish this using the
    Reflection class although I am not a pro at using it. I would appreciate
    if someone could tell me if it is indeed possible and how I can go about
    doing this. Thanks so much.

    Cheers,
    Serge
     
    Serge, Feb 13, 2004
    #1
    1. Advertising

  2. Serge wrote:
    ....
    > I am wondering if I could use the Java reflection API to dynamically
    > modify the methods of a class.


    No.

    > ..For example, I need a function that
    > will evaluate any f(x) at some x. Say I have something like:
    >
    > static double eval_f(double val){
    > double result = (val)**2 + 2*(val) + 2;
    > return result;
    > }
    >
    > The above eval_f() will evaluate f(x) = x^2 + 2x + 2 at any x.
    > However, in my program I need to be able to modify f(x) to whatever
    > the user wants (the user enters the function he wants and it is
    > copied to a string; my program simply finds roots of these functions
    > using different numerical methods).


    First figure how to parse the
    string to a function.
    That is 95% of the problem.

    By the time you have done that,
    evaluating it will be easy.

    --
    Andrew Thompson
    * http://www.PhySci.org/ Open-source software suite
    * http://www.PhySci.org/codes/ Web & IT Help
    * http://www.1point1C.org/ Science & Technology
     
    Andrew Thompson, Feb 13, 2004
    #2
    1. Advertising

  3. Serge

    Adam Jenkins Guest

    Serge wrote:
    > Hello,
    >
    > I am wondering if I could use the Java reflection API to dynamically
    > modify the methods of a class. For example, I need a function that will
    > evaluate any f(x) at some x. Say I have something like:
    >
    > static double eval_f(double val){
    > double result = (val)**2 + 2*(val) + 2;
    > return result;
    > }
    >
    > The above eval_f() will evaluate f(x) = x^2 + 2x + 2 at any x. However, in
    > my program I need to be able to modify f(x) to whatever the user wants
    > (the user enters the function he wants and it is copied to a string; my
    > program simply finds roots of these functions using different numerical
    > methods). Thus, if the user wants f(x) = x^11 + 2 I want to be able to
    > dynamically modify the first line in the eval_f() function to:
    >
    > double result = (val)**11 + 2;


    What you want to do is beyond the scope of what reflection provides. If
    you want to allow a user to enter functions at runtime, the simplest
    solution would be to use an embeddable scripting language like Beanshell
    or Rhino. Beanshell implementes an interpreted java-like language, and
    Rhino is a Javascript interpreter written in Java. You can get
    Beanshell at
    http://www.beanshell.org
    and Rhino at
    http://www.mozilla.org/rhino
    I think there's another one called Dynamic Java. All these make it easy
    to access java classes from the scripting language as well should you
    need to do so.

    Adam



    >
    > It seems to me as if there might be a way to accomplish this using the
    > Reflection class although I am not a pro at using it. I would appreciate
    > if someone could tell me if it is indeed possible and how I can go about
    > doing this. Thanks so much.
    >
    > Cheers,
    > Serge
    >
     
    Adam Jenkins, Feb 13, 2004
    #3
  4. Serge

    Serge Guest

    Thanks for the help. I have actually considered using a scripting language
    before. Actually, I simply passed arguments to a Perl script
    evaluated the function using eval() in Perl and passed the result back to
    my program by means of a temp file. However, all that stuff made the
    program rather slow. On another note, I can't really use things like Rhino
    because I have to make all my programs work on an external computer on
    which I don't have install rights (it has jsdk 1.4.2 though and all the
    std unix stuff). So I am wondering if there is any way to implement what i
    discussed without using external tools (outside the jsdk 1.4.2). Thanks in
    advance for the help.

    On Fri, 13 Feb 2004 14:29:35 -0500, Adam Jenkins wrote:

    > Serge wrote:
    >> Hello,
    >>
    >> I am wondering if I could use the Java reflection API to dynamically
    >> modify the methods of a class. For example, I need a function that will
    >> evaluate any f(x) at some x. Say I have something like:
    >>
    >> static double eval_f(double val){
    >> double result = (val)**2 + 2*(val) + 2;
    >> return result;
    >> }
    >>
    >> The above eval_f() will evaluate f(x) = x^2 + 2x + 2 at any x. However, in
    >> my program I need to be able to modify f(x) to whatever the user wants
    >> (the user enters the function he wants and it is copied to a string; my
    >> program simply finds roots of these functions using different numerical
    >> methods). Thus, if the user wants f(x) = x^11 + 2 I want to be able to
    >> dynamically modify the first line in the eval_f() function to:
    >>
    >> double result = (val)**11 + 2;

    >
    > What you want to do is beyond the scope of what reflection provides. If
    > you want to allow a user to enter functions at runtime, the simplest
    > solution would be to use an embeddable scripting language like Beanshell
    > or Rhino. Beanshell implementes an interpreted java-like language, and
    > Rhino is a Javascript interpreter written in Java. You can get
    > Beanshell at
    > http://www.beanshell.org
    > and Rhino at
    > http://www.mozilla.org/rhino
    > I think there's another one called Dynamic Java. All these make it easy
    > to access java classes from the scripting language as well should you
    > need to do so.
    >
    > Adam
    >
    >
    >
    >>
    >> It seems to me as if there might be a way to accomplish this using the
    >> Reflection class although I am not a pro at using it. I would appreciate
    >> if someone could tell me if it is indeed possible and how I can go about
    >> doing this. Thanks so much.
    >>
    >> Cheers,
    >> Serge
    >>
     
    Serge, Feb 13, 2004
    #4
  5. Serge

    Adam Jenkins Guest

    Serge wrote:
    > Thanks for the help. I have actually considered using a scripting language
    > before. Actually, I simply passed arguments to a Perl script
    > evaluated the function using eval() in Perl and passed the result back to
    > my program by means of a temp file. However, all that stuff made the
    > program rather slow. On another note, I can't really use things like Rhino
    > because I have to make all my programs work on an external computer on
    > which I don't have install rights (it has jsdk 1.4.2 though and all the
    > std unix stuff). So I am wondering if there is any way to implement what i
    > discussed without using external tools (outside the jsdk 1.4.2). Thanks in
    > advance for the help.


    I'm not sure I understand the problem. You obviously have some install
    rights since you're able to run the Java classes that you're writing.
    Beanshell and Rhino are just jar files containing some Java classes
    which implement an interpreter, which you can use from your own code.
    You can just put them wherever you're putting your own classes. You can
    even unpack the Beanshell or Rhino jar and make a single jar containing
    your classes and the Rhino or Beanshell classes.

    The performance should be a bit better than passing scripts to perl,
    since you don't have to start a separate process an communicate via temp
    files; you just call the evaluate function of whatever scripting library
    you choose. Also, Beanshell and Rhino both integrate very well with
    Java; you can access Java classes from your scripts, you can call script
    functions from Java, and the script functions can return Java objects.

    Adam
     
    Adam Jenkins, Feb 13, 2004
    #5
  6. Serge

    Serge Guest

    Oh if its just a jar file then I can surely use it! Thanks. But what about
    the precision though. When I used Perl, it gave me precision problems.
    It would work fine, for low precision, but when I looped a lot of times,
    the approximation I got for the root of f(x) would not change. I wonder if
    it's possible to use BigDecimal and BigInteger classes with Beanshell or
    Rhino? Thanks for all the help.


    On Fri, 13 Feb 2004 16:12:59 -0500, Adam Jenkins wrote:

    >
    > Serge wrote:
    >> Thanks for the help. I have actually considered using a scripting language
    >> before. Actually, I simply passed arguments to a Perl script
    >> evaluated the function using eval() in Perl and passed the result back to
    >> my program by means of a temp file. However, all that stuff made the
    >> program rather slow. On another note, I can't really use things like Rhino
    >> because I have to make all my programs work on an external computer on
    >> which I don't have install rights (it has jsdk 1.4.2 though and all the
    >> std unix stuff). So I am wondering if there is any way to implement what i
    >> discussed without using external tools (outside the jsdk 1.4.2). Thanks in
    >> advance for the help.

    >
    > I'm not sure I understand the problem. You obviously have some install
    > rights since you're able to run the Java classes that you're writing.
    > Beanshell and Rhino are just jar files containing some Java classes
    > which implement an interpreter, which you can use from your own code.
    > You can just put them wherever you're putting your own classes. You can
    > even unpack the Beanshell or Rhino jar and make a single jar containing
    > your classes and the Rhino or Beanshell classes.
    >
    > The performance should be a bit better than passing scripts to perl,
    > since you don't have to start a separate process an communicate via temp
    > files; you just call the evaluate function of whatever scripting library
    > you choose. Also, Beanshell and Rhino both integrate very well with
    > Java; you can access Java classes from your scripts, you can call script
    > functions from Java, and the script functions can return Java objects.
    >
    > Adam
     
    Serge, Feb 14, 2004
    #6
  7. Serge

    Jon A. Cruz Guest

    Serge wrote:
    > Oh if its just a jar file then I can surely use it! Thanks. But what about
    > the precision though. When I used Perl, it gave me precision problems.
    > It would work fine, for low precision, but when I looped a lot of times,
    > the approximation I got for the root of f(x) would not change. I wonder if
    > it's possible to use BigDecimal and BigInteger classes with Beanshell or
    > Rhino? Thanks for all the help.
    >


    Other alternatives include Jython and JACL.

    http://www.jython.org/

    http://tcljava.sourceforge.net/docs/website/index.html

    I've used the latter, and it was quite trivial to integrat and use.
     
    Jon A. Cruz, Feb 14, 2004
    #7
  8. Serge

    Adam Jenkins Guest

    Serge wrote:
    > Oh if its just a jar file then I can surely use it! Thanks. But what about
    > the precision though. When I used Perl, it gave me precision problems.
    > It would work fine, for low precision, but when I looped a lot of times,
    > the approximation I got for the root of f(x) would not change. I wonder if
    > it's possible to use BigDecimal and BigInteger classes with Beanshell or
    > Rhino? Thanks for all the help.


    Yup, as I said, both Rhino and Beanshell make it trivial to use any Java
    class from them. Their own number types are probably just based on Java
    numerical types as well.

    Adam
     
    Adam Jenkins, Feb 17, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Soefara
    Replies:
    2
    Views:
    6,347
    pete kirkham
    Aug 20, 2003
  2. imran
    Replies:
    1
    Views:
    18,028
    Thomas Fritsch
    Feb 1, 2005
  3. ravi
    Replies:
    3
    Views:
    730
  4. Jay Loden
    Replies:
    7
    Views:
    332
    Bruno Desthuilliers
    Jun 15, 2007
  5. Hal E. Fulton
    Replies:
    0
    Views:
    101
    Hal E. Fulton
    Aug 8, 2003
Loading...

Share This Page