Migrate AOT tests to use GeneratedClasses and refine/polish AOT APIs
Migrate all AOT tests to make use of `GeneratedClasses` rather than directly generating Java files. This commit also refines and polishes AOT APIs to being greater consistency. Specifically: - The `MethodGenerator` interface has been removed in favor of working directly with `GeneratedMethods`. - The visibility of several constructors and methods has been reduced to package-private. - The `using(...)` and `builder` methods have been removed in favor of setting the `Consumer` callbacks directly as constructor arguments. - Variable names for builders are now named `type` or `method` depending on what they're building. Closes gh-28831
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright 2002-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.beans.testfixture.beans.factory.aot;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import org.springframework.javapoet.TypeSpec;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* {@link TypeSpec.Builder} {@link Consumer} that can be used to defer the to
|
||||
* another consumer that is set at a later point.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @since 6.0
|
||||
*/
|
||||
public class DeferredTypeBuilder implements Consumer<TypeSpec.Builder> {
|
||||
|
||||
private Consumer<TypeSpec.Builder> type;
|
||||
|
||||
@Override
|
||||
public void accept(TypeSpec.Builder type) {
|
||||
Assert.notNull(this.type, "No type builder set");
|
||||
this.type.accept(type);
|
||||
}
|
||||
|
||||
public void set(Consumer<TypeSpec.Builder> type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -20,7 +20,9 @@ import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
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.BeanFactoryInitializationCode;
|
||||
|
||||
@@ -28,16 +30,33 @@ import org.springframework.beans.factory.aot.BeanFactoryInitializationCode;
|
||||
* Mock {@link BeanFactoryInitializationCode} implementation.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
public class MockBeanFactoryInitializationCode implements BeanFactoryInitializationCode {
|
||||
|
||||
private final GeneratedMethods generatedMethods = new GeneratedMethods();
|
||||
private final GeneratedClass generatedClass;
|
||||
|
||||
private final List<MethodReference> initializers = new ArrayList<>();
|
||||
|
||||
private final DeferredTypeBuilder typeBuilder = new DeferredTypeBuilder();
|
||||
|
||||
|
||||
public MockBeanFactoryInitializationCode(GenerationContext generationContext) {
|
||||
this.generatedClass = generationContext.getGeneratedClasses().addForFeature("TestCode", typeBuilder);
|
||||
}
|
||||
|
||||
|
||||
public DeferredTypeBuilder getTypeBuilder() {
|
||||
return typeBuilder;
|
||||
}
|
||||
|
||||
public GeneratedClass getGeneratedClass() {
|
||||
return generatedClass;
|
||||
}
|
||||
|
||||
@Override
|
||||
public GeneratedMethods getMethodGenerator() {
|
||||
return this.generatedMethods;
|
||||
public GeneratedMethods getMethods() {
|
||||
return this.generatedClass.getMethods();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -20,7 +20,9 @@ import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
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;
|
||||
@@ -33,28 +35,34 @@ import org.springframework.javapoet.ClassName;
|
||||
*/
|
||||
public class MockBeanRegistrationCode implements BeanRegistrationCode {
|
||||
|
||||
private final ClassName className;
|
||||
|
||||
private final GeneratedMethods generatedMethods = new GeneratedMethods();
|
||||
private final GeneratedClass generatedClass;
|
||||
|
||||
private final List<MethodReference> instancePostProcessors = new ArrayList<>();
|
||||
|
||||
public MockBeanRegistrationCode(ClassName className) {
|
||||
this.className = className;
|
||||
private final DeferredTypeBuilder typeBuilder = new DeferredTypeBuilder();
|
||||
|
||||
|
||||
public MockBeanRegistrationCode(GenerationContext generationContext) {
|
||||
this.generatedClass = generationContext.getGeneratedClasses().addForFeature("TestCode", this.typeBuilder);
|
||||
}
|
||||
|
||||
public MockBeanRegistrationCode() {
|
||||
this(ClassName.get("com.example", "Test"));
|
||||
|
||||
public DeferredTypeBuilder getTypeBuilder() {
|
||||
return typeBuilder;
|
||||
}
|
||||
|
||||
public GeneratedClass getGeneratedClass() {
|
||||
return this.generatedClass;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ClassName getClassName() {
|
||||
return this.className;
|
||||
return this.generatedClass.getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public GeneratedMethods getMethodGenerator() {
|
||||
return this.generatedMethods;
|
||||
public GeneratedMethods getMethods() {
|
||||
return this.generatedClass.getMethods();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -16,7 +16,9 @@
|
||||
|
||||
package org.springframework.beans.testfixture.beans.factory.aot;
|
||||
|
||||
import org.springframework.aot.generate.GeneratedClass;
|
||||
import org.springframework.aot.generate.GeneratedMethods;
|
||||
import org.springframework.aot.generate.GenerationContext;
|
||||
import org.springframework.beans.factory.aot.BeanRegistrationsCode;
|
||||
import org.springframework.javapoet.ClassName;
|
||||
|
||||
@@ -28,26 +30,32 @@ import org.springframework.javapoet.ClassName;
|
||||
*/
|
||||
public class MockBeanRegistrationsCode implements BeanRegistrationsCode {
|
||||
|
||||
private final ClassName className;
|
||||
private final GeneratedClass generatedClass;
|
||||
|
||||
private final GeneratedMethods generatedMethods = new GeneratedMethods();
|
||||
private final DeferredTypeBuilder typeBuilder = new DeferredTypeBuilder();
|
||||
|
||||
public MockBeanRegistrationsCode(ClassName className) {
|
||||
this.className = className;
|
||||
|
||||
public MockBeanRegistrationsCode(GenerationContext generationContext) {
|
||||
this.generatedClass = generationContext.getGeneratedClasses().addForFeature("TestCode", this.typeBuilder);
|
||||
}
|
||||
|
||||
public MockBeanRegistrationsCode() {
|
||||
this(ClassName.get("com.example", "Test"));
|
||||
|
||||
public DeferredTypeBuilder getTypeBuilder() {
|
||||
return typeBuilder;
|
||||
}
|
||||
|
||||
public GeneratedClass getGeneratedClass() {
|
||||
return this.generatedClass;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ClassName getClassName() {
|
||||
return this.className;
|
||||
return this.generatedClass.getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public GeneratedMethods getMethodGenerator() {
|
||||
return this.generatedMethods;
|
||||
public GeneratedMethods getMethods() {
|
||||
return this.generatedClass.getMethods();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user