summaryrefslogtreecommitdiff
path: root/test/200-reflection-errors/src/Main.java
diff options
context:
space:
mode:
author Elliott Hughes <enh@google.com> 2012-05-16 15:54:30 -0700
committer Elliott Hughes <enh@google.com> 2012-05-16 15:54:30 -0700
commitaaa5edcf2deb1bddcbf5fb27820ad2240ac5b4f2 (patch)
treec2d0f408237ad5a30bfd67819e5c9a72cc3ac45e /test/200-reflection-errors/src/Main.java
parent983f2e411aee6b1d09e6da30e059b782b2699909 (diff)
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
Diffstat (limited to 'test/200-reflection-errors/src/Main.java')
-rw-r--r--test/200-reflection-errors/src/Main.java52
1 files changed, 52 insertions, 0 deletions
diff --git a/test/200-reflection-errors/src/Main.java b/test/200-reflection-errors/src/Main.java
new file mode 100644
index 0000000000..86bbfd4d2b
--- /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) {}
+}