Support unnamed package name
Bug: 32342385
Test: Unit tests in the CL.
Change-Id: I607ffc5079d2a8c07d628573758cd441f83a122d
diff --git a/lifecycle/compiler/src/main/kotlin/com/android/support/lifecycle/LifecycleProcessor.kt b/lifecycle/compiler/src/main/kotlin/com/android/support/lifecycle/LifecycleProcessor.kt
index 66b4a0c..93cc22b 100644
--- a/lifecycle/compiler/src/main/kotlin/com/android/support/lifecycle/LifecycleProcessor.kt
+++ b/lifecycle/compiler/src/main/kotlin/com/android/support/lifecycle/LifecycleProcessor.kt
@@ -178,8 +178,8 @@
private fun writeAdapter(observer: LifecycleObserverInfo) {
val packageElement = MoreElements.getPackage(observer.type)
val qName = observer.type.qualifiedName.toString()
- // TODO what if no package
- val partialName = qName.substring(packageElement.toString().length + 1)
+ val partialName = if (packageElement.isUnnamed) qName else qName.substring(
+ packageElement.toString().length + 1)
val adapterName = Lifecycling.getAdapterName(partialName)
val providerParam = ParameterSpec.builder(LIFECYCLE_PROVIDER, "provider").build()
diff --git a/lifecycle/compiler/src/tests/kotlin/com/android/support/lifecycle/ValidCasesTest.kt b/lifecycle/compiler/src/tests/kotlin/com/android/support/lifecycle/ValidCasesTest.kt
index 2a1ee5d..a5dd507 100644
--- a/lifecycle/compiler/src/tests/kotlin/com/android/support/lifecycle/ValidCasesTest.kt
+++ b/lifecycle/compiler/src/tests/kotlin/com/android/support/lifecycle/ValidCasesTest.kt
@@ -37,16 +37,22 @@
@Test
fun testInheritance2() {
processClass("InheritanceOk2").compilesWithoutError().and().generatesSources(
- load("InheritanceOk2Base_LifecycleAdapter", "expected"),
- load("InheritanceOk2Derived_LifecycleAdapter", "expected")
+ load("foo", "InheritanceOk2Base_LifecycleAdapter", "expected"),
+ load("foo", "InheritanceOk2Derived_LifecycleAdapter", "expected")
)
}
@Test
fun testInheritance3() {
processClass("InheritanceOk3").compilesWithoutError().and().generatesSources(
- load("InheritanceOk3Base_LifecycleAdapter", "expected"),
- load("InheritanceOk3Derived_LifecycleAdapter", "expected")
+ load("foo", "InheritanceOk3Base_LifecycleAdapter", "expected"),
+ load("foo", "InheritanceOk3Derived_LifecycleAdapter", "expected")
)
}
+
+ @Test
+ fun testNoPackageClass() {
+ processClass("NoPackageOk", "").compilesWithoutError()
+ }
+
}
\ No newline at end of file
diff --git a/lifecycle/compiler/src/tests/kotlin/com/android/support/lifecycle/utils/TestUtils.kt b/lifecycle/compiler/src/tests/kotlin/com/android/support/lifecycle/utils/TestUtils.kt
index b3f78d4..3b06b10 100644
--- a/lifecycle/compiler/src/tests/kotlin/com/android/support/lifecycle/utils/TestUtils.kt
+++ b/lifecycle/compiler/src/tests/kotlin/com/android/support/lifecycle/utils/TestUtils.kt
@@ -25,15 +25,16 @@
import java.nio.charset.Charset
import javax.tools.JavaFileObject
-fun load(className: String, folder: String = ""): JavaFileObject {
- val folderPath = "src/tests/test-data/${if (folder.isEmpty()) "" else folder + "/" }"
+fun load(packageName: String, className: String, folder: String): JavaFileObject {
+ val folderPath = "src/tests/test-data/${if (folder.isEmpty()) "" else folder + "/"}"
val code = File("$folderPath/$className.java").readText(Charset.defaultCharset())
- return JavaFileObjects.forSourceString("foo.$className", code);
+ val fullName = "$packageName${if (packageName.isEmpty()) "" else "."}$className"
+ return JavaFileObjects.forSourceString(fullName, code);
}
-fun processClass(className: String): CompileTester {
+fun processClass(className: String, packageName: String = "foo"): CompileTester {
val processedWith = Truth.assertAbout(JavaSourceSubjectFactory.javaSource())
- .that(load(className))
+ .that(load(packageName, className, ""))
.processedWith(LifecycleProcessor())
return checkNotNull(processedWith)
}
diff --git a/lifecycle/compiler/src/tests/test-data/NoPackageOk.java b/lifecycle/compiler/src/tests/test-data/NoPackageOk.java
new file mode 100644
index 0000000..fdb95fd
--- /dev/null
+++ b/lifecycle/compiler/src/tests/test-data/NoPackageOk.java
@@ -0,0 +1,25 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import static com.android.support.lifecycle.Lifecycle.ON_STOP;
+
+import com.android.support.lifecycle.LifecycleProvider;
+import com.android.support.lifecycle.OnLifecycleEvent;
+
+class NoPackageOk {
+ @OnLifecycleEvent(ON_STOP)
+ void onStop(LifecycleProvider provider, int prevstate){}
+}
\ No newline at end of file