This question already has an answer here:
- Check type of primitive field 3 answers
Most probably I oversee something in this trivial use case. My code iterates over annotated fields in a class and the for every field I'd like to run some code dependent on the type. The simplest is just to set a value:
field.setAccessible(true);
final Class<?> type = field.getType();
if (type.equals(Boolean.class)) {
field.set(this, Boolean.parseBoolean(property));
} else if (type.equals(Integer.class)) {
field.set(this, Integer.parseInt(property));
} else if (type.equals(String.class)) {
field.set(this, property);
} else {
LOGGER.warn("Cannot parse property -{}{}. Unknown type defined.", option.getOpt(),
field.getName());
}
However this check:
if (type.equals(Boolean.class))
dosn't work as expected e.g. for a field defined as private boolean isVerbose;
. After inspection of type
I got the property name
as just only "boolean"
where the Boolean.class
property name
was filled with "java.lang.Boolean"
. These object were different.
What would be the right implementation of this scenario?
Aucun commentaire:
Enregistrer un commentaire