package com.reflection;
import org.apache.log4j.Logger;
import java.lang.reflect.Method;
public class ReflectInvoke
{
/**
* Logger for this class
*/
private static final Logger logger = Logger.getLogger(ReflectInvoke.class);
public int showNumber(int i)
{
return i;
}
public String display(String str)
{
return str;
}
public static void main(String[] args) throws Exception
{
Class classType = ReflectInvoke.class;
Object invokeTester = classType.newInstance();
Method addMethod = classType.getMethod("showNumber", new Class[] { int.class });
Object result = addMethod.invoke(invokeTester, new Object[] { new Integer(9999) });
if (logger.isInfoEnabled()) {
logger.info("showNumber Methor Result:" + (Integer) result);
}
Method echoMethod = classType.getMethod("display", new Class[] { String.class });
result = echoMethod.invoke(invokeTester, new Object[] { "zxs" });
if (logger.isInfoEnabled()) {
logger.info("display Methor Result:" + (String) result);
}
}
}
import org.apache.log4j.Logger;
import java.lang.reflect.Method;
public class ReflectInvoke
{
/**
* Logger for this class
*/
private static final Logger logger = Logger.getLogger(ReflectInvoke.class);
public int showNumber(int i)
{
return i;
}
public String display(String str)
{
return str;
}
public static void main(String[] args) throws Exception
{
Class classType = ReflectInvoke.class;
Object invokeTester = classType.newInstance();
Method addMethod = classType.getMethod("showNumber", new Class[] { int.class });
Object result = addMethod.invoke(invokeTester, new Object[] { new Integer(9999) });
if (logger.isInfoEnabled()) {
logger.info("showNumber Methor Result:" + (Integer) result);
}
Method echoMethod = classType.getMethod("display", new Class[] { String.class });
result = echoMethod.invoke(invokeTester, new Object[] { "zxs" });
if (logger.isInfoEnabled()) {
logger.info("display Methor Result:" + (String) result);
}
}
}