test for exceptions
This commit is contained in:
parent
3e636667a9
commit
bba00f1cbd
3 changed files with 26 additions and 1 deletions
|
|
@ -75,6 +75,12 @@ public class InvokerFactory {
|
||||||
classNode.accept(classWriter);
|
classNode.accept(classWriter);
|
||||||
byte[] byteArray = classWriter.toByteArray();
|
byte[] byteArray = classWriter.toByteArray();
|
||||||
|
|
||||||
|
// try (FileOutputStream out = new FileOutputStream("C.class")) {
|
||||||
|
// out.write(byteArray);
|
||||||
|
// } catch (IOException e) {
|
||||||
|
// e.printStackTrace();
|
||||||
|
// }
|
||||||
|
|
||||||
// load it into the JVM
|
// load it into the JVM
|
||||||
ByteClassLoader.INSTANCE.addClass(className, byteArray);
|
ByteClassLoader.INSTANCE.addClass(className, byteArray);
|
||||||
return getInvoker(classNode.name);
|
return getInvoker(classNode.name);
|
||||||
|
|
|
||||||
|
|
@ -65,4 +65,8 @@ public class Result<T> {
|
||||||
return new Result<>(null, error);
|
return new Result<>(null, error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isOk() {
|
||||||
|
return error == null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package com.github.shautvast.reflective;
|
package com.github.shautvast.reflective;
|
||||||
|
|
||||||
import com.github.shautvast.rusty.Panic;
|
import com.github.shautvast.rusty.Panic;
|
||||||
|
import com.github.shautvast.rusty.Result;
|
||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
|
@ -25,7 +26,7 @@ public class ReflectiveTest {
|
||||||
|
|
||||||
Set<MetaMethod> methods = metaDummy.getMethods();
|
Set<MetaMethod> methods = metaDummy.getMethods();
|
||||||
assertFalse(methods.isEmpty());
|
assertFalse(methods.isEmpty());
|
||||||
assertEquals(5, methods.size());
|
assertEquals(6, methods.size());
|
||||||
|
|
||||||
MetaMethod equals = metaDummy.getMethod("equals").orElseGet(Assertions::fail);
|
MetaMethod equals = metaDummy.getMethod("equals").orElseGet(Assertions::fail);
|
||||||
assertEquals(boolean.class, equals.getReturnParameter().getType());
|
assertEquals(boolean.class, equals.getReturnParameter().getType());
|
||||||
|
|
@ -68,6 +69,16 @@ public class ReflectiveTest {
|
||||||
assertEquals("foo", dummy.getName()); // after invoke
|
assertEquals("foo", dummy.getName()); // after invoke
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testInvocationExceptionHappened() {
|
||||||
|
Dummy dummy = new Dummy("bar");
|
||||||
|
MetaClass metaForClass = Reflective.getMetaClass(dummy.getClass());
|
||||||
|
MetaMethod throwEx = metaForClass.getMethod("throwEx").orElseGet(Assertions::fail);
|
||||||
|
|
||||||
|
Result<Void> result = throwEx.invoke(dummy, "foo");
|
||||||
|
assertFalse(result.isOk());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public static class Dummy {
|
public static class Dummy {
|
||||||
private String name;
|
private String name;
|
||||||
|
|
@ -84,6 +95,10 @@ public class ReflectiveTest {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void throwEx() throws Exception {
|
||||||
|
throw new Exception("something must have gone wrong");
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object obj) {
|
public boolean equals(Object obj) {
|
||||||
return obj instanceof Dummy;
|
return obj instanceof Dummy;
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue