Skip to content

Commit 7796437

Browse files
committed
Do not attempt to load pre-enhanced class for reloadable classes
Closes gh-33024 (cherry picked from commit 089e4e6)
1 parent 67686d7 commit 7796437

File tree

2 files changed

+97
-1
lines changed

2 files changed

+97
-1
lines changed

spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassEnhancer.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import org.springframework.cglib.proxy.NoOp;
4848
import org.springframework.cglib.transform.ClassEmitterTransformer;
4949
import org.springframework.cglib.transform.TransformingClassGenerator;
50+
import org.springframework.core.SmartClassLoader;
5051
import org.springframework.lang.Nullable;
5152
import org.springframework.objenesis.ObjenesisException;
5253
import org.springframework.objenesis.SpringObjenesis;
@@ -123,13 +124,21 @@ private Enhancer newEnhancer(Class<?> configSuperClass, @Nullable ClassLoader cl
123124
enhancer.setInterfaces(new Class<?>[] {EnhancedConfiguration.class});
124125
enhancer.setUseFactory(false);
125126
enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE);
126-
enhancer.setAttemptLoad(true);
127+
enhancer.setAttemptLoad(!isClassReloadable(configSuperClass, classLoader));
127128
enhancer.setStrategy(new BeanFactoryAwareGeneratorStrategy(classLoader));
128129
enhancer.setCallbackFilter(CALLBACK_FILTER);
129130
enhancer.setCallbackTypes(CALLBACK_FILTER.getCallbackTypes());
130131
return enhancer;
131132
}
132133

134+
/**
135+
* Checks whether the given configuration class is reloadable.
136+
*/
137+
private boolean isClassReloadable(Class<?> configSuperClass, @Nullable ClassLoader classLoader) {
138+
return (classLoader instanceof SmartClassLoader smartClassLoader &&
139+
smartClassLoader.isClassReloadable(configSuperClass));
140+
}
141+
133142
/**
134143
* Uses enhancer to generate a subclass of superclass,
135144
* ensuring that callbacks are registered for the new subclass.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright 2002-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.context.annotation;
18+
19+
import java.io.IOException;
20+
import java.io.InputStream;
21+
import java.security.SecureClassLoader;
22+
23+
import org.junit.jupiter.api.Test;
24+
25+
import org.springframework.core.SmartClassLoader;
26+
import org.springframework.util.StreamUtils;
27+
28+
import static org.assertj.core.api.Assertions.assertThat;
29+
30+
/**
31+
* @author Phillip Webb
32+
* @author Juergen Hoeller
33+
*/
34+
class ConfigurationClassEnhancerTests {
35+
36+
@Test
37+
void enhanceReloadedClass() throws Exception {
38+
ConfigurationClassEnhancer configurationClassEnhancer = new ConfigurationClassEnhancer();
39+
ClassLoader parentClassLoader = getClass().getClassLoader();
40+
CustomClassLoader classLoader = new CustomClassLoader(parentClassLoader);
41+
Class<?> myClass = parentClassLoader.loadClass(MyConfig.class.getName());
42+
configurationClassEnhancer.enhance(myClass, parentClassLoader);
43+
Class<?> myReloadedClass = classLoader.loadClass(MyConfig.class.getName());
44+
Class<?> enhancedReloadedClass = configurationClassEnhancer.enhance(myReloadedClass, classLoader);
45+
assertThat(enhancedReloadedClass.getClassLoader()).isEqualTo(classLoader);
46+
}
47+
48+
49+
@Configuration
50+
static class MyConfig {
51+
52+
@Bean
53+
public String myBean() {
54+
return "bean";
55+
}
56+
}
57+
58+
59+
static class CustomClassLoader extends SecureClassLoader implements SmartClassLoader {
60+
61+
CustomClassLoader(ClassLoader parent) {
62+
super(parent);
63+
}
64+
65+
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
66+
if (name.contains("MyConfig")) {
67+
String path = name.replace('.', '/').concat(".class");
68+
try (InputStream in = super.getResourceAsStream(path)) {
69+
byte[] bytes = StreamUtils.copyToByteArray(in);
70+
if (bytes.length > 0) {
71+
return defineClass(name, bytes, 0, bytes.length);
72+
}
73+
}
74+
catch (IOException ex) {
75+
throw new IllegalStateException(ex);
76+
}
77+
}
78+
return super.loadClass(name, resolve);
79+
}
80+
81+
@Override
82+
public boolean isClassReloadable(Class<?> clazz) {
83+
return clazz.getName().contains("MyConfig");
84+
}
85+
}
86+
87+
}

0 commit comments

Comments
 (0)