Fix JavacFiler.writeSource() when the passed package name is empty
Test: GeneratedCodeMatchTest
Change-Id: If3c278fbc2b5e98d4438cc91fa033454494e94f1
diff --git a/room/room-compiler-processing-testing/src/test/java/androidx/room/compiler/processing/util/GeneratedCodeMatchTest.kt b/room/room-compiler-processing-testing/src/test/java/androidx/room/compiler/processing/util/GeneratedCodeMatchTest.kt
index fc964f9..11372ae 100644
--- a/room/room-compiler-processing-testing/src/test/java/androidx/room/compiler/processing/util/GeneratedCodeMatchTest.kt
+++ b/room/room-compiler-processing-testing/src/test/java/androidx/room/compiler/processing/util/GeneratedCodeMatchTest.kt
@@ -93,6 +93,38 @@
}
@Test
+ fun successfulGeneratedJavaCodeMatchWithWriteSourceNoPackage() {
+ val file = JavaFile.builder(
+ "",
+ TypeSpec.classBuilder("Baz").build()
+ ).build()
+ runTest { invocation ->
+ if (invocation.processingEnv.findTypeElement("Baz") == null) {
+ val originatingElements: List<XElement> =
+ file.typeSpec.originatingElements.map {
+ it.toXProcessing(invocation.processingEnv)
+ }
+ invocation.processingEnv.filer.writeSource(
+ file.packageName,
+ file.typeSpec.name,
+ "java",
+ originatingElements
+ ).bufferedWriter().use {
+ it.write(file.toString())
+ }
+ }
+ invocation.assertCompilationResult {
+ generatedSource(
+ Source.java(
+ "Baz",
+ file.toString()
+ )
+ )
+ }
+ }
+ }
+
+ @Test
fun missingGeneratedCode() {
val result = runCatching {
runTest { invocation ->
@@ -186,6 +218,38 @@
}
@Test
+ fun successfulGeneratedKotlinCodeMatchWithWriteSourceNoPackage() {
+ // java environment will not generate kotlin files
+ runTest.assumeCanCompileKotlin()
+
+ val type = KTypeSpec.classBuilder("Baz").build()
+ val file = FileSpec.builder("", "Baz")
+ .addType(type)
+ .build()
+ runTest { invocation ->
+ if (invocation.processingEnv.findTypeElement("Baz") == null) {
+ val originatingElements: List<XElement> =
+ type.originatingElements.map {
+ it.toXProcessing(invocation.processingEnv)
+ }
+ invocation.processingEnv.filer.writeSource(
+ file.packageName,
+ file.name,
+ "kt",
+ originatingElements
+ ).bufferedWriter().use {
+ it.write(file.toString())
+ }
+ }
+ invocation.assertCompilationResult {
+ generatedSource(
+ Source.kotlin("Baz.kt", file.toString())
+ )
+ }
+ }
+ }
+
+ @Test
fun successfulGeneratedKotlinCodeMatch() {
// java environment will not generate kotlin files
runTest.assumeCanCompileKotlin()
diff --git a/room/room-compiler-processing/src/main/java/androidx/room/compiler/processing/javac/JavacFiler.kt b/room/room-compiler-processing/src/main/java/androidx/room/compiler/processing/javac/JavacFiler.kt
index 2043d41..779539c 100644
--- a/room/room-compiler-processing/src/main/java/androidx/room/compiler/processing/javac/JavacFiler.kt
+++ b/room/room-compiler-processing/src/main/java/androidx/room/compiler/processing/javac/JavacFiler.kt
@@ -60,8 +60,13 @@
originatingElements.filterIsInstance<JavacElement>().map { it.element }.toTypedArray()
return when (extension) {
"java" -> {
+ val name = if (packageName.isEmpty()) {
+ fileNameWithoutExtension
+ } else {
+ "$packageName.$fileNameWithoutExtension"
+ }
delegate.createSourceFile(
- "$packageName.$fileNameWithoutExtension",
+ name,
*javaOriginatingElements
).openOutputStream()
}