0

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.

2
  • JsonConverter.java:89 <-- which line is this? Commented Apr 29, 2013 at 12:23
  • f.setAccessible(false); Commented Apr 29, 2013 at 12:27

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks Marko, still i get this 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"
I think that this isn't the problem here. Check my answer.
Listen to Adam's advice, he's got it right. You aren't creating an instance of the class, so your current code only works for static fields.
@AdamArold It is not true that this isn't the problem: it just isn't the only problem.
I see, you are right. We are facing multiple problems here. But hey, it is reflection...
2

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())

5 Comments

i created new instance like the following but still i get the same objectClass.newInstance();
Can you update your code please? With something indicating on which line you get the Exception.
` if(! Modifier.isFinal(f.getModifiers()) ){ System.out.println(f.get(objectClass)); }` is the place where i get exception
The same exception as before?
Can you launch your debugger and figure it out that on which method call does it throw the exception?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.