Handle default package with AOT processing

Adding generated code in the default package is not supported as we
intend to import it, most probably from another package, and that is
not supported. While this situation is hard to replicate with Java,
Kotlin is unfortunately more lenient and users can end up in that
situation if they forget to add a package statement.

This commit checks for the presence of a valid package, and throws
a dedicated exception if necessary.

Closes gh-31628
This commit is contained in:
Stéphane Nicoll 2023-11-20 11:44:12 +01:00
parent 7d2ea7e7e1
commit f146d09607
2 changed files with 28 additions and 0 deletions

View File

@ -20,6 +20,7 @@ import org.springframework.core.io.InputStreamSource;
import org.springframework.javapoet.JavaFile;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;
import org.springframework.util.function.ThrowingConsumer;
/**
@ -43,6 +44,7 @@ public interface GeneratedFiles {
* @param javaFile the java file to add
*/
default void addSourceFile(JavaFile javaFile) {
validatePackage(javaFile.packageName, javaFile.typeSpec.name);
String className = javaFile.packageName + "." + javaFile.typeSpec.name;
addSourceFile(className, javaFile::writeTo);
}
@ -161,11 +163,20 @@ public interface GeneratedFiles {
private static String getClassNamePath(String className) {
Assert.hasLength(className, "'className' must not be empty");
validatePackage(ClassUtils.getPackageName(className), className);
Assert.isTrue(isJavaIdentifier(className),
"'className' must be a valid identifier, got '" + className + "'");
return ClassUtils.convertClassNameToResourcePath(className) + ".java";
}
private static void validatePackage(String packageName, String className) {
if (!StringUtils.hasLength(packageName)) {
throw new IllegalArgumentException("Could not add '" + className + "', "
+ "processing classes in the default package is not supported. "
+ "Did you forget to add a package statement?");
}
}
private static boolean isJavaIdentifier(String className) {
char[] chars = className.toCharArray();
for (int i = 0; i < chars.length; i++) {

View File

@ -60,6 +60,15 @@ class GeneratedFilesTests {
.contains("Hello, World!");
}
@Test
void addSourceFileWithJavaFileInTheDefaultPackageThrowsException() {
TypeSpec helloWorld = TypeSpec.classBuilder("HelloWorld").build();
JavaFile javaFile = JavaFile.builder("", helloWorld).build();
assertThatIllegalArgumentException().isThrownBy(() -> this.generatedFiles.addSourceFile(javaFile))
.withMessage("Could not add 'HelloWorld', processing classes in the "
+ "default package is not supported. Did you forget to add a package statement?");
}
@Test
void addSourceFileWithCharSequenceAddsFile() throws Exception {
this.generatedFiles.addSourceFile("com.example.HelloWorld", "{}");
@ -73,6 +82,14 @@ class GeneratedFilesTests {
.withMessage("'className' must not be empty");
}
@Test
void addSourceFileWithCharSequenceWhenClassNameIsInTheDefaultPackageThrowsException() {
assertThatIllegalArgumentException()
.isThrownBy(() -> this.generatedFiles.addSourceFile("HelloWorld", "{}"))
.withMessage("Could not add 'HelloWorld', processing classes in the "
+ "default package is not supported. Did you forget to add a package statement?");
}
@Test
void addSourceFileWithCharSequenceWhenClassNameIsInvalidThrowsException() {
assertThatIllegalArgumentException()