From 17b01a9e0e321cbf919b2fd8c240dadc70a3bb05 Mon Sep 17 00:00:00 2001 From: "totally-not-ai[bot]" <290682512+totally-not-ai[bot]@users.noreply.github.com> Date: Wed, 25 Nov 2026 13:07:39 +0000 Subject: [PATCH 1/8] feat: let Binder read and write non-public bean classes Fixes #27367 --- .../flow/data/binder/BeanPropertySet.java | 12 +++- .../com/vaadin/flow/data/binder/Binder.java | 9 ++- .../flow/data/binder/BeanPropertySetTest.java | 20 ++++++ .../vaadin/flow/data/binder/BinderTest.java | 16 +++++ .../flow/tests/data/bean/NonPublicBeans.java | 65 +++++++++++++++++++ 5 files changed, 119 insertions(+), 3 deletions(-) create mode 100644 flow-data/src/test/java/com/vaadin/flow/tests/data/bean/NonPublicBeans.java diff --git a/flow-data/src/main/java/com/vaadin/flow/data/binder/BeanPropertySet.java b/flow-data/src/main/java/com/vaadin/flow/data/binder/BeanPropertySet.java index 669db995960..5babeb78145 100644 --- a/flow-data/src/main/java/com/vaadin/flow/data/binder/BeanPropertySet.java +++ b/flow-data/src/main/java/com/vaadin/flow/data/binder/BeanPropertySet.java @@ -548,10 +548,20 @@ private static boolean hasNonObjectReadMethod( && readMethod.getDeclaringClass() != Object.class; } + // S3011: bean types do not need to be public, e.g. a data class nested + // in a view, so their public accessors are not accessible as such + @SuppressWarnings("java:S3011") private static Object invokeWrapExceptions(Method method, Object target, Object... parameters) { try { - return method.invoke(target, parameters); + try { + return method.invoke(target, parameters); + } catch (IllegalAccessException e) { + if (!method.trySetAccessible()) { + throw e; + } + return method.invoke(target, parameters); + } } catch (IllegalAccessException | InvocationTargetException e) { throw new RuntimeException(e); } diff --git a/flow-data/src/main/java/com/vaadin/flow/data/binder/Binder.java b/flow-data/src/main/java/com/vaadin/flow/data/binder/Binder.java index 4e12098f3ef..959cf5886a6 100644 --- a/flow-data/src/main/java/com/vaadin/flow/data/binder/Binder.java +++ b/flow-data/src/main/java/com/vaadin/flow/data/binder/Binder.java @@ -16,6 +16,7 @@ package com.vaadin.flow.data.binder; import java.io.Serializable; +import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.RecordComponent; @@ -3089,8 +3090,12 @@ public BEAN writeRecord() throws ValidationException { values.forEach(value -> value.ifOk(convertedValues::add)); try { - record = beanType.cast(beanType.getDeclaredConstructors()[0] - .newInstance(convertedValues.toArray())); + Constructor constructor = beanType + .getDeclaredConstructors()[0]; + // the record type does not need to be public + constructor.trySetAccessible(); + record = beanType.cast( + constructor.newInstance(convertedValues.toArray())); } catch (Exception e) { throw ReflectTools.convertInstantiationException(e, beanType); } diff --git a/flow-data/src/test/java/com/vaadin/flow/data/binder/BeanPropertySetTest.java b/flow-data/src/test/java/com/vaadin/flow/data/binder/BeanPropertySetTest.java index 3c75ac17913..bf92c1098ab 100644 --- a/flow-data/src/test/java/com/vaadin/flow/data/binder/BeanPropertySetTest.java +++ b/flow-data/src/test/java/com/vaadin/flow/data/binder/BeanPropertySetTest.java @@ -41,6 +41,7 @@ import com.vaadin.flow.tests.data.bean.Address; import com.vaadin.flow.tests.data.bean.Country; import com.vaadin.flow.tests.data.bean.FatherAndSon; +import com.vaadin.flow.tests.data.bean.NonPublicBeans; import com.vaadin.flow.tests.data.bean.Sex; import com.vaadin.flow.tests.server.ClassesSerializableUtils; @@ -550,4 +551,23 @@ void includesDefaultMethodsFromInterfaces() { assertEquals(String.class, defs.get(3).getType()); } + + @Test + @SuppressWarnings({ "unchecked", "rawtypes" }) + void nonPublicBeanClass_getterAndSetterWork() { + Object bean = NonPublicBeans.newBeanWithChild(); + PropertySet set = BeanPropertySet + .get((Class) NonPublicBeans.BEAN_TYPE); + + PropertyDefinition name = (PropertyDefinition) set + .getProperty("name").get(); + name.getSetter().get().accept(bean, "parent"); + assertEquals("parent", name.getGetter().apply(bean)); + + PropertyDefinition childName = (PropertyDefinition) set + .getProperty("child.name").get(); + childName.getSetter().get().accept(bean, "child"); + assertEquals("child", childName.getGetter().apply(bean)); + assertEquals("parent", name.getGetter().apply(bean)); + } } diff --git a/flow-data/src/test/java/com/vaadin/flow/data/binder/BinderTest.java b/flow-data/src/test/java/com/vaadin/flow/data/binder/BinderTest.java index fc741f002ca..1fbfb6f80c7 100644 --- a/flow-data/src/test/java/com/vaadin/flow/data/binder/BinderTest.java +++ b/flow-data/src/test/java/com/vaadin/flow/data/binder/BinderTest.java @@ -51,6 +51,7 @@ import com.vaadin.flow.data.validator.NotEmptyValidator; import com.vaadin.flow.data.validator.StringLengthValidator; import com.vaadin.flow.internal.CurrentInstance; +import com.vaadin.flow.tests.data.bean.NonPublicBeans; import com.vaadin.flow.tests.data.bean.Person; import com.vaadin.flow.tests.data.bean.Sex; @@ -2975,6 +2976,21 @@ void readRecord_writeRecord() throws ValidationException { assertThrows(ValidationException.class, () -> binder.writeRecord()); } + @Test + @SuppressWarnings({ "unchecked", "rawtypes" }) + void readRecord_writeRecord_nonPublicRecord() throws ValidationException { + Binder binder = new Binder<>( + (Class) NonPublicBeans.RECORD_TYPE); + TestTextField nameField = new TestTextField(); + binder.forField(nameField).bind("name"); + + binder.readBean(NonPublicBeans.newRecord("test")); + assertEquals("test", nameField.getValue()); + + nameField.setValue("foo"); + assertEquals(NonPublicBeans.newRecord("foo"), binder.writeRecord()); + } + private TestTextField createNullRejectingFieldWithEmptyValue( String emptyValue) { return new TestTextField() { diff --git a/flow-data/src/test/java/com/vaadin/flow/tests/data/bean/NonPublicBeans.java b/flow-data/src/test/java/com/vaadin/flow/tests/data/bean/NonPublicBeans.java new file mode 100644 index 00000000000..38f409be8be --- /dev/null +++ b/flow-data/src/test/java/com/vaadin/flow/tests/data/bean/NonPublicBeans.java @@ -0,0 +1,65 @@ +/* + * Copyright 2000-2026 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.flow.tests.data.bean; + +/** + * Bean types that are not public, declared outside the binder package so that + * reflective access from the binder classes is checked against the class + * visibility. + */ +public final class NonPublicBeans { + + public static final Class BEAN_TYPE = Bean.class; + + public static final Class RECORD_TYPE = Record.class; + + private NonPublicBeans() { + } + + public static Object newBeanWithChild() { + Bean bean = new Bean(); + bean.setChild(new Bean()); + return bean; + } + + public static Object newRecord(String name) { + return new Record(name); + } + + static class Bean { + private String name; + private Bean child; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Bean getChild() { + return child; + } + + public void setChild(Bean child) { + this.child = child; + } + } + + record Record(String name) { + } +} From 7abb18a1ad185ee986066832b3aa12f5472e5e60 Mon Sep 17 00:00:00 2001 From: "totally-not-ai[bot]" <290682512+totally-not-ai[bot]@users.noreply.github.com> Date: Wed, 25 Nov 2026 13:09:02 +0000 Subject: [PATCH 2/8] feat: explain how to open package when bean accessor stays inaccessible --- .../com/vaadin/flow/data/binder/BeanPropertySet.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/flow-data/src/main/java/com/vaadin/flow/data/binder/BeanPropertySet.java b/flow-data/src/main/java/com/vaadin/flow/data/binder/BeanPropertySet.java index 5babeb78145..7deec7e9124 100644 --- a/flow-data/src/main/java/com/vaadin/flow/data/binder/BeanPropertySet.java +++ b/flow-data/src/main/java/com/vaadin/flow/data/binder/BeanPropertySet.java @@ -558,7 +558,14 @@ private static Object invokeWrapExceptions(Method method, Object target, return method.invoke(target, parameters); } catch (IllegalAccessException e) { if (!method.trySetAccessible()) { - throw e; + Class type = method.getDeclaringClass(); + throw new IllegalStateException(String.format( + "Unable to access '%s' of '%s'. Its module does " + + "not open the package '%s' to Vaadin. " + + "Make the class public, or open the " + + "package in module-info.java.", + method.getName(), type.getName(), + type.getPackageName()), e); } return method.invoke(target, parameters); } From 32b2d88f546b0ca74e7367960750004f9719b8ed Mon Sep 17 00:00:00 2001 From: "totally-not-ai[bot]" <290682512+totally-not-ai[bot]@users.noreply.github.com> Date: Wed, 25 Nov 2026 13:41:55 +0000 Subject: [PATCH 3/8] refactor: move accessor access fallback out of nested try Also cover the error raised when the package is not opened. --- .../flow/data/binder/BeanPropertySet.java | 42 +++++++++++-------- .../flow/data/binder/BeanPropertySetTest.java | 17 ++++++++ 2 files changed, 41 insertions(+), 18 deletions(-) diff --git a/flow-data/src/main/java/com/vaadin/flow/data/binder/BeanPropertySet.java b/flow-data/src/main/java/com/vaadin/flow/data/binder/BeanPropertySet.java index 7deec7e9124..ba82b203eb9 100644 --- a/flow-data/src/main/java/com/vaadin/flow/data/binder/BeanPropertySet.java +++ b/flow-data/src/main/java/com/vaadin/flow/data/binder/BeanPropertySet.java @@ -548,32 +548,38 @@ private static boolean hasNonObjectReadMethod( && readMethod.getDeclaringClass() != Object.class; } - // S3011: bean types do not need to be public, e.g. a data class nested - // in a view, so their public accessors are not accessible as such - @SuppressWarnings("java:S3011") private static Object invokeWrapExceptions(Method method, Object target, Object... parameters) { try { - try { - return method.invoke(target, parameters); - } catch (IllegalAccessException e) { - if (!method.trySetAccessible()) { - Class type = method.getDeclaringClass(); - throw new IllegalStateException(String.format( - "Unable to access '%s' of '%s'. Its module does " - + "not open the package '%s' to Vaadin. " - + "Make the class public, or open the " - + "package in module-info.java.", - method.getName(), type.getName(), - type.getPackageName()), e); - } - return method.invoke(target, parameters); - } + return method.invoke(target, parameters); + } catch (IllegalAccessException e) { + makeAccessible(method, e); + } catch (InvocationTargetException e) { + throw new RuntimeException(e); + } + try { + return method.invoke(target, parameters); } catch (IllegalAccessException | InvocationTargetException e) { throw new RuntimeException(e); } } + // S3011: bean types do not need to be public, e.g. a data class nested + // in a view, so their public accessors are not accessible as such + @SuppressWarnings("java:S3011") + private static void makeAccessible(Method method, + IllegalAccessException cause) { + if (!method.trySetAccessible()) { + Class type = method.getDeclaringClass(); + throw new IllegalStateException(String.format( + "Unable to access '%s' of '%s'. Its module does not open " + + "the package '%s' to Vaadin. Make the class " + + "public, or open the package in module-info.java.", + method.getName(), type.getName(), type.getPackageName()), + cause); + } + } + @Override public String toString() { return "Property set for bean " + instanceKey.type.getName(); diff --git a/flow-data/src/test/java/com/vaadin/flow/data/binder/BeanPropertySetTest.java b/flow-data/src/test/java/com/vaadin/flow/data/binder/BeanPropertySetTest.java index bf92c1098ab..fdee1ecb240 100644 --- a/flow-data/src/test/java/com/vaadin/flow/data/binder/BeanPropertySetTest.java +++ b/flow-data/src/test/java/com/vaadin/flow/data/binder/BeanPropertySetTest.java @@ -22,6 +22,7 @@ import java.io.ObjectOutputStream; import java.io.Serializable; import java.lang.reflect.Field; +import java.nio.file.Path; import java.util.Arrays; import java.util.Comparator; import java.util.HashSet; @@ -570,4 +571,20 @@ void nonPublicBeanClass_getterAndSetterWork() { assertEquals("child", childName.getGetter().apply(bean)); assertEquals("parent", name.getGetter().apply(bean)); } + + @Test + @SuppressWarnings({ "unchecked", "rawtypes" }) + void beanClassInPackageNotOpened_getterFailsWithHint() { + // the JDK's Path implementation lives in a package that is neither + // exported nor opened, so its accessors cannot be made accessible + Path bean = Path.of("file.txt"); + PropertySet set = BeanPropertySet.get((Class) bean.getClass()); + ValueProvider getter = set.getProperty("fileName").get() + .getGetter(); + + IllegalStateException exception = assertThrows( + IllegalStateException.class, () -> getter.apply(bean)); + assertTrue(exception.getMessage().contains("does not open the package '" + + bean.getClass().getPackageName() + "'")); + } } From e1714414297695e5ad6c1060d12d415c241e4d81 Mon Sep 17 00:00:00 2001 From: "totally-not-ai[bot]" <290682512+totally-not-ai[bot]@users.noreply.github.com> Date: Wed, 25 Nov 2026 13:42:59 +0000 Subject: [PATCH 4/8] test: assert full hint for package that is not opened --- .../vaadin/flow/data/binder/BeanPropertySetTest.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/flow-data/src/test/java/com/vaadin/flow/data/binder/BeanPropertySetTest.java b/flow-data/src/test/java/com/vaadin/flow/data/binder/BeanPropertySetTest.java index fdee1ecb240..c3fb581f93e 100644 --- a/flow-data/src/test/java/com/vaadin/flow/data/binder/BeanPropertySetTest.java +++ b/flow-data/src/test/java/com/vaadin/flow/data/binder/BeanPropertySetTest.java @@ -48,6 +48,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotSame; import static org.junit.jupiter.api.Assertions.assertNull; @@ -584,7 +585,12 @@ void beanClassInPackageNotOpened_getterFailsWithHint() { IllegalStateException exception = assertThrows( IllegalStateException.class, () -> getter.apply(bean)); - assertTrue(exception.getMessage().contains("does not open the package '" - + bean.getClass().getPackageName() + "'")); + assertEquals(String.format( + "Unable to access 'getFileName' of '%s'. Its module does not " + + "open the package '%s' to Vaadin. Make the class " + + "public, or open the package in module-info.java.", + bean.getClass().getName(), bean.getClass().getPackageName()), + exception.getMessage()); + assertInstanceOf(IllegalAccessException.class, exception.getCause()); } } From 22f40368f84402a6c55d207fc2ae047fbcdb15e6 Mon Sep 17 00:00:00 2001 From: "totally-not-ai[bot]" <290682512+totally-not-ai[bot]@users.noreply.github.com> Date: Wed, 2 Dec 2026 06:45:38 +0000 Subject: [PATCH 5/8] refactor: move bean accessor accessibility helper to ReflectTools writeRecord() now uses the same helper, so a record in a package that is not opened to Vaadin fails with the descriptive message instead of a generic access error. The tests assert the parts of the message that matter instead of its exact wording. --- .../flow/data/binder/BeanPropertySet.java | 20 ++----------- .../com/vaadin/flow/data/binder/Binder.java | 7 ++--- .../flow/data/binder/BeanPropertySetTest.java | 16 ++++------- .../vaadin/flow/internal/ReflectTools.java | 28 +++++++++++++++++++ .../flow/internal/ReflectToolsTest.java | 15 ++++++++++ 5 files changed, 55 insertions(+), 31 deletions(-) diff --git a/flow-data/src/main/java/com/vaadin/flow/data/binder/BeanPropertySet.java b/flow-data/src/main/java/com/vaadin/flow/data/binder/BeanPropertySet.java index ba82b203eb9..125cf4525ff 100644 --- a/flow-data/src/main/java/com/vaadin/flow/data/binder/BeanPropertySet.java +++ b/flow-data/src/main/java/com/vaadin/flow/data/binder/BeanPropertySet.java @@ -36,6 +36,7 @@ import com.vaadin.flow.function.ValueProvider; import com.vaadin.flow.internal.BeanUtil; +import com.vaadin.flow.internal.ReflectTools; /** * A {@link PropertySet} that uses reflection to find bean properties. @@ -553,7 +554,8 @@ private static Object invokeWrapExceptions(Method method, Object target, try { return method.invoke(target, parameters); } catch (IllegalAccessException e) { - makeAccessible(method, e); + // the bean type does not need to be public + ReflectTools.makeAccessible(method); } catch (InvocationTargetException e) { throw new RuntimeException(e); } @@ -564,22 +566,6 @@ private static Object invokeWrapExceptions(Method method, Object target, } } - // S3011: bean types do not need to be public, e.g. a data class nested - // in a view, so their public accessors are not accessible as such - @SuppressWarnings("java:S3011") - private static void makeAccessible(Method method, - IllegalAccessException cause) { - if (!method.trySetAccessible()) { - Class type = method.getDeclaringClass(); - throw new IllegalStateException(String.format( - "Unable to access '%s' of '%s'. Its module does not open " - + "the package '%s' to Vaadin. Make the class " - + "public, or open the package in module-info.java.", - method.getName(), type.getName(), type.getPackageName()), - cause); - } - } - @Override public String toString() { return "Property set for bean " + instanceKey.type.getName(); diff --git a/flow-data/src/main/java/com/vaadin/flow/data/binder/Binder.java b/flow-data/src/main/java/com/vaadin/flow/data/binder/Binder.java index 959cf5886a6..13a19496dbd 100644 --- a/flow-data/src/main/java/com/vaadin/flow/data/binder/Binder.java +++ b/flow-data/src/main/java/com/vaadin/flow/data/binder/Binder.java @@ -3089,11 +3089,10 @@ public BEAN writeRecord() throws ValidationException { final List convertedValues = new ArrayList<>(); values.forEach(value -> value.ifOk(convertedValues::add)); + Constructor constructor = beanType.getDeclaredConstructors()[0]; + // the record type does not need to be public + ReflectTools.makeAccessible(constructor); try { - Constructor constructor = beanType - .getDeclaredConstructors()[0]; - // the record type does not need to be public - constructor.trySetAccessible(); record = beanType.cast( constructor.newInstance(convertedValues.toArray())); } catch (Exception e) { diff --git a/flow-data/src/test/java/com/vaadin/flow/data/binder/BeanPropertySetTest.java b/flow-data/src/test/java/com/vaadin/flow/data/binder/BeanPropertySetTest.java index c3fb581f93e..b4d20b1a119 100644 --- a/flow-data/src/test/java/com/vaadin/flow/data/binder/BeanPropertySetTest.java +++ b/flow-data/src/test/java/com/vaadin/flow/data/binder/BeanPropertySetTest.java @@ -48,7 +48,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotSame; import static org.junit.jupiter.api.Assertions.assertNull; @@ -583,14 +582,11 @@ void beanClassInPackageNotOpened_getterFailsWithHint() { ValueProvider getter = set.getProperty("fileName").get() .getGetter(); - IllegalStateException exception = assertThrows( - IllegalStateException.class, () -> getter.apply(bean)); - assertEquals(String.format( - "Unable to access 'getFileName' of '%s'. Its module does not " - + "open the package '%s' to Vaadin. Make the class " - + "public, or open the package in module-info.java.", - bean.getClass().getName(), bean.getClass().getPackageName()), - exception.getMessage()); - assertInstanceOf(IllegalAccessException.class, exception.getCause()); + IllegalArgumentException exception = assertThrows( + IllegalArgumentException.class, () -> getter.apply(bean)); + String message = exception.getMessage(); + assertTrue(message.contains("'getFileName'"), message); + assertTrue(message.contains(bean.getClass().getName()), message); + assertTrue(message.contains(bean.getClass().getPackageName()), message); } } diff --git a/flow-server/src/main/java/com/vaadin/flow/internal/ReflectTools.java b/flow-server/src/main/java/com/vaadin/flow/internal/ReflectTools.java index 6ee6811d256..3b763cd5841 100644 --- a/flow-server/src/main/java/com/vaadin/flow/internal/ReflectTools.java +++ b/flow-server/src/main/java/com/vaadin/flow/internal/ReflectTools.java @@ -22,6 +22,7 @@ import java.lang.reflect.AnnotatedElement; import java.lang.reflect.Array; import java.lang.reflect.Constructor; +import java.lang.reflect.Executable; import java.lang.reflect.Field; import java.lang.reflect.GenericArrayType; import java.lang.reflect.InvocationTargetException; @@ -71,6 +72,7 @@ public class ReflectTools implements Serializable { static final String CREATE_INSTANCE_FAILED_CONSTRUCTOR_THREW_EXCEPTION = "Unable to create an instance of '%s'. The constructor threw an exception."; static final String CREATE_INSTANCE_FAILED_NO_NON_PRIVATE_NOARG_CONSTRUCTOR = "Unable to create an instance of '%s'. Make sure the class has a no-arg constructor that is not private."; static final String CREATE_INSTANCE_FAILED_INACCESSIBLE_CONSTRUCTOR = "Unable to create an instance of '%s'. Its module does not open the package '%s' to Vaadin. Make the class and its no-arg constructor public, or open the package in module-info.java."; + static final String ACCESS_FAILED_PACKAGE_NOT_OPEN = "Unable to access %s of '%s'. Its module does not open the package '%s' to Vaadin. Make the class public, or open the package in module-info.java."; private static final Predicate IS_SYNTHETIC = Method::isSynthetic; @@ -626,6 +628,32 @@ public static T createInstanceWithAccessibleConstructor(Class cls) { } } + /** + * Makes the given method or constructor accessible, so that it can be + * invoked even when its declaring class is not public, for example a data + * class nested in a view. + * + * @param member + * the method or constructor to make accessible + * @throws IllegalArgumentException + * if the member cannot be made accessible because the package + * of its declaring class is in a named module that does not + * open it to Vaadin + */ + // S3011: making non-public members accessible is the purpose of the method + @SuppressWarnings("java:S3011") + public static void makeAccessible(Executable member) { + if (!member.trySetAccessible()) { + Class type = member.getDeclaringClass(); + String memberName = member instanceof Constructor + ? "the constructor" + : "'" + member.getName() + "'"; + throw new IllegalArgumentException( + String.format(ACCESS_FAILED_PACKAGE_NOT_OPEN, memberName, + type.getName(), type.getPackageName())); + } + } + /** * Creates an instance of the given {@code proxyClass} with no-arg * constructor. diff --git a/flow-server/src/test/java/com/vaadin/flow/internal/ReflectToolsTest.java b/flow-server/src/test/java/com/vaadin/flow/internal/ReflectToolsTest.java index 0b664c21e3d..1c85abd3b5c 100644 --- a/flow-server/src/test/java/com/vaadin/flow/internal/ReflectToolsTest.java +++ b/flow-server/src/test/java/com/vaadin/flow/internal/ReflectToolsTest.java @@ -234,6 +234,21 @@ void createInstanceWithAccessibleConstructor_packageNotOpen_descriptiveError() cls.getName(), cls.getPackageName()), exception.getMessage()); } + @Test + void makeAccessible_packageNotOpen_descriptiveError() + throws ClassNotFoundException { + // A JDK class whose package java.base neither exports nor opens + Class cls = Class.forName("sun.nio.cs.UTF_8"); + + IllegalArgumentException exception = assertThrows( + IllegalArgumentException.class, () -> ReflectTools + .makeAccessible(cls.getDeclaredConstructors()[0])); + String message = exception.getMessage(); + assertTrue(message.contains("the constructor"), message); + assertTrue(message.contains(cls.getName()), message); + assertTrue(message.contains(cls.getPackageName()), message); + } + @Test void createInstanceWithAccessibleConstructor_privateConstructor_rejected() { IllegalArgumentException exception = assertThrows( From 2aa71b7055335408c898a35b3f2eef3fa1e1af7a Mon Sep 17 00:00:00 2001 From: "totally-not-ai[bot]" <290682512+totally-not-ai[bot]@users.noreply.github.com> Date: Wed, 2 Dec 2026 06:48:13 +0000 Subject: [PATCH 6/8] fix: only make the record constructor accessible when needed A public record whose package is exported only to Vaadin can be created directly, while trySetAccessible() fails for it. Check access first so such records keep working. Tests now also check the package and the fix-it hint in the message. Only the helper is tested for the package-not-open case; no JDK record in such a package fits a Binder test. --- .../src/main/java/com/vaadin/flow/data/binder/Binder.java | 6 ++++-- .../com/vaadin/flow/data/binder/BeanPropertySetTest.java | 5 ++++- .../java/com/vaadin/flow/internal/ReflectToolsTest.java | 8 ++++---- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/flow-data/src/main/java/com/vaadin/flow/data/binder/Binder.java b/flow-data/src/main/java/com/vaadin/flow/data/binder/Binder.java index 13a19496dbd..dcbfcdc8558 100644 --- a/flow-data/src/main/java/com/vaadin/flow/data/binder/Binder.java +++ b/flow-data/src/main/java/com/vaadin/flow/data/binder/Binder.java @@ -3090,8 +3090,10 @@ public BEAN writeRecord() throws ValidationException { values.forEach(value -> value.ifOk(convertedValues::add)); Constructor constructor = beanType.getDeclaredConstructors()[0]; - // the record type does not need to be public - ReflectTools.makeAccessible(constructor); + if (!constructor.canAccess(null)) { + // the record type does not need to be public + ReflectTools.makeAccessible(constructor); + } try { record = beanType.cast( constructor.newInstance(convertedValues.toArray())); diff --git a/flow-data/src/test/java/com/vaadin/flow/data/binder/BeanPropertySetTest.java b/flow-data/src/test/java/com/vaadin/flow/data/binder/BeanPropertySetTest.java index b4d20b1a119..88c06d38fd3 100644 --- a/flow-data/src/test/java/com/vaadin/flow/data/binder/BeanPropertySetTest.java +++ b/flow-data/src/test/java/com/vaadin/flow/data/binder/BeanPropertySetTest.java @@ -587,6 +587,9 @@ void beanClassInPackageNotOpened_getterFailsWithHint() { String message = exception.getMessage(); assertTrue(message.contains("'getFileName'"), message); assertTrue(message.contains(bean.getClass().getName()), message); - assertTrue(message.contains(bean.getClass().getPackageName()), message); + assertTrue(message.contains( + "open the package '" + bean.getClass().getPackageName() + "'"), + message); + assertTrue(message.contains("module-info.java"), message); } } diff --git a/flow-server/src/test/java/com/vaadin/flow/internal/ReflectToolsTest.java b/flow-server/src/test/java/com/vaadin/flow/internal/ReflectToolsTest.java index 1c85abd3b5c..a61038f2556 100644 --- a/flow-server/src/test/java/com/vaadin/flow/internal/ReflectToolsTest.java +++ b/flow-server/src/test/java/com/vaadin/flow/internal/ReflectToolsTest.java @@ -243,10 +243,10 @@ void makeAccessible_packageNotOpen_descriptiveError() IllegalArgumentException exception = assertThrows( IllegalArgumentException.class, () -> ReflectTools .makeAccessible(cls.getDeclaredConstructors()[0])); - String message = exception.getMessage(); - assertTrue(message.contains("the constructor"), message); - assertTrue(message.contains(cls.getName()), message); - assertTrue(message.contains(cls.getPackageName()), message); + assertEquals( + String.format(ReflectTools.ACCESS_FAILED_PACKAGE_NOT_OPEN, + "the constructor", cls.getName(), cls.getPackageName()), + exception.getMessage()); } @Test From d6ac3e28949d94cbd92da6f382edd5ab22c977a5 Mon Sep 17 00:00:00 2001 From: "totally-not-ai[bot]" <290682512+totally-not-ai[bot]@users.noreply.github.com> Date: Wed, 2 Dec 2026 07:11:51 +0000 Subject: [PATCH 7/8] refactor: check accessor access up front instead of catching the denial Getters and setters now check canAccess() and make the method accessible before the call, like writeRecord() does, instead of catching the IllegalAccessException and retrying. Also fixes the operator precedence and single-throwing-call lambda findings. --- .../java/com/vaadin/flow/data/binder/BeanPropertySet.java | 8 +++----- .../main/java/com/vaadin/flow/internal/ReflectTools.java | 2 +- .../java/com/vaadin/flow/internal/ReflectToolsTest.java | 6 ++++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/flow-data/src/main/java/com/vaadin/flow/data/binder/BeanPropertySet.java b/flow-data/src/main/java/com/vaadin/flow/data/binder/BeanPropertySet.java index 125cf4525ff..f04f490207c 100644 --- a/flow-data/src/main/java/com/vaadin/flow/data/binder/BeanPropertySet.java +++ b/flow-data/src/main/java/com/vaadin/flow/data/binder/BeanPropertySet.java @@ -551,13 +551,11 @@ private static boolean hasNonObjectReadMethod( private static Object invokeWrapExceptions(Method method, Object target, Object... parameters) { - try { - return method.invoke(target, parameters); - } catch (IllegalAccessException e) { + // canAccess() rejects a null target; invoke() then throws the + // NullPointerException callers expect for a broken property chain + if (target != null && !method.canAccess(target)) { // the bean type does not need to be public ReflectTools.makeAccessible(method); - } catch (InvocationTargetException e) { - throw new RuntimeException(e); } try { return method.invoke(target, parameters); diff --git a/flow-server/src/main/java/com/vaadin/flow/internal/ReflectTools.java b/flow-server/src/main/java/com/vaadin/flow/internal/ReflectTools.java index 3b763cd5841..e6bc688ac74 100644 --- a/flow-server/src/main/java/com/vaadin/flow/internal/ReflectTools.java +++ b/flow-server/src/main/java/com/vaadin/flow/internal/ReflectTools.java @@ -645,7 +645,7 @@ public static T createInstanceWithAccessibleConstructor(Class cls) { public static void makeAccessible(Executable member) { if (!member.trySetAccessible()) { Class type = member.getDeclaringClass(); - String memberName = member instanceof Constructor + String memberName = (member instanceof Constructor) ? "the constructor" : "'" + member.getName() + "'"; throw new IllegalArgumentException( diff --git a/flow-server/src/test/java/com/vaadin/flow/internal/ReflectToolsTest.java b/flow-server/src/test/java/com/vaadin/flow/internal/ReflectToolsTest.java index a61038f2556..b197da74a88 100644 --- a/flow-server/src/test/java/com/vaadin/flow/internal/ReflectToolsTest.java +++ b/flow-server/src/test/java/com/vaadin/flow/internal/ReflectToolsTest.java @@ -21,6 +21,7 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.ParameterizedType; @@ -239,10 +240,11 @@ void makeAccessible_packageNotOpen_descriptiveError() throws ClassNotFoundException { // A JDK class whose package java.base neither exports nor opens Class cls = Class.forName("sun.nio.cs.UTF_8"); + Constructor constructor = cls.getDeclaredConstructors()[0]; IllegalArgumentException exception = assertThrows( - IllegalArgumentException.class, () -> ReflectTools - .makeAccessible(cls.getDeclaredConstructors()[0])); + IllegalArgumentException.class, + () -> ReflectTools.makeAccessible(constructor)); assertEquals( String.format(ReflectTools.ACCESS_FAILED_PACKAGE_NOT_OPEN, "the constructor", cls.getName(), cls.getPackageName()), From 5746141a7757e17d2b4fdff7587ce67ce7e6d16b Mon Sep 17 00:00:00 2001 From: "totally-not-ai[bot]" <290682512+totally-not-ai[bot]@users.noreply.github.com> Date: Wed, 2 Dec 2026 07:37:13 +0000 Subject: [PATCH 8/8] refactor: avoid concatenation inside the member name ternary --- .../src/main/java/com/vaadin/flow/internal/ReflectTools.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/flow-server/src/main/java/com/vaadin/flow/internal/ReflectTools.java b/flow-server/src/main/java/com/vaadin/flow/internal/ReflectTools.java index e6bc688ac74..98691fd4b33 100644 --- a/flow-server/src/main/java/com/vaadin/flow/internal/ReflectTools.java +++ b/flow-server/src/main/java/com/vaadin/flow/internal/ReflectTools.java @@ -645,9 +645,9 @@ public static T createInstanceWithAccessibleConstructor(Class cls) { public static void makeAccessible(Executable member) { if (!member.trySetAccessible()) { Class type = member.getDeclaringClass(); - String memberName = (member instanceof Constructor) + String memberName = member instanceof Constructor ? "the constructor" - : "'" + member.getName() + "'"; + : String.format("'%s'", member.getName()); throw new IllegalArgumentException( String.format(ACCESS_FAILED_PACKAGE_NOT_OPEN, memberName, type.getName(), type.getPackageName()));