Improve reflection IllegalArgumentException detail messages.

Also add a missing InstanceOf check that was causing CheckJNI to kill us
if someone tried to pass an inappropriate reference type through
Method.invoke. (Amusingly, CheckJNI produced pretty much the exact detail
message that Method.invoke should have.)

Plus a new test for this stuff.

Bug: 6504175
Change-Id: Ice95eecbdba5a0927c6eaf68e56d6500dc52ad2e
diff --git a/test/200-reflection-errors/src/Main.java b/test/200-reflection-errors/src/Main.java
new file mode 100644
index 0000000..86bbfd4
--- /dev/null
+++ b/test/200-reflection-errors/src/Main.java
@@ -0,0 +1,52 @@
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+
+public class Main {
+  public static void main(String[] args) throws Exception {
+    // Can't assign Integer to a String field.
+    try {
+      Field field = A.class.getField("b");
+      field.set(new A(), 5);
+    } catch (IllegalArgumentException expected) {
+      System.out.println(expected.getMessage());
+    }
+
+    // Can't unbox null to a primitive.
+    try {
+      Field field = A.class.getField("i");
+      field.set(new A(), null);
+    } catch (IllegalArgumentException expected) {
+      System.out.println(expected.getMessage());
+    }
+
+    // Can't unbox String to a primitive.
+    try {
+      Field field = A.class.getField("i");
+      field.set(new A(), "hello, world!");
+    } catch (IllegalArgumentException expected) {
+      System.out.println(expected.getMessage());
+    }
+
+    // Can't pass an Integer as a String.
+    try {
+      Method m = A.class.getMethod("m", int.class, String.class);
+      m.invoke(new A(), 2, 2);
+    } catch (IllegalArgumentException expected) {
+      System.out.println(expected.getMessage());
+    }
+
+    // Can't pass null as an int.
+    try {
+      Method m = A.class.getMethod("m", int.class, String.class);
+      m.invoke(new A(), null, "");
+    } catch (IllegalArgumentException expected) {
+      System.out.println(expected.getMessage());
+    }
+  }
+}
+
+class A {
+  public String b;
+  public int i;
+  public void m(int i, String s) {}
+}