[ACCEPTED]-Setting variables by name in Java-eval

Accepted answer
Score: 35

Here's how you might implement setAttribute using reflection 3 (I've renamed the function; there are different 2 reflection functions for different field 1 types):

public void setIntField(String fieldName, int value)
        throws NoSuchFieldException, IllegalAccessException {
    Field field = getClass().getDeclaredField(fieldName);
    field.setInt(this, value);
}
Score: 5

In general, you want to use Reflection. Here 12 is a good introduction to the topic with examples

In particular, the 11 "Changing Values of Fields" section 10 describes how to do what you'd like to do.

I 9 note that the author says, "This feature 8 is extremely powerful and has no equivalent 7 in other conventional languages." Of 6 course, in the last ten years (the article 5 was written in 1998) we have seen great 4 strides made in dynamic languages. The above 3 is fairly easily done in Perl, Python, PHP, Ruby, and 2 so on. I suspect this is the direction you 1 might have come from based on the "eval" tag.

Score: 4

Also, take a look at BeanUtils which can hide some 2 of the complexity of using reflection from 1 you.

Score: 3

The question is specific to ints, which 4 is helpful, however here is something a 3 bit more general. This type of method is 2 useful if you are loading in String representations 1 of field name / field value pairs.

import java.lang.reflect.Field;

public class FieldTest {

    static boolean isValid = false;
    static int count = 5;

    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
        FieldTest test = new FieldTest();
        test.setProperty("count", "24");
        System.out.println(count);
        test.setProperty("isValid", "true");
        System.out.println(isValid);
    }

    public void setProperty(String fieldName, String value) throws NoSuchFieldException, IllegalAccessException {
        Field field = this.getClass().getDeclaredField(fieldName);
        if (field.getType() == Character.TYPE) {field.set(getClass(), value.charAt(0)); return;}
        if (field.getType() == Short.TYPE) {field.set(getClass(), Short.parseShort(value)); return;}
        if (field.getType() == Integer.TYPE) {field.set(getClass(), Integer.parseInt(value)); return;}
        if (field.getType() == Long.TYPE) {field.set(getClass(), Long.parseLong(value)); return;}
        if (field.getType() == Float.TYPE) {field.set(getClass(), Float.parseFloat(value)); return;}
        if (field.getType() == Double.TYPE) {field.set(getClass(), Double.parseDouble(value)); return;}
        if (field.getType() == Byte.TYPE) {field.set(getClass(), Byte.parseByte(value)); return;}
        if (field.getType() == Boolean.TYPE) {field.set(getClass(), Boolean.parseBoolean(value)); return;}
        field.set(getClass(), value);
    }

}
Score: 1

Depending on the usage, you can use reflection 2 as advised above, or perhaps a HashMap would 1 be better suited...

More Related questions