Reland "Ensure getDeclaredField() marks field as being written"
This reverts commit 30cf36eb3bb226784d8d330659a69a9d39eaed01.
Reason for revert: This was not the offending commit causing the bots to fail.
Change-Id: I511943c127b013a5ad1464e83cceb8b6b3c28cb6
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 2306b28..e7ab7ed 100644
--- a/src/main/java/com/android/tools/r8/shaking/Enqueuer.java
+++ b/src/main/java/com/android/tools/r8/shaking/Enqueuer.java
@@ -2644,11 +2644,9 @@
if (keepClass) {
markInstantiated(clazz, null, KeepReason.reflectiveUseIn(method));
}
- markFieldAsKept(clazz, encodedField, KeepReason.reflectiveUseIn(method));
- // Fields accessed by reflection is marked as both read and written.
- registerFieldRead(encodedField.field, method);
- registerFieldWrite(encodedField.field, method);
-
+ if (pinnedItems.add(encodedField.field)) {
+ markFieldAsKept(clazz, encodedField, KeepReason.reflectiveUseIn(method));
+ }
} else {
assert identifierItem.isDexMethod();
DexMethod targetedMethod = identifierItem.asDexMethod();
diff --git a/src/test/java/com/android/tools/r8/shaking/reflection/GetDeclaredFieldShouldMarkFieldAsWrittenTest.java b/src/test/java/com/android/tools/r8/shaking/reflection/GetDeclaredFieldShouldMarkFieldAsWrittenTest.java
new file mode 100644
index 0000000..0efa7fa
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/shaking/reflection/GetDeclaredFieldShouldMarkFieldAsWrittenTest.java
@@ -0,0 +1,48 @@
+// Copyright (c) 2019, 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.reflection;
+
+import com.android.tools.r8.TestBase;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.TestParametersCollection;
+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 GetDeclaredFieldShouldMarkFieldAsWrittenTest extends TestBase {
+
+ private final TestParameters parameters;
+
+ @Parameters(name = "{0}")
+ public static TestParametersCollection data() {
+ return getTestParameters().withAllRuntimesAndApiLevels().build();
+ }
+
+ public GetDeclaredFieldShouldMarkFieldAsWrittenTest(TestParameters parameters) {
+ this.parameters = parameters;
+ }
+
+ @Test
+ public void test() throws Exception {
+ testForR8(parameters.getBackend())
+ .addProgramClasses(TestClass.class)
+ .addKeepMainRule(TestClass.class)
+ .setMinApi(parameters.getApiLevel())
+ .run(parameters.getRuntime(), TestClass.class)
+ .assertSuccessWithOutputLines("42");
+ }
+
+ static class TestClass {
+
+ static int f = -1;
+
+ public static void main(String[] args) throws Exception {
+ TestClass.class.getDeclaredField("f").set(null, 42);
+ System.out.println(f);
+ }
+ }
+}