Browse Source
We now replace ManagedTypes bean definitions with generated code that contain the discovered types to avoid class path scaning. Closes: #2680 Original pull request: #2682.pull/2700/head
9 changed files with 543 additions and 6 deletions
@ -0,0 +1,27 @@
@@ -0,0 +1,27 @@
|
||||
/* |
||||
* Copyright 2022 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.data.aot; |
||||
|
||||
import org.springframework.beans.factory.aot.BeanRegistrationAotContribution; |
||||
import org.springframework.beans.factory.support.RegisteredBean; |
||||
|
||||
/** |
||||
* @author Christoph Strobl |
||||
*/ |
||||
public interface RegisteredBeanAotContribution extends BeanRegistrationAotContribution { |
||||
|
||||
RegisteredBean getSource(); |
||||
} |
||||
@ -0,0 +1,92 @@
@@ -0,0 +1,92 @@
|
||||
/* |
||||
* Copyright 2022 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.data.aot; |
||||
|
||||
import java.util.function.Consumer; |
||||
|
||||
import javax.lang.model.element.Modifier; |
||||
|
||||
import org.mockito.Mockito; |
||||
import org.springframework.aot.test.generate.TestGenerationContext; |
||||
import org.springframework.aot.test.generate.compile.Compiled; |
||||
import org.springframework.aot.test.generate.compile.TestCompiler; |
||||
import org.springframework.beans.factory.aot.BeanRegistrationAotContribution; |
||||
import org.springframework.beans.factory.aot.BeanRegistrationCodeFragments; |
||||
import org.springframework.beans.factory.support.InstanceSupplier; |
||||
import org.springframework.javapoet.CodeBlock; |
||||
import org.springframework.javapoet.MethodSpec; |
||||
import org.springframework.javapoet.ParameterizedTypeName; |
||||
|
||||
/** |
||||
* @author Christoph Strobl |
||||
*/ |
||||
public class AotTestCodeContributionBuilder { |
||||
|
||||
TestGenerationContext generationContext; |
||||
MockBeanRegistrationCode beanRegistrationCode; |
||||
BeanRegistrationAotContribution contribution; |
||||
|
||||
static AotTestCodeContributionBuilder withContextFor(Class<?> type) { |
||||
return withContext(new TestGenerationContext(type)); |
||||
} |
||||
|
||||
static AotTestCodeContributionBuilder withContext(TestGenerationContext ctx) { |
||||
|
||||
AotTestCodeContributionBuilder codeGenerationBuilder = new AotTestCodeContributionBuilder(); |
||||
codeGenerationBuilder.generationContext = ctx; |
||||
codeGenerationBuilder.beanRegistrationCode = new MockBeanRegistrationCode(ctx); |
||||
return codeGenerationBuilder; |
||||
} |
||||
|
||||
BeanRegistrationCodeFragments getFragments(BeanRegistrationAotContribution contribution) { |
||||
|
||||
this.contribution = contribution; |
||||
|
||||
return contribution.customizeBeanRegistrationCodeFragments(generationContext, |
||||
Mockito.mock(BeanRegistrationCodeFragments.class)); |
||||
} |
||||
|
||||
AotTestCodeContributionBuilder writeContentFor(BeanRegistrationAotContribution contribution) { |
||||
|
||||
CodeBlock codeBlock = getFragments(contribution).generateInstanceSupplierCode(generationContext, |
||||
beanRegistrationCode, null, false); |
||||
|
||||
Class<?> beanType = Object.class; |
||||
try { |
||||
beanType = contribution instanceof RegisteredBeanAotContribution |
||||
? ((RegisteredBeanAotContribution) contribution).getSource().getBeanClass() |
||||
: Object.class; |
||||
} catch (Exception e) {} |
||||
|
||||
ParameterizedTypeName parameterizedReturnTypeName = ParameterizedTypeName.get(InstanceSupplier.class, beanType); |
||||
beanRegistrationCode.getTypeBuilder().set(type -> { |
||||
type.addModifiers(Modifier.PUBLIC); |
||||
type.addMethod(MethodSpec.methodBuilder("get").addModifiers(Modifier.PUBLIC).returns(parameterizedReturnTypeName) |
||||
.addStatement("return $L", codeBlock).build()); |
||||
}); |
||||
|
||||
return this; |
||||
} |
||||
|
||||
public void compile() { |
||||
compile(it -> {}); |
||||
} |
||||
|
||||
public void compile(Consumer<Compiled> compiled) { |
||||
generationContext.writeGeneratedContent(); |
||||
TestCompiler.forSystem().withFiles(generationContext.getGeneratedFiles()).compile(compiled); |
||||
} |
||||
} |
||||
@ -0,0 +1,42 @@
@@ -0,0 +1,42 @@
|
||||
/* |
||||
* Copyright 2022 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.data.aot; |
||||
|
||||
import java.util.function.Consumer; |
||||
|
||||
import org.springframework.javapoet.TypeSpec; |
||||
import org.springframework.javapoet.TypeSpec.Builder; |
||||
import org.springframework.lang.Nullable; |
||||
import org.springframework.util.Assert; |
||||
|
||||
/** |
||||
* @author Christoph Strobl |
||||
*/ |
||||
public class DeferredTypeBuilder implements Consumer<Builder> { |
||||
|
||||
@Nullable |
||||
private Consumer<Builder> type; |
||||
|
||||
@Override |
||||
public void accept(Builder type) { |
||||
Assert.notNull(this.type, "No type builder set"); |
||||
this.type.accept(type); |
||||
} |
||||
|
||||
public void set(Consumer<Builder> type) { |
||||
this.type = type; |
||||
} |
||||
} |
||||
@ -0,0 +1,61 @@
@@ -0,0 +1,61 @@
|
||||
/* |
||||
* Copyright 2022 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.data.aot; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
import org.springframework.aot.generate.GeneratedClass; |
||||
import org.springframework.aot.generate.GeneratedMethods; |
||||
import org.springframework.aot.generate.GenerationContext; |
||||
import org.springframework.aot.generate.MethodReference; |
||||
import org.springframework.beans.factory.aot.BeanRegistrationCode; |
||||
import org.springframework.javapoet.ClassName; |
||||
|
||||
/** |
||||
* @author Christoph Strobl |
||||
*/ |
||||
public class MockBeanRegistrationCode implements BeanRegistrationCode { |
||||
|
||||
private final GeneratedClass generatedClass; |
||||
|
||||
private final List<MethodReference> instancePostProcessors = new ArrayList<>(); |
||||
|
||||
private final DeferredTypeBuilder typeBuilder = new DeferredTypeBuilder(); |
||||
|
||||
public MockBeanRegistrationCode(GenerationContext generationContext) { |
||||
this.generatedClass = generationContext.getGeneratedClasses().addForFeature("TestCode", this.typeBuilder); |
||||
} |
||||
|
||||
public DeferredTypeBuilder getTypeBuilder() { |
||||
return this.typeBuilder; |
||||
} |
||||
|
||||
@Override |
||||
public ClassName getClassName() { |
||||
return this.generatedClass.getName(); |
||||
} |
||||
|
||||
@Override |
||||
public GeneratedMethods getMethods() { |
||||
return this.generatedClass.getMethods(); |
||||
} |
||||
|
||||
@Override |
||||
public void addInstancePostProcessor(MethodReference methodReference) { |
||||
this.instancePostProcessors.add(methodReference); |
||||
} |
||||
} |
||||
Loading…
Reference in new issue