VarHandles: fix missing reference for static field VarHandles

Static field VarHandles should have a reference to the declaring class
of the field.

Fix: 191980149
Test: art/test.py --host
Change-Id: Ib48f0c2bb5d6f22c0e6cc623aaeed98f2ee88b26
diff --git a/libart/src/main/java/java/lang/invoke/FieldVarHandle.java b/libart/src/main/java/java/lang/invoke/FieldVarHandle.java
index 0921040..beb056a 100644
--- a/libart/src/main/java/java/lang/invoke/FieldVarHandle.java
+++ b/libart/src/main/java/java/lang/invoke/FieldVarHandle.java
@@ -20,27 +20,24 @@
 import java.lang.reflect.Modifier;
 
 /**
- * A VarHandle that's associated with an ArtField.
+ * A VarHandle that's associated with an instance field.
  * @hide
  */
-final class FieldVarHandle extends VarHandle {
+class FieldVarHandle extends VarHandle {
     private final long artField;
 
-    private FieldVarHandle(Field field, Class<?> declaringClass) {
-        super(field.getType(), Modifier.isFinal(field.getModifiers()), declaringClass);
+    private FieldVarHandle(Field field, Class<?> coordinateType0) {
+        super(field.getType(), Modifier.isFinal(field.getModifiers()), coordinateType0);
         artField = field.getArtField();
     }
 
-    private FieldVarHandle(Field field) {
+    protected FieldVarHandle(Field field) {
         super(field.getType(), Modifier.isFinal(field.getModifiers()));
         artField = field.getArtField();
     }
 
     static FieldVarHandle create(Field field) {
-        if (Modifier.isStatic(field.getModifiers())) {
-            return new FieldVarHandle(field);
-        } else {
-            return new FieldVarHandle(field, field.getDeclaringClass());
-        }
+        assert !Modifier.isStatic(field.getModifiers());
+        return new FieldVarHandle(field, field.getDeclaringClass());
     }
 }
diff --git a/libart/src/main/java/java/lang/invoke/StaticFieldVarHandle.java b/libart/src/main/java/java/lang/invoke/StaticFieldVarHandle.java
new file mode 100644
index 0000000..e5faeb7
--- /dev/null
+++ b/libart/src/main/java/java/lang/invoke/StaticFieldVarHandle.java
@@ -0,0 +1,38 @@
+/*
+ * Copyright (C) 2021 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 java.lang.invoke;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
+
+/**
+ * A VarHandle that's associated with a static field.
+ * @hide
+ */
+final class StaticFieldVarHandle extends FieldVarHandle {
+    private final Class declaringClass;
+
+    private StaticFieldVarHandle(Field staticField) {
+        super(staticField);
+        declaringClass = staticField.getDeclaringClass();
+    }
+
+    static StaticFieldVarHandle create(Field staticField) {
+        assert Modifier.isStatic(staticField.getModifiers());
+        return new StaticFieldVarHandle(staticField);
+    }
+}
diff --git a/non_openjdk_java_files.bp b/non_openjdk_java_files.bp
index 8b67ef7..5ba1d79 100755
--- a/non_openjdk_java_files.bp
+++ b/non_openjdk_java_files.bp
@@ -90,6 +90,7 @@
         "libart/src/main/java/java/lang/invoke/ByteArrayViewVarHandle.java",
         "libart/src/main/java/java/lang/invoke/ByteBufferViewVarHandle.java",
         "libart/src/main/java/java/lang/invoke/FieldVarHandle.java",
+        "libart/src/main/java/java/lang/invoke/StaticFieldVarHandle.java",
     ],
     path: "libart/src/main",
     visibility: ["//visibility:private"],
diff --git a/ojluni/src/main/java/java/lang/invoke/MethodHandles.java b/ojluni/src/main/java/java/lang/invoke/MethodHandles.java
index 1188ac6..2475f3a 100644
--- a/ojluni/src/main/java/java/lang/invoke/MethodHandles.java
+++ b/ojluni/src/main/java/java/lang/invoke/MethodHandles.java
@@ -1505,7 +1505,7 @@
             final boolean isStatic = true;
             final boolean performAccessChecks = true;
             commonFieldChecks(field, decl, type, isStatic, performAccessChecks);
-            return FieldVarHandle.create(field);
+            return StaticFieldVarHandle.create(field);
         }
         // END Android-changed: OpenJDK 9+181 VarHandle API factory method.
 
@@ -1828,7 +1828,7 @@
             final boolean isStatic = Modifier.isStatic(f.getModifiers());
             final boolean performAccessChecks = true;
             commonFieldChecks(f, f.getDeclaringClass(), f.getType(), isStatic, performAccessChecks);
-            return FieldVarHandle.create(f);
+            return isStatic ? StaticFieldVarHandle.create(f) : FieldVarHandle.create(f);
         }
         // END Android-changed: OpenJDK 9+181 VarHandle API factory method.