|
| 1 | +/* |
| 2 | + * Copyright 2012-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.boot.testcontainers.context; |
| 18 | + |
| 19 | +import java.lang.reflect.Field; |
| 20 | + |
| 21 | +import javax.lang.model.element.Modifier; |
| 22 | + |
| 23 | +import org.testcontainers.containers.Container; |
| 24 | + |
| 25 | +import org.springframework.aot.generate.GeneratedMethod; |
| 26 | +import org.springframework.aot.generate.GenerationContext; |
| 27 | +import org.springframework.beans.factory.aot.BeanRegistrationAotContribution; |
| 28 | +import org.springframework.beans.factory.aot.BeanRegistrationAotProcessor; |
| 29 | +import org.springframework.beans.factory.aot.BeanRegistrationCode; |
| 30 | +import org.springframework.beans.factory.aot.BeanRegistrationCodeFragments; |
| 31 | +import org.springframework.beans.factory.aot.BeanRegistrationCodeFragmentsDecorator; |
| 32 | +import org.springframework.beans.factory.support.InstanceSupplier; |
| 33 | +import org.springframework.beans.factory.support.RegisteredBean; |
| 34 | +import org.springframework.beans.factory.support.RootBeanDefinition; |
| 35 | +import org.springframework.javapoet.ClassName; |
| 36 | +import org.springframework.javapoet.CodeBlock; |
| 37 | +import org.springframework.util.Assert; |
| 38 | +import org.springframework.util.ClassUtils; |
| 39 | +import org.springframework.util.ReflectionUtils; |
| 40 | + |
| 41 | +/** |
| 42 | + * {@link BeanRegistrationAotProcessor} that replaces InstanceSupplier of |
| 43 | + * {@link Container} by a reflection equivalent. |
| 44 | + * |
| 45 | + * @author Dmytro Nosan |
| 46 | + */ |
| 47 | +class TestcontainersBeanRegistrationAotProcessor implements BeanRegistrationAotProcessor { |
| 48 | + |
| 49 | + @Override |
| 50 | + public BeanRegistrationAotContribution processAheadOfTime(RegisteredBean registeredBean) { |
| 51 | + RootBeanDefinition bd = registeredBean.getMergedBeanDefinition(); |
| 52 | + String attributeName = TestcontainerFieldBeanDefinition.class.getName(); |
| 53 | + Object field = bd.getAttribute(attributeName); |
| 54 | + if (field != null) { |
| 55 | + Assert.isInstanceOf(Field.class, field, |
| 56 | + "BeanDefinition attribute '" + attributeName + "' value must be a type of '" + Field.class + "'"); |
| 57 | + return BeanRegistrationAotContribution.withCustomCodeFragments( |
| 58 | + (codeFragments) -> new AotContribution(codeFragments, registeredBean, ((Field) field))); |
| 59 | + } |
| 60 | + return null; |
| 61 | + } |
| 62 | + |
| 63 | + static class AotContribution extends BeanRegistrationCodeFragmentsDecorator { |
| 64 | + |
| 65 | + private final RegisteredBean registeredBean; |
| 66 | + |
| 67 | + private final Field field; |
| 68 | + |
| 69 | + AotContribution(BeanRegistrationCodeFragments delegate, RegisteredBean registeredBean, Field field) { |
| 70 | + super(delegate); |
| 71 | + this.registeredBean = registeredBean; |
| 72 | + this.field = field; |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public ClassName getTarget(RegisteredBean registeredBean) { |
| 77 | + return ClassName.get(this.registeredBean.getBeanClass()); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public CodeBlock generateInstanceSupplierCode(GenerationContext generationContext, |
| 82 | + BeanRegistrationCode beanRegistrationCode, boolean allowDirectSupplierShortcut) { |
| 83 | + Class<?> beanClass = this.registeredBean.getBeanClass(); |
| 84 | + Class<?> testClass = this.field.getDeclaringClass(); |
| 85 | + String fieldName = this.field.getName(); |
| 86 | + GeneratedMethod generatedMethod = beanRegistrationCode.getMethods() |
| 87 | + .add("getInstance", (method) -> method |
| 88 | + .addJavadoc("Get the bean instance for '$L'.", this.registeredBean.getBeanName()) |
| 89 | + .addModifiers(Modifier.PRIVATE, Modifier.STATIC) |
| 90 | + .returns(beanClass) |
| 91 | + .addStatement("$T<?> testClass = $T.forName($S, null)", Class.class, ClassUtils.class, |
| 92 | + testClass.getName()) |
| 93 | + .addStatement("$T field = $T.findField(testClass, $S)", Field.class, ReflectionUtils.class, |
| 94 | + fieldName) |
| 95 | + .addStatement("$T.notNull(field, $S)", Assert.class, "Field '" + fieldName + "' is not found") |
| 96 | + .addStatement("$T.makeAccessible(field)", ReflectionUtils.class) |
| 97 | + .addStatement("$T container = ($T) $T.getField(field, null)", beanClass, beanClass, |
| 98 | + ReflectionUtils.class) |
| 99 | + .addStatement("$T.notNull(container, $S)", Assert.class, |
| 100 | + "Container field '" + fieldName + "' must not have a null value") |
| 101 | + .addStatement("return container") |
| 102 | + .addException(ClassNotFoundException.class)); |
| 103 | + return CodeBlock.of("$T.using($T::$L)", InstanceSupplier.class, beanRegistrationCode.getClassName(), |
| 104 | + generatedMethod.getName()); |
| 105 | + } |
| 106 | + |
| 107 | + } |
| 108 | + |
| 109 | +} |
0 commit comments