Olá galera, to com um probleminha aqui e gostaria q vcs me ajudassem, sou novo na área de programação POO e to com umas dificuldades com reflexão. Tipo tenho o seguinte método:
public Collection<Object> select(Object o, String campo, String busca, String ordem, String direcao){
if(o == null){
throw new NullPointerException("Não foi encontrado um objeto para a inserção");
}
if(!o.getClass().isAnnotationPresent(Tabela.class)){
throw new RuntimeException("Objeto não pode ser persistido,\nnão existe a anotação para a tabela");
}
Class cls = o.getClass();
StringBuilder Sql = new StringBuilder("SELECT ");
Field[] fields = cls.getDeclaredFields();
int x = 1;
int chave = 0;
for(Field field : fields){
Annotation anot = field.getAnnotation(Coluna.class);
Annotation anotChave = field.getAnnotation(ChavePrimaria.class);
if(anotChave instanceof ChavePrimaria){
chave += 1;
}
if(chave > 1){
throw new RuntimeException("Não pode conter 2 chaves primarias");
}
if(anot instanceof Coluna){
Sql.append(field.getName()+(x==fields.length?" FROM ":", "));
}
x++;
}
for(Annotation anot : cls.getAnnotations()){
if(anot instanceof Tabela){
Tabela tabela = (Tabela)anot;
Sql.append(tabela.nome()+" WHERE ");
}
}
if(campo!=null && busca!=null){
Sql.append(campo+" like '"+busca+"%'");
}
if(ordem!=null && direcao!=null){
Sql.append(" ORDER BY "+ ordem +" "+direcao );
}
Statement stm = null;
ResultSet rs = null;
Collection<Object> retorno = null;
try {
stm = this.conn.createStatement();
rs = stm.executeQuery(Sql.toString());
while(rs.next()){
retorno.add(rs.getInt("codigo"));
retorno.add(rs.getString("nome"));
retorno.add(rs.getString("telefone"));
retorno.add(rs.getString("dataNascimento"));
}
stm.close();
} catch (SQLException e) {
e.printStackTrace();
}
return retorno;
}
esse metodo me etorna uma Collection de objetos, correto. Só que eu preciso converter essa colection na regra de negócio em uma coleção de objetos do tipo da classe q eu criei como pessoa, para que eu possa passa-la para minha página jsp.
O meu método que não estou conseguindo concluir e esse:
public Collection<Pessoa> doSelect(String campo, String busca , String ordem, String direcao){
DefaultDAO dao = new DefaultDAO();
Collection<Object> arr = dao.select(new Pessoa(), campo, busca, ordem, direcao);
/* O meu problema se encontra aqui
* em como converter essa Collection<Object>
* em uma Collection<Pessoa> para que eu possa fazer um
* laço em minha página jsp
*/
return null;
}
Se alguem puder me ajudar já agradeço, Obrigado galera
Valewwwwwwwwwwwwwww!!!