wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Equivalent of eval() function in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Equivalent of eval() function in java" Watch "Equivalent of eval() function in java" New topic
Author

Equivalent of eval() function in java

Pradyumna Hari
Greenhorn

Joined: May 27, 2002
Posts: 15
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

Joined: Mar 06, 2001
Posts: 13459

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Equivalent of eval() function in java
 
Similar Threads
calling a method in jsp
Undefined on jQuery Ajax Request
calculate a string expression
Eval in java
Why to use eval function inside Javascript