Skip to content

Commit

Permalink
Handle default package with AOT processing
Browse files Browse the repository at this point in the history
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
  • Loading branch information
snicoll committed Nov 20, 2023
1 parent 7d2ea7e commit f146d09
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
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;

/**
Expand All @@ -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);
}
Expand Down Expand Up @@ -161,11 +163,20 @@ default void addFile(Kind kind, String path, ThrowingConsumer<Appendable> conten

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++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ void addSourceFileWithJavaFileAddsFile() throws Exception {
.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", "{}");
Expand All @@ -73,6 +82,14 @@ void addSourceFileWithCharSequenceWhenClassNameIsEmptyThrowsException() {
.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()
Expand Down

0 comments on commit f146d09

Please sign in to comment.