From 44de925c5e313c38032d872483ba1b9e51d75b9f Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Fri, 17 Oct 2025 14:34:15 +0200 Subject: [PATCH] Add reflection hints for SpringPersistenceUnitInfo Fixes gh-35655 --- ...SpringPersistenceUnitInfoRuntimeHints.java | 41 ++++++++++++++ .../resources/META-INF/spring/aot.factories | 3 +- ...gPersistenceUnitInfoRuntimeHintsTests.java | 53 +++++++++++++++++++ 3 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 spring-orm/src/main/java/org/springframework/orm/jpa/persistenceunit/SpringPersistenceUnitInfoRuntimeHints.java create mode 100644 spring-orm/src/test/java/org/springframework/orm/jpa/persistenceunit/SpringPersistenceUnitInfoRuntimeHintsTests.java diff --git a/spring-orm/src/main/java/org/springframework/orm/jpa/persistenceunit/SpringPersistenceUnitInfoRuntimeHints.java b/spring-orm/src/main/java/org/springframework/orm/jpa/persistenceunit/SpringPersistenceUnitInfoRuntimeHints.java new file mode 100644 index 00000000000..f144b48e758 --- /dev/null +++ b/spring-orm/src/main/java/org/springframework/orm/jpa/persistenceunit/SpringPersistenceUnitInfoRuntimeHints.java @@ -0,0 +1,41 @@ +/* + * Copyright 2025-present the original author or authors. + * + * 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 + * + * https://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 org.springframework.orm.jpa.persistenceunit; + + +import org.jspecify.annotations.Nullable; + +import org.springframework.aot.hint.MemberCategory; +import org.springframework.aot.hint.RuntimeHints; +import org.springframework.aot.hint.RuntimeHintsRegistrar; + +/** + * {@link RuntimeHintsRegistrar} implementation that registers runtime hints for + * the {@link SpringPersistenceUnitInfo}. + * + * @author Brian Clozel + * @since 7.0 + */ +public class SpringPersistenceUnitInfoRuntimeHints implements RuntimeHintsRegistrar { + + @Override + public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader) { + hints.reflection().registerType(SpringPersistenceUnitInfo.class, MemberCategory.INVOKE_PUBLIC_METHODS); + hints.reflection().registerType(MutablePersistenceUnitInfo.class, MemberCategory.INVOKE_PUBLIC_METHODS); + } + +} diff --git a/spring-orm/src/main/resources/META-INF/spring/aot.factories b/spring-orm/src/main/resources/META-INF/spring/aot.factories index e8ba1dea986..85e6818b08c 100644 --- a/spring-orm/src/main/resources/META-INF/spring/aot.factories +++ b/spring-orm/src/main/resources/META-INF/spring/aot.factories @@ -2,4 +2,5 @@ org.springframework.beans.factory.aot.BeanRegistrationAotProcessor=\ org.springframework.orm.jpa.persistenceunit.PersistenceManagedTypesBeanRegistrationAotProcessor org.springframework.aot.hint.RuntimeHintsRegistrar=\ -org.springframework.orm.jpa.EntityManagerRuntimeHints +org.springframework.orm.jpa.EntityManagerRuntimeHints,\ +org.springframework.orm.jpa.persistenceunit.SpringPersistenceUnitInfoRuntimeHints diff --git a/spring-orm/src/test/java/org/springframework/orm/jpa/persistenceunit/SpringPersistenceUnitInfoRuntimeHintsTests.java b/spring-orm/src/test/java/org/springframework/orm/jpa/persistenceunit/SpringPersistenceUnitInfoRuntimeHintsTests.java new file mode 100644 index 00000000000..46ffcb89b90 --- /dev/null +++ b/spring-orm/src/test/java/org/springframework/orm/jpa/persistenceunit/SpringPersistenceUnitInfoRuntimeHintsTests.java @@ -0,0 +1,53 @@ +/* + * Copyright 2025-present the original author or authors. + * + * 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 + * + * https://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 org.springframework.orm.jpa.persistenceunit; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import org.springframework.aot.hint.RuntimeHints; +import org.springframework.aot.hint.RuntimeHintsRegistrar; +import org.springframework.aot.hint.predicate.RuntimeHintsPredicates; +import org.springframework.beans.factory.aot.AotServices; +import org.springframework.util.ClassUtils; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link SpringPersistenceUnitInfoRuntimeHints}. + * + * @author Brian Clozel + */ +class SpringPersistenceUnitInfoRuntimeHintsTests { + + private final RuntimeHints hints = new RuntimeHints(); + + @BeforeEach + void setup() { + AotServices.factories().load(RuntimeHintsRegistrar.class) + .forEach(registrar -> registrar.registerHints(this.hints, + ClassUtils.getDefaultClassLoader())); + } + + @Test + void springPersistenceUnitInfoHasReflectionHints() { + assertThat(RuntimeHintsPredicates.reflection() + .onMethodInvocation(SpringPersistenceUnitInfo.class, "setPersistenceProviderPackageName")) + .accepts(this.hints); + } + +}