improve exception message for InvocationTargetException (#28)
Change-Id: Id2bb1d4b90d1f091f4949d7ba4cb6d7df9efb8b0
diff --git a/src/main/java/com/baidu/hugegraph/testutil/Whitebox.java b/src/main/java/com/baidu/hugegraph/testutil/Whitebox.java
index 834d579..3c17192 100644
--- a/src/main/java/com/baidu/hugegraph/testutil/Whitebox.java
+++ b/src/main/java/com/baidu/hugegraph/testutil/Whitebox.java
@@ -22,9 +22,11 @@
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
+import java.util.Arrays;
import java.util.Objects;
import com.baidu.hugegraph.util.E;
+import com.google.common.primitives.Primitives;
public class Whitebox {
@@ -130,7 +132,7 @@
int i = 0;
for (Object arg : args) {
E.checkArgument(arg != null, "The argument can't be null");
- classes[i++] = arg.getClass();
+ classes[i++] = Primitives.unwrap(arg.getClass());
}
return invoke(clazz, classes, methodName, self, args);
}
@@ -145,12 +147,20 @@
return result;
} catch (NoSuchMethodException e) {
throw new RuntimeException(String.format(
- "Can't find method '%s' of class '%s'",
- methodName, clazz), e);
- } catch (IllegalAccessException | InvocationTargetException e) {
+ "Can't find method '%s' with args %s of class '%s'",
+ methodName, Arrays.asList(classes), clazz), e);
+ } catch (IllegalAccessException e) {
throw new RuntimeException(String.format(
- "Can't invoke method '%s' of class '%s'",
- methodName, clazz), e);
+ "Can't invoke method '%s' of class '%s': %s",
+ methodName, clazz, e.getMessage()), e);
+ } catch (InvocationTargetException e) {
+ Throwable target = e.getTargetException();
+ if (target instanceof RuntimeException) {
+ throw (RuntimeException) target;
+ }
+ throw new RuntimeException(String.format(
+ "Can't invoke method '%s' of class '%s': %s",
+ methodName, clazz, target.getMessage()), target);
}
}
}
diff --git a/src/test/java/com/baidu/hugegraph/testutil/WhiteboxTest.java b/src/test/java/com/baidu/hugegraph/testutil/WhiteboxTest.java
index 9e84a59..f9941c4 100644
--- a/src/test/java/com/baidu/hugegraph/testutil/WhiteboxTest.java
+++ b/src/test/java/com/baidu/hugegraph/testutil/WhiteboxTest.java
@@ -101,13 +101,22 @@
public void testInvokeStatic() {
Assert.assertEquals(1, Whitebox.invokeStatic(Test1.class, "svalue"));
Assert.assertEquals(2, Whitebox.invokeStatic(Test1.class, "svalue", 2));
- Assert.assertEquals(2, Whitebox.invokeStatic(Test1.class, "svalue", 2));
+ Assert.assertEquals(2, Whitebox.invokeStatic(Test1.class, "svalue",
+ new Integer(2)));
Assert.assertEquals(2d, Whitebox.invokeStatic(Test1.class,
new Class[]{Object.class},
"svalue", 2d));
+
Assert.assertThrows(RuntimeException.class, () -> {
Whitebox.invokeStatic(Test1.class, "svalue2");
});
+
+ Assert.assertThrows(IllegalArgumentException.class, () -> {
+ Whitebox.invokeStatic(Test1.class, "throwfunc1");
+ });
+ Assert.assertThrows(RuntimeException.class, () -> {
+ Whitebox.invokeStatic(Test1.class, "throwfunc2");
+ });
}
@Test
@@ -115,6 +124,8 @@
Test1 test1 = newTest();
Assert.assertEquals(1, Whitebox.invoke(test1.getClass(),
"value", test1));
+ Assert.assertEquals(3, Whitebox.invoke(test1.getClass(),
+ "addValue", test1, 2));
Assert.assertEquals(2f, Whitebox.invoke(test1, "test2", "value"));
Assert.assertEquals(2, Whitebox.invoke(test1, "test2",
new Class[]{Object.class},
@@ -129,6 +140,9 @@
Assert.assertThrows(RuntimeException.class, () -> {
Whitebox.invoke(test1, "test2", "value", 2);
});
+ Assert.assertThrows(RuntimeException.class, () -> {
+ Whitebox.invoke(test1.getClass(), "addValue", test1, 2.0);
+ });
}
private static Test1 newTest() {
@@ -149,17 +163,29 @@
return this.ivalue;
}
+ private int addValue(int i) {
+ return this.ivalue + i;
+ }
+
private static int svalue() {
return 1;
}
- private static int svalue(Integer i) {
+ private static int svalue(int i) {
return i;
}
private static <T> T svalue(T o) {
return o;
}
+
+ private static int throwfunc1() {
+ throw new IllegalArgumentException("fake runtime exception");
+ }
+
+ private static int throwfunc2() throws Exception {
+ throw new Exception("fake exception");
+ }
}
@SuppressWarnings("unused")