Java reflection simple record

Source: Internet
Author: User

1. The first major function of the Java reflection API is to obtainProgramThe internal structure of the runtime. This is a very practical function for the checking tool and debugger of the program. Just a dozen linesCodeYou can traverse the internal structure of a Java class, including the constructor, declared domain, and defined method. This is a powerful capability. With the java. Lang. Class class object, you can obtain the constructor, domain, and method in the class through the methods in it. The corresponding methods are getconstructor, getfield, and getmethod. The three methods also have the corresponding getdeclaredxxx version. The difference is that the getdeclaredxxx version method only obtains the elements declared by the class itself, and does not consider inheritance. The constructor, field, and method classes represent the constructor, field, and method classes. The methods in these classes can obtain the metadata of the corresponding structure.

2. classes to be tested

Java code

  1. PackageCom. Home. Action. Test. mode. reflection;
  2. ImportJava. util. Map;
  3. Public ClassCounter {
  4. Public IntCount;
  5. PublicMap <string, Object> map;
  6. PublicCounter (IntCount ){
  7. This. Count = count;
  8. }
  9. Public VoidIncrease (IntStep ){
  10. Count = count + step;
  11. System. Out. println ("Count :"+ Count );
  12. }
  13. }
Package COM. home. action. test. mode. reflection; import Java. util. map; public class counter {public int count; Public Map <string, Object> map; public counter (INT count) {This. count = count;} public void increase (INT step) {COUNT = count + step; system. out. println ("count:" + count );}}

3. Test Method

Java code

  1. PackageCom. Home. Action. Test. mode. reflection;
  2. ImportJava. Lang. Reflect. constructor;
  3. ImportJava. Lang. Reflect. field;
  4. ImportJava. Lang. Reflect. invocationtargetexception;
  5. ImportJava. Lang. Reflect. method;
  6. ImportJava. Lang. Reflect. parameterizedtype;
  7. ImportJava. Lang. Reflect. type;
  8. /** 
  9. * Test reflection of basic classes 
  10. * @ Author Li 
  11. */
  12. Public ClassTest {
  13. @ Suppresswarnings("Rawtypes")
  14. Public Static VoidMain (string [] ARGs ){
  15. Try{
  16. // Constructor
  17. Constructor <counter> contructor = counter.Class. Getconstructor (Int.Class);
  18. System. Out. println ("Contructorname :"+ Contructor. getname ());
  19. Counter = contructor. newinstance (10);
  20. Counter. Increase (10);
  21. // Basic Method
  22. Method [] Methods = counter.Class. Getmethods ();
  23. For(Method: Methods ){
  24. System. Out. println ("Method :"+ Method. getname ());
  25. }
  26. Method method = counter.Class. Getmethod ("Increase",Int.Class);
  27. Method. Invoke (counter,6);
  28. // Attribute Value
  29. Field [] fields = counter.Class. Getfields ();
  30. For(Field: fields ){
  31. System. Out. println ("Fieldname :"+ Field. getname ());
  32. }
  33. // Set public for the Count type. Otherwise, an exception message is displayed.
  34. Field field = counter.Class. Getfield ("Count");
  35. System. Out. println ("Countfield :"+ Field. getname ());
  36. // Process generics
  37. Field mapfield = counter.Class. Getdeclaredfield ("Map");
  38. Type type = mapfield. getgenerictype ();
  39. System. Out. println ("Maptype :"+ Type. tostring ());
  40. If(TypeInstanceofParameterizedtype ){
  41. Parameterizedtype paramerizedtype = (parameterizedtype) type;
  42. Type [] actualtypes = paramerizedtype. getactualtypearguments ();
  43. For(Type T: actualtypes ){
  44. If(TInstanceofClass ){
  45. Class clz = (class) T;
  46. System. Out. println ("Classtype :"+ Clz. getname ());
  47. }
  48. }
  49. }
  50. }Catch(Securityexception e ){
  51. E. printstacktrace ();
  52. }Catch(Nosuchmethodexception e ){
  53. E. printstacktrace ();
  54. }Catch(Illegalargumentexception e ){
  55. E. printstacktrace ();
  56. }Catch(Instantiationexception e ){
  57. E. printstacktrace ();
  58. }Catch(Illegalaccessexception e ){
  59. E. printstacktrace ();
  60. }Catch(Invocationtargetexception e ){
  61. E. printstacktrace ();
  62. }Catch(Nosuchfieldexception e ){
  63. E. printstacktrace ();
  64. }
  65. }
  66. }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.