Changeset 16645 in josm


Ignore:
Timestamp:
2020-06-14T22:59:08+02:00 (4 years ago)
Author:
simon04
Message:

StructUtils: use StringParser

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/StructUtils.java

    r16589 r16645  
    3535import org.openstreetmap.josm.tools.MultiMap;
    3636import org.openstreetmap.josm.tools.ReflectionUtils;
     37import org.openstreetmap.josm.tools.StringParser;
    3738import org.openstreetmap.josm.tools.Utils;
    3839
     
    4546 */
    4647public final class StructUtils {
     48
     49    private static final StringParser STRING_PARSER = new StringParser(StringParser.DEFAULT)
     50            .registerParser(Map.class, StructUtils::mapFromJson)
     51            .registerParser(MultiMap.class, StructUtils::multiMapFromJson);
    4752
    4853    private StructUtils() {
     
    221226        }
    222227        for (Map.Entry<String, String> keyValue : hash.entrySet()) {
    223             Object value;
    224228            Field f = getDeclaredFieldInClassOrSuperTypes(klass, keyValue.getKey().replace('-', '_'));
    225229
     
    228232            }
    229233            ReflectionUtils.setObjectsAccessible(f);
    230             if (f.getType() == Boolean.class || f.getType() == boolean.class) {
    231                 value = Boolean.valueOf(keyValue.getValue());
    232             } else if (f.getType() == Integer.class || f.getType() == int.class) {
    233                 try {
    234                     value = Integer.valueOf(keyValue.getValue());
    235                 } catch (NumberFormatException nfe) {
    236                     continue;
    237                 }
    238             } else if (f.getType() == Double.class || f.getType() == double.class) {
    239                 try {
    240                     value = Double.valueOf(keyValue.getValue());
    241                 } catch (NumberFormatException nfe) {
    242                     continue;
    243                 }
    244             } else if (f.getType() == String.class) {
    245                 value = keyValue.getValue();
    246             } else if (f.getType().isAssignableFrom(Map.class)) {
    247                 value = mapFromJson(keyValue.getValue());
    248             } else if (f.getType().isAssignableFrom(MultiMap.class)) {
    249                 value = multiMapFromJson(keyValue.getValue());
    250             } else
    251                 throw new JosmRuntimeException("unsupported preference primitive type");
    252 
     234            Object value = STRING_PARSER.parse(f.getType(), keyValue.getValue());
    253235            try {
    254236                f.set(struct, value);
Note: See TracChangeset for help on using the changeset viewer.