Merge "Fix JavacFiler.writeSource() when the passed package name is empty" into androidx-main am: 3e75b8b3ca
Original change: https://android-review.googlesource.com/c/platform/frameworks/support/+/2585510
Change-Id: Ibd0c3dff13311fe3ee72a58ffdc9241beb54e3e1
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
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 7a2731c..ba43888 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()
}