My program will serialize an instance of a class called Configuration. First, take keys values of the atributes from a configuration txt file like:
SECONDS=60
NAME=JINGGLE
LIFE=true
So, before serialize, I must to take this key values and translate to the instance:
for(Entry<String, String> entry : attributes.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
Field field = c.getDeclaredField(key);
field.setAccessible(true);
try {
//Get the data type and transform the string to this type
Class<?> type = field.getType();
Object atribute ;
atribute = type.getConstructor(String.class).newInstance(value);
field.set(o, atribute);
This works, for example, with Strings and Integers, but no for primitive values or booleans... How can I do for instance a boolean using reflection and getting the valor with a string like true or false? Or how do it with primitive values int.
Aucun commentaire:
Enregistrer un commentaire