Java反射Reflectionでクラス属性、メソッド、構築メソッド、および呼び出しクラスメソッドを取得してオブジェクト属性を変更するテスト

15665 ワード

 1 package com.li.test;

 2 /**

 3  *  User

 4  * @author Administrator

 5  */

 6 public class User {

 7     

 8     /**

 9      * name

10      */

11     private String name;//name

12     private int age;//age

13 

14     public User() {

15     }

16     public User(String name,int age) {

17         this.name = name;

18         this.age = age;

19     }

20 

21     public String getName() {

22         return name;

23     }

24 

25     public void setName(String name) {

26         this.name = name;

27     }

28 

29     public int getAge() {

30         return age;

31     }

32 

33     public void setAge(int age) {

34         this.age = age;

35     }

36 }
 1 package com.li.test;

 2 

 3 import java.lang.reflect.Constructor;

 4 import java.lang.reflect.Field;

 5 import java.lang.reflect.InvocationTargetException;

 6 import java.lang.reflect.Method;

 7 

 8 /**

 9  *   java Reflection     、 、     

10  * 

11  * @author Administrator

12  * 

13  */

14 public class TestReflect {

15 

16     /**

17      * @param args

18      */

19     public static void main(String[] args) {

20         //    

21         Field[] field = User.class.getDeclaredFields();

22         System.out.println("User ----begin-----");

23         for (int i = 0; i < field.length; i++) {

24             System.out.println(field[i].getName());

25         }

26         System.out.println("User ----end-----");

27         //    

28         Method method[] = User.class.getDeclaredMethods();

29         System.out.println("User ----begin-----");

30         for (int i = 0; i < method.length; i++) {

31             System.out.println(method[i].getName());

32         }

33         System.out.println("User ----end-----");

34         //    

35         Constructor constructor[] = User.class.getDeclaredConstructors();

36         System.out.println("User ----begin-----");

37         for (int i = 0; i < constructor.length; i++) {

38             System.out.println(constructor[i]);

39         }

40         System.out.println("User ----end-----");

41         //  

42         User user1 = new User("A", 1);

43         System.out.println("begin " + user1.getName() + user1.getAge());

44         Field fieldName = null;

45         try {

46             fieldName = User.class.getDeclaredField("name");

47         } catch (SecurityException e1) {

48             // TODO Auto-generated catch block

49             e1.printStackTrace();

50         } catch (NoSuchFieldException e1) {

51             // TODO Auto-generated catch block

52             e1.printStackTrace();

53         }

54         Method setNameMethod = null;

55         Method getNameMethod = null;

56         Object name = null;

57         try {

58             setNameMethod = user1.getClass().getDeclaredMethod("setName",new Class[]{fieldName.getType()});

59             getNameMethod = user1.getClass().getDeclaredMethod("getName",new Class[]{});

60         } catch (SecurityException e) {

61             e.printStackTrace();

62         } catch (NoSuchMethodException e) {

63             e.printStackTrace();

64         }

65         try {

66             setNameMethod.invoke(user1, new Object[]{"B"});

67             name = getNameMethod.invoke(user1, new Class[]{});

68             

69         } catch (IllegalArgumentException e) {

70             // TODO Auto-generated catch block

71             e.printStackTrace();

72         } catch (IllegalAccessException e) {

73             // TODO Auto-generated catch block

74             e.printStackTrace();

75         } catch (InvocationTargetException e) {

76             // TODO Auto-generated catch block

77             e.printStackTrace();

78         }

79 

80         System.out.println("end " + user1.getName() + user1.getAge());

81         System.out.println("end " + (String)name);

82     }

83 

84 }

 
コンソール出力は次のとおりです.
Userクラス属性------begin------nameageUserクラス属性------end------Userクラスメソッド------begin------get AgesetAgegetNamesetNameUserクラスメソッド------end------Userクラス構造メソッド------begin------public com.li.test.User()public com.li.test.User(java.lang.String,int)Userクラス構築方法------end------begin A 1 end B 1 end B