Apply soft pinning to targeted methods

Change-Id: Id538794491937a55e477a35fdaf1677b79a92af5
diff --git a/src/main/java/com/android/tools/r8/shaking/Enqueuer.java b/src/main/java/com/android/tools/r8/shaking/Enqueuer.java
index 8b7c63a..339a56d 100644
--- a/src/main/java/com/android/tools/r8/shaking/Enqueuer.java
+++ b/src/main/java/com/android/tools/r8/shaking/Enqueuer.java
@@ -3885,8 +3885,6 @@
     // Add all dependent members to the workqueue.
     enqueueRootItems(rootSet.getDependentItems(definition));
 
-    checkDefinitionForSoftPinning(method);
-
     // Notify analyses.
     analyses.forEach(analysis -> analysis.processNewlyLiveMethod(method, context));
   }
@@ -3911,6 +3909,7 @@
   }
 
   void traceMethodDefinitionExcludingCode(ProgramMethod method) {
+    checkDefinitionForSoftPinning(method);
     markReferencedTypesAsLive(method);
     processAnnotations(method);
     method
diff --git a/src/test/java/com/android/tools/r8/shaking/annotations/AlwaysRetainRetentionAnnotationTest.java b/src/test/java/com/android/tools/r8/shaking/annotations/AlwaysRetainRetentionAnnotationTest.java
new file mode 100644
index 0000000..597c835
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/shaking/annotations/AlwaysRetainRetentionAnnotationTest.java
@@ -0,0 +1,101 @@
+// Copyright (c) 2018, the R8 project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+package com.android.tools.r8.shaking.annotations;
+
+import static com.android.tools.r8.utils.codeinspector.Matchers.isPresent;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assume.assumeTrue;
+
+import com.android.tools.r8.TestBase;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.shaking.enums.EnumInAnnotationTest.MyAnnotation;
+import com.android.tools.r8.utils.BooleanUtils;
+import com.android.tools.r8.utils.codeinspector.AnnotationSubject;
+import com.android.tools.r8.utils.codeinspector.ClassSubject;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+import java.util.List;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+
+@RunWith(Parameterized.class)
+public class AlwaysRetainRetentionAnnotationTest extends TestBase {
+
+  private final boolean enableProguardCompatibilityMode;
+  private final boolean keepAllowShrinking;
+  private final TestParameters parameters;
+
+  @Parameters(name = "{2}, compat: {0}, keep: {1}")
+  public static List<Object[]> data() {
+    return buildParameters(
+        BooleanUtils.values(),
+        BooleanUtils.values(),
+        getTestParameters().withAllRuntimesAndApiLevels().build());
+  }
+
+  public AlwaysRetainRetentionAnnotationTest(
+      boolean enableProguardCompatibilityMode,
+      boolean keepAllowShrinking,
+      TestParameters parameters) {
+    this.enableProguardCompatibilityMode = enableProguardCompatibilityMode;
+    this.keepAllowShrinking = keepAllowShrinking;
+    this.parameters = parameters;
+  }
+
+  @Test
+  public void test() throws Exception {
+    assumeTrue(!enableProguardCompatibilityMode || !keepAllowShrinking);
+    testForR8Compat(parameters.getBackend(), enableProguardCompatibilityMode)
+        .addInnerClasses(getClass())
+        .addKeepMainRule(TestClass.class)
+        .addKeepRuntimeVisibleAnnotations()
+        .applyIf(
+            keepAllowShrinking,
+            builder -> {
+              assertFalse(enableProguardCompatibilityMode);
+              builder.addKeepRules(
+                  "-keep,allowshrinking,allowobfuscation class "
+                      + MyAnnotation.class.getTypeName());
+            })
+        .setMinApi(parameters.getApiLevel())
+        .compile()
+        .inspect(
+            inspector -> {
+              ClassSubject annotationClassSubject = inspector.clazz(MyAnnotation.class);
+              assertThat(annotationClassSubject, isPresent());
+
+              AnnotationSubject retentionAnnotationSubject =
+                  annotationClassSubject.annotation(Retention.class.getTypeName());
+              assertThat(retentionAnnotationSubject, isPresent());
+
+              AnnotationSubject targetAnnotationSubject =
+                  annotationClassSubject.annotation(Target.class.getTypeName());
+              assertThat(targetAnnotationSubject, isPresent());
+
+              AnnotationSubject myAnnotationAnnotationSubject =
+                  annotationClassSubject.annotation(MyAnnotation.class.getTypeName());
+              assertThat(myAnnotationAnnotationSubject, isPresent());
+            })
+        .run(parameters.getRuntime(), TestClass.class)
+        .assertSuccessWithOutputLines("3");
+  }
+
+  static class TestClass {
+
+    public static void main(String[] args) {
+      System.out.println(MyAnnotation.class.getAnnotations().length);
+    }
+  }
+
+  @Retention(RetentionPolicy.RUNTIME)
+  @Target({ElementType.TYPE})
+  @MyAnnotation
+  @interface MyAnnotation {}
+}