Update ClassNameGenerator to work with a ClassName target

This commit updates ClassNameGenerator so that it uses a ClassName for
its default target. This makes sure that a target that has been
generated can be used.

See gh-29027
This commit is contained in:
Stephane Nicoll
2022-09-22 14:22:17 +02:00
parent 1707a22c18
commit 8ef850ff91
9 changed files with 99 additions and 81 deletions

View File

@@ -23,6 +23,7 @@ import org.springframework.aot.generate.DefaultGenerationContext;
import org.springframework.aot.generate.GenerationContext;
import org.springframework.aot.generate.InMemoryGeneratedFiles;
import org.springframework.core.test.tools.TestCompiler;
import org.springframework.javapoet.ClassName;
/**
* {@link GenerationContext} test implementation that uses
@@ -35,6 +36,11 @@ import org.springframework.core.test.tools.TestCompiler;
*/
public class TestGenerationContext extends DefaultGenerationContext implements UnaryOperator<TestCompiler> {
/**
* The default test target {@link ClassName}.
*/
public static final ClassName TEST_TARGET = ClassName.get("com.example", "TestTarget");
/**
* Create an instance using the specified {@link ClassNameGenerator}.
* @param classNameGenerator the class name generator to use
@@ -47,15 +53,15 @@ public class TestGenerationContext extends DefaultGenerationContext implements U
* Create an instance using the specified {@code target}.
* @param target the default target class to use
*/
public TestGenerationContext(Class<?> target) {
public TestGenerationContext(ClassName target) {
this(new ClassNameGenerator(target));
}
/**
* Create an instance using {@link TestTarget} as the {@code target}.
* Create an instance using {@link #TEST_TARGET} as the {@code target}.
*/
public TestGenerationContext() {
this(TestTarget.class);
this(TEST_TARGET);
}

View File

@@ -1,26 +0,0 @@
/*
* 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.aot.test.generate;
/**
* A <em>default target class</em> used by tests of code generation.
*
* @author Stephane Nicoll
* @since 6.0
*/
public class TestTarget {
}