|
| 1 | +/* |
| 2 | + * Copyright 2002-2022 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.beans.factory.generator; |
| 18 | + |
| 19 | +import java.util.function.Consumer; |
| 20 | +import java.util.function.Supplier; |
| 21 | + |
| 22 | +import javax.lang.model.element.Modifier; |
| 23 | + |
| 24 | +import org.springframework.aot.generator.GeneratedType; |
| 25 | +import org.springframework.aot.generator.GeneratedTypeContext; |
| 26 | +import org.springframework.aot.generator.ProtectedAccess; |
| 27 | +import org.springframework.beans.factory.BeanFactory; |
| 28 | +import org.springframework.beans.factory.support.DefaultListableBeanFactory; |
| 29 | +import org.springframework.javapoet.CodeBlock; |
| 30 | +import org.springframework.javapoet.CodeBlock.Builder; |
| 31 | +import org.springframework.javapoet.MethodSpec; |
| 32 | + |
| 33 | +/** |
| 34 | + * The initialization of a {@link BeanFactory}. |
| 35 | + * |
| 36 | + * @author Andy Wilkinson |
| 37 | + * @author Stephane Nicoll |
| 38 | + * @since 6.0 |
| 39 | + */ |
| 40 | +public class BeanFactoryInitialization { |
| 41 | + |
| 42 | + private final GeneratedTypeContext generatedTypeContext; |
| 43 | + |
| 44 | + private final CodeBlock.Builder codeContributions; |
| 45 | + |
| 46 | + public BeanFactoryInitialization(GeneratedTypeContext generatedTypeContext) { |
| 47 | + this.generatedTypeContext = generatedTypeContext; |
| 48 | + this.codeContributions = CodeBlock.builder(); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Return the {@link GeneratedTypeContext} to use to contribute |
| 53 | + * additional methods or hints. |
| 54 | + * @return the generation context |
| 55 | + */ |
| 56 | + public GeneratedTypeContext generatedTypeContext() { |
| 57 | + return this.generatedTypeContext; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Contribute code that initializes the bean factory and that does not |
| 62 | + * require any privileged access. |
| 63 | + * @param code the code to contribute |
| 64 | + */ |
| 65 | + public void contribute(Consumer<Builder> code) { |
| 66 | + CodeBlock.Builder builder = CodeBlock.builder(); |
| 67 | + code.accept(builder); |
| 68 | + CodeBlock codeBlock = builder.build(); |
| 69 | + this.codeContributions.add(codeBlock); |
| 70 | + if (!codeBlock.toString().endsWith("\n")) { |
| 71 | + this.codeContributions.add("\n"); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Contribute code that initializes the bean factory. If privileged access |
| 77 | + * is required, a public method in the target package is created and |
| 78 | + * invoked, rather than contributing the code directly. |
| 79 | + * @param protectedAccess the {@link ProtectedAccess} instance to use |
| 80 | + * @param methodName a method name to use if privileged access is required |
| 81 | + * @param methodBody the contribution |
| 82 | + */ |
| 83 | + public void contribute(ProtectedAccess protectedAccess, Supplier<String> methodName, |
| 84 | + Consumer<Builder> methodBody) { |
| 85 | + String targetPackageName = this.generatedTypeContext.getMainGeneratedType().getClassName().packageName(); |
| 86 | + String protectedPackageName = protectedAccess.getPrivilegedPackageName(targetPackageName); |
| 87 | + if (protectedPackageName != null) { |
| 88 | + GeneratedType type = this.generatedTypeContext.getGeneratedType(protectedPackageName); |
| 89 | + MethodSpec.Builder method = MethodSpec.methodBuilder(methodName.get()) |
| 90 | + .addModifiers(Modifier.PUBLIC, Modifier.STATIC) |
| 91 | + .addParameter(DefaultListableBeanFactory.class, "beanFactory"); |
| 92 | + CodeBlock.Builder code = CodeBlock.builder(); |
| 93 | + methodBody.accept(code); |
| 94 | + method.addCode(code.build()); |
| 95 | + contribute(main -> main.addStatement("$T.$N(beanFactory)", type.getClassName(), type.addMethod(method))); |
| 96 | + } |
| 97 | + else { |
| 98 | + contribute(methodBody); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Return the code that has been contributed to this instance. |
| 104 | + * @return the code |
| 105 | + */ |
| 106 | + public CodeBlock toCodeBlock() { |
| 107 | + return this.codeContributions.build(); |
| 108 | + } |
| 109 | + |
| 110 | +} |
0 commit comments