aspose file tools
The moose likes Beginning Java and the fly likes how to Execute a String expression in Java 1.6 Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Mongo DB Applied Patterns this week in the MongoDB forum
or a resume review from Five Year Itch in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "how to Execute a String expression in Java 1.6" Watch "how to Execute a String expression in Java 1.6" New topic
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.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: how to Execute a String expression in Java 1.6
 
Similar Threads
parsing data and storing in the xml
XML JAXB Serializable
Annotation in Validation
How to open a file using Java
Implementing HttpSession in the Spring MVC