From cc7552ec61a8a0a3a041359038740b454388fc7f Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Mon, 19 Sep 2022 09:37:36 -0700 Subject: [PATCH] Rename @CompileWithTargetClassAccess Rename `@CompileWithTargetClassAccess` to `@CompileWithForkedClassLoaderClassLoader`. Closes gh-29173 --- ...nBeanRegistrationAotContributionTests.java | 6 ++--- .../BeanDefinitionMethodGeneratorTests.java | 4 ++-- ...java => CompileWithForkedClassLoader.java} | 7 +++--- ...pileWithForkedClassLoaderClassLoader.java} | 24 ++++++++++--------- ...ompileWithForkedClassLoaderExtension.java} | 8 +++---- .../generate/compile/DynamicClassLoader.java | 4 ++-- .../test/generate/compile/TestCompiler.java | 2 +- .../generate/compile/TestCompilerTests.java | 4 ++-- ...BeanPostProcessorAotContributionTests.java | 4 ++-- .../test/context/aot/AotIntegrationTests.java | 4 ++-- .../aot/TestContextAotGeneratorTests.java | 4 ++-- src/checkstyle/checkstyle-suppressions.xml | 2 +- 12 files changed, 37 insertions(+), 36 deletions(-) rename spring-core-test/src/main/java/org/springframework/aot/test/generate/compile/{CompileWithTargetClassAccess.java => CompileWithForkedClassLoader.java} (89%) rename spring-core-test/src/main/java/org/springframework/aot/test/generate/compile/{CompileWithTargetClassAccessClassLoader.java => CompileWithForkedClassLoaderClassLoader.java} (86%) rename spring-core-test/src/main/java/org/springframework/aot/test/generate/compile/{CompileWithTargetClassAccessExtension.java => CompileWithForkedClassLoaderExtension.java} (94%) diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanRegistrationAotContributionTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanRegistrationAotContributionTests.java index 5e425d305e..6cdb0392c5 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanRegistrationAotContributionTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanRegistrationAotContributionTests.java @@ -27,7 +27,7 @@ import org.springframework.aot.generate.MethodReference; import org.springframework.aot.generate.MethodReference.ArgumentCodeGenerator; import org.springframework.aot.hint.predicate.RuntimeHintsPredicates; import org.springframework.aot.test.generate.TestGenerationContext; -import org.springframework.aot.test.generate.compile.CompileWithTargetClassAccess; +import org.springframework.aot.test.generate.compile.CompileWithForkedClassLoader; import org.springframework.aot.test.generate.compile.Compiled; import org.springframework.aot.test.generate.compile.TestCompiler; import org.springframework.beans.factory.aot.BeanRegistrationAotContribution; @@ -85,7 +85,7 @@ class AutowiredAnnotationBeanRegistrationAotContributionTests { } @Test - @CompileWithTargetClassAccess + @CompileWithForkedClassLoader void contributeWhenPackagePrivateFieldInjectionInjectsUsingConsumer() { Environment environment = new StandardEnvironment(); this.beanFactory.registerSingleton("environment", environment); @@ -122,7 +122,7 @@ class AutowiredAnnotationBeanRegistrationAotContributionTests { } @Test - @CompileWithTargetClassAccess + @CompileWithForkedClassLoader void contributeWhenPackagePrivateMethodInjectionInjectsUsingConsumer() { Environment environment = new StandardEnvironment(); this.beanFactory.registerSingleton("environment", environment); diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/aot/BeanDefinitionMethodGeneratorTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/aot/BeanDefinitionMethodGeneratorTests.java index 9020bf8626..9bc165da2f 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/aot/BeanDefinitionMethodGeneratorTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/aot/BeanDefinitionMethodGeneratorTests.java @@ -32,7 +32,7 @@ import org.springframework.aot.generate.GenerationContext; import org.springframework.aot.generate.MethodReference; import org.springframework.aot.generate.MethodReference.ArgumentCodeGenerator; import org.springframework.aot.test.generate.TestGenerationContext; -import org.springframework.aot.test.generate.compile.CompileWithTargetClassAccess; +import org.springframework.aot.test.generate.compile.CompileWithForkedClassLoader; import org.springframework.aot.test.generate.compile.Compiled; import org.springframework.aot.test.generate.compile.TestCompiler; import org.springframework.aot.test.generate.file.SourceFile; @@ -387,7 +387,7 @@ class BeanDefinitionMethodGeneratorTests { } @Test - @CompileWithTargetClassAccess + @CompileWithForkedClassLoader void generateBeanDefinitionMethodWhenPackagePrivateBean() { RegisteredBean registeredBean = registerBean( new RootBeanDefinition(PackagePrivateTestBean.class)); diff --git a/spring-core-test/src/main/java/org/springframework/aot/test/generate/compile/CompileWithTargetClassAccess.java b/spring-core-test/src/main/java/org/springframework/aot/test/generate/compile/CompileWithForkedClassLoader.java similarity index 89% rename from spring-core-test/src/main/java/org/springframework/aot/test/generate/compile/CompileWithTargetClassAccess.java rename to spring-core-test/src/main/java/org/springframework/aot/test/generate/compile/CompileWithForkedClassLoader.java index 195fc51899..630f0ee291 100644 --- a/spring-core-test/src/main/java/org/springframework/aot/test/generate/compile/CompileWithTargetClassAccess.java +++ b/spring-core-test/src/main/java/org/springframework/aot/test/generate/compile/CompileWithForkedClassLoader.java @@ -26,8 +26,7 @@ import org.junit.jupiter.api.extension.ExtendWith; /** * Annotation that registers a JUnit Jupiter extension for test classes or test - * methods that need a {@link TestCompiler} with non-public access to target - * classes. + * methods that need to use a forked classloader with compiled code. * *

The extension allows the compiler to define classes without polluting the * test {@link ClassLoader}. @@ -45,6 +44,6 @@ import org.junit.jupiter.api.extension.ExtendWith; @Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.TYPE, ElementType.METHOD }) @Documented -@ExtendWith(CompileWithTargetClassAccessExtension.class) -public @interface CompileWithTargetClassAccess { +@ExtendWith(CompileWithForkedClassLoaderExtension.class) +public @interface CompileWithForkedClassLoader { } diff --git a/spring-core-test/src/main/java/org/springframework/aot/test/generate/compile/CompileWithTargetClassAccessClassLoader.java b/spring-core-test/src/main/java/org/springframework/aot/test/generate/compile/CompileWithForkedClassLoaderClassLoader.java similarity index 86% rename from spring-core-test/src/main/java/org/springframework/aot/test/generate/compile/CompileWithTargetClassAccessClassLoader.java rename to spring-core-test/src/main/java/org/springframework/aot/test/generate/compile/CompileWithForkedClassLoaderClassLoader.java index cfc90bc977..d755979da8 100644 --- a/spring-core-test/src/main/java/org/springframework/aot/test/generate/compile/CompileWithTargetClassAccessClassLoader.java +++ b/spring-core-test/src/main/java/org/springframework/aot/test/generate/compile/CompileWithForkedClassLoaderClassLoader.java @@ -26,29 +26,38 @@ import org.springframework.lang.Nullable; /** * {@link ClassLoader} implementation to support - * {@link CompileWithTargetClassAccess @CompileWithTargetClassAccess}. + * {@link CompileWithForkedClassLoader @CompileWithForkedClassLoader}. * * @author Phillip Webb + * @author Andy Wilkinson + * @author Stephane Nicoll * @since 6.0 */ -final class CompileWithTargetClassAccessClassLoader extends ClassLoader { +final class CompileWithForkedClassLoaderClassLoader extends ClassLoader { private final ClassLoader testClassLoader; private Function classResourceLookup = name -> null; - public CompileWithTargetClassAccessClassLoader(ClassLoader testClassLoader) { + public CompileWithForkedClassLoaderClassLoader(ClassLoader testClassLoader) { super(testClassLoader.getParent()); this.testClassLoader = testClassLoader; } - // Invoked reflectively by DynamicClassLoader constructor + // Invoked reflectively by DynamicClassLoader @SuppressWarnings("unused") void setClassResourceLookup(Function classResourceLookup) { this.classResourceLookup = classResourceLookup; } + // Invoked reflectively by DynamicClassLoader + @SuppressWarnings("unused") + Class defineDynamicClass(String name, byte[] b, int off, int len) { + return super.defineClass(name, b, off, len); + } + + @Override public Class loadClass(String name) throws ClassNotFoundException { if (name.startsWith("org.junit") || name.startsWith("org.testng")) { @@ -82,13 +91,6 @@ final class CompileWithTargetClassAccessClassLoader extends ClassLoader { return null; } - // Invoked reflectively by DynamicClassLoader.findDefineClassMethod(ClassLoader) - @SuppressWarnings("unused") - Class defineClassWithTargetAccess(String name, byte[] b, int off, int len) { - return super.defineClass(name, b, off, len); - } - - @Override protected Enumeration findResources(String name) throws IOException { return this.testClassLoader.getResources(name); diff --git a/spring-core-test/src/main/java/org/springframework/aot/test/generate/compile/CompileWithTargetClassAccessExtension.java b/spring-core-test/src/main/java/org/springframework/aot/test/generate/compile/CompileWithForkedClassLoaderExtension.java similarity index 94% rename from spring-core-test/src/main/java/org/springframework/aot/test/generate/compile/CompileWithTargetClassAccessExtension.java rename to spring-core-test/src/main/java/org/springframework/aot/test/generate/compile/CompileWithForkedClassLoaderExtension.java index b3e72919b3..91d9fea7b5 100644 --- a/spring-core-test/src/main/java/org/springframework/aot/test/generate/compile/CompileWithTargetClassAccessExtension.java +++ b/spring-core-test/src/main/java/org/springframework/aot/test/generate/compile/CompileWithForkedClassLoaderExtension.java @@ -34,14 +34,14 @@ import static org.junit.platform.launcher.EngineFilter.includeEngines; /** * JUnit Jupiter {@link InvocationInterceptor} to support - * {@link CompileWithTargetClassAccess @CompileWithTargetClassAccess}. + * {@link CompileWithForkedClassLoader @CompileWithForkedClassLoader}. * * @author Christoph Dreis * @author Phillip Webb * @author Sam Brannen * @since 6.0 */ -class CompileWithTargetClassAccessExtension implements InvocationInterceptor { +class CompileWithForkedClassLoaderExtension implements InvocationInterceptor { @Override public void interceptBeforeAllMethod(Invocation invocation, @@ -105,7 +105,7 @@ class CompileWithTargetClassAccessExtension implements InvocationInterceptor { Class testClass = extensionContext.getRequiredTestClass(); ClassLoader classLoader = testClass.getClassLoader(); return classLoader.getClass().getName() - .equals(CompileWithTargetClassAccessClassLoader.class.getName()); + .equals(CompileWithForkedClassLoaderClassLoader.class.getName()); } private void runTestWithModifiedClassPath( @@ -115,7 +115,7 @@ class CompileWithTargetClassAccessExtension implements InvocationInterceptor { Class testClass = extensionContext.getRequiredTestClass(); Method testMethod = invocationContext.getExecutable(); ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader(); - ClassLoader forkedClassPathClassLoader = new CompileWithTargetClassAccessClassLoader( + ClassLoader forkedClassPathClassLoader = new CompileWithForkedClassLoaderClassLoader( testClass.getClassLoader()); Thread.currentThread().setContextClassLoader(forkedClassPathClassLoader); try { diff --git a/spring-core-test/src/main/java/org/springframework/aot/test/generate/compile/DynamicClassLoader.java b/spring-core-test/src/main/java/org/springframework/aot/test/generate/compile/DynamicClassLoader.java index 4b8c080de4..844c60fd3e 100644 --- a/spring-core-test/src/main/java/org/springframework/aot/test/generate/compile/DynamicClassLoader.java +++ b/spring-core-test/src/main/java/org/springframework/aot/test/generate/compile/DynamicClassLoader.java @@ -63,14 +63,14 @@ public class DynamicClassLoader extends ClassLoader { this.classFiles = classFiles; this.compiledClasses = compiledClasses; Class parentClass = parent.getClass(); - if (parentClass.getName().equals(CompileWithTargetClassAccessClassLoader.class.getName())) { + if (parentClass.getName().equals(CompileWithForkedClassLoaderClassLoader.class.getName())) { Method setClassResourceLookupMethod = ReflectionUtils.findMethod(parentClass, "setClassResourceLookup", Function.class); ReflectionUtils.makeAccessible(setClassResourceLookupMethod); ReflectionUtils.invokeMethod(setClassResourceLookupMethod, getParent(), (Function) this::findClassBytes); this.defineClassMethod = ReflectionUtils.findMethod(parentClass, - "defineClassWithTargetAccess", String.class, byte[].class, int.class, int.class); + "defineDynamicClass", String.class, byte[].class, int.class, int.class); ReflectionUtils.makeAccessible(this.defineClassMethod); this.compiledClasses.forEach((name, file) -> defineClass(name, file.getBytes())); } diff --git a/spring-core-test/src/main/java/org/springframework/aot/test/generate/compile/TestCompiler.java b/spring-core-test/src/main/java/org/springframework/aot/test/generate/compile/TestCompiler.java index 40021f42bf..c1fd47caae 100644 --- a/spring-core-test/src/main/java/org/springframework/aot/test/generate/compile/TestCompiler.java +++ b/spring-core-test/src/main/java/org/springframework/aot/test/generate/compile/TestCompiler.java @@ -276,7 +276,7 @@ public final class TestCompiler { } catch (IllegalAccessError ex) { throw new IllegalAccessError(ex.getMessage() + ". " + - "For non-public access ensure you annotate your test class or test method with @CompileWithTargetClassAccess"); + "For non-public access ensure you annotate your test class or test method with @CompileWithForkedClassLoader"); } finally { Thread.currentThread().setContextClassLoader(previousClassLoader); diff --git a/spring-core-test/src/test/java/org/springframework/aot/test/generate/compile/TestCompilerTests.java b/spring-core-test/src/test/java/org/springframework/aot/test/generate/compile/TestCompilerTests.java index 0eac02675c..64984ec69c 100644 --- a/spring-core-test/src/test/java/org/springframework/aot/test/generate/compile/TestCompilerTests.java +++ b/spring-core-test/src/test/java/org/springframework/aot/test/generate/compile/TestCompilerTests.java @@ -206,7 +206,7 @@ class TestCompilerTests { } @Test - @CompileWithTargetClassAccess + @CompileWithForkedClassLoader void compiledCodeCanAccessExistingPackagePrivateClassIfAnnotated() throws LinkageError { SourceFiles sourceFiles = SourceFiles.of(SourceFile.of(""" package com.example; @@ -240,7 +240,7 @@ class TestCompilerTests { assertThatExceptionOfType(IllegalAccessError.class) .isThrownBy(() -> TestCompiler.forSystem().compile(sourceFiles, compiled -> compiled.getInstance(PublicInterface.class, "com.example.Test").perform())) - .withMessageContaining(ClassUtils.getShortName(CompileWithTargetClassAccess.class)); + .withMessageContaining(ClassUtils.getShortName(CompileWithForkedClassLoader.class)); } @Test diff --git a/spring-orm/src/test/java/org/springframework/orm/jpa/support/PersistenceAnnotationBeanPostProcessorAotContributionTests.java b/spring-orm/src/test/java/org/springframework/orm/jpa/support/PersistenceAnnotationBeanPostProcessorAotContributionTests.java index e68dde6a01..96db4669e1 100644 --- a/spring-orm/src/test/java/org/springframework/orm/jpa/support/PersistenceAnnotationBeanPostProcessorAotContributionTests.java +++ b/spring-orm/src/test/java/org/springframework/orm/jpa/support/PersistenceAnnotationBeanPostProcessorAotContributionTests.java @@ -34,7 +34,7 @@ import org.junit.jupiter.api.Test; import org.springframework.aot.hint.TypeReference; import org.springframework.aot.test.generate.TestGenerationContext; -import org.springframework.aot.test.generate.compile.CompileWithTargetClassAccess; +import org.springframework.aot.test.generate.compile.CompileWithForkedClassLoader; import org.springframework.aot.test.generate.compile.Compiled; import org.springframework.aot.test.generate.compile.TestCompiler; import org.springframework.beans.factory.aot.BeanRegistrationAotContribution; @@ -53,7 +53,7 @@ import static org.mockito.Mockito.mock; * @author Stephane Nicoll * @author Phillip Webb */ -@CompileWithTargetClassAccess +@CompileWithForkedClassLoader class PersistenceAnnotationBeanPostProcessorAotContributionTests { private DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); diff --git a/spring-test/src/test/java/org/springframework/test/context/aot/AotIntegrationTests.java b/spring-test/src/test/java/org/springframework/test/context/aot/AotIntegrationTests.java index 1aaf81736f..5a59a26cde 100644 --- a/spring-test/src/test/java/org/springframework/test/context/aot/AotIntegrationTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/aot/AotIntegrationTests.java @@ -37,7 +37,7 @@ import org.opentest4j.MultipleFailuresError; import org.springframework.aot.AotDetector; import org.springframework.aot.generate.GeneratedFiles.Kind; import org.springframework.aot.generate.InMemoryGeneratedFiles; -import org.springframework.aot.test.generate.compile.CompileWithTargetClassAccess; +import org.springframework.aot.test.generate.compile.CompileWithForkedClassLoader; import org.springframework.aot.test.generate.compile.TestCompiler; import org.springframework.test.context.aot.samples.basic.BasicSpringJupiterSharedConfigTests; import org.springframework.test.context.aot.samples.basic.BasicSpringJupiterTests; @@ -54,7 +54,7 @@ import static org.junit.platform.launcher.TagFilter.excludeTags; * @author Sam Brannen * @since 6.0 */ -@CompileWithTargetClassAccess +@CompileWithForkedClassLoader class AotIntegrationTests extends AbstractAotTests { private static final String CLASSPATH_ROOT = "AotSmokeTests.classpath_root"; diff --git a/spring-test/src/test/java/org/springframework/test/context/aot/TestContextAotGeneratorTests.java b/spring-test/src/test/java/org/springframework/test/context/aot/TestContextAotGeneratorTests.java index bcbd7c58b3..dd9a86f0f6 100644 --- a/spring-test/src/test/java/org/springframework/test/context/aot/TestContextAotGeneratorTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/aot/TestContextAotGeneratorTests.java @@ -33,7 +33,7 @@ import org.springframework.aot.generate.InMemoryGeneratedFiles; import org.springframework.aot.hint.MemberCategory; import org.springframework.aot.hint.RuntimeHints; import org.springframework.aot.hint.TypeReference; -import org.springframework.aot.test.generate.compile.CompileWithTargetClassAccess; +import org.springframework.aot.test.generate.compile.CompileWithForkedClassLoader; import org.springframework.aot.test.generate.compile.TestCompiler; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextInitializer; @@ -80,7 +80,7 @@ import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppC * @author Sam Brannen * @since 6.0 */ -@CompileWithTargetClassAccess +@CompileWithForkedClassLoader class TestContextAotGeneratorTests extends AbstractAotTests { /** diff --git a/src/checkstyle/checkstyle-suppressions.xml b/src/checkstyle/checkstyle-suppressions.xml index d4bb6c256f..b8e2a43db4 100644 --- a/src/checkstyle/checkstyle-suppressions.xml +++ b/src/checkstyle/checkstyle-suppressions.xml @@ -40,7 +40,7 @@ - +