Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Equivalent of eval() function in java

 
Pradyumna Hari
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Can any one tell me what is the equivalent of
eval function ( this is a function in javascript ) in java. The requirement is to evaluate the value of an expression but the expression is itself a string.
Example
ClassA a = new ClassA();
String strExpression = "a.getName()" ;

Now I want to print the value returned by a.getName(). Is there any function ftn such that
System.out.println(ftn(strExpression)) would print the value returned by a.getName()
Thanks

[ June 04, 2002: Message edited by: Pradyumna Hari ]
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're looking for reflection.
You can get a Class from an instance with

then you can have a look at the class and do things like find and call methods etc.

or something like that...
I didn't actually test the code, but it should get you started.
Dave
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic