1

I got a problem with IllegalAccessException in my program

here's my code

    private static void setdata(Field field, Object dto, Object value) throws IllegalArgumentException, IllegalAccessException {
    boolean accessible = field.isAccessible();
    if (!accessible)
        field.setAccessible(true);
    if (value instanceof java.lang.String) {
        if (value != null) {
            value = String.valueOf(value).trim();
        }
    }
    field.set(dto, value);
    if (accessible)
        field.setAccessible(false);
}

to prevent 'IllegalAccessException' I added check logic.

boolean accessible = field.isAccessible();' if (!accessible) field.setAccessible(true);

but sometimes a IllegalAccessException is occured in my program.

the Exception raised on the line - 'field.set(dto, value);'

The Exception is as belows

java.lang.IllegalAccessException: Class com.comm.util.FileReadUtils can not access a member of class com.dto.myDto with modifiers "private"

At first, I think the 'static' is might be problem.

but as far as I know, static method do make own stack frame when it is called.

so I got nothing.

please let me know what did I do something stupid~

my program runs on Spring 3.x and java 1.6

jerr
  • 21
  • 1
  • 4
  • Since there's no SecurityException, what you're trying to do is generally possible here. Will need a little more information on what fields the Exception is raised - maybe the specific fields are declared final? – Ray Apr 24 '14 at 08:58
  • the Exception is raised on a line - 'field.set(dto, value);' java.lang.IllegalAccessException: Class com.comm.util.FlatFileReadUtils can not access a member of class com.dto.myDto with modifiers "private" – jerr Apr 24 '14 at 09:17
  • Yes, but on which `field` arguments? If the `field` is declared `final` in the original class, this line can result in exactly this exception. – Ray Apr 24 '14 at 09:19
  • that field is not declared final, but just private. and thank you for your help~~ – jerr Apr 24 '14 at 09:20
  • And the Exception is not occured everytime. – jerr Apr 24 '14 at 09:22
  • A comment to your check logic: at the end, it should say `if (!accessible) field.setAccessible(false)` to revert to the former state. – stuXnet Apr 24 '14 at 09:28

1 Answers1

0
public static void setField(Object object, String fieldName, Object fieldValue)
{
    try
    {
        Field field = object.getClass().getDeclaredField(fieldName);
        field.setAccessible(true);
        field.set(object, fieldValue);
    }
    catch(Exception exception)
    {
        // Log error
    }
}

Please try the above code and it should work(I didn't compiled this, check for syntax errors if any) How did you got the Field instance? There might be some issues in that

DreamUth
  • 122
  • 2
  • Thank you for your help~. i think my code runs same way as what you suggest. And as you wrote that the issue is in getting field instance. that field is come from a static map variable. so i think it might makes error. ooh~~ problem is I even don't know how to make error!! It occurs only one in a thounds... anyway i really appriciate for your help~~. thank you – jerr Apr 25 '14 at 07:56