ART: Add GetClassLoader

Add support for GetClassLoader. Add a test.

Bug: 31684578
Test: m test-art-host-run-test-912-classes
Change-Id: I629ec2a1f4843bc3b28e40111805e250be44d993
diff --git a/test/912-classes/src/Main.java b/test/912-classes/src/Main.java
index 7a7f4c0..69e5a4c 100644
--- a/test/912-classes/src/Main.java
+++ b/test/912-classes/src/Main.java
@@ -71,6 +71,11 @@
     testInterfaces(ClassA.class);
     testInterfaces(ClassB.class);
     testInterfaces(ClassC.class);
+
+    testClassLoader(String.class);
+    testClassLoader(String[].class);
+    testClassLoader(InfA.class);
+    testClassLoader(getProxyClass());
   }
 
   private static Class<?> proxyClass = null;
@@ -121,6 +126,29 @@
     System.out.println(c + " " + Arrays.toString(getImplementedInterfaces(c)));
   }
 
+  private static boolean IsBootClassLoader(ClassLoader l) {
+    // Hacky check for Android's fake boot classloader.
+    return l.getClass().getName().equals("java.lang.BootClassLoader");
+  }
+
+  private static void testClassLoader(Class<?> c) {
+    Object cl = getClassLoader(c);
+    System.out.println(c + " " + (cl != null ? cl.getClass().getName() : "null"));
+    if (cl == null) {
+      if (c.getClassLoader() != null && !IsBootClassLoader(c.getClassLoader())) {
+        throw new RuntimeException("Expected " + c.getClassLoader() + ", but got null.");
+      }
+    } else {
+      if (!(cl instanceof ClassLoader)) {
+        throw new RuntimeException("Unexpected \"classloader\": " + cl + " (" + cl.getClass() +
+            ")");
+      }
+      if (cl != c.getClassLoader()) {
+        throw new RuntimeException("Unexpected classloader: " + c.getClassLoader() + " vs " + cl);
+      }
+    }
+  }
+
   private static native String[] getClassSignature(Class<?> c);
 
   private static native boolean isInterface(Class<?> c);
@@ -134,6 +162,8 @@
 
   private static native int getClassStatus(Class<?> c);
 
+  private static native Object getClassLoader(Class<?> c);
+
   private static class TestForNonInit {
     public static double dummy = Math.random();  // So it can't be compile-time initialized.
   }