As you all know it is possible to fetch a method with Reflection
and invoke it through the returned Method
instance.
My question is however; once it is fetched by Reflection
and I invoke the Method
over and over again will the performance of the method be slower than the normal way of calling a method?
For example:
import java.lang.reflect.Method;
public class ReflectionTest {
private static Method test;
public ReflectionTest() throws Exception {
test = this.getClass().getMethod("testMethod", null);
}
public void testMethod() {
//execute code here
}
public static void main(String[] args) throws Exception {
ReflectionTest rt = new ReflectionTest();
for (int i = 0; i < 1000; i++) {
rt.test.invoke(null, null);
}
for (int i = 0; i < 1000; i++) {
rt.testMethod();
}
}
}
I am asking this because I am making an event system that, upon registering the listener it scans for annotations. The methods are put into a map and then they are executed each time an event occurs of their required parameter type. I don't know if this is performant enough for, for example a game.