Merge changes from topic "exclude-requiresapi"

* changes:
  Allow excluding annotations from metalava output
  Rename --exclude-annotations metalava option
diff --git a/src/main/java/com/android/tools/metalava/Options.kt b/src/main/java/com/android/tools/metalava/Options.kt
index 52bd926..c5ad2fb 100644
--- a/src/main/java/com/android/tools/metalava/Options.kt
+++ b/src/main/java/com/android/tools/metalava/Options.kt
@@ -83,7 +83,7 @@
 const val ARG_DOC_STUBS_SOURCE_LIST = "--write-doc-stubs-source-list"
 const val ARG_PROGUARD = "--proguard"
 const val ARG_EXTRACT_ANNOTATIONS = "--extract-annotations"
-const val ARG_EXCLUDE_ANNOTATIONS = "--exclude-annotations"
+const val ARG_EXCLUDE_ALL_ANNOTATIONS = "--exclude-all-annotations"
 const val ARG_EXCLUDE_DOCUMENTATION_FROM_STUBS = "--exclude-documentation-from-stubs"
 const val ARG_HIDE_PACKAGE = "--hide-package"
 const val ARG_MANIFEST = "--manifest"
@@ -147,6 +147,7 @@
 const val ARG_REWRITE_ANNOTATIONS = "--rewrite-annotations"
 const val ARG_INCLUDE_SOURCE_RETENTION = "--include-source-retention"
 const val ARG_PASS_THROUGH_ANNOTATION = "--pass-through-annotation"
+const val ARG_EXCLUDE_ANNOTATION = "--exclude-annotation"
 const val ARG_INCLUDE_SIG_VERSION = "--include-signature-version"
 const val ARG_UPDATE_API = "--only-update-api"
 const val ARG_CHECK_API = "--only-check-api"
@@ -218,7 +219,8 @@
     private val mutableConvertToXmlFiles: MutableList<ConvertFile> = mutableListOf()
     /** Internal list backing [passThroughAnnotations] */
     private val mutablePassThroughAnnotations: MutableSet<String> = mutableSetOf()
-
+    /** Internal list backing [excludeAnnotations] */
+    private val mutableExcludeAnnotations: MutableSet<String> = mutableSetOf()
     /** Ignored flags we've already warned about - store here such that we don't keep reporting them */
     private val alreadyWarned: MutableSet<String> = mutableSetOf()
 
@@ -490,6 +492,9 @@
     /** The set of annotation classes that should be passed through unchanged */
     var passThroughAnnotations = mutablePassThroughAnnotations
 
+    /** The set of annotation classes that should be removed from all outputs */
+    var excludeAnnotations = mutableExcludeAnnotations
+
     /**
      * A signature file to migrate nullness data from
      */
@@ -916,7 +921,7 @@
                 ARG_STUBS_SOURCE_LIST -> stubsSourceList = stringToNewFile(getValue(args, ++index))
                 ARG_DOC_STUBS_SOURCE_LIST -> docStubsSourceList = stringToNewFile(getValue(args, ++index))
 
-                ARG_EXCLUDE_ANNOTATIONS -> generateAnnotations = false
+                ARG_EXCLUDE_ALL_ANNOTATIONS -> generateAnnotations = false
 
                 ARG_EXCLUDE_DOCUMENTATION_FROM_STUBS -> includeDocumentationInStubs = false
 
@@ -932,6 +937,13 @@
                     }
                 }
 
+                ARG_EXCLUDE_ANNOTATION -> {
+                    val annotations = getValue(args, ++index)
+                    annotations.split(",").forEach { path ->
+                        mutableExcludeAnnotations.add(path)
+                    }
+                }
+
                 // Flag used by test suite to avoid including locations in
                 // the output when diffing against golden files
                 "--omit-locations" -> omitLocations = true
@@ -2367,9 +2379,11 @@
             ARG_KOTLIN_STUBS, "[CURRENTLY EXPERIMENTAL] If specified, stubs generated from Kotlin source code will " +
                 "be written in Kotlin rather than the Java programming language.",
             ARG_INCLUDE_ANNOTATIONS, "Include annotations such as @Nullable in the stub files.",
-            ARG_EXCLUDE_ANNOTATIONS, "Exclude annotations such as @Nullable from the stub files; the default.",
+            ARG_EXCLUDE_ALL_ANNOTATIONS, "Exclude annotations such as @Nullable from the stub files; the default.",
             "$ARG_PASS_THROUGH_ANNOTATION <annotation classes>", "A comma separated list of fully qualified names of " +
                 "annotation classes that must be passed through unchanged.",
+            "$ARG_EXCLUDE_ANNOTATION <annotation classes>", "A comma separated list of fully qualified names of " +
+                "annotation classes that must be stripped from metalava's outputs.",
             ARG_EXCLUDE_DOCUMENTATION_FROM_STUBS, "Exclude element documentation (javadoc and kdoc) " +
                 "from the generated stubs. (Copyright notices are not affected by this, they are always included. " +
                 "Documentation stubs (--doc-stubs) are not affected.)",
diff --git a/src/main/java/com/android/tools/metalava/model/AnnotationItem.kt b/src/main/java/com/android/tools/metalava/model/AnnotationItem.kt
index 8291145..67cc713 100644
--- a/src/main/java/com/android/tools/metalava/model/AnnotationItem.kt
+++ b/src/main/java/com/android/tools/metalava/model/AnnotationItem.kt
@@ -176,6 +176,9 @@
             if (options.passThroughAnnotations.contains(qualifiedName)) {
                 return qualifiedName
             }
+            if (options.excludeAnnotations.contains(qualifiedName)) {
+                return null
+            }
             when (qualifiedName) {
                 // Resource annotations
                 "android.support.annotation.AnimRes",
diff --git a/src/test/java/com/android/tools/metalava/ApiFileTest.kt b/src/test/java/com/android/tools/metalava/ApiFileTest.kt
index 0b9098e..c8cafc7 100644
--- a/src/test/java/com/android/tools/metalava/ApiFileTest.kt
+++ b/src/test/java/com/android/tools/metalava/ApiFileTest.kt
@@ -1708,7 +1708,7 @@
         // part of the source tree, ensure that we compute the right retention (runtime, meaning
         // it should show up in the stubs file.).
         check(
-            extraArguments = arrayOf(ARG_EXCLUDE_ANNOTATIONS),
+            extraArguments = arrayOf(ARG_EXCLUDE_ALL_ANNOTATIONS),
             sourceFiles = arrayOf(
                 java(
                     """
diff --git a/src/test/java/com/android/tools/metalava/OptionsTest.kt b/src/test/java/com/android/tools/metalava/OptionsTest.kt
index 6797b24..b2687b9 100644
--- a/src/test/java/com/android/tools/metalava/OptionsTest.kt
+++ b/src/test/java/com/android/tools/metalava/OptionsTest.kt
@@ -227,11 +227,14 @@
                                              code will be written in Kotlin rather than the Java programming language.
 --include-annotations
                                              Include annotations such as @Nullable in the stub files.
---exclude-annotations
+--exclude-all-annotations
                                              Exclude annotations such as @Nullable from the stub files; the default.
 --pass-through-annotation <annotation classes>
                                              A comma separated list of fully qualified names of annotation classes that
                                              must be passed through unchanged.
+--exclude-annotation <annotation classes>
+                                             A comma separated list of fully qualified names of annotation classes that
+                                             must be stripped from metalava's outputs.
 --exclude-documentation-from-stubs
                                              Exclude element documentation (javadoc and kdoc) from the generated stubs.
                                              (Copyright notices are not affected by this, they are always included.
diff --git a/src/test/java/com/android/tools/metalava/stub/StubsTest.kt b/src/test/java/com/android/tools/metalava/stub/StubsTest.kt
index 690c76c..84249a0 100644
--- a/src/test/java/com/android/tools/metalava/stub/StubsTest.kt
+++ b/src/test/java/com/android/tools/metalava/stub/StubsTest.kt
@@ -21,11 +21,12 @@
 import com.android.tools.lint.checks.infrastructure.LintDetectorTest.source
 import com.android.tools.lint.checks.infrastructure.TestFile
 import com.android.tools.metalava.ARG_CHECK_API
-import com.android.tools.metalava.ARG_EXCLUDE_ANNOTATIONS
+import com.android.tools.metalava.ARG_EXCLUDE_ALL_ANNOTATIONS
 import com.android.tools.metalava.ARG_EXCLUDE_DOCUMENTATION_FROM_STUBS
 import com.android.tools.metalava.ARG_HIDE_PACKAGE
 import com.android.tools.metalava.ARG_KOTLIN_STUBS
 import com.android.tools.metalava.ARG_PASS_THROUGH_ANNOTATION
+import com.android.tools.metalava.ARG_EXCLUDE_ANNOTATION
 import com.android.tools.metalava.ARG_UPDATE_API
 import com.android.tools.metalava.DriverTest
 import com.android.tools.metalava.FileFormat
@@ -2054,6 +2055,52 @@
     }
 
     @Test
+    fun `Skip RequiresApi annotation`() {
+        check(
+            extraArguments = arrayOf(
+                ARG_EXCLUDE_ANNOTATION, "androidx.annotation.RequiresApi"
+            ),
+            sourceFiles = arrayOf(
+                java(
+                    """
+                    package my.pkg;
+                    public class MyClass {
+                        @androidx.annotation.RequiresApi(21)
+                        public void testMethod() {}
+                    }
+                    """
+                ),
+                requiresApiSource
+            ),
+            expectedIssues = "",
+            api = """
+                    package androidx.annotation {
+                      public abstract class RequiresApi implements java.lang.annotation.Annotation {
+                      }
+                    }
+                    package my.pkg {
+                      public class MyClass {
+                        ctor public MyClass();
+                        method public void testMethod();
+                      }
+                    }
+                    """,
+            stubFiles = arrayOf(
+                java(
+                    """
+                    package my.pkg;
+                    @SuppressWarnings({"unchecked", "deprecation", "all"})
+                    public class MyClass {
+                    public MyClass() { throw new RuntimeException("Stub!"); }
+                    public void testMethod() { throw new RuntimeException("Stub!"); }
+                    }
+                    """
+                )
+            )
+        )
+    }
+
+    @Test
     fun `Test inaccessible constructors`() {
         // If the constructors of a class are not visible, and the class has subclasses,
         // those subclass stubs will need to reference these inaccessible constructors.
@@ -3486,9 +3533,9 @@
     }
 
     @Test
-    fun `Ensure we emit both deprecated javadoc and annotation with exclude-annotations`() {
+    fun `Ensure we emit both deprecated javadoc and annotation with exclude-all-annotations`() {
         check(
-            extraArguments = arrayOf(ARG_EXCLUDE_ANNOTATIONS),
+            extraArguments = arrayOf(ARG_EXCLUDE_ALL_ANNOTATIONS),
             compatibilityMode = false,
             sourceFiles = arrayOf(
                 java(
@@ -3528,7 +3575,7 @@
     @Test
     fun `Ensure we emit runtime and deprecated annotations in stubs with exclude-annotations`() {
         check(
-            extraArguments = arrayOf(ARG_EXCLUDE_ANNOTATIONS),
+            extraArguments = arrayOf(ARG_EXCLUDE_ALL_ANNOTATIONS),
             compatibilityMode = false,
             sourceFiles = arrayOf(
                 java(
@@ -3845,7 +3892,7 @@
         check(
             extraArguments = arrayOf(
                 ARG_UPDATE_API,
-                ARG_EXCLUDE_ANNOTATIONS
+                ARG_EXCLUDE_ALL_ANNOTATIONS
             ),
             compatibilityMode = false,
             sourceFiles = arrayOf(
@@ -3886,7 +3933,7 @@
         check(
             extraArguments = arrayOf(
                 ARG_CHECK_API,
-                ARG_EXCLUDE_ANNOTATIONS
+                ARG_EXCLUDE_ALL_ANNOTATIONS
             ),
             compatibilityMode = false,
             sourceFiles = arrayOf(