vendredi 22 mai 2015

Java Reflection. Access the value of a field

I am trying to complete a program with Java Reflection. I have a problem with this part. I can not get the value of the field I am looking for.

Field [] fx = ArrayUtils.addAll(c.getDeclaredFields(),c.getFields());
for (int j = 0; i < fx.length; j++){
    System.out.println(fx[j].toString());
    if( fx[j].isAnnotationPresent(Searchable.class)){
        Searchable ann = fx[j].getAnnotation(Searchable.class);
        System.out.println(ann.field() + " " + fx[j].getGenericType());
        if (ann.field().equals(field)){
    System.out.println ("Found it!");
    try {
        fx[j].setAccessible(true);
        System.out.println((String)fx[j].get(c));
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    break;
        }
    }
}

With this code I get this message:

java.lang.IllegalArgumentException: Can not set java.lang.String field store.model.Book.publisher to java.lang.Class

Any idea how I can do it? Thanks a lot in advance.





Aucun commentaire:

Enregistrer un commentaire