|
| 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