Solved

Dynamic evaluation

Posted on 2006-07-07
4
741 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB Script to add site to Java Exception List 4 48
split string containing \r\n in Java 46 38
session migration servlets 2 24
expectj telnet failing 5 19
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

914 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

22 Experts available now in Live!

Get 1:1 Help Now