-
Notifications
You must be signed in to change notification settings - Fork 40.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TestcontainersBeanRegistrationAotProcessor that replaces InstanceSupp…
…lier of Container by either direct field usage or a reflection equivalent. If the field is private, the reflection will be used; otherwise, direct access to the field will be used
- Loading branch information
Showing
5 changed files
with
234 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
...ringframework/boot/testcontainers/context/TestcontainersBeanRegistrationAotProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
* Copyright 2012-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.boot.testcontainers.context; | ||
|
||
import java.lang.reflect.Field; | ||
|
||
import javax.lang.model.element.Modifier; | ||
|
||
import org.testcontainers.containers.Container; | ||
|
||
import org.springframework.aot.generate.AccessControl; | ||
import org.springframework.aot.generate.GeneratedMethod; | ||
import org.springframework.aot.generate.GenerationContext; | ||
import org.springframework.beans.factory.aot.BeanRegistrationAotContribution; | ||
import org.springframework.beans.factory.aot.BeanRegistrationAotProcessor; | ||
import org.springframework.beans.factory.aot.BeanRegistrationCode; | ||
import org.springframework.beans.factory.aot.BeanRegistrationCodeFragments; | ||
import org.springframework.beans.factory.aot.BeanRegistrationCodeFragmentsDecorator; | ||
import org.springframework.beans.factory.support.RegisteredBean; | ||
import org.springframework.beans.factory.support.RootBeanDefinition; | ||
import org.springframework.javapoet.ClassName; | ||
import org.springframework.javapoet.CodeBlock; | ||
import org.springframework.util.Assert; | ||
import org.springframework.util.ReflectionUtils; | ||
|
||
/** | ||
* {@link BeanRegistrationAotProcessor} that replaces InstanceSupplier of | ||
* {@link Container} by either direct field usage or a reflection equivalent. | ||
* <p> | ||
* If the field is private, the reflection will be used; otherwise, direct access to the | ||
* field will be used. | ||
* | ||
* @author Dmytro Nosan | ||
*/ | ||
class TestcontainersBeanRegistrationAotProcessor implements BeanRegistrationAotProcessor { | ||
|
||
@Override | ||
public BeanRegistrationAotContribution processAheadOfTime(RegisteredBean registeredBean) { | ||
RootBeanDefinition bd = registeredBean.getMergedBeanDefinition(); | ||
String attributeName = TestcontainerFieldBeanDefinition.class.getName(); | ||
Object field = bd.getAttribute(attributeName); | ||
if (field != null) { | ||
Assert.isInstanceOf(Field.class, field, | ||
"BeanDefinition attribute '" + attributeName + "' value must be a type of '" + Field.class + "'"); | ||
return BeanRegistrationAotContribution.withCustomCodeFragments( | ||
(codeFragments) -> new AotContribution(codeFragments, registeredBean, ((Field) field))); | ||
} | ||
return null; | ||
} | ||
|
||
static class AotContribution extends BeanRegistrationCodeFragmentsDecorator { | ||
|
||
private final RegisteredBean registeredBean; | ||
|
||
private final Field field; | ||
|
||
AotContribution(BeanRegistrationCodeFragments delegate, RegisteredBean registeredBean, Field field) { | ||
super(delegate); | ||
this.registeredBean = registeredBean; | ||
this.field = field; | ||
} | ||
|
||
@Override | ||
public ClassName getTarget(RegisteredBean registeredBean) { | ||
return ClassName.get(this.field.getDeclaringClass()); | ||
} | ||
|
||
@Override | ||
public CodeBlock generateInstanceSupplierCode(GenerationContext generationContext, | ||
BeanRegistrationCode beanRegistrationCode, boolean allowDirectSupplierShortcut) { | ||
if (AccessControl.forMember(this.field).isAccessibleFrom(beanRegistrationCode.getClassName())) { | ||
return CodeBlock.of("() -> $T.$L", this.field.getDeclaringClass(), this.field.getName()); | ||
} | ||
generationContext.getRuntimeHints().reflection().registerField(this.field); | ||
GeneratedMethod generatedMethod = beanRegistrationCode.getMethods() | ||
.add("getInstance", (method) -> method.addModifiers(Modifier.PRIVATE, Modifier.STATIC) | ||
.addJavadoc("Get the bean instance for '$L'.", this.registeredBean.getBeanName()) | ||
.returns(this.registeredBean.getBeanClass()) | ||
.addStatement("$T field = $T.findField($T.class, $S)", Field.class, ReflectionUtils.class, | ||
this.field.getDeclaringClass(), this.field.getName()) | ||
.addStatement("$T.notNull(field, $S)", Assert.class, | ||
"Field '" + this.field.getName() + "' is not found") | ||
.addStatement("$T.makeAccessible(field)", ReflectionUtils.class) | ||
.addStatement("$T container = $T.getField(field, null)", Object.class, ReflectionUtils.class) | ||
.addStatement("$T.notNull(container, $S)", Assert.class, | ||
"Container field '" + this.field.getName() + "' must not have a null value") | ||
.addStatement("return ($T) container", this.registeredBean.getBeanClass())); | ||
return generatedMethod.toMethodReference().toCodeBlock(); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 5 additions & 1 deletion
6
...-boot-project/spring-boot-testcontainers/src/main/resources/META-INF/spring/aot.factories
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
org.springframework.beans.factory.aot.BeanRegistrationExcludeFilter=\ | ||
org.springframework.boot.testcontainers.service.connection.ConnectionDetailsRegistrar.ServiceConnectionBeanRegistrationExcludeFilter | ||
org.springframework.boot.testcontainers.service.connection.ConnectionDetailsRegistrar.ServiceConnectionBeanRegistrationExcludeFilter,\ | ||
org.springframework.boot.testcontainers.properties.TestcontainersPropertySource.TestcontainersEventPublisherBeanRegistrationExcludeFilter | ||
|
||
org.springframework.aot.hint.RuntimeHintsRegistrar=\ | ||
org.springframework.boot.testcontainers.service.connection.ContainerConnectionDetailsFactory.ContainerConnectionDetailsFactoriesRuntimeHints | ||
|
||
org.springframework.beans.factory.aot.BeanRegistrationAotProcessor=\ | ||
org.springframework.boot.testcontainers.context.TestcontainersBeanRegistrationAotProcessor |