Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

how to Execute a String expression in Java 1.6  RSS feed

 
Ramki Dirisala
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
decided to use java reflection.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!