forked from ToToTec/CmdOption
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddToCollectionHandler.java
More file actions
28 lines (24 loc) · 890 Bytes
/
AddToCollectionHandler.java
File metadata and controls
28 lines (24 loc) · 890 Bytes
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
package de.tototec.cmdoption.handler;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.util.Collection;
/**
* Add an one-arg option argument to a mutable collection of strings.
*/
public class AddToCollectionHandler implements CmdOptionHandler {
public void applyParams(final Object config, final AccessibleObject element, final String[] args,
final String optionName) {
try {
final Field field = (Field) element;
@SuppressWarnings("unchecked")
final Collection<String> collection = (Collection<String>) field.get(config);
collection.add(args[0]);
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
public boolean canHandle(final AccessibleObject element, final int argCount) {
return argCount == 1 && element instanceof Field
&& Collection.class.isAssignableFrom(((Field) element).getType());
}
}