Java反射可执行的实例


一、实例目标

根据传入的完整类名字符串类名,实现创建对应类的实例

根据传入的类实例,以及传入的方法名字符串,实现动态调用指定的方法,返回方法的返回值

在FanSheTest 单元测试中实现使用FanShe类传入"cn.com.rwq.test.Entity"字符串实现创建Entity类,并且根据传入的字符串动态调用类中的与字符串同名的方法

二、代码

1、测试类

 
  1. package cn.com.rwq.test; 
  2.  
  3. import junit.framework.TestCase; 
  4.  
  5. public class FanSheTest  extends TestCase { 
  6.     private FanShe fanShe ; 
  7.  
  8.     @Override 
  9.     protected void setUp() throws Exception { 
  10.         super.setUp(); 
  11.         fanShe = new FanShe() ; 
  12.     } 
  13.  
  14.     @Override 
  15.     protected void tearDown() throws Exception { 
  16.         super.tearDown(); 
  17.          
  18.     } 
  19.      
  20.     public void testCreateClass() throws Exception{ 
  21.         Object object = fanShe.createClass("cn.com.rwq.test.Entity"); 
  22.         assertNotNull(object); 
  23.         Common common = (Common)object; 
  24.         assertEquals("123456789", common.getName()); 
  25.         assertEquals("12345678", ((Entity)object).toString()); 
  26.          
  27.     } 
  28.      
  29.     public void testCreateObject() throws Exception{ 
  30.         Object object = fanShe.createClass("cn.com.rwq.test.Entity"); 
  31.         fanShe.createObject(object, "print"); 
  32.          
  33. //      fanShe.createObject(object, "two");  
  34. //      strPrint  
  35. //      fanShe.createObject(object, "siyou");  
  36.          
  37.         String a =(String)fanShe.createObject(object, "strPrint"); 
  38.         assertEquals("abs", a); 
  39.          
  40.         int b =(int)fanShe.createObject(object, "intPrint"); 
  41.         assertEquals(123, b); 
  42.         Common common = (Common)object; 
  43.         fanShe.createObject(common, "printName"); 
  44.     } 

2、反射了的实现

 
  1. package cn.com.rwq.test; 
  2.  
  3. import java.lang.reflect.InvocationTargetException; 
  4. import java.lang.reflect.Method; 
  5.  
  6. public class FanShe { 
  7.     /** 
  8.      * 根据完整类名创建对应类的实体 
  9.      * @param className 完整类名 
  10.      * @return 创建的实体 
  11.      */ 
  12.     public Object createClass(String className) 
  13.          throws ClassNotFoundException, InstantiationException, IllegalAccessException {   
  14.                 Class clazz = Class.forName(className);   
  15.                 Method m[] = clazz.getDeclaredMethods(); 
  16.                 for(Method one : m){ 
  17.                     System.out.println(one.toString()); 
  18.                 } 
  19.                 Object object= clazz.newInstance();   
  20.                 return object; 
  21.     } 
  22.     /** 
  23.      * 根据类,以及方法名,动态调用方法 
  24.      * @param object 类 
  25.      * @param actionName 方法名 
  26.      * @return 调用的方法的返回值 
  27.      */ 
  28.     public static Object createObject(Object object,String actionName)   
  29.             throws ClassNotFoundException, InstantiationException, IllegalAccessException {   
  30.              Method aMethod; 
  31.             try { 
  32.                 aMethod = object.getClass().getMethod(actionName,null); 
  33.                  return aMethod.invoke(object,null); 
  34.             } catch (NoSuchMethodException | SecurityException e1) { 
  35.                 e1.printStackTrace(); 
  36.             } catch (IllegalArgumentException | InvocationTargetException e) { 
  37.                     e.printStackTrace(); 
  38.                 } 
  39.             return null;   
  40.     }   

3、javaBean

 
  1. package cn.com.rwq.test; 
  2. /** 
  3.  * javaBean 
  4.  * @author Administrator 
  5.  */ 
  6. public class Entity extends Common { 
  7.  
  8.     public Entity(){ 
  9.         System.out.println("Entity 构造方法"); 
  10.     } 
  11.     public void print(){ 
  12.         System.out.println("执行printe 无返回值"); 
  13.     } 
  14.     void two(){ 
  15.         System.out.println("执行two 方法"); 
  16.     } 
  17.     private void  siyou(){ 
  18.         System.out.println("执行siyou 私有方法"); 
  19.     } 
  20.     public String strPrint(){ 
  21.         System.out.println("执行strPrint 有返回值"); 
  22.         return "abs"
  23.     } 
  24.     public int intPrint(){ 
  25.         System.out.println("执行intPrint 有返回值"); 
  26.         return 123
  27.     } 
  28.     public void printName(){ 
  29.         System.out.println("11111111    "+super.getName()); 
  30.     } 
  31.     public String toString(){ 
  32.         return "12345678"
  33.     } 
  34.     public static void main(String[] args){ 
  35.         Entity fanshe = new Entity(); 
  36.         fanshe.print(); 
  37.         fanshe.two(); 
  38.         fanshe.siyou(); 
  39.         System.out.println(fanshe.strPrint()); 
  40.     } 

 4、父类

 

 
  1. package cn.com.rwq.test; 
  2. /** 
  3.  * 父类 
  4.  */ 
  5. public class Common { 
  6.     private String name = "123456789"
  7.  
  8.     public String getName() { 
  9.         return name; 
  10.     } 
  11.  
  12.     public void setName(String name) { 
  13.         this.name = name; 
  14.     } 

 三、重点理解
1.待实现的实体类必须有无参的构造函数
2. Class<?> demo= Class.forName(""); 根据完整类名的字符串得到指定的类
3.取得一个类的全部框架

 

 
  1.         Class<?> demo = Class.forName("cn.com.rwq.test.Entity"); 
  2.         System.out.println("===============本类属性========================"); 
  3.         // 取得本类的全部属性  
  4.         Field[] field = demo.getDeclaredFields(); 
  5.         for (int i = 0; i < field.length; i++) { 
  6.             // 权限修饰符  
  7.             int mo = field[i].getModifiers(); 
  8.             String priv = Modifier.toString(mo); 
  9.             // 属性类型  
  10.             Class<?> type = field[i].getType(); 
  11.             System.out.println(priv + " " + type.getName() + " " 
  12.                     + field[i].getName() + ";"); 
  13.         } 
  14.         System.out.println("=========实现的接口或者父类的属性=============="); 
  15.         // 取得实现的接口或者父类的属性  
  16.         Field[] filed1 = demo.getFields(); 
  17.         for (int j = 0; j < filed1.length; j++) { 
  18.             // 权限修饰符  
  19.             int mo = filed1[j].getModifiers(); 
  20.             String priv = Modifier.toString(mo); 
  21.             // 属性类型  
  22.             Class<?> type = filed1[j].getType(); 
  23.             System.out.println(priv + " " + type.getName() + " " 
  24.                     + filed1[j].getName() + ";"); 
  25.         } 
  26.     } 
  27. }

 4.//调用无参方法(Class<?> demo 实例化的类)
  Method method=demo.getMethod("方法名");
  method.invoke(demo.newInstance());
 //调用有参数的方法
    method=demo.getMethod("方法名", String.class,int.class);
    method.invoke(demo.newInstance(),"Rollen",20);

相关内容