lundi 22 mai 2017

Problems with Reflection JAVA

Good Morning. I'm getting the values from the fields of a class by reflection. I'm getting the object by parameter, but when I show the value I got, it comes in blank. I'm getting the attribute, but not the value. From what I noticed, it looks like it is not catching the instance of the object that was passed. Thanks for your help.

public static void transformarCamposCaixaAlta(BaseModel bm) {
    Class<?> classe = bm.getClass();
    for(Field item : classe.getDeclaredFields()) {
        item.setAccessible(true);

        if (item.getType() == BaseModel.class) {
            try {
                transformarCamposCaixaAlta((BaseModel) item.get(bm));
            } catch (IllegalArgumentException | IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        if (item.isAnnotationPresent(FieldUpperCase.class)) {
            try {
                if(item.get(bm) != null) {

                    System.out.println("Valor: "+ item.get(bm).toString() + " Tipo: "+ item.getName());
                    System.out.println("------");
                    item.set(bm, ((String)item.get(bm)).toUpperCase());
                }
            } catch (IllegalArgumentException | IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }
}





Aucun commentaire:

Enregistrer un commentaire