Common Java reflection methods

Source: Internet
Author: User

 

Example:

  1. Package com;
  2. /***
  3. *
  4. * Birds
  5. *
  6. * @ Author Administrator
  7. *
  8. */
  9. Public class bird implements animal {
  10. Public bird (){
  11. }
  12. Public String color;
  13. Private int age;
  14. Public int getage (){
  15. Return age;
  16. }
  17. Public void setage (INT age ){
  18. This. Age = age;
  19. }
  20. Public String getcolor (){
  21. Return color;
  22. }
  23. Public void setcolor (string color ){
  24. This. Color = color;
  25. }
  26. }

 

  1. Package com;
  2. Import java. Lang. Reflect. field;
  3. Import java. Lang. Reflect. invocationtargetexception;
  4. Import java. Lang. Reflect. method;
  5. Import java. Lang. Reflect. modifier;
  6. Public class test {
  7. /**
  8. * @ Param ARGs
  9. * @ Throws classnotfoundexception
  10. * @ Throws nosuchfieldexception
  11. * @ Throws securityexception
  12. * @ Throws illegalaccessexception
  13. * @ Throws illegalargumentexception
  14. * @ Throws nosuchmethodexception
  15. * @ Throws invocationtargetexception
  16. */
  17. Public static void main (string [] ARGs) throws classnotfoundexception, securityexception, nosuchfieldexception, illegalargumentexception, illegalaccessexception, nosuchmethodexception, invocationtargetexception {
  18. // Test the Java reflection mechanism
  19. Class C = NULL;
  20. // Create a class instance
  21. C = Class. forname ("com. Bird ");
  22. System. Out. println (C. getname ());
  23. Package P = NULL;
  24. P = C. getpackage ();
  25. System. Out. println (P. getname ());
  26. // Return a Field object, which reflects the declared fields specified by the class object or interface.
  27. Field [] f = C. getdeclaredfields ();
  28. For (INT I = 0; I <F. length; I ++ ){
  29. System. Out. println (F [I]. GetType (). getname ());
  30. System. Out. println (modifier. tostring (F [I]. getmodifiers ()));
  31. System. Out. println (F [I]. getname ());
  32. }
  33. Class [] cc = C. getinterfaces ();
  34. For (class cite: CC ){
  35. System. Out. println (CITE. getname ());
  36. }
  37. C = Class. forname ("com. Bird ");
  38. Field FF = C. getfield ("color ");
  39. Bird = new bird ();
  40. System. Out. println ("bird color:" + bird. getcolor ());
  41. Ff. Set (Bird, "Red ");
  42. System. Out. println ("bird color:" + bird. getcolor ());
  43. Method M = C. getmethod ("setcolor", class. forname ("Java. Lang. String "));
  44. Bird BF = new bird ();
  45. System. Out. println ("BF color:" + BF. getcolor ());
  46. M. Invoke (Bf, "orange ");
  47. System. Out. println ("BF color:" + BF. getcolor ());
  48. }
  49. }

 

Running result:

Com. Bird
Com
Java. Lang. String
Public
Color
Int
Private
Age
Com. Animal
Bird color: NULL
Bird color: red
BF color: NULL
BF color: Orange

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.