-1
Good afternoon. I’m new to using the java Reflection API. I’m having the following problem, when trying to create a generic Dao class to be used in a CRUD for sqlite on android, I can’t cast a field for the corresponding type in the object class passed as input parameter. Follows the code:
public class DaoGenerico<E>{
    private E entrada;
    private Class classe;
    private SQLiteDatabase db;
    private List<Field> campos;
    private List<Method> metodos;
    private String banco;
    public DaoGenerico(E entrada, SQLiteDatabase db) {
        iniciaComponetes(entrada, db);
        if(!classe.isAnnotationPresent(TabelaNoBanco.class)) {
            throw new RuntimeException("Classe não pode ser persistida!");
        }
        TabelaNoBanco anotacao = (TabelaNoBanco) classe.getAnnotation(TabelaNoBanco.class);
        this.banco = anotacao.nomeTabela();
    }
    public boolean inserir() throws IllegalAccessException {
        try {
            ContentValues values = new ContentValues();
            for (Field f : campos) {
                f.setAccessible(true);
                Class<?> type = f.getType();
                /*Linha onde deve ser efetuado o cast, porem apresenta erro em type*/
                values.put(f.getName(), (type) f.get(entrada));
            }
            db.insert(banco, null, values);
            db.close();
            return true;
        }catch(ClassCastException e){
            throw new RuntimeException("Campo não pode ser convertido");
        } catch (Exception e){
            throw new RuntimeException("Não foi possivel inserir  objeto no banco de dados!");
        }
    }
    private List<Field> obterCampos(){
        return Arrays.asList(classe.getFields());
    }
    private List<Method> obterMetodos(){
        return Arrays.asList(classe.getDeclaredMethods());
    }
    private void iniciaComponetes(E entrada, SQLiteDatabase db) {
        this.entrada = entrada;
        this.db = db;
        this.classe = entrada.getClass();
        this.campos = obterCampos();
        this.metodos = obterMetodos();
    }
}
						
Puts the exact text of the error in the question
– Sorack
No error the android studio itself underlines red as error
– Markswell Menezes