forked from ToToTec/CmdOption
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPutIntoMapHandler.java
37 lines (31 loc) · 1.26 KB
/
PutIntoMapHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package de.tototec.cmdoption.handler;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Map;
import de.tototec.cmdoption.internal.I18n;
import de.tototec.cmdoption.internal.I18n.PreparedI18n;
import de.tototec.cmdoption.internal.I18nFactory;
/**
* Apply an two-arg option to an mutable {@link Map}.
*
*/
public class PutIntoMapHandler implements CmdOptionHandler {
public void applyParams(final Object config, final AccessibleObject element, final String[] args,
final String optionName) throws CmdOptionHandlerException {
try {
final Field field = (Field) element;
@SuppressWarnings("unchecked")
final Map<String, String> map = (Map<String, String>) field.get(config);
map.put(args[0], args[1]);
} catch (final Exception e) {
final I18n i18n = I18nFactory.getI18n(PutIntoMapHandler.class);
final PreparedI18n msg = i18n.preparetr("Could not apply parameters {0} to field {1}",
Arrays.toString(args), element);
throw new CmdOptionHandlerException(msg.notr(), e, msg.tr());
}
}
public boolean canHandle(final AccessibleObject element, final int argCount) {
return argCount == 2 && element instanceof Field && Map.class.isAssignableFrom(((Field) element).getType());
}
}