test for exceptions

This commit is contained in:
Shautvast 2023-07-31 15:39:46 +02:00
parent 3e636667a9
commit bba00f1cbd
3 changed files with 26 additions and 1 deletions

View file

@ -75,6 +75,12 @@ public class InvokerFactory {
classNode.accept(classWriter);
byte[] byteArray = classWriter.toByteArray();
// try (FileOutputStream out = new FileOutputStream("C.class")) {
// out.write(byteArray);
// } catch (IOException e) {
// e.printStackTrace();
// }
// load it into the JVM
ByteClassLoader.INSTANCE.addClass(className, byteArray);
return getInvoker(classNode.name);

View file

@ -65,4 +65,8 @@ public class Result<T> {
return new Result<>(null, error);
}
}
public boolean isOk() {
return error == null;
}
}

View file

@ -1,6 +1,7 @@
package com.github.shautvast.reflective;
import com.github.shautvast.rusty.Panic;
import com.github.shautvast.rusty.Result;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
@ -25,7 +26,7 @@ public class ReflectiveTest {
Set<MetaMethod> methods = metaDummy.getMethods();
assertFalse(methods.isEmpty());
assertEquals(5, methods.size());
assertEquals(6, methods.size());
MetaMethod equals = metaDummy.getMethod("equals").orElseGet(Assertions::fail);
assertEquals(boolean.class, equals.getReturnParameter().getType());
@ -68,6 +69,16 @@ public class ReflectiveTest {
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 {
private String name;
@ -84,6 +95,10 @@ public class ReflectiveTest {
this.name = name;
}
public void throwEx() throws Exception {
throw new Exception("something must have gone wrong");
}
@Override
public boolean equals(Object obj) {
return obj instanceof Dummy;