Illegalaccessexception when reading values with Reflection

Asked

Viewed 95 times

1

I cannot read the attributes values of the class with Reflection:

    ContentValues values = new ContentValues();

    Field[] fields = this.getClass().getDeclaredFields();

    for (Field field: fields ) {
        field.setAccessible(true);
        values.put(field.getName(), field.get(this.getClass()));
    }

I have the exception: IllegalAccessException in the get method: field.get(this.getClass()) I also tried to field.get(this) and still passing the instance of the object field.get(model)

Model object attributes are not private. If you do not use the method field.setAccessible(true); the exception is the same.

At the request of friend Ramaral, follows the whole class:

public class GenericModel {

    public Long id;

    public ContentValues GetContentValues()
    {
        ContentValues values = new ContentValues();

        Field[] fields = this.getClass().getDeclaredFields();

        for (Field field: fields ) {
            field.setAccessible(true);

            try {
                values.put(field.getName(), field.get(this.getClass()));
            } catch (IllegalAccessException e) {

            }
        }

        return values;
    }
}
  • That code doesn’t even compile.

  • I don’t know much about Reflection, but I think you need to prevent Exception with Try in dynamic methods, so compile, but do not perform accordingly. If you have suggestions with other methods or Pattern

  • Yes you have to put the Try/catch but the line values.put(field.getName(), field.get(this.getClass())); will continue to make a mistake. On its own field.get(this.getClass()) should not be an error. Edit the question and put the class attributes.

1 answer

1


There are 3 things wrong with your code:

  1. The method Field#get() returns an object, it is necessary to cast a type that Contentvalues#put() accepted and this type must be compatible with the type of the field concerned.

    (Long)field.get(...)
    
  2. To the method Field#get() the object containing the field represented by this Field and the value of which is to be obtained.

    (Long)field.get(this)
    
  3. The field id has to be initialized.

    public Long id = 3l;
    

With the changes you’ll be like this:

public class GenericModel {

    public Long id = 3l;

    public ContentValues GetContentValues()
    {
        ContentValues values = new ContentValues();

        Field[] fields = this.getClass().getDeclaredFields();

        for (Field field: fields ) {
            field.setAccessible(true);

            try {
                values.put(field.getName(), (Long)field.get(this));
            } catch (IllegalAccessException e) {
                //Faça algo aqui, nem que seja apenas um log
            }
        }

        return values;
    }
}

Note:
If Genericmodel have more fields and these are not all of the type Long you will need to change the method GetContentValues() to deal with this situation.

  • 1

    Thanks for the effort and help. It seems to work. I had to switch to Field Type.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.