Merge "Fix issue with ClassName when simple name contains '$'" into androidx-main
diff --git a/room/room-compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KSTypeJavaPoetExt.kt b/room/room-compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KSTypeJavaPoetExt.kt
index 6bcc71f..70a827c 100644
--- a/room/room-compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KSTypeJavaPoetExt.kt
+++ b/room/room-compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KSTypeJavaPoetExt.kt
@@ -96,25 +96,21 @@
     // KSP may improve that later and if not, we can improve it in Room
     // TODO: https://issuetracker.google.com/issues/168639183
     val qualified = qualifiedName?.asString() ?: return ERROR_JTYPE_NAME
+    val pkg = getNormalizedPackageName()
 
     // Note: To match KAPT behavior, a type annotated with @JvmInline is only replaced with the
     // underlying type if the inline type is used directly (e.g. MyInlineType) rather than in the
     // type args of another type, (e.g. List<MyInlineType>).
-    val isInline = isAnnotationPresent(JvmInline::class) || modifiers.contains(Modifier.INLINE)
-    val isOriginalType =
-        typeResolutionContext.originalType?.declaration?.qualifiedName?.asString() == qualified
-    if (!isInline || isOriginalType) {
-        resolver.mapToJvmSignature(this).let { jvmSignature ->
-            if (!jvmSignature.isNullOrBlank()) {
-                return jvmSignature.typeNameFromJvmSignature()
-            }
+    val isInlineUsedDirectly =
+        (isAnnotationPresent(JvmInline::class) || modifiers.contains(Modifier.INLINE)) &&
+            typeResolutionContext.originalType?.declaration?.qualifiedName?.asString() == qualified
+    if (pkg == "kotlin" || pkg.startsWith("kotlin.") || isInlineUsedDirectly) {
+        val jvmSignature = resolver.mapToJvmSignature(this)
+        if (!jvmSignature.isNullOrBlank()) {
+            return jvmSignature.typeNameFromJvmSignature()
         }
     }
 
-    // fallback to custom generation, it is very likely that this is an unresolved type
-    // get the package name first, it might throw for invalid types, hence we use
-    // safeGetPackageName
-    val pkg = getNormalizedPackageName()
     // using qualified name and pkg, figure out the short names.
     val shortNames = if (pkg == "") {
         qualified
diff --git a/room/room-compiler-processing/src/test/java/androidx/room/compiler/processing/XTypeElementTest.kt b/room/room-compiler-processing/src/test/java/androidx/room/compiler/processing/XTypeElementTest.kt
index 079024d..d5dd62f 100644
--- a/room/room-compiler-processing/src/test/java/androidx/room/compiler/processing/XTypeElementTest.kt
+++ b/room/room-compiler-processing/src/test/java/androidx/room/compiler/processing/XTypeElementTest.kt
@@ -2089,6 +2089,24 @@
         }
     }
 
+    @Test
+    fun testClassNameWithDollarSign() {
+        runTest(
+            sources = listOf(
+                Source.java(
+                    "test.MyClass\$Foo",
+                    """
+                    package test;
+                    class MyClass${'$'}Foo {}
+                    """.trimIndent()
+                )
+            )
+        ) { invocation ->
+            val subject = invocation.processingEnv.requireTypeElement("test.MyClass\$Foo")
+            assertThat(subject.asClassName().canonicalName).isEqualTo("test.MyClass\$Foo")
+        }
+    }
+
     /**
      * it is good to exclude methods coming from Object when testing as they differ between KSP
      * and KAPT but irrelevant for Room.