terça-feira, 12 de maio de 2009

Java: Call method by reflection

Here's a simple and yet handy function that one can easily include in an Utility class that given an object and one of its method's name, invokes it (with no parameters).


public void callIt(String funcName, Object obj)
{
try
{
Class class= obj.getClass();
Method funcToCall = _class.getMethod(funcName, null);
funcToCall.invoke(obj, null);
}
catch ( Exception _e )
{
System.out.println("Failed to invoke function " + _e);
}
}

Sem comentários:

Enviar um comentário