/
TemplateModelBeanUtil.java
293 lines (258 loc) · 11.2 KB
/
TemplateModelBeanUtil.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
* Copyright 2000-2016 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.hummingbird.template.model;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.Objects;
import java.util.StringTokenizer;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import com.vaadin.hummingbird.StateNode;
import com.vaadin.hummingbird.nodefeature.ModelMap;
import com.vaadin.util.ReflectTools;
/**
* Utility class for mapping Bean values to {@link TemplateModel} values.
*
* @author Vaadin Ltd
*/
public class TemplateModelBeanUtil {
private static final Class<?>[] SUPPORTED_PROPERTY_TYPES = new Class[] {
Boolean.class, Double.class, Integer.class, String.class };
/**
* Internal implementation of Pair / Tuple that encapsulates a value and the
* method that it was retrieved from.
*/
private static final class ModelPropertyWrapper {
private String propertyName;
private Type propertyType;
private Object value;
public ModelPropertyWrapper(String propertyName, Type propertyType,
Object value) {
this.propertyName = propertyName;
this.propertyType = propertyType;
this.value = value;
}
}
private TemplateModelBeanUtil() {
// NOOP
}
static void importBeanIntoModel(Supplier<StateNode> stateNodeSupplier,
Object bean, String pathPrefix, Predicate<String> filter) {
assert pathPrefix != null
&& (pathPrefix.isEmpty() || pathPrefix.endsWith("."));
if (bean == null) {
throw new IllegalArgumentException("Bean cannot be null");
}
Method[] getterMethods = ReflectTools.getGetterMethods(bean.getClass())
.toArray(Method[]::new);
if (getterMethods.length == 0) {
throw new IllegalArgumentException("Given object of type "
+ bean.getClass().getName()
+ " is not a Bean - it has no public getter methods!");
}
Predicate<Method> methodBasedFilter = method -> filter
.test(pathPrefix + ReflectTools.getPropertyName(method));
Stream<ModelPropertyWrapper> values = Stream.of(getterMethods)
.filter(methodBasedFilter)
.map(method -> mapBeanValueToProperty(method, bean));
// don't resolve the state node used until all the bean values have been
// resolved properly
ModelMap modelMap = stateNodeSupplier.get().getFeature(ModelMap.class);
values.forEach(wrapper -> setModelValue(wrapper, modelMap, pathPrefix,
filter));
}
private static void setModelValue(ModelPropertyWrapper modelPropertyWrapper,
ModelMap targetModelMap, String pathPrefix,
Predicate<String> filter) {
setModelValue(targetModelMap, modelPropertyWrapper.propertyName,
modelPropertyWrapper.propertyType, modelPropertyWrapper.value,
pathPrefix, filter);
}
static void setModelValue(ModelMap modelMap, String propertyName,
Type expectedType, Object value, String pathPrefix,
Predicate<String> filter) {
Object oldValue = modelMap.getValue(propertyName);
// this might cause scenario where invalid type is not
// caught because
// both values are null
if (Objects.equals(value, oldValue)) {
return;
}
if (expectedType instanceof Class<?>) {
setModelValueBasicType(modelMap, propertyName,
(Class<?>) expectedType, value, pathPrefix, filter);
return;
}
throw createUnsupportedTypeException(expectedType, propertyName);
}
static Object getProxy(StateNode stateNode, Object[] args) {
assert args.length == 2;
if (args[0] == null || args[1] == null) {
throw new IllegalArgumentException(
"Method getProxy() may not accept null arguments");
}
assert args[0] instanceof String;
assert args[1] instanceof Class<?>;
String modelPath = (String) args[0];
Class<?> beanClass = (Class<?>) args[1];
if (modelPath.isEmpty()) {
return TemplateModelProxyHandler.createModelProxy(stateNode,
beanClass);
}
StateNode node = stateNode;
StringTokenizer tokenizer = new StringTokenizer(modelPath, ".");
String last = null;
while (tokenizer.hasMoreTokens()) {
String name = tokenizer.nextToken();
node = resolveStateNode(node, name);
last = name;
}
return getModelValue(node.getParent().getFeature(ModelMap.class), last,
beanClass);
}
private static void setModelValueBasicType(ModelMap modelMap,
String propertyName, Class<?> expectedType, Object value,
String pathPrefix, Predicate<String> filter) {
if (isSupportedNonPrimitiveType(expectedType)) {
modelMap.setValue(propertyName, (Serializable) value);
return;
}
// primitives have different "default" values than their boxed
// versions, e.g. boolean is false and Boolean is null
if (expectedType.isPrimitive()) {
if (isSupportedPrimitiveType(expectedType)) {
modelMap.setValue(propertyName, (Serializable) value);
return;
}
// not supported primitive, throw exception for now
throw createUnsupportedTypeException(expectedType, propertyName);
} else {
// handle other types as beans
String newPathPrefix = pathPrefix + propertyName + ".";
importBeanIntoModel(
() -> resolveStateNode(modelMap.getNode(), propertyName),
value, newPathPrefix, filter);
return;
}
}
static Object getModelValue(ModelMap modelMap, String propertyName,
Type returnType) {
Object value = modelMap.getValue(propertyName);
if (returnType instanceof Class<?>) {
return getModelValueBasicType(value, propertyName,
(Class<?>) returnType);
}
throw createUnsupportedTypeException(returnType, propertyName);
}
private static Object getModelValueBasicType(Object value,
String propertyName, Class<?> returnClazz) {
if (isSupportedNonPrimitiveType(returnClazz)) {
return value;
}
// primitives have different "default" values than their boxed
// versions, e.g. boolean is false and Boolean is null
if (returnClazz.isPrimitive()
&& isSupportedPrimitiveType(returnClazz)) {
return value != null ? value
: getPrimitiveDefaultValue(returnClazz);
} else if (!returnClazz.isPrimitive() && value instanceof StateNode) {
return TemplateModelProxyHandler.createModelProxy((StateNode) value,
returnClazz);
}
throw createUnsupportedTypeException(returnClazz, propertyName);
}
private static ModelPropertyWrapper mapBeanValueToProperty(
Method getterMethod, Object bean) {
try {
Object value = getterMethod.invoke(bean, (Object[]) null);
return new ModelPropertyWrapper(
ReflectTools.getPropertyName(getterMethod),
getterMethod.getGenericReturnType(), value);
} catch (IllegalAccessException e) {
throw new IllegalArgumentException("Given method was not accesible "
+ bean.getClass().getName() + "::" + getterMethod.getName(),
e);
} catch (InvocationTargetException e) {
throw new IllegalStateException(
"Exception thrown while reading bean value from "
+ bean.getClass().getName() + "::"
+ getterMethod.getName()
+ ", getters should not throw exceptions.",
e);
}
}
private static StateNode resolveStateNode(StateNode parentNode,
String childNodePath) {
ModelMap parentLevel = parentNode.getFeature(ModelMap.class);
if (parentLevel.hasValue(childNodePath)) {
Serializable value = parentLevel.getValue(childNodePath);
if (value instanceof StateNode
&& ((StateNode) value).hasFeature(ModelMap.class)) {
// reuse old one
return (StateNode) value;
} else {
// just override
return createModelMap(parentLevel, childNodePath);
}
} else {
return createModelMap(parentLevel, childNodePath);
}
}
private static StateNode createModelMap(ModelMap parent,
String propertyName) {
StateNode node = new StateNode(ModelMap.class);
parent.setValue(propertyName, node);
return node;
}
private static String getSupportedTypesString() {
return Stream.of(SUPPORTED_PROPERTY_TYPES).map(Class::getName)
.collect(Collectors.joining(", "))
+ " (and corresponding primitive types)";
}
private static boolean isSupportedNonPrimitiveType(Class<?> clazz) {
return Stream.of(SUPPORTED_PROPERTY_TYPES)
.anyMatch(type -> type.isAssignableFrom(clazz));
}
private static boolean isSupportedPrimitiveType(Class<?> primitiveType) {
return isSupportedNonPrimitiveType(
ReflectTools.convertPrimitiveType(primitiveType));
}
private static UnsupportedOperationException createUnsupportedTypeException(
Type type, String propertyName) {
return new UnsupportedOperationException(
"Template model does not support type " + type.getTypeName()
+ " (" + propertyName + "), supported types are:"
+ getSupportedTypesString());
}
private static Object getPrimitiveDefaultValue(Class<?> primitiveType) {
if (primitiveType == int.class) {
return Integer.valueOf(0);
} else if (primitiveType == double.class) {
return Double.valueOf(0);
} else if (primitiveType == boolean.class) {
return false;
}
throw new UnsupportedOperationException(
"Template model does not support primitive type "
+ primitiveType.getName()
+ ", all supported types are: "
+ getSupportedTypesString());
}
}