Package D;
Import java. Lang. Reflect. field;
Public class bean01 {
Public static void main (string [] ARGs ){
Bean02 bean = new bean02 (); bean. setid (222 );
System. Out. println (getpojolog (bean ));
}
// List all the content of a pojo so that data can be checked during testing.
Public static string getpojolog (Object pojo ){
Class C = pojo. getclass ();
Stringbuffer out = new stringbuffer (C. getname () + ": \ n ");
Field [] fields = C. getdeclaredfields (); // or field [] fields = C. getfields (); for the differences between the two, see the appendix.
Field. setaccessible (fields, true );
For (INT I = 0; I <fields. length; I ++ ){
Field field = Fields [I];
String name = field. getname ();
If (name. startswith ("SF _")){
Continue;
}
Try {
Object Vo = field. Get (pojo );
String value = "";
If (vo! = NULL ){
Value = field. Get (pojo). tostring ();
}
Out. append ("" + name + "=" + value + "\ n ");
} Catch (exception e ){
E. printstacktrace ();
}
}
Return out. tostring ();
}
}
Package D;
Public class bean02 {
Public int ID;
Public String [] love;
Public String [] getlove (){
Return love;
}
Public void setlove (string [] Love ){
This. Love = love;
}
Public int GETID (){
Return ID;
}
Public void setid (int id ){
This. ID = ID;
}
}
Appendix:
1. Package study. reflection;
2.
3. Public class people {
4. Public string name = NULL;
5. Private string sex = NULL;
6. Private string age = NULL;
7. Private string Tel = NULL;
8. Private string address = NULL;
9. Public static string S = NULL;
10. Static {
11. system. Out. println ("loading people ");
12 .}
13.
14. Public static void showpeople (){
15.
16 .}
17.
18. Public people (string name ){
19. This. Name = Name;
20 .}
21.
22. Private people (){
23. This. Name = Name;
24 .}
25.
26. Private void showpeopleinfo (){
27. system. Out. println (name + "" + sex + "" + age + "" + Tel + ""
28. + address );
29 .}
30.
31. Public String getname (){
32. Return name;
33 .}
34.
35. Public void setname (string name ){
36. This. Name = Name;
37 .}
38.
39. Public String getsex (){
40. Return sex;
41 .}
42.
43. Public void setsex (string sex ){
44. This. Sex = sex;
45 .}
46.
47. Public String getage (){
48. Return age;
49 .}
50.
51. Public void setage (string age ){
52. This. Age = age;
53 .}
54.
55. Public String gettel (){
56. Return Tel;
57 .}
58.
59. Public void settel (string Tel ){
60. This. Tel = Tel;
61 .}
62.
63. Public String getaddress (){
64. Return address;
65 .}
66.
67. Public void setaddress (string address ){
68. This. Address = address;
69 .}
70.
71 .}
72.
73. Package ESG;
74.
75. Import java. Lang. Reflect. constructor;
76. Import java. Lang. Reflect. field;
77. Import java. Lang. Reflect. method;
78.
79. Import study. reflection. People;
80.
81. Public class ESG {
82.
83. Public static void main (string [] A) throws classnotfoundexception {
84. Class C1 = people. Class;
85.
86. Field [] FS = c1.getfields ();
87. system. Out. println ("******** getfields ()*********");
88. For (INT I = 0; I <fs. length; I ++ ){
89. system. Out. println (FS [I]. getname ());
90 .}
91. system. Out. println ("******** getdeclaredfields ()*********");
92. FS = c1.getdeclaredfields ();
93. For (INT I = 0; I <fs. length; I ++ ){
94. system. Out. println (FS [I]. getname ());
95 .}
96. system. Out. println ("******** getmethods ()*********");
97. method [] MD = c1.getmethods ();
98. For (INT I = 0; I <Md. length; I ++ ){
99. system. Out. println (MD [I]. getname ());
100 .}
101. system. Out. println ("******** getdeclaredmethods ()*********");
102. md = c1.getdeclaredmethods ();
103. For (INT I = 0; I <Md. length; I ++ ){
104. system. Out. println (MD [I]. getname ());
105 .}
106.
107. system. Out. println ("******** getconstructors ()*********");
108. constructor [] con = c1.getconstructors ();
109. For (INT I = 0; I <con. length; I ++ ){
110. system. Out. println (CON [I]. getname ());
111 .}
112. system. Out. println ("******** getdeclaredconstructors ()*********");
113. Con = c1.getdeclaredconstructors ();
114. For (INT I = 0; I <con. length; I ++ ){
115. system. Out. println (CON [I]. getname ());
116 .}
117 .}
118 .}* Difference between getfields () and getdeclaredfields (): getfields () can only be declared as a public field in the response class, and private fields cannot be accessed. getdeclaredfields () is applicable to all fields in the lifecycle class and has nothing to do with public, private, and protect.
* Difference between getmethods () and getdeclaredmethods (): getmethods () can only be declared as a public method in the methods class, private methods cannot be accessed, and public methods inherited from other classes can be accessed. getdeclaredfields () can enumerate all fields in the class, and has nothing to do with public, private, and protect. It cannot access methods inherited from other classes.
* Difference between getconstructors () and getdeclaredconstructors (): getconstructors () can only be a public constructor in the declare class. getdeclaredconstructors () can be a constructor in the declare class, and has nothing to do with public, private