Skip to content

Commit 74498aa

Browse files
committed
Avoid duplicate AOP proxy class definition with FilteredClassLoader
Fixes gh-28528
1 parent fbe614a commit 74498aa

File tree

3 files changed

+61
-3
lines changed

3 files changed

+61
-3
lines changed

spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/FilteredClassLoader.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,12 +20,14 @@
2020
import java.io.InputStream;
2121
import java.net.URL;
2222
import java.net.URLClassLoader;
23+
import java.security.ProtectionDomain;
2324
import java.util.Arrays;
2425
import java.util.Collection;
2526
import java.util.Collections;
2627
import java.util.Enumeration;
2728
import java.util.function.Predicate;
2829

30+
import org.springframework.core.SmartClassLoader;
2931
import org.springframework.core.io.ClassPathResource;
3032

3133
/**
@@ -37,7 +39,7 @@
3739
* @author Roy Jacobs
3840
* @since 2.0.0
3941
*/
40-
public class FilteredClassLoader extends URLClassLoader {
42+
public class FilteredClassLoader extends URLClassLoader implements SmartClassLoader {
4143

4244
private final Collection<Predicate<String>> classesFilters;
4345

@@ -129,6 +131,16 @@ public InputStream getResourceAsStream(String name) {
129131
return super.getResourceAsStream(name);
130132
}
131133

134+
@Override
135+
public Class<?> publicDefineClass(String name, byte[] b, ProtectionDomain protectionDomain) {
136+
for (Predicate<String> filter : this.classesFilters) {
137+
if (filter.test(name)) {
138+
throw new IllegalArgumentException(String.format("Defining class with name %s is not supported", name));
139+
}
140+
}
141+
return defineClass(name, b, 0, b.length, protectionDomain);
142+
}
143+
132144
/**
133145
* Filter to restrict the classes that can be loaded.
134146
*/

spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/FilteredClassLoaderTests.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,6 +26,7 @@
2626

2727
import static org.assertj.core.api.Assertions.assertThat;
2828
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
29+
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
2930

3031
/**
3132
* Tests for {@link FilteredClassLoader}.
@@ -111,4 +112,13 @@ void loadResourceAsStreamWhenNotFilteredShouldLoadResource() throws Exception {
111112
}
112113
}
113114

115+
@Test
116+
void publicDefineClassWhenFilteredThrowsException() throws Exception {
117+
Class<FilteredClassLoaderTests> hiddenClass = FilteredClassLoaderTests.class;
118+
try (FilteredClassLoader classLoader = new FilteredClassLoader(hiddenClass)) {
119+
assertThatIllegalArgumentException()
120+
.isThrownBy(() -> classLoader.publicDefineClass(hiddenClass.getName(), new byte[] {}, null));
121+
}
122+
}
123+
114124
}

spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/runner/AbstractApplicationContextRunnerTests.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import org.springframework.beans.factory.ObjectProvider;
2929
import org.springframework.beans.factory.annotation.Autowired;
3030
import org.springframework.boot.context.annotation.UserConfigurations;
31+
import org.springframework.boot.context.properties.ConfigurationProperties;
32+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
3133
import org.springframework.boot.test.context.FilteredClassLoader;
3234
import org.springframework.boot.test.context.assertj.ApplicationContextAssertProvider;
3335
import org.springframework.context.ConfigurableApplicationContext;
@@ -36,6 +38,7 @@
3638
import org.springframework.context.annotation.ConditionContext;
3739
import org.springframework.context.annotation.Conditional;
3840
import org.springframework.context.annotation.Configuration;
41+
import org.springframework.context.annotation.Lazy;
3942
import org.springframework.core.env.Environment;
4043
import org.springframework.core.type.AnnotatedTypeMetadata;
4144
import org.springframework.util.ClassUtils;
@@ -168,6 +171,15 @@ void runWithClassLoaderShouldSetClassLoaderOnConditionContext() {
168171
.run((context) -> assertThat(context).hasSingleBean(ConditionalConfig.class));
169172
}
170173

174+
@Test
175+
void consecutiveRunWithFilteredClassLoaderShouldHaveBeanWithLazyProperties() {
176+
get().withClassLoader(new FilteredClassLoader(Gson.class)).withUserConfiguration(LazyConfig.class)
177+
.run((context) -> assertThat(context).hasSingleBean(ExampleBeanWithLazyProperties.class));
178+
179+
get().withClassLoader(new FilteredClassLoader(Gson.class)).withUserConfiguration(LazyConfig.class)
180+
.run((context) -> assertThat(context).hasSingleBean(ExampleBeanWithLazyProperties.class));
181+
}
182+
171183
@Test
172184
void thrownRuleWorksWithCheckedException() {
173185
get().run((context) -> assertThatIOException().isThrownBy(() -> throwCheckedException("Expected message"))
@@ -260,6 +272,30 @@ static class ConditionalConfig {
260272

261273
}
262274

275+
@Configuration(proxyBeanMethods = false)
276+
@EnableConfigurationProperties(ExampleProperties.class)
277+
static class LazyConfig {
278+
279+
@Bean
280+
ExampleBeanWithLazyProperties exampleBeanWithLazyProperties() {
281+
return new ExampleBeanWithLazyProperties();
282+
}
283+
284+
}
285+
286+
static class ExampleBeanWithLazyProperties {
287+
288+
@Autowired
289+
@Lazy
290+
ExampleProperties exampleProperties;
291+
292+
}
293+
294+
@ConfigurationProperties
295+
public static class ExampleProperties {
296+
297+
}
298+
263299
static class FilteredClassLoaderCondition implements Condition {
264300

265301
@Override

0 commit comments

Comments
 (0)