Skip to content

Commit ff10370

Browse files
authored
feat: record support in beanpropertyset (#19259)
1 parent 6317a21 commit ff10370

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

flow-data/src/test/java/com/vaadin/flow/data/binder/BeanPropertySetTest.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.vaadin.flow.data.binder;
1717

18+
import java.beans.PropertyDescriptor;
1819
import java.io.ByteArrayInputStream;
1920
import java.io.ByteArrayOutputStream;
2021
import java.io.ObjectInputStream;
@@ -34,6 +35,7 @@
3435
import org.junit.Test;
3536

3637
import com.vaadin.flow.function.ValueProvider;
38+
import com.vaadin.flow.internal.BeanUtil;
3739
import com.vaadin.flow.tests.data.bean.Address;
3840
import com.vaadin.flow.tests.data.bean.Country;
3941
import com.vaadin.flow.tests.data.bean.FatherAndSon;
@@ -72,6 +74,9 @@ public String toString() {
7274
}
7375
}
7476

77+
public record TestRecord(String name, int age) {
78+
}
79+
7580
interface Iface3 extends Iface2, Iface {
7681
}
7782

@@ -217,6 +222,39 @@ public void testSerializeDeserialize_nestedPropertyDefinition()
217222
address.getPostalCode(), postalCode);
218223
}
219224

225+
@Test
226+
public void testSerializeDeserializeRecord() throws Exception {
227+
PropertyDefinition<TestRecord, ?> definition = BeanPropertySet
228+
.get(TestRecord.class).getProperty("name")
229+
.orElseThrow(AssertionFailedError::new);
230+
231+
PropertyDefinition<TestRecord, ?> deserializedDefinition = ClassesSerializableUtils
232+
.serializeAndDeserialize(definition);
233+
234+
ValueProvider<TestRecord, ?> getter = deserializedDefinition
235+
.getGetter();
236+
237+
TestRecord testRecord = new TestRecord("someone", 42);
238+
239+
String name = (String) getter.apply(testRecord);
240+
241+
Assert.assertEquals("Deserialized definition should be functional",
242+
"someone", name);
243+
244+
PropertyDescriptor namePropertyDescriptor = BeanUtil
245+
.getPropertyDescriptor(TestRecord.class, "name");
246+
Assert.assertNotNull(namePropertyDescriptor);
247+
Assert.assertEquals("Property has unexpected name",
248+
namePropertyDescriptor.getName(), "name");
249+
Assert.assertEquals("Property read method has unexpected name",
250+
namePropertyDescriptor.getReadMethod().getName(), "name");
251+
252+
Class<?> namePropertyType = BeanUtil.getPropertyType(TestRecord.class,
253+
"name");
254+
Assert.assertEquals("Property type is unexpected", namePropertyType,
255+
String.class);
256+
}
257+
220258
@Test
221259
public void nestedPropertyDefinition_samePropertyNameOnMultipleLevels() {
222260
PropertyDefinition<FatherAndSon, ?> definition = BeanPropertySet

flow-server/src/main/java/com/vaadin/flow/internal/BeanUtil.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.io.Serializable;
2323
import java.lang.reflect.InvocationTargetException;
2424
import java.lang.reflect.Method;
25+
import java.lang.reflect.RecordComponent;
2526
import java.util.ArrayList;
2627
import java.util.List;
2728

@@ -42,7 +43,7 @@ private BeanUtil() {
4243
}
4344

4445
/**
45-
* Returns the property descriptors of a class or an interface.
46+
* Returns the property descriptors of a class, a record, or an interface.
4647
*
4748
* For an interface, superinterfaces are also iterated as Introspector does
4849
* not take them into account (Oracle Java bug 4275879), but in that case,
@@ -66,6 +67,17 @@ private BeanUtil() {
6667
*/
6768
public static List<PropertyDescriptor> getBeanPropertyDescriptors(
6869
final Class<?> beanType) throws IntrospectionException {
70+
71+
if (beanType.isRecord()) {
72+
List<PropertyDescriptor> propertyDescriptors = new ArrayList<>();
73+
74+
for (RecordComponent component : beanType.getRecordComponents()) {
75+
propertyDescriptors.add(new PropertyDescriptor(
76+
component.getName(), component.getAccessor(), null));
77+
}
78+
79+
return propertyDescriptors;
80+
}
6981
// Oracle bug 4275879: Introspector does not consider superinterfaces of
7082
// an interface
7183
if (beanType.isInterface()) {

0 commit comments

Comments
 (0)