Using the Reflection API to dynamically modify methods

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

  1. Serge

    Serge Guest


    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.

    Serge, Feb 13, 2004
    1. Advertisements

  2. Serge wrote:
    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, Feb 13, 2004
    1. Advertisements

  3. Serge

    Adam Jenkins Guest

    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
    and Rhino at
    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 Jenkins, Feb 13, 2004
  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.
    Serge, Feb 13, 2004
  5. Serge

    Adam Jenkins Guest

    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 Jenkins, Feb 13, 2004
  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.
    Serge, Feb 14, 2004
  7. Serge

    Jon A. Cruz Guest

    Other alternatives include Jython and JACL.

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

    Adam Jenkins Guest

    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 Jenkins, Feb 17, 2004
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.