• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Equivalent of eval() function in java

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic