mardi 16 avril 2019

Exception on retrieving an Integer field value using Reflection

I am on JDK 1.8 and trying to retrieve the value of a field, which is marked as Integer and not int.

I can't change Data Type to int because this class is used by Lucene and an OR/M, so I can only use Reflection to play with it

private Integer fee;

public Integer getFee() {
    return fee;
}

public void setFee(Integer fee) {
    this.fee = fee;
}

When I do the following:

Field field = myClass.getClass().getDeclaredField("fee");
field.setAccessible(true);
Integer fee = field.getInt("fee");

I get this exception:

java.lang.IllegalArgumentException: 
Attempt to get java.lang.Integer field "com.xyz.models.MyModel.fee" 
with illegal data type conversion to int





Aucun commentaire:

Enregistrer un commentaire