dimanche 5 juin 2016

Illegal access exception when using reflection

On all the field.set lines of the static method I get an illegal access exception, why is that? As far as I can tell I am using them appropriately.

public class Definition {

    public int id;
    public String regex;
    private String from;
    private boolean unreadOnly;
    private boolean unrepliedOnly;
    private int minutesPast;
    private List<Action> actions;

    public Definition(){

    }
    public static List<Definition> fromCursor(Cursor cursor){
        Field fields[] = Definition.class.getFields();
        List<Definition> results = new ArrayList<Definition>();
        cursor.moveToPosition(-1);
        while(cursor.moveToNext()) {
            Definition d = new Definition();
            for(Field field : fields){
                if(field.getName().startsWith("_")) {
                    continue;
                } else {
                    int col = cursor.getColumnIndexOrThrow(field.getName());
                    int type = cursor.getType(col);
                    switch (type) {
                        case Cursor.FIELD_TYPE_NULL:
                            field.set(d,null);
                            break;
                        case Cursor.FIELD_TYPE_INTEGER:
                            field.setInt(d,cursor.getInt(col));
                            break;
                        case Cursor.FIELD_TYPE_FLOAT:
                            field.setFloat(d, cursor.getFloat(col));
                            break;
                        case Cursor.FIELD_TYPE_STRING:
                            field.set(d, cursor.getString(col));
                            break;
                        case Cursor.FIELD_TYPE_BLOB:
                            field.setByte(d, cursor.getBlob(col));
                            break;
                    }
                }

            }
        }
        return results;
    }


}





Aucun commentaire:

Enregistrer un commentaire