| Author |
how to Execute a String expression in Java 1.6
|
Ramki Dirisala
Greenhorn
Joined: Mar 25, 2011
Posts: 7
|
|
Hi All,
in my project have a requirement like this.
in the above code i will parse .xml file to get the expression and i want to execute expression on User Object. There will be some complex expressions willl be there.
i want to do it in javacode (no servlets or jsp's)
how we can do this. your help is appreciated.
Thanks in Advace.
Ramki.
|
 |
Ralph Cook
Ranch Hand
Joined: May 29, 2005
Posts: 479
|
|
Well, the brute force method just has your code translate a string to an action:
etc.
Or you could use reflection to look up a method based on its name. This method can be made more generic, but it restricts your "expressions" to legal and useful method names, and is a bit more complicated. It doesn't look likely that your actions are all going to have the same signature, so you might have to represent signatures and parameters, as well as methods, somehow.
rc
|
 |
Ramki Dirisala
Greenhorn
Joined: Mar 25, 2011
Posts: 7
|
|
Hi,
Thanks for quick reply. what ever you suggested looks good, but in future if i change configuration in .xml file then again i have to go and change the java code.
java reflection is second option. actually i am looking for better solution.
anyway thanks for your suggestion
Thanks,
Ramki.
|
 |
Ralph Cook
Ranch Hand
Joined: May 29, 2005
Posts: 479
|
|
Well, you have to decide what flexibility you want. You did not say "how can I do this so I can change the xml without changing the code", in fact, you didn't say anything at all about what needed to be flexible and what didn't.
So you're looking for something "better" than reflection. Better how?
|
 |
Ramki Dirisala
Greenhorn
Joined: Mar 25, 2011
Posts: 7
|
|
|
decided to use java reflection.
|
 |
 |
|
|
subject: how to Execute a String expression in Java 1.6
|
|
|