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
|
/*
* 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.data;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import com.vaadin.data.util.BeanUtil;
import com.vaadin.server.Setter;
import com.vaadin.shared.util.SharedUtil;
import com.vaadin.util.ReflectTools;
/**
* A {@link PropertySet} that uses reflection to find bean properties.
*
* @author Vaadin Ltd
*
* @since 8.0
*
* @param <T>
* the type of the bean
*/
public class BeanPropertySet<T> implements PropertySet<T> {
/**
* Serialized form of a property set. When deserialized, the property set
* for the corresponding bean type is requested, which either returns the
* existing cached instance or creates a new one.
*
* @see #readResolve()
* @see BeanPropertyDefinition#writeReplace()
*/
private static class SerializedPropertySet implements Serializable {
private final Class<?> beanType;
private SerializedPropertySet(Class<?> beanType) {
this.beanType = beanType;
}
private Object readResolve() {
/*
* When this instance is deserialized, it will be replaced with a
* property set for the corresponding bean type and property name.
*/
return get(beanType);
}
}
/**
* Serialized form of a property definition. When deserialized, the property
* set for the corresponding bean type is requested, which either returns
* the existing cached instance or creates a new one. The right property
* definition is then fetched from the property set.
*
* @see #readResolve()
* @see BeanPropertySet#writeReplace()
*/
private static class SerializedPropertyDefinition implements Serializable {
private final Class<?> beanType;
private final String propertyName;
private SerializedPropertyDefinition(Class<?> beanType,
String propertyName) {
this.beanType = beanType;
this.propertyName = propertyName;
}
private Object readResolve() throws IOException {
/*
* When this instance is deserialized, it will be replaced with a
* property definition for the corresponding bean type and property
* name.
*/
return get(beanType).getProperty(propertyName)
.orElseThrow(() -> new IOException(
beanType + " no longer has a property named "
+ propertyName));
}
}
private static class BeanPropertyDefinition<T, V>
implements PropertyDefinition<T, V> {
private final PropertyDescriptor descriptor;
private final BeanPropertySet<T> propertySet;
public BeanPropertyDefinition(BeanPropertySet<T> propertySet,
PropertyDescriptor descriptor) {
this.propertySet = propertySet;
this.descriptor = descriptor;
if (descriptor.getReadMethod() == null) {
throw new IllegalArgumentException(
"Bean property has no accessible getter: "
+ propertySet.beanType + "."
+ descriptor.getName());
}
}
@Override
public ValueProvider<T, V> getGetter() {
return bean -> {
Method readMethod = descriptor.getReadMethod();
Object value = invokeWrapExceptions(readMethod, bean);
return getType().cast(value);
};
}
@Override
public Optional<Setter<T, V>> getSetter() {
if (descriptor.getWriteMethod() == null) {
return Optional.empty();
}
Setter<T, V> setter = (bean, value) -> {
// Do not "optimize" this getter call,
// if its done outside the code block, that will produce
// NotSerializableException because of some lambda compilation magic
Method innerSetter = descriptor.getWriteMethod();
invokeWrapExceptions(innerSetter, bean, value);
};
return Optional.of(setter);
}
@SuppressWarnings("unchecked")
@Override
public Class<V> getType() {
return (Class<V>) ReflectTools
.convertPrimitiveType(descriptor.getPropertyType());
}
@Override
public String getName() {
return descriptor.getName();
}
@Override
public String getCaption() {
return SharedUtil.propertyIdToHumanFriendly(getName());
}
@Override
public BeanPropertySet<T> getPropertySet() {
return propertySet;
}
private Object writeReplace() {
/*
* Instead of serializing this actual property definition, only
* serialize a DTO that when deserialized will get the corresponding
* property definition from the cache.
*/
return new SerializedPropertyDefinition(getPropertySet().beanType,
getName());
}
}
private static final ConcurrentMap<Class<?>, BeanPropertySet<?>> instances = new ConcurrentHashMap<>();
private final Class<T> beanType;
private final Map<String, PropertyDefinition<T, ?>> definitions;
private BeanPropertySet(Class<T> beanType) {
this.beanType = beanType;
try {
definitions = BeanUtil.getBeanPropertyDescriptors(beanType).stream()
.filter(BeanPropertySet::hasNonObjectReadMethod)
.map(descriptor -> new BeanPropertyDefinition<>(this,
descriptor))
.collect(Collectors.toMap(PropertyDefinition::getName,
Function.identity()));
} catch (IntrospectionException e) {
throw new IllegalArgumentException(
"Cannot find property descriptors for "
+ beanType.getName(),
e);
}
}
/**
* Gets a {@link BeanPropertySet} for the given bean type.
*
* @param beanType
* the bean type to get a property set for, not <code>null</code>
* @return the bean property set, not <code>null</code>
*/
@SuppressWarnings("unchecked")
public static <T> PropertySet<T> get(Class<? extends T> beanType) {
Objects.requireNonNull(beanType, "Bean type cannot be null");
// Cache the reflection results
return (PropertySet<T>) instances.computeIfAbsent(beanType,
BeanPropertySet::new);
}
@Override
public Stream<PropertyDefinition<T, ?>> getProperties() {
return definitions.values().stream();
}
@Override
public Optional<PropertyDefinition<T, ?>> getProperty(String name) {
return Optional.ofNullable(definitions.get(name));
}
private static boolean hasNonObjectReadMethod(
PropertyDescriptor descriptor) {
Method readMethod = descriptor.getReadMethod();
return readMethod != null
&& readMethod.getDeclaringClass() != Object.class;
}
private static Object invokeWrapExceptions(Method method, Object target,
Object... parameters) {
try {
return method.invoke(target, parameters);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}
@Override
public String toString() {
return "Property set for bean " + beanType.getName();
}
private Object writeReplace() {
/*
* Instead of serializing this actual property set, only serialize a DTO
* that when deserialized will get the corresponding property set from
* the cache.
*/
return new SerializedPropertySet(beanType);
}
}
|