Java反射可执行的实例
一、实例目标
根据传入的完整类名字符串类名,实现创建对应类的实例
根据传入的类实例,以及传入的方法名字符串,实现动态调用指定的方法,返回方法的返回值
在FanSheTest 单元测试中实现使用FanShe类传入"cn.com.rwq.test.Entity"字符串实现创建Entity类,并且根据传入的字符串动态调用类中的与字符串同名的方法
二、代码
1、测试类
- package cn.com.rwq.test;
-
- import junit.framework.TestCase;
-
- public class FanSheTest extends TestCase {
- private FanShe fanShe ;
-
- @Override
- protected void setUp() throws Exception {
- super.setUp();
- fanShe = new FanShe() ;
- }
-
- @Override
- protected void tearDown() throws Exception {
- super.tearDown();
-
- }
-
- public void testCreateClass() throws Exception{
- Object object = fanShe.createClass("cn.com.rwq.test.Entity");
- assertNotNull(object);
- Common common = (Common)object;
- assertEquals("123456789", common.getName());
- assertEquals("12345678", ((Entity)object).toString());
-
- }
-
- public void testCreateObject() throws Exception{
- Object object = fanShe.createClass("cn.com.rwq.test.Entity");
- fanShe.createObject(object, "print");
-
-
-
-
-
- String a =(String)fanShe.createObject(object, "strPrint");
- assertEquals("abs", a);
-
- int b =(int)fanShe.createObject(object, "intPrint");
- assertEquals(123, b);
- Common common = (Common)object;
- fanShe.createObject(common, "printName");
- }
- }
|
2、反射了的实现
- package cn.com.rwq.test;
-
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
-
- public class FanShe {
-
-
-
-
-
- public Object createClass(String className)
- throws ClassNotFoundException, InstantiationException, IllegalAccessException {
- Class clazz = Class.forName(className);
- Method m[] = clazz.getDeclaredMethods();
- for(Method one : m){
- System.out.println(one.toString());
- }
- Object object= clazz.newInstance();
- return object;
- }
-
-
-
-
-
-
- public static Object createObject(Object object,String actionName)
- throws ClassNotFoundException, InstantiationException, IllegalAccessException {
- Method aMethod;
- try {
- aMethod = object.getClass().getMethod(actionName,null);
- return aMethod.invoke(object,null);
- } catch (NoSuchMethodException | SecurityException e1) {
- e1.printStackTrace();
- } catch (IllegalArgumentException | InvocationTargetException e) {
- e.printStackTrace();
- }
- return null;
- }
- }
|
3、javaBean
- package cn.com.rwq.test;
-
-
-
-
- public class Entity extends Common {
-
- public Entity(){
- System.out.println("Entity 构造方法");
- }
- public void print(){
- System.out.println("执行printe 无返回值");
- }
- void two(){
- System.out.println("执行two 方法");
- }
- private void siyou(){
- System.out.println("执行siyou 私有方法");
- }
- public String strPrint(){
- System.out.println("执行strPrint 有返回值");
- return "abs";
- }
- public int intPrint(){
- System.out.println("执行intPrint 有返回值");
- return 123;
- }
- public void printName(){
- System.out.println("11111111 "+super.getName());
- }
- public String toString(){
- return "12345678";
- }
- public static void main(String[] args){
- Entity fanshe = new Entity();
- fanshe.print();
- fanshe.two();
- fanshe.siyou();
- System.out.println(fanshe.strPrint());
- }
- }
|
4、父类
- package cn.com.rwq.test;
-
-
-
- public class Common {
- private String name = "123456789";
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
- }
|
三、重点理解
1.待实现的实体类必须有无参的构造函数
2. Class<?> demo= Class.forName(""); 根据完整类名的字符串得到指定的类
3.取得一个类的全部框架
- Class<?> demo = Class.forName("cn.com.rwq.test.Entity");
- System.out.println("===============本类属性========================");
-
- Field[] field = demo.getDeclaredFields();
- for (int i = 0; i < field.length; i++) {
-
- int mo = field[i].getModifiers();
- String priv = Modifier.toString(mo);
-
- Class<?> type = field[i].getType();
- System.out.println(priv + " " + type.getName() + " "
- + field[i].getName() + ";");
- }
- System.out.println("=========实现的接口或者父类的属性==============");
-
- Field[] filed1 = demo.getFields();
- for (int j = 0; j < filed1.length; j++) {
-
- int mo = filed1[j].getModifiers();
- String priv = Modifier.toString(mo);
-
- Class<?> type = filed1[j].getType();
- System.out.println(priv + " " + type.getName() + " "
- + filed1[j].getName() + ";");
- }
- }
- }
|
4.//调用无参方法(Class<?> demo 实例化的类)
Method method=demo.getMethod("方法名");
method.invoke(demo.newInstance());
//调用有参数的方法
method=demo.getMethod("方法名", String.class,int.class);
method.invoke(demo.newInstance(),"Rollen",20);
评论暂时关闭