JAVA 反射之方法调用
来源:互联网 发布:ps淘宝主图广告 编辑:程序博客网 时间:2024/05/01 16:21
刚才看Tomcat6源码时,发现一段有趣的使用JAVA反射机制的代码。考出来处理,独立运行,成功。贴与大家分享。
package angel.lang.reflect.ut;
import java.lang.reflect.Method;
import junit.framework.TestCase;
/** *//**
*
* <pre>
* Description:
* TODO Unit测试类
*
* Revision History:
* Feb 1, 2008 Fity.Wang initial version.
*
* </pre>
*/
public class MethodUT extends TestCase
...{
public void testInvoke()
...{
try
...{
Class commonfor = Class.forName("angel.lang.reflect.ut.CommonFor");
Object cfinstance = (Object) commonfor.newInstance();
Class paramTypes[] = new Class[1];
paramTypes[0] = Class.forName("java.lang.String");
Object paramValues[] = new Object[1];
paramValues[0] = new String("Hello Method invoke!");
Method method = cfinstance.getClass().getMethod("methodForInvoke",
paramTypes);
method.invoke(cfinstance, paramValues);
}
catch (Exception e)
...{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.lang.reflect.Method;
import junit.framework.TestCase;
/** *//**
*
* <pre>
* Description:
* TODO Unit测试类
*
* Revision History:
* Feb 1, 2008 Fity.Wang initial version.
*
* </pre>
*/
public class MethodUT extends TestCase
...{
public void testInvoke()
...{
try
...{
Class commonfor = Class.forName("angel.lang.reflect.ut.CommonFor");
Object cfinstance = (Object) commonfor.newInstance();
Class paramTypes[] = new Class[1];
paramTypes[0] = Class.forName("java.lang.String");
Object paramValues[] = new Object[1];
paramValues[0] = new String("Hello Method invoke!");
Method method = cfinstance.getClass().getMethod("methodForInvoke",
paramTypes);
method.invoke(cfinstance, paramValues);
}
catch (Exception e)
...{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package angel.lang.reflect.ut;
import static java.lang.System.out;
/** *//**
*
* <pre>
* Description:
* TODO 反射测试目标类
*
* Revision History:
* Feb 1, 2008 Fity.Wang initial version.
*
* </pre>
*/
public class CommonFor
...{
public static String getClassName()
...{
return "[" + CommonFor.class.getName() + "]";
}
public void methodForInvoke(String s)
...{
out.println(getClassName() + "methodForInvoke()" + s);
}
// public static void main(String args[])
// {
// }
}
import static java.lang.System.out;
/** *//**
*
* <pre>
* Description:
* TODO 反射测试目标类
*
* Revision History:
* Feb 1, 2008 Fity.Wang initial version.
*
* </pre>
*/
public class CommonFor
...{
public static String getClassName()
...{
return "[" + CommonFor.class.getName() + "]";
}
public void methodForInvoke(String s)
...{
out.println(getClassName() + "methodForInvoke()" + s);
}
// public static void main(String args[])
// {
// }
}
- JAVA 反射之方法调用
- JAVA反射之调用方法
- java反射调用方法
- Java反射调用方法
- Java 反射 方法调用
- java 反射 方法调用
- java反射调用方法
- JAVA 反射调用方法
- java 反射调用方法
- Java反射调用方法
- Java反射之调用构造方法and操作数组
- java反射机制之Method invoke执行调用方法例子
- java反射机制之Method invoke执行调用方法例子
- java反射机制之Method invoke执行调用方法例子
- java反射机制之Method invoke执行调用方法例子
- javaSE之方法反射调用
- 反射基础之方法调用
- java反射(3)--反射的调用方法
- 生活小技巧
- 这个冬天有大雪
- 如何导出存储过程和自定义函数?
- 中文字符无法在CB中编译
- struts 2学习(有关Unable to load bean: type: class:com.opensymphony.xwork2.ObjectFactory的错误)
- JAVA 反射之方法调用
- 我心里的流星...
- 为IE找个新死法
- 用JAVA 实现“生产者-消费者”问题
- 【住院记】--那麻木中的一刀:一、前记之6. 体检
- XML编程接口的比较:DOM SAX JDOM JAXP
- JAVA基础
- 读神经所所长2007年会摘记
- 做个优秀的程序员