Java Questions
Java Questions

Reputation: 7953

Java - Reflection setting value at runtime

i try to set values for the field available in a java bean like the following and i want to omit the static final fields:

public Class creatObjectWithDefaultValue(String className) throws IllegalArgumentException, IllegalAccessException {
        DefaultParamValues defaultParamValues = null;
        Class objectClass = null;
        try {
            objectClass = Class.forName(className);
             Field[] fields = objectClass.getDeclaredFields();

             for(Field f:fields){
                 f.setAccessible(true);
                    //if(!f.isAccessible()){
                       // f.setAccessible(true);
                        Class<?> type = f.getType();


                        if(! Modifier.isFinal(f.getModifiers()) && type.equals(Integer.class)){
                            f.set(objectClass, defaultParamValues.INTEGER);  
                        } else if(! Modifier.isFinal(f.getModifiers()) && type.equals(BigInteger.class)){
                            f.set(objectClass, defaultParamValues.BIGINTEGER);  
                        }/*else if(! Modifier.isFinal(f.getModifiers()) && type.equals(LocalDate.class)){
                            f.set(objectClass, defaultParamValues.DATE);  
                        }*/else if(! Modifier.isFinal(f.getModifiers()) && type.equals(Boolean.class)){
                            f.set(objectClass, defaultParamValues.BOOLEAN);  
                        }else if(! Modifier.isFinal(f.getModifiers()) && type.equals(Long.class)){
                            f.set(objectClass, defaultParamValues.LONGVALUE);  
                        }
                        f.setAccessible(false);
                    //}
                        //To print the value set
                        if(! Modifier.isFinal(f.getModifiers()) ){
                             System.out.println(f.get(objectClass));
                        }

                }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return objectClass;
    }

i get the following exception when i run the program : the complete stack strace is :

Exception in thread "main" java.lang.IllegalAccessException: Class com.hexgen.tools.JsonConverter can not access a member of class com.hexgen.ro.request.CreateRequisitionRO with modifiers "private"
    at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:65)
    at java.lang.reflect.Field.doSecurityCheck(Field.java:960)
    at java.lang.reflect.Field.getFieldAccessor(Field.java:896)
    at java.lang.reflect.Field.get(Field.java:358)
    at com.hexgen.tools.JsonConverter.creatObjectWithDefaultValue(JsonConverter.java:89)
    at com.hexgen.tools.JsonConverter.main(JsonConverter.java:181)

what is the problem ? Could somebody help me to fix this?

Best Regards.

Upvotes: 0

Views: 1530

Answers (2)

Adam Arold
Adam Arold

Reputation: 30528

I think that the problem is that you are trying to set fields on a class not an instance of that class.

First you should create an instance of your objectClass and set the values of the instance!

Here:

f.set(objectClass, defaultParamValues.INTEGER);

you are passing the class object, not an instance of that class. The problem occurs when your program encounters a field which is not static, hence your Exception.

If you want to filter for static fields you can use:

java.lang.reflect.Modifier.isStatic(field.getModifiers())

Upvotes: 2

Marko Topolnik
Marko Topolnik

Reputation: 200138

You revert the accessible property of the field to false and then you go on to access its value.

Don't bother with setting accessible back to false.

Upvotes: 2

Related Questions