Add method modifier check
Bug: 32342385
Test: Unit tests in the CL.
Change-Id: If1811c1ed3f10358ed061be01b73a5bd7d27e400
diff --git a/lifecycle/processor/src/main/kotlin/com/android/support/lifecycle/LifecycleProcessor.kt b/lifecycle/processor/src/main/kotlin/com/android/support/lifecycle/LifecycleProcessor.kt
index d733e3d..c9b6a91 100644
--- a/lifecycle/processor/src/main/kotlin/com/android/support/lifecycle/LifecycleProcessor.kt
+++ b/lifecycle/processor/src/main/kotlin/com/android/support/lifecycle/LifecycleProcessor.kt
@@ -36,6 +36,8 @@
" must be an int and represent the previous state"
const val INVALID_FIRST_ARGUMENT = "1st argument of a callback method must be " +
"a LifecycleProvider which represents the source of the event"
+ const val INVALID_METHOD_MODIFIER = "method marked with OnState annotation can not be " +
+ "private"
}
private val LIFECYCLE_PROVIDER = ClassName.get(LifecycleProvider::class.java)
@@ -54,10 +56,12 @@
}
private fun validateMethod(method: ExecutableElement) {
+ if (Modifier.PRIVATE in method.modifiers) {
+ printErrorMessage(INVALID_METHOD_MODIFIER, method)
+ }
if (method.parameters.size > 2) {
printErrorMessage(TOO_MANY_ARGS_ERROR_MSG, method)
}
-
if (method.parameters.size > 1) {
// 2nd parameter must be an int
checkParameter(method.parameters[1], Integer.TYPE, INVALID_SECOND_ARGUMENT)
diff --git a/lifecycle/processor/src/tests/kotlin/com/android/support/lifecycle/ProcessorTest.kt b/lifecycle/processor/src/tests/kotlin/com/android/support/lifecycle/ProcessorTest.kt
index 60e9337..1cb5c93 100644
--- a/lifecycle/processor/src/tests/kotlin/com/android/support/lifecycle/ProcessorTest.kt
+++ b/lifecycle/processor/src/tests/kotlin/com/android/support/lifecycle/ProcessorTest.kt
@@ -50,6 +50,12 @@
}
@Test
+ fun testInvalidMethodModifier() {
+ processClass("InvalidMethodModifier").failsToCompile()?.withErrorContaining(
+ LifecycleProcessor.INVALID_METHOD_MODIFIER)
+ }
+
+ @Test
fun testTooManyArguments() {
processClass("TooManyArgs").failsToCompile()?.withErrorContaining(
LifecycleProcessor.TOO_MANY_ARGS_ERROR_MSG)
diff --git a/lifecycle/processor/src/tests/test-data/InvalidMethodModifier.java b/lifecycle/processor/src/tests/test-data/InvalidMethodModifier.java
new file mode 100644
index 0000000..f8231c8
--- /dev/null
+++ b/lifecycle/processor/src/tests/test-data/InvalidMethodModifier.java
@@ -0,0 +1,26 @@
+/*
+ * 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.
+ */
+
+package foo;
+
+import static com.android.support.lifecycle.Lifecycle.STOPPED;
+
+import com.android.support.lifecycle.OnState;
+
+public class InvalidMethodModifier {
+ @OnState(STOPPED)
+ private void onStop(int prevState){}
+}