diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanRegistrationAotContribution.java b/spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanRegistrationAotContribution.java index 4febbdd8bc..42a16c1523 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanRegistrationAotContribution.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanRegistrationAotContribution.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2023 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. @@ -19,6 +19,7 @@ package org.springframework.beans.factory.aot; import java.util.function.UnaryOperator; import org.springframework.aot.generate.GenerationContext; +import org.springframework.lang.Nullable; import org.springframework.util.Assert; /** @@ -82,4 +83,31 @@ public interface BeanRegistrationAotContribution { }; } + /** + * Create a contribution that applies the contribution of the first contribution + * followed by the second contribution. Any contribution can be {@code null} to be + * ignored and the concatenated contribution is {@code null} if both inputs are + * {@code null}. + * @param a the first contribution + * @param b the second contribution + * @return the concatenation of the two contributions, or {@code null} if + * they are both {@code null}. + * @since 6.1 + */ + @Nullable + static BeanRegistrationAotContribution concat(@Nullable BeanRegistrationAotContribution a, + @Nullable BeanRegistrationAotContribution b) { + + if (a == null) { + return b; + } + if (b == null) { + return a; + } + return (generationContext, beanRegistrationCode) -> { + a.applyTo(generationContext, beanRegistrationCode); + b.applyTo(generationContext, beanRegistrationCode); + }; + } + } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/aot/BeanRegistrationAotContributionTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/aot/BeanRegistrationAotContributionTests.java new file mode 100644 index 0000000000..8f85becbe8 --- /dev/null +++ b/spring-beans/src/test/java/org/springframework/beans/factory/aot/BeanRegistrationAotContributionTests.java @@ -0,0 +1,70 @@ +/* + * Copyright 2002-2023 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.beans.factory.aot; + +import org.junit.jupiter.api.Test; +import org.mockito.InOrder; + +import org.springframework.aot.test.generate.TestGenerationContext; +import org.springframework.beans.testfixture.beans.factory.aot.MockBeanRegistrationCode; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.inOrder; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verifyNoInteractions; + +/** + * Tests for {@link BeanRegistrationAotContribution}. + * + * @author Stephane Nicoll + */ +class BeanRegistrationAotContributionTests { + + @Test + void concatWithBothNullReturnsNull() { + assertThat(BeanRegistrationAotContribution.concat(null, null)).isNull(); + } + + @Test + void concatWithFirstNullReturnsSecondAsIs() { + BeanRegistrationAotContribution contribution = mock(BeanRegistrationAotContribution.class); + assertThat(BeanRegistrationAotContribution.concat(null, contribution)).isSameAs(contribution); + verifyNoInteractions(contribution); + } + + @Test + void concatWithSecondNullReturnsFirstAsIs() { + BeanRegistrationAotContribution contribution = mock(BeanRegistrationAotContribution.class); + assertThat(BeanRegistrationAotContribution.concat(contribution, null)).isSameAs(contribution); + verifyNoInteractions(contribution); + } + + @Test + void concatApplyContributionsInOrder() { + BeanRegistrationAotContribution first = mock(BeanRegistrationAotContribution.class); + BeanRegistrationAotContribution second = mock(BeanRegistrationAotContribution.class); + BeanRegistrationAotContribution combined = BeanRegistrationAotContribution.concat(first, second); + assertThat(combined).isNotNull(); + TestGenerationContext generationContext = new TestGenerationContext(); + BeanRegistrationCode beanRegistrationCode = new MockBeanRegistrationCode(generationContext); + combined.applyTo(generationContext, beanRegistrationCode); + InOrder ordered = inOrder(first, second); + ordered.verify(first).applyTo(generationContext, beanRegistrationCode); + ordered.verify(second).applyTo(generationContext, beanRegistrationCode); + } + +}