Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

how to Execute a String expression in Java 1.6

 
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.
 
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.
 
Don't MAKE me come back there with this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic