[KeepAnno] Remove the constraint enum value ANNOTATIONS

The `constrainAnnotations` property is both more clear and flexible.

Change-Id: Ib997baac4acff749d19b55f0d6d9eba649e87e6d
diff --git a/src/keepanno/java/com/android/tools/r8/keepanno/annotations/KeepConstraint.java b/src/keepanno/java/com/android/tools/r8/keepanno/annotations/KeepConstraint.java
index 8cfa99d..bd6f7c5 100644
--- a/src/keepanno/java/com/android/tools/r8/keepanno/annotations/KeepConstraint.java
+++ b/src/keepanno/java/com/android/tools/r8/keepanno/annotations/KeepConstraint.java
@@ -209,12 +209,4 @@
    * non-visible uses requires the same annotations to preserve as for reflective uses.
    */
   CLASS_OPEN_HIERARCHY,
-
-  /**
-   * Indicates that the annotations on the target item are being accessed reflectively.
-   *
-   * <p>If only a particular set of annotations is accessed, you should set the TBD property on the
-   * target item.
-   */
-  ANNOTATIONS,
 }
diff --git a/src/keepanno/java/com/android/tools/r8/keepanno/annotations/KeepTarget.java b/src/keepanno/java/com/android/tools/r8/keepanno/annotations/KeepTarget.java
index e5be58a..20e5492 100644
--- a/src/keepanno/java/com/android/tools/r8/keepanno/annotations/KeepTarget.java
+++ b/src/keepanno/java/com/android/tools/r8/keepanno/annotations/KeepTarget.java
@@ -119,13 +119,7 @@
    * the item will be removed too. If the annotation types themselves are used reflectively then
    * they too need a keep annotation or rule to ensure they remain in the program.
    *
-   * <p>Setting this to a non-empty array implicitly includes {@link KeepConstraint#ANNOTATIONS} in
-   * the set of active constraints.
-   *
-   * <p>By default no annotation patterns are defined and no annotations are required to remain,
-   * unless constraints explicitly includes {@link KeepConstraint#ANNOTATIONS} in that case the
-   * default pattern is the default {@link AnnotationPattern} which matches all annotations with
-   * retention policy {@code RetentionPolicy#RUNTIME}
+   * <p>By default no annotation patterns are defined and no annotations are required to remain.
    *
    * @return Annotation patterns
    */
diff --git a/src/keepanno/java/com/android/tools/r8/keepanno/annotations/UsedByNative.java b/src/keepanno/java/com/android/tools/r8/keepanno/annotations/UsedByNative.java
index f81d218..076a93c 100644
--- a/src/keepanno/java/com/android/tools/r8/keepanno/annotations/UsedByNative.java
+++ b/src/keepanno/java/com/android/tools/r8/keepanno/annotations/UsedByNative.java
@@ -141,13 +141,7 @@
    * the item will be removed too. If the annotation types themselves are used reflectively then
    * they too need a keep annotation or rule to ensure they remain in the program.
    *
-   * <p>Setting this to a non-empty array implicitly includes {@link KeepConstraint#ANNOTATIONS} in
-   * the set of active constraints.
-   *
-   * <p>By default no annotation patterns are defined and no annotations are required to remain,
-   * unless constraints explicitly includes {@link KeepConstraint#ANNOTATIONS} in that case the
-   * default pattern is the default {@link AnnotationPattern} which matches all annotations with
-   * retention policy {@code RetentionPolicy#RUNTIME}
+   * <p>By default no annotation patterns are defined and no annotations are required to remain.
    *
    * @return Annotation patterns
    */
diff --git a/src/keepanno/java/com/android/tools/r8/keepanno/annotations/UsedByReflection.java b/src/keepanno/java/com/android/tools/r8/keepanno/annotations/UsedByReflection.java
index a64c7c8..cf2225d 100644
--- a/src/keepanno/java/com/android/tools/r8/keepanno/annotations/UsedByReflection.java
+++ b/src/keepanno/java/com/android/tools/r8/keepanno/annotations/UsedByReflection.java
@@ -141,13 +141,7 @@
    * the item will be removed too. If the annotation types themselves are used reflectively then
    * they too need a keep annotation or rule to ensure they remain in the program.
    *
-   * <p>Setting this to a non-empty array implicitly includes {@link KeepConstraint#ANNOTATIONS} in
-   * the set of active constraints.
-   *
-   * <p>By default no annotation patterns are defined and no annotations are required to remain,
-   * unless constraints explicitly includes {@link KeepConstraint#ANNOTATIONS} in that case the
-   * default pattern is the default {@link AnnotationPattern} which matches all annotations with
-   * retention policy {@code RetentionPolicy#RUNTIME}
+   * <p>By default no annotation patterns are defined and no annotations are required to remain.
    *
    * @return Annotation patterns
    */
diff --git a/src/keepanno/java/com/android/tools/r8/keepanno/asm/KeepConstraintsVisitor.java b/src/keepanno/java/com/android/tools/r8/keepanno/asm/KeepConstraintsVisitor.java
index d044892..33bf5be 100644
--- a/src/keepanno/java/com/android/tools/r8/keepanno/asm/KeepConstraintsVisitor.java
+++ b/src/keepanno/java/com/android/tools/r8/keepanno/asm/KeepConstraintsVisitor.java
@@ -67,9 +67,6 @@
       case Constraints.CLASS_OPEN_HIERARCHY:
         builder.add(KeepConstraint.classOpenHierarchy());
         break;
-      case Constraints.ANNOTATIONS:
-        builder.add(KeepConstraint.annotationsAllWithRuntimeRetention());
-        break;
       default:
         super.visitEnum(ignore, descriptor, value);
     }
diff --git a/src/keepanno/java/com/android/tools/r8/keepanno/asm/KeepEdgeReader.java b/src/keepanno/java/com/android/tools/r8/keepanno/asm/KeepEdgeReader.java
index 36c6de6..1c6c71e 100644
--- a/src/keepanno/java/com/android/tools/r8/keepanno/asm/KeepEdgeReader.java
+++ b/src/keepanno/java/com/android/tools/r8/keepanno/asm/KeepEdgeReader.java
@@ -756,30 +756,23 @@
       if (isDefault()) {
         return null;
       }
-      // If only the annotations are set, add them as an extension of the defaults.
-      if (constraintsParser.isDefault()) {
-        assert annotationsParser.isDeclared();
-        KeepConstraints.Builder builder = KeepConstraints.builder();
-        annotationsParser
-            .getValue()
-            .forEach(pattern -> builder.add(KeepConstraint.annotation(pattern)));
-        return KeepConstraints.defaultAdditions(builder.build());
-      }
       // If only the constraints are set then those are the constraints as is.
       if (annotationsParser.isDefault()) {
         assert constraintsParser.isDeclared();
         return constraintsParser.getValue();
       }
-      // Finally if both are set, the explicit annotations override the constraint set.
-      // Filter out the default and add all the explicit patterns.
-      assert constraintsParser.isDeclared();
-      assert annotationsParser.isDeclared();
-      KeepConstraints.Builder builder =
-          KeepConstraints.builder().copyFrom(constraintsParser.getValue()).removeAnnotations();
-      List<KeepAnnotationPattern> annotationPatterns = annotationsParser.getValue();
-      for (KeepAnnotationPattern pattern : annotationPatterns) {
-        builder.add(KeepConstraint.annotation(pattern));
+      KeepConstraints.Builder builder;
+      if (constraintsParser.isDeclared()) {
+        // If constraints are set use it as the initial set.
+        builder = KeepConstraints.builder().copyFrom(constraintsParser.getValue());
+        assert builder.verifyNoAnnotations();
+      } else {
+        // If only the annotations are set, add them as an extension of the defaults.
+        builder = KeepConstraints.builder().copyFrom(KeepConstraints.defaultConstraints());
       }
+      annotationsParser
+          .getValue()
+          .forEach(pattern -> builder.add(KeepConstraint.annotation(pattern)));
       return builder.build();
     }
   }
diff --git a/src/keepanno/java/com/android/tools/r8/keepanno/ast/AnnotationConstants.java b/src/keepanno/java/com/android/tools/r8/keepanno/ast/AnnotationConstants.java
index a2249c5..239dd0e 100644
--- a/src/keepanno/java/com/android/tools/r8/keepanno/ast/AnnotationConstants.java
+++ b/src/keepanno/java/com/android/tools/r8/keepanno/ast/AnnotationConstants.java
@@ -170,7 +170,6 @@
     public static final String FIELD_REPLACE = "FIELD_REPLACE";
     public static final String NEVER_INLINE = "NEVER_INLINE";
     public static final String CLASS_OPEN_HIERARCHY = "CLASS_OPEN_HIERARCHY";
-    public static final String ANNOTATIONS = "ANNOTATIONS";
   }
 
   public static final class Option {
diff --git a/src/keepanno/java/com/android/tools/r8/keepanno/ast/KeepConstraints.java b/src/keepanno/java/com/android/tools/r8/keepanno/ast/KeepConstraints.java
index fd543ed..eb5cc2e 100644
--- a/src/keepanno/java/com/android/tools/r8/keepanno/ast/KeepConstraints.java
+++ b/src/keepanno/java/com/android/tools/r8/keepanno/ast/KeepConstraints.java
@@ -54,9 +54,9 @@
       return this;
     }
 
-    public Builder removeAnnotations() {
-      constraints.removeIf(constraint -> constraint instanceof Annotation);
-      return this;
+    public boolean verifyNoAnnotations() {
+      assert constraints.stream().noneMatch(constraint -> constraint instanceof Annotation);
+      return true;
     }
 
     public Builder add(KeepConstraint constraint) {
diff --git a/src/test/java/com/android/tools/r8/keepanno/ClassAnnotatedByAnyAnnoPatternTest.java b/src/test/java/com/android/tools/r8/keepanno/ClassAnnotatedByAnyAnnoPatternTest.java
index 684a4e2..6e8ea51 100644
--- a/src/test/java/com/android/tools/r8/keepanno/ClassAnnotatedByAnyAnnoPatternTest.java
+++ b/src/test/java/com/android/tools/r8/keepanno/ClassAnnotatedByAnyAnnoPatternTest.java
@@ -10,6 +10,7 @@
 import com.android.tools.r8.TestBase;
 import com.android.tools.r8.TestParameters;
 import com.android.tools.r8.TestParametersCollection;
+import com.android.tools.r8.keepanno.annotations.AnnotationPattern;
 import com.android.tools.r8.keepanno.annotations.ClassNamePattern;
 import com.android.tools.r8.keepanno.annotations.KeepConstraint;
 import com.android.tools.r8.keepanno.annotations.KeepItemKind;
@@ -91,11 +92,14 @@
 
   static class Reflector {
 
-    @UsesReflection({
-      @KeepTarget(
-          classAnnotatedByClassNamePattern = @ClassNamePattern,
-          constraints = {KeepConstraint.ANNOTATIONS, KeepConstraint.NAME}),
-    })
+    @UsesReflection(
+        @KeepTarget(
+            classAnnotatedByClassNamePattern = @ClassNamePattern,
+            constraints = KeepConstraint.NAME,
+            constrainAnnotations = {
+              @AnnotationPattern(constant = A1.class),
+              @AnnotationPattern(constant = A2.class)
+            }))
     public void foo(Class<?>... classes) throws Exception {
       for (Class<?> clazz : classes) {
         if (clazz.getAnnotations().length > 0) {
diff --git a/src/test/java/com/android/tools/r8/keepanno/ClassAnnotatedByPatternsTest.java b/src/test/java/com/android/tools/r8/keepanno/ClassAnnotatedByPatternsTest.java
index 6d7e6c8..d828d7f 100644
--- a/src/test/java/com/android/tools/r8/keepanno/ClassAnnotatedByPatternsTest.java
+++ b/src/test/java/com/android/tools/r8/keepanno/ClassAnnotatedByPatternsTest.java
@@ -11,6 +11,7 @@
 import com.android.tools.r8.TestBase;
 import com.android.tools.r8.TestParameters;
 import com.android.tools.r8.TestParametersCollection;
+import com.android.tools.r8.keepanno.annotations.AnnotationPattern;
 import com.android.tools.r8.keepanno.annotations.KeepConstraint;
 import com.android.tools.r8.keepanno.annotations.KeepItemKind;
 import com.android.tools.r8.keepanno.annotations.KeepTarget;
@@ -95,7 +96,8 @@
     @UsesReflection(
         @KeepTarget(
             classAnnotatedByClassConstant = A1.class,
-            constraints = {KeepConstraint.ANNOTATIONS, KeepConstraint.NAME}))
+            constraints = KeepConstraint.NAME,
+            constrainAnnotations = @AnnotationPattern(constant = A1.class)))
     public void foo(Class<?>... classes) throws Exception {
       for (Class<?> clazz : classes) {
         if (clazz.isAnnotationPresent(A1.class)) {
diff --git a/src/test/java/com/android/tools/r8/keepanno/KeepAnnotationViaSuperTest.java b/src/test/java/com/android/tools/r8/keepanno/KeepAnnotationViaSuperTest.java
index dda99e1..6b4bfac 100644
--- a/src/test/java/com/android/tools/r8/keepanno/KeepAnnotationViaSuperTest.java
+++ b/src/test/java/com/android/tools/r8/keepanno/KeepAnnotationViaSuperTest.java
@@ -10,7 +10,7 @@
 import com.android.tools.r8.TestBase;
 import com.android.tools.r8.TestParameters;
 import com.android.tools.r8.TestParametersCollection;
-import com.android.tools.r8.keepanno.annotations.KeepConstraint;
+import com.android.tools.r8.keepanno.annotations.AnnotationPattern;
 import com.android.tools.r8.keepanno.annotations.KeepTarget;
 import com.android.tools.r8.keepanno.annotations.UsesReflection;
 import com.android.tools.r8.utils.AndroidApiLevel;
@@ -106,7 +106,8 @@
     @UsesReflection({
       @KeepTarget(
           instanceOfClassConstantExclusive = Base.class,
-          constraints = {KeepConstraint.ANNOTATIONS})
+          constraints = {},
+          constrainAnnotations = @AnnotationPattern(constant = Anno.class))
     })
     public Base() {
       Anno annotation = getClass().getAnnotation(Anno.class);
diff --git a/src/test/java/com/android/tools/r8/keepanno/KeepUsedByNativeAnnotationTest.java b/src/test/java/com/android/tools/r8/keepanno/KeepUsedByNativeAnnotationTest.java
index 19e2290..7c57ea6 100644
--- a/src/test/java/com/android/tools/r8/keepanno/KeepUsedByNativeAnnotationTest.java
+++ b/src/test/java/com/android/tools/r8/keepanno/KeepUsedByNativeAnnotationTest.java
@@ -10,8 +10,8 @@
 import com.android.tools.r8.TestBase;
 import com.android.tools.r8.TestParameters;
 import com.android.tools.r8.TestParametersCollection;
+import com.android.tools.r8.keepanno.annotations.AnnotationPattern;
 import com.android.tools.r8.keepanno.annotations.KeepCondition;
-import com.android.tools.r8.keepanno.annotations.KeepConstraint;
 import com.android.tools.r8.keepanno.annotations.KeepItemKind;
 import com.android.tools.r8.keepanno.annotations.UsedByNative;
 import com.android.tools.r8.utils.AndroidApiLevel;
@@ -108,7 +108,7 @@
         preconditions = {@KeepCondition(classConstant = A.class, methodName = "foo")},
         // Both the class and method are reflectively accessed.
         kind = KeepItemKind.CLASS_AND_MEMBERS,
-        constraintAdditions = {KeepConstraint.ANNOTATIONS})
+        constrainAnnotations = @AnnotationPattern(constant = Anno.class))
     @Anno("anno-on-bar")
     public static void bar() {
       System.out.println("Hello, world");
diff --git a/src/test/java/com/android/tools/r8/keepanno/MembersAnnotatedByPatternsTest.java b/src/test/java/com/android/tools/r8/keepanno/MembersAnnotatedByPatternsTest.java
index 642e269..0f2aadf 100644
--- a/src/test/java/com/android/tools/r8/keepanno/MembersAnnotatedByPatternsTest.java
+++ b/src/test/java/com/android/tools/r8/keepanno/MembersAnnotatedByPatternsTest.java
@@ -11,8 +11,8 @@
 import com.android.tools.r8.TestBase;
 import com.android.tools.r8.TestParameters;
 import com.android.tools.r8.TestParametersCollection;
+import com.android.tools.r8.keepanno.annotations.AnnotationPattern;
 import com.android.tools.r8.keepanno.annotations.ClassNamePattern;
-import com.android.tools.r8.keepanno.annotations.KeepConstraint;
 import com.android.tools.r8.keepanno.annotations.KeepItemKind;
 import com.android.tools.r8.keepanno.annotations.KeepTarget;
 import com.android.tools.r8.keepanno.annotations.UsedByReflection;
@@ -29,6 +29,7 @@
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
 import java.util.List;
+import org.checkerframework.checker.units.qual.A;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.junit.runners.Parameterized;
@@ -122,19 +123,23 @@
           classConstant = OnMembers.class,
           kind = KeepItemKind.CLASS_AND_MEMBERS,
           memberAnnotatedByClassConstant = A.class,
-          constraintAdditions = {KeepConstraint.ANNOTATIONS}),
+          constrainAnnotations = @AnnotationPattern(constant = A.class)),
       @KeepTarget(
           classConstant = OnFields.class,
           kind = KeepItemKind.CLASS_AND_FIELDS,
           fieldAnnotatedByClassName =
               "com.android.tools.r8.keepanno.MembersAnnotatedByPatternsTest$B",
-          constraintAdditions = {KeepConstraint.ANNOTATIONS}),
+          constrainAnnotations =
+              @AnnotationPattern(
+                  name = "com.android.tools.r8.keepanno.MembersAnnotatedByPatternsTest$B")),
       @KeepTarget(
           classConstant = OnMethods.class,
           kind = KeepItemKind.CLASS_AND_METHODS,
           methodAnnotatedByClassNamePattern =
               @ClassNamePattern(simpleName = "MembersAnnotatedByPatternsTest$C"),
-          constraintAdditions = {KeepConstraint.ANNOTATIONS})
+          constrainAnnotations =
+              @AnnotationPattern(
+                  namePattern = @ClassNamePattern(simpleName = "MembersAnnotatedByPatternsTest$C")))
     })
     public void foo(Class<?> clazz) throws Exception {
       for (Field field : clazz.getDeclaredFields()) {
diff --git a/src/test/java/com/android/tools/r8/keepanno/utils/KeepItemAnnotationGenerator.java b/src/test/java/com/android/tools/r8/keepanno/utils/KeepItemAnnotationGenerator.java
index 4eb00fc..475813b 100644
--- a/src/test/java/com/android/tools/r8/keepanno/utils/KeepItemAnnotationGenerator.java
+++ b/src/test/java/com/android/tools/r8/keepanno/utils/KeepItemAnnotationGenerator.java
@@ -542,17 +542,8 @@
               "If the annotation types themselves are used reflectively then they too need a",
               "keep annotation or rule to ensure they remain in the program.")
           .addParagraph(
-              "Setting this to a non-empty array implicitly includes ",
-              docLink(KeepConstraint.ANNOTATIONS),
-              "in the set of active constraints.")
-          .addParagraph(
               "By default no annotation patterns are defined and no annotations are required to",
-              "remain, unless",
-              CONSTRAINTS_GROUP,
-              "explicitly includes",
-              docLink(KeepConstraint.ANNOTATIONS),
-              "in that case the default pattern is the default " + docLink(AnnotationPattern.class),
-              "which matches all annotations with retention policy {@code RetentionPolicy#RUNTIME}")
+              "remain.")
           .setDocReturn("Annotation patterns")
           .defaultArrayEmpty(AnnotationPattern.class);
     }