-
Notifications
You must be signed in to change notification settings - Fork 22
/
ValueInjector.java
220 lines (203 loc) · 8.53 KB
/
ValueInjector.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package de.uka.ilkd.key.macros.scripts.meta;
import de.uka.ilkd.key.macros.scripts.ProofScriptCommand;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author Alexander Weigl
* @version 1 (29.03.17)
*/
public class ValueInjector {
/**
* A default instance
*
* @see #getInstance()
*/
private static ValueInjector instance;
/**
* A mapping between desired types and suitable @{@link StringConverter}.
* <p>
* Should be
*
* <pre>
* T --> StringConverter<T>
* </pre>
*/
private Map<Class, StringConverter> converters = new HashMap<>();
/**
* Injects the given {@code arguments} in the {@code obj}. For more details see
* {@link #inject(ProofScriptCommand, Object, Map)}
*
* @param command a proof script command
* @param obj a parameter class with annotation
* @param arguments a non-null map of string pairs
* @param <T> an arbitrary type
* @return the same object as {@code obj}
* @throws ArgumentRequiredException a required argument was not given in {@code arguments}
* @throws InjectionReflectionException an access on some reflection methods occurred
* @throws NoSpecifiedConverterException unknown type for the current converter map
* @throws ConversionException an converter could not translate the given value in arguments
*/
public static <T> T injection(ProofScriptCommand<?> command, T obj,
Map<String, String> arguments) throws ArgumentRequiredException,
InjectionReflectionException, NoSpecifiedConverterException, ConversionException {
return getInstance().inject(command, obj, arguments);
}
/**
* Returns the default instance of a {@link ValueInjector} Use with care. No multi-threading.
*
* @return a static reference to the default converter.
* @see #createDefault()
*/
public static ValueInjector getInstance() {
if (instance == null) {
instance = createDefault();
}
return instance;
}
/**
* Returns a fresh instance of a {@link ValueInjector} with the support for basic primitive data
* types.
*
* @return a fresh instance
*/
public static ValueInjector createDefault() {
ValueInjector vi = new ValueInjector();
vi.addConverter(Integer.class, Integer::parseInt);
vi.addConverter(Long.class, Long::parseLong);
vi.addConverter(Boolean.class, Boolean::parseBoolean);
vi.addConverter(Double.class, Double::parseDouble);
vi.addConverter(String.class, (String s) -> s);
vi.addConverter(Boolean.TYPE, Boolean::parseBoolean);
vi.addConverter(Byte.TYPE, Byte::parseByte);
vi.addConverter(Character.TYPE, s -> s.charAt(0));
vi.addConverter(Short.TYPE, Short::parseShort);
vi.addConverter(Integer.TYPE, Integer::parseInt);
vi.addConverter(Long.TYPE, Long::parseLong);
vi.addConverter(Double.TYPE, Double::parseDouble);
vi.addConverter(Float.TYPE, Float::parseFloat);
return vi;
}
/**
* Injects the converted version of the given {@code arguments} in the given {@code obj}.
*
* @param command a proof script command
* @param obj a non-null instance of a parameter class (with annotation)
* @param arguments a non-null string map
* @param <T> type safety
* @return the same object as {@code obj}
* @throws ArgumentRequiredException a required argument was not given in {@code arguments}
* @throws InjectionReflectionException an access on some reflection methods occurred
* @throws NoSpecifiedConverterException unknown type for the current converter map
* @throws ConversionException an converter could not translate the given value in arguments
* @see Option
* @see Flag
*/
public <T> T inject(ProofScriptCommand<?> command, T obj, Map<String, String> arguments)
throws ConversionException, InjectionReflectionException, NoSpecifiedConverterException,
ArgumentRequiredException {
List<ProofScriptArgument> meta =
ArgumentsLifter.inferScriptArguments(obj.getClass(), command);
List<ProofScriptArgument> varArgs = new ArrayList<>(meta.size());
List<String> usedKeys = new ArrayList<>();
for (ProofScriptArgument<?> arg : meta) {
if (arg.hasVariableArguments()) {
varArgs.add(arg);
} else {
injectIntoField(arg, arguments, obj);
usedKeys.add(arg.getName());
}
}
for (ProofScriptArgument<?> vararg : varArgs) {
final Map<String, Object> map = getStringMap(obj, vararg);
final int prefixLength = vararg.getName().length();
for (Map.Entry<String, String> e : arguments.entrySet()) {
String k = e.getKey();
String v = e.getValue();
if (!usedKeys.contains(k) && k.startsWith(vararg.getName())) {
map.put(k.substring(prefixLength), convert(vararg, v));
usedKeys.add(k);
}
}
}
return obj;
}
@SuppressWarnings("unchecked")
private Map<String, Object> getStringMap(Object obj, ProofScriptArgument<?> vararg)
throws InjectionReflectionException {
try {
Map<String, Object> map = (Map<String, Object>) vararg.getField().get(obj);
if (map == null) {
map = new HashMap<>();
vararg.getField().set(obj, map);
}
return map;
} catch (IllegalAccessException e) {
throw new InjectionReflectionException(
"Error on using reflection on class " + obj.getClass(), e, vararg);
}
}
private void injectIntoField(ProofScriptArgument<?> meta, Map<String, String> args, Object obj)
throws InjectionReflectionException, ArgumentRequiredException, ConversionException,
NoSpecifiedConverterException {
final String val = args.get(meta.getName());
if (val == null) {
if (meta.isRequired()) {
throw new ArgumentRequiredException(String.format(
"Argument %s:%s is required, but %s was given. " + "For comamnd class: '%s'",
meta.getName(), meta.getField().getType(), val, meta.getCommand().getClass()),
meta);
}
} else {
Object value = convert(meta, val);
try {
// if (meta.getType() != value.getClass())
// throw new ConversionException("The typed returned '" + val.getClass()
// + "' from the converter mismatched with the
// type of the field " + meta.getType(), meta);
// FIXME: I had to add this, otherwise I would receive an illegal access exception.
meta.getField().setAccessible(true);
meta.getField().set(obj, value);
} catch (IllegalAccessException e) {
throw new InjectionReflectionException("Could not inject values via reflection", e,
meta);
}
}
}
private Object convert(ProofScriptArgument<?> meta, String val)
throws NoSpecifiedConverterException, ConversionException {
StringConverter<?> converter = getConverter(meta.getType());
if (converter == null) {
throw new NoSpecifiedConverterException(
"No converter registered for class: " + meta.getField().getType(), meta);
}
try {
return converter.convert(val);
} catch (Exception e) {
throw new ConversionException(String.format("Could not convert value %s to type %s",
val, meta.getField().getType().getName()), e, meta);
}
}
/**
* Registers the given converter for the specified class.
*
* @param clazz a class
* @param conv a converter for the given class
* @param <T> an arbitrary type
*/
public <T> void addConverter(Class<T> clazz, StringConverter<T> conv) {
converters.put(clazz, conv);
}
/**
* Finds a converter for the given class.
*
* @param clazz a non-null class
* @param <T> an arbitrary type
* @return null or a suitable converter (registered) converter for the requested class.
*/
@SuppressWarnings("unchecked")
public <T> StringConverter<T> getConverter(Class<T> clazz) {
return (StringConverter<T>) converters.get(clazz);
}
}