Skip to content

Commit 089e4e6

Browse files
committed
Do not attempt to load pre-enhanced class for reloadable classes
Closes gh-33024
1 parent 77755ff commit 089e4e6

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
@@ -49,6 +49,7 @@
4949
import org.springframework.cglib.proxy.NoOp;
5050
import org.springframework.cglib.transform.ClassEmitterTransformer;
5151
import org.springframework.cglib.transform.TransformingClassGenerator;
52+
import org.springframework.core.SmartClassLoader;
5253
import org.springframework.lang.Nullable;
5354
import org.springframework.objenesis.ObjenesisException;
5455
import org.springframework.objenesis.SpringObjenesis;
@@ -132,13 +133,21 @@ private Enhancer newEnhancer(Class<?> configSuperClass, @Nullable ClassLoader cl
132133
enhancer.setInterfaces(new Class<?>[] {EnhancedConfiguration.class});
133134
enhancer.setUseFactory(false);
134135
enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE);
135-
enhancer.setAttemptLoad(true);
136+
enhancer.setAttemptLoad(!isClassReloadable(configSuperClass, classLoader));
136137
enhancer.setStrategy(new BeanFactoryAwareGeneratorStrategy(classLoader));
137138
enhancer.setCallbackFilter(CALLBACK_FILTER);
138139
enhancer.setCallbackTypes(CALLBACK_FILTER.getCallbackTypes());
139140
return enhancer;
140141
}
141142

143+
/**
144+
* Checks whether the given configuration class is reloadable.
145+
*/
146+
private boolean isClassReloadable(Class<?> configSuperClass, @Nullable ClassLoader classLoader) {
147+
return (classLoader instanceof SmartClassLoader smartClassLoader &&
148+
smartClassLoader.isClassReloadable(configSuperClass));
149+
}
150+
142151
/**
143152
* Uses enhancer to generate a subclass of superclass,
144153
* 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)