Solved

Dynamic evaluation

Posted on 2006-07-07
4
729 Views
Last Modified: 2008-02-20
Java experts,

Is it possible to do dynamic evaluation in Java?

public class test
{
    public static String username;    

    public static void set(String name, String value)
    {
        eval(name + '=' + value);
    }

}



If I called:
      test.set("username","some_name");

the value "some_name" would get placed in the username String.
0
Comment
Question by:a1programmer
  • 2
4 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 17058248
You could use a Java app-enabled scripting language (try Groovy) or beanshell
0
 
LVL 12

Accepted Solution

by:
enachemc earned 250 total points
ID: 17058327
You can do that through reflection (the java.lang.Reflect package). You can dinamicaly obtain a reference to a field or method. You can assign to the field references or invoke the methods. If you want to set field values (you can use the setters) to instances of some, you would first require the instance.

Example of setting an instance's field value:

public static void set(Object instance, String name, String value)
    {
       Field f = instance.getClass().getField(name);
       //the type of your field should be String (if it's another type, you should convert from String to the required type)
       f.set(instance, value);
    }

0
 
LVL 7

Author Comment

by:a1programmer
ID: 17058621
That's exactly what I was looking for.  

Thanks!
0
 
LVL 7

Author Comment

by:a1programmer
ID: 17058626
CEHJ,
   Thank you as well for recommending those scripting language.   I had never heard of either, but will keep them at hand for the future.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
array11 challenge 16 51
computer science syllabus 3 52
pairs challenge 5 44
Java / Linux and Regular Expressions 11 43
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now