From 7c2453c3738426327d1f489006775c619c497694 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Thu, 11 Aug 2022 14:53:06 +0200 Subject: [PATCH] Integrate class proxy generation in AOT processing This commit updates ApplicationContextAotGenerator to register a handler that process Cglib generated classes. The handler registers such classes to the GeneratedFiles and provide a hint so that it can be instantiated using reflection. Closes gh-28954 --- .../aot/ApplicationContextAotGenerator.java | 27 +++++-- .../context/aot/GeneratedClassHandler.java | 60 ++++++++++++++ .../ApplicationContextAotGeneratorTests.java | 30 ++++++- .../aot/GeneratedClassHandlerTests.java | 79 +++++++++++++++++++ .../annotation/CglibConfiguration.java | 39 +++++++++ .../cglib/core/ReflectUtils.java | 21 +++-- .../cglib/core/SpringNamingPolicy.java | 3 +- 7 files changed, 237 insertions(+), 22 deletions(-) create mode 100644 spring-context/src/main/java/org/springframework/context/aot/GeneratedClassHandler.java create mode 100644 spring-context/src/test/java/org/springframework/context/aot/GeneratedClassHandlerTests.java create mode 100644 spring-context/src/testFixtures/java/org/springframework/context/testfixture/context/generator/annotation/CglibConfiguration.java diff --git a/spring-context/src/main/java/org/springframework/context/aot/ApplicationContextAotGenerator.java b/spring-context/src/main/java/org/springframework/context/aot/ApplicationContextAotGenerator.java index de976b2f8b..f7df128ca6 100644 --- a/spring-context/src/main/java/org/springframework/context/aot/ApplicationContextAotGenerator.java +++ b/spring-context/src/main/java/org/springframework/context/aot/ApplicationContextAotGenerator.java @@ -16,9 +16,12 @@ package org.springframework.context.aot; +import java.util.function.Supplier; + import org.springframework.aot.generate.GenerationContext; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.support.DefaultListableBeanFactory; +import org.springframework.cglib.core.ReflectUtils; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextInitializer; import org.springframework.context.support.GenericApplicationContext; @@ -47,12 +50,24 @@ public class ApplicationContextAotGenerator { */ public ClassName processAheadOfTime(GenericApplicationContext applicationContext, GenerationContext generationContext) { - applicationContext.refreshForAotProcessing(); - DefaultListableBeanFactory beanFactory = applicationContext.getDefaultListableBeanFactory(); - ApplicationContextInitializationCodeGenerator codeGenerator = - new ApplicationContextInitializationCodeGenerator(generationContext); - new BeanFactoryInitializationAotContributions(beanFactory).applyTo(generationContext, codeGenerator); - return codeGenerator.getGeneratedClass().getName(); + return withGeneratedClassHandler(new GeneratedClassHandler(generationContext), () -> { + applicationContext.refreshForAotProcessing(); + DefaultListableBeanFactory beanFactory = applicationContext.getDefaultListableBeanFactory(); + ApplicationContextInitializationCodeGenerator codeGenerator = + new ApplicationContextInitializationCodeGenerator(generationContext); + new BeanFactoryInitializationAotContributions(beanFactory).applyTo(generationContext, codeGenerator); + return codeGenerator.getGeneratedClass().getName(); + }); + } + + private T withGeneratedClassHandler(GeneratedClassHandler generatedClassHandler, Supplier task) { + try { + ReflectUtils.setGeneratedClassHandler(generatedClassHandler); + return task.get(); + } + finally { + ReflectUtils.setGeneratedClassHandler(null); + } } } diff --git a/spring-context/src/main/java/org/springframework/context/aot/GeneratedClassHandler.java b/spring-context/src/main/java/org/springframework/context/aot/GeneratedClassHandler.java new file mode 100644 index 0000000000..d74c4bdb59 --- /dev/null +++ b/spring-context/src/main/java/org/springframework/context/aot/GeneratedClassHandler.java @@ -0,0 +1,60 @@ +/* + * Copyright 2002-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.context.aot; + +import java.util.function.BiConsumer; +import java.util.function.Consumer; + +import org.springframework.aot.generate.GeneratedFiles; +import org.springframework.aot.generate.GeneratedFiles.Kind; +import org.springframework.aot.generate.GenerationContext; +import org.springframework.aot.hint.MemberCategory; +import org.springframework.aot.hint.RuntimeHints; +import org.springframework.aot.hint.TypeHint.Builder; +import org.springframework.aot.hint.TypeReference; +import org.springframework.cglib.core.ReflectUtils; +import org.springframework.core.io.ByteArrayResource; + +/** + * Handle generated classes by adding them to a {@link GenerationContext}, + * and register the necessary hints so that they can be instantiated. + * + * @author Stephane Nicoll + * @see ReflectUtils#setGeneratedClassHandler(BiConsumer) + */ +class GeneratedClassHandler implements BiConsumer { + + private static final Consumer asCglibProxy = hint -> + hint.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS); + + private final RuntimeHints runtimeHints; + + private final GeneratedFiles generatedFiles; + + GeneratedClassHandler(GenerationContext generationContext) { + this.runtimeHints = generationContext.getRuntimeHints(); + this.generatedFiles = generationContext.getGeneratedFiles(); + } + + @Override + public void accept(String className, byte[] content) { + this.runtimeHints.reflection().registerType(TypeReference.of(className), asCglibProxy); + String path = className.replace(".", "/") + ".class"; + this.generatedFiles.addFile(Kind.CLASS, path, new ByteArrayResource(content)); + } + +} diff --git a/spring-context/src/test/java/org/springframework/context/aot/ApplicationContextAotGeneratorTests.java b/spring-context/src/test/java/org/springframework/context/aot/ApplicationContextAotGeneratorTests.java index c3970d78bd..34f601e032 100644 --- a/spring-context/src/test/java/org/springframework/context/aot/ApplicationContextAotGeneratorTests.java +++ b/spring-context/src/test/java/org/springframework/context/aot/ApplicationContextAotGeneratorTests.java @@ -16,10 +16,15 @@ package org.springframework.context.aot; +import java.io.IOException; import java.util.function.BiConsumer; import org.junit.jupiter.api.Test; +import org.springframework.aot.generate.GeneratedFiles.Kind; +import org.springframework.aot.hint.MemberCategory; +import org.springframework.aot.hint.TypeReference; +import org.springframework.aot.hint.predicate.RuntimeHintsPredicates; import org.springframework.aot.test.generator.compile.Compiled; import org.springframework.aot.test.generator.compile.TestCompiler; import org.springframework.beans.BeansException; @@ -36,11 +41,13 @@ import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.RegisteredBean; import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.context.ApplicationContextInitializer; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigUtils; import org.springframework.context.annotation.CommonAnnotationBeanPostProcessor; import org.springframework.context.support.GenericApplicationContext; import org.springframework.context.testfixture.context.generator.SimpleComponent; import org.springframework.context.testfixture.context.generator.annotation.AutowiredComponent; +import org.springframework.context.testfixture.context.generator.annotation.CglibConfiguration; import org.springframework.context.testfixture.context.generator.annotation.InitDestroyComponent; import org.springframework.core.testfixture.aot.generate.TestGenerationContext; @@ -161,13 +168,30 @@ class ApplicationContextAotGeneratorTests { }); } - @SuppressWarnings({ "rawtypes", "unchecked" }) - private void testCompiledResult(GenericApplicationContext applicationContext, - BiConsumer, Compiled> result) { + @Test + void processAheadOfTimeWhenHasCglibProxyWriteProxyAndGenerateReflectionHints() throws IOException { + GenericApplicationContext applicationContext = new AnnotationConfigApplicationContext(); + applicationContext.registerBean(CglibConfiguration.class); + TestGenerationContext context = processAheadOfTime(applicationContext); + String proxyClassName = CglibConfiguration.class.getName() + "$$SpringCGLIB$$0"; + assertThat(context.getGeneratedFiles() + .getGeneratedFileContent(Kind.CLASS, proxyClassName.replace('.', '/') + ".class")).isNotNull(); + assertThat(RuntimeHintsPredicates.reflection().onType(TypeReference.of(proxyClassName)) + .withMemberCategory(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)).accepts(context.getRuntimeHints()); + } + + private static TestGenerationContext processAheadOfTime(GenericApplicationContext applicationContext) { ApplicationContextAotGenerator generator = new ApplicationContextAotGenerator(); TestGenerationContext generationContext = new TestGenerationContext(); generator.processAheadOfTime(applicationContext, generationContext); generationContext.writeGeneratedContent(); + return generationContext; + } + + @SuppressWarnings({ "rawtypes", "unchecked" }) + private void testCompiledResult(GenericApplicationContext applicationContext, + BiConsumer, Compiled> result) { + TestGenerationContext generationContext = processAheadOfTime(applicationContext); TestCompiler.forSystem().withFiles(generationContext.getGeneratedFiles()).compile(compiled -> result.accept(compiled.getInstance(ApplicationContextInitializer.class), compiled)); } diff --git a/spring-context/src/test/java/org/springframework/context/aot/GeneratedClassHandlerTests.java b/spring-context/src/test/java/org/springframework/context/aot/GeneratedClassHandlerTests.java new file mode 100644 index 0000000000..6d57fd4b6e --- /dev/null +++ b/spring-context/src/test/java/org/springframework/context/aot/GeneratedClassHandlerTests.java @@ -0,0 +1,79 @@ +/* + * Copyright 2002-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.context.aot; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; + +import org.junit.jupiter.api.Test; + +import org.springframework.aot.generate.GeneratedFiles.Kind; +import org.springframework.aot.generate.InMemoryGeneratedFiles; +import org.springframework.aot.hint.MemberCategory; +import org.springframework.aot.hint.TypeReference; +import org.springframework.aot.hint.predicate.RuntimeHintsPredicates; +import org.springframework.core.io.InputStreamSource; +import org.springframework.core.testfixture.aot.generate.TestGenerationContext; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link GeneratedClassHandler}. + * + * @author Stephane Nicoll + */ +class GeneratedClassHandlerTests { + + private static final byte[] TEST_CONTENT = new byte[] { 'a' }; + + private final TestGenerationContext generationContext; + + private final GeneratedClassHandler handler; + + public GeneratedClassHandlerTests() { + this.generationContext = new TestGenerationContext(); + this.handler = new GeneratedClassHandler(this.generationContext); + } + + @Test + void handlerGenerateRuntimeHints() { + String className = "com.example.Test$$Proxy$$1"; + this.handler.accept(className, TEST_CONTENT); + assertThat(RuntimeHintsPredicates.reflection().onType(TypeReference.of(className)) + .withMemberCategory(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)) + .accepts(this.generationContext.getRuntimeHints()); + } + + @Test + void handlerRegisterGeneratedClass() throws IOException { + String className = "com.example.Test$$Proxy$$1"; + this.handler.accept(className, TEST_CONTENT); + InMemoryGeneratedFiles generatedFiles = this.generationContext.getGeneratedFiles(); + assertThat(generatedFiles.getGeneratedFiles(Kind.SOURCE)).isEmpty(); + assertThat(generatedFiles.getGeneratedFiles(Kind.RESOURCE)).isEmpty(); + String expectedPath = "com/example/Test$$Proxy$$1.class"; + assertThat(generatedFiles.getGeneratedFiles(Kind.CLASS)).containsOnlyKeys(expectedPath); + assertContent(generatedFiles.getGeneratedFiles(Kind.CLASS).get(expectedPath), TEST_CONTENT); + } + + private void assertContent(InputStreamSource source, byte[] expectedContent) throws IOException { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + source.getInputStream().transferTo(out); + assertThat(out.toByteArray()).isEqualTo(expectedContent); + } + +} diff --git a/spring-context/src/testFixtures/java/org/springframework/context/testfixture/context/generator/annotation/CglibConfiguration.java b/spring-context/src/testFixtures/java/org/springframework/context/testfixture/context/generator/annotation/CglibConfiguration.java new file mode 100644 index 0000000000..dd33cb40ce --- /dev/null +++ b/spring-context/src/testFixtures/java/org/springframework/context/testfixture/context/generator/annotation/CglibConfiguration.java @@ -0,0 +1,39 @@ +/* + * Copyright 2002-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.context.testfixture.context.generator.annotation; + +import java.util.concurrent.atomic.AtomicInteger; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class CglibConfiguration { + + private static final AtomicInteger counter = new AtomicInteger(); + + @Bean + public String prefix() { + return "Hello" + counter.getAndIncrement(); + } + + @Bean + public String text() { + return prefix() + " World"; + } + +} diff --git a/spring-core/src/main/java/org/springframework/cglib/core/ReflectUtils.java b/spring-core/src/main/java/org/springframework/cglib/core/ReflectUtils.java index 2fe4a85e68..4e3c65685a 100644 --- a/spring-core/src/main/java/org/springframework/cglib/core/ReflectUtils.java +++ b/spring-core/src/main/java/org/springframework/cglib/core/ReflectUtils.java @@ -20,16 +20,12 @@ import java.beans.BeanInfo; import java.beans.IntrospectionException; import java.beans.Introspector; import java.beans.PropertyDescriptor; -import java.io.ByteArrayInputStream; -import java.io.OutputStream; import java.lang.invoke.MethodHandles; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Member; import java.lang.reflect.Method; import java.lang.reflect.Modifier; -import java.nio.file.Files; -import java.nio.file.Path; import java.security.ProtectionDomain; import java.util.ArrayList; import java.util.Arrays; @@ -38,6 +34,7 @@ import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.function.BiConsumer; import org.springframework.asm.Type; @@ -64,6 +61,8 @@ public class ReflectUtils { private static final List OBJECT_METHODS = new ArrayList(); + private static BiConsumer generatedClassHandler; + // SPRING PATCH BEGIN static { // Resolve protected ClassLoader.defineClass method for fallback use @@ -441,6 +440,10 @@ public class ReflectUtils { return defineClass(className, b, loader, protectionDomain, null); } + public static void setGeneratedClassHandler(BiConsumer handler) { + generatedClassHandler = handler; + } + @SuppressWarnings({"deprecation", "serial"}) public static Class defineClass(String className, byte[] b, ClassLoader loader, ProtectionDomain protectionDomain, Class contextClass) throws Exception { @@ -448,13 +451,9 @@ public class ReflectUtils { Class c = null; Throwable t = THROWABLE; - String generatedClasses = System.getProperty("cglib.generatedClasses"); - if (generatedClasses != null) { - Path path = Path.of(generatedClasses + "/" + className.replace(".", "/") + ".class"); - Files.createDirectories(path.getParent()); - try (OutputStream os = Files.newOutputStream(path)) { - new ByteArrayInputStream(b).transferTo(os); - } + BiConsumer handlerToUse = generatedClassHandler; + if (handlerToUse != null) { + handlerToUse.accept(className, b); } // Preferred option: JDK 9+ Lookup.defineClass API if ClassLoader matches diff --git a/spring-core/src/main/java/org/springframework/cglib/core/SpringNamingPolicy.java b/spring-core/src/main/java/org/springframework/cglib/core/SpringNamingPolicy.java index a3ddcfe354..78edb12db6 100644 --- a/spring-core/src/main/java/org/springframework/cglib/core/SpringNamingPolicy.java +++ b/spring-core/src/main/java/org/springframework/cglib/core/SpringNamingPolicy.java @@ -22,8 +22,7 @@ package org.springframework.cglib.core; * and using a plain counter suffix instead of a hash code suffix (as of 6.0). * *

This allows for reliably discovering pre-generated Spring proxy classes - * in the classpath (as written at runtime when the "cglib.generatedClasses" - * system property points to a specific directory to store the proxy classes). + * in the classpath. * * @author Juergen Hoeller * @since 3.2.8 / 6.0