diff --git a/spring-data-jpa/src/main/java/org/springframework/data/jpa/aot/DataJpaRuntimeHints.java b/spring-data-jpa/src/main/java/org/springframework/data/jpa/aot/JpaRuntimeHintsRegistrar.java similarity index 85% rename from spring-data-jpa/src/main/java/org/springframework/data/jpa/aot/DataJpaRuntimeHints.java rename to spring-data-jpa/src/main/java/org/springframework/data/jpa/aot/JpaRuntimeHintsRegistrar.java index b7a71648f..e7492417d 100644 --- a/spring-data-jpa/src/main/java/org/springframework/data/jpa/aot/DataJpaRuntimeHints.java +++ b/spring-data-jpa/src/main/java/org/springframework/data/jpa/aot/JpaRuntimeHintsRegistrar.java @@ -31,7 +31,7 @@ import org.springframework.util.ClassUtils; * @author Christoph Strobl * @since 3.0 */ -public class DataJpaRuntimeHints implements RuntimeHintsRegistrar { +public class JpaRuntimeHintsRegistrar implements RuntimeHintsRegistrar { @Override public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader) { @@ -46,12 +46,12 @@ public class DataJpaRuntimeHints implements RuntimeHintsRegistrar { hints.reflection().registerType( TypeReference.of("org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect"), hint -> hint .withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS, MemberCategory.INVOKE_DECLARED_METHODS)); - } - hints.reflection().registerTypes(Arrays.asList( // - TypeReference.of(AuditingBeanFactoryPostProcessor.class), // - TypeReference.of(AuditingEntityListener.class)), - hint -> hint.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS, MemberCategory.INVOKE_DECLARED_METHODS)); + hints.reflection().registerTypes(Arrays.asList( // + TypeReference.of(AuditingBeanFactoryPostProcessor.class), // + TypeReference.of(AuditingEntityListener.class)), + hint -> hint.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS, MemberCategory.INVOKE_DECLARED_METHODS)); + } hints.reflection().registerType(TypeReference.of(SimpleJpaRepository.class), hint -> hint.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS, MemberCategory.INVOKE_PUBLIC_METHODS)); diff --git a/spring-data-jpa/src/main/resources/META-INF/spring/aot.factories b/spring-data-jpa/src/main/resources/META-INF/spring/aot.factories index bb56670aa..8822976a9 100644 --- a/spring-data-jpa/src/main/resources/META-INF/spring/aot.factories +++ b/spring-data-jpa/src/main/resources/META-INF/spring/aot.factories @@ -1,2 +1,2 @@ org.springframework.aot.hint.RuntimeHintsRegistrar=\ - org.springframework.data.jpa.aot.DataJpaRuntimeHints + org.springframework.data.jpa.aot.JpaRuntimeHintsRegistrar diff --git a/spring-data-jpa/src/test/java/org/springframework/data/jpa/aot/JpaRuntimeHintsRegistrarUnitTests.java b/spring-data-jpa/src/test/java/org/springframework/data/jpa/aot/JpaRuntimeHintsRegistrarUnitTests.java new file mode 100644 index 000000000..e83333407 --- /dev/null +++ b/spring-data-jpa/src/test/java/org/springframework/data/jpa/aot/JpaRuntimeHintsRegistrarUnitTests.java @@ -0,0 +1,58 @@ +/* + * Copyright 2022 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.data.jpa.aot; + +import static org.assertj.core.api.AssertionsForClassTypes.*; +import static org.springframework.aot.hint.RuntimeHintsPredicates.*; + +import org.junit.jupiter.api.Test; +import org.springframework.aot.hint.RuntimeHints; +import org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect; +import org.springframework.data.jpa.domain.support.AuditingBeanFactoryPostProcessor; +import org.springframework.data.jpa.domain.support.AuditingEntityListener; +import org.springframework.data.jpa.util.HidingClassLoader; + +/** + * @author Christoph Strobl + */ +class JpaRuntimeHintsRegistrarUnitTests { + + @Test // GH-2497 + void registersAuditing() { + + RuntimeHints hints = new RuntimeHints(); + + JpaRuntimeHintsRegistrar registrar = new JpaRuntimeHintsRegistrar(); + registrar.registerHints(hints, null); + + assertThat(hints).matches(reflection().onType(AnnotationBeanConfigurerAspect.class)) + .matches(reflection().onType(AuditingEntityListener.class)) + .matches(reflection().onType(AuditingBeanFactoryPostProcessor.class)); + } + + @Test // GH-2497 + void skipsAuditingHintsIfAspectjNotPresent() { + + RuntimeHints hints = new RuntimeHints(); + + JpaRuntimeHintsRegistrar registrar = new JpaRuntimeHintsRegistrar(); + registrar.registerHints(hints, HidingClassLoader.hidePackages("org.springframework.beans.factory.aspectj")); + + assertThat(hints).matches(reflection().onType(AnnotationBeanConfigurerAspect.class).negate()) + .matches(reflection().onType(AuditingEntityListener.class).negate()) + .matches(reflection().onType(AuditingBeanFactoryPostProcessor.class).negate()); + } +} diff --git a/spring-data-jpa/src/test/java/org/springframework/data/jpa/util/HidingClassLoader.java b/spring-data-jpa/src/test/java/org/springframework/data/jpa/util/HidingClassLoader.java new file mode 100644 index 000000000..f68a87220 --- /dev/null +++ b/spring-data-jpa/src/test/java/org/springframework/data/jpa/util/HidingClassLoader.java @@ -0,0 +1,114 @@ +/* + * Copyright 2017-2022 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.data.jpa.util; + +import java.net.URLClassLoader; +import java.util.Arrays; +import java.util.Collection; +import java.util.stream.Collectors; + +import org.springframework.instrument.classloading.ShadowingClassLoader; +import org.springframework.util.Assert; + +/** + * is intended for testing code that depends on the presence/absence of certain classes. Classes can be: + * + * + * @author Jens Schauder + * @author Oliver Gierke + * @author Christoph Strobl + */ +public class HidingClassLoader extends ShadowingClassLoader { + + private final Collection hidden; + + HidingClassLoader(Collection hidden) { + + super(URLClassLoader.getSystemClassLoader(), false); + + this.hidden = hidden; + } + + /** + * Creates a new {@link HidingClassLoader} with the packages of the given classes hidden. + * + * @param packages must not be {@literal null}. + * @return + */ + public static HidingClassLoader hide(Class... packages) { + + Assert.notNull(packages, "Packages must not be null"); + + return new HidingClassLoader(Arrays.stream(packages)// + .map(it -> it.getPackage().getName())// + .collect(Collectors.toList())); + } + + /** + * Creates a new {@link HidingClassLoader} with the packages of the given classes hidden. + * + * @param packages must not be {@literal null}. + * @return + */ + public static HidingClassLoader hidePackages(String... packages) { + + Assert.notNull(packages, "Packages must not be null"); + + return new HidingClassLoader(Arrays.asList(packages)); + } + + public static HidingClassLoader hideTypes(Class... types) { + + Assert.notNull(types, "Types must not be null!"); + + return new HidingClassLoader(Arrays.stream(types)// + .map(it -> it.getName())// + .collect(Collectors.toList())); + } + + @Override + public Class loadClass(String name) throws ClassNotFoundException { + + Class loaded = super.loadClass(name); + checkIfHidden(loaded); + return loaded; + } + + @Override + protected boolean isEligibleForShadowing(String className) { + return isExcluded(className); + } + + @Override + protected Class findClass(String name) throws ClassNotFoundException { + + Class loaded = super.findClass(name); + checkIfHidden(loaded); + return loaded; + } + + private void checkIfHidden(Class type) throws ClassNotFoundException { + + if (hidden.stream().anyMatch(it -> type.getName().startsWith(it))) { + throw new ClassNotFoundException(); + } + } +}