/
LongHandler.java
66 lines (56 loc) · 2.09 KB
/
LongHandler.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package de.tototec.cmdoption.handler;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import de.tototec.cmdoption.internal.I18n;
import de.tototec.cmdoption.internal.I18n.PreparedI18n;
import de.tototec.cmdoption.internal.I18nFactory;
/**
* Apply an one-arg option to a {@link Long} (or <code>long</code>) field or
* method.
*
* @since 0.5.0
*/
public class LongHandler implements CmdOptionHandler {
public LongHandler() {}
public boolean canHandle(final AccessibleObject element, final int argCount) {
if (element instanceof Field && argCount == 1) {
final Field field = (Field) element;
return !Modifier.isFinal(field.getModifiers())
&& (field.getType().equals(Long.class) || field.getType().equals(long.class));
} else if (element instanceof Method && argCount == 1) {
final Method method = (Method) element;
if (method.getParameterTypes().length == 1) {
final Class<?> type = method.getParameterTypes()[0];
return long.class.equals(type) || Long.class.equals(type);
}
}
return false;
}
public void applyParams(final Object config, final AccessibleObject element, final String[] args,
final String optionName) throws CmdOptionHandlerException {
long parsedValue = 0;
try {
final String arg = args[0];
parsedValue = Long.parseLong(arg);
} catch (final NumberFormatException e) {
final I18n i18n = I18nFactory.getI18n(LongHandler.class);
final PreparedI18n msg = i18n.preparetr("Could not read long value \"{0}\".", args[0]);
throw new CmdOptionHandlerException(msg.notr(), e, msg.tr());
}
try {
if (element instanceof Field) {
final Field field = (Field) element;
field.set(config, parsedValue);
} else {
final Method method = (Method) element;
method.invoke(config, parsedValue);
}
} catch (final Exception e) {
final I18n i18n = I18nFactory.getI18n(LongHandler.class);
final PreparedI18n msg = i18n.preparetr("Could not apply argument \"{0}\".", args[0]);
throw new CmdOptionHandlerException(msg.notr(), e, msg.tr());
}
}
}