1 | |
package com.lexicalscope.jewel.cli; |
2 | |
|
3 | |
import static com.lexicalscope.fluentreflection.FluentReflection.type; |
4 | |
import static com.lexicalscope.fluentreflection.ReflectionMatchers.*; |
5 | |
|
6 | |
import java.util.Collection; |
7 | |
import java.util.List; |
8 | |
import java.util.Set; |
9 | |
|
10 | |
import com.lexicalscope.fluentreflection.FluentClass; |
11 | |
import com.lexicalscope.fluentreflection.FluentMethod; |
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
abstract class AbstractOptionAdapter implements OptionAdapter { |
30 | |
private final FluentClass<?> klass; |
31 | |
protected final FluentMethod method; |
32 | |
private final FluentClass<?> methodType; |
33 | |
|
34 | |
AbstractOptionAdapter( |
35 | |
final FluentClass<?> klass, |
36 | 574 | final FluentMethod method) { |
37 | 574 | this.klass = klass; |
38 | 574 | this.method = method; |
39 | 574 | if (isMutator().matches(method)) |
40 | |
{ |
41 | 50 | this.methodType = method.args().get(0); |
42 | |
} |
43 | |
else |
44 | |
{ |
45 | 524 | this.methodType = method.type(); |
46 | |
} |
47 | 574 | } |
48 | |
|
49 | |
@Override public final FluentMethod method() { |
50 | 2166 | return method; |
51 | |
} |
52 | |
|
53 | |
@Override public final FluentMethod correspondingOptionalityMethod() { |
54 | 1596 | if (isMutator().matches(method)) |
55 | |
{ |
56 | 176 | return null; |
57 | |
} |
58 | |
|
59 | 1420 | final List<FluentMethod> methods = |
60 | |
klass.methods( |
61 | |
hasName(addPrefix("is", method.property())).and(isExistence())); |
62 | 1420 | if (!methods.isEmpty()) { |
63 | 408 | return methods.get(0); |
64 | |
} |
65 | 1012 | return null; |
66 | |
} |
67 | |
|
68 | |
private String addPrefix(final String prefix, final String name) { |
69 | 1420 | return prefix + name.substring(0, 1).toUpperCase() + name.substring(1); |
70 | |
} |
71 | |
|
72 | |
@Override public final boolean isMultiValued() { |
73 | 4026 | return methodType.isType(reflectingOn(Collection.class)) && (methodType.assignableFrom(List.class) || methodType.assignableFrom(Set.class)); |
74 | |
} |
75 | |
|
76 | |
@Override public final FluentClass<? extends Object> getValueType() { |
77 | 2890 | final FluentClass<? extends Object> valueType = |
78 | |
isMultiValued() |
79 | |
? methodType.asType(reflectingOn(Collection.class)).typeArgument(0) |
80 | |
: methodType; |
81 | |
|
82 | 2890 | return reflectingOn(Object.class).matches(valueType) |
83 | |
? type(String.class) |
84 | |
: valueType; |
85 | |
} |
86 | |
|
87 | |
@Override public final boolean hasDefaultValue() { |
88 | 574 | return !(defaultValue().length == 1 && defaultValue()[0].equals(Option.stringToMarkNoDefault)); |
89 | |
} |
90 | |
} |