Java reflection Gets the value of the property and sets the value of the property

Source: Internet
Author: User

 PackageCom.whbs.bean;  Public classUserBean {PrivateInteger ID; Private intAge ; PrivateString name; PrivateString address;  PublicUserBean () {System.out.println ("Instantiation"); }            PublicInteger getId () {returnID; }       Public voidsetId (Integer id) { This. ID =ID; }       Public intGetage () {returnAge ; }       Public voidSetage (intAge ) {          This. Age =Age ; }       PublicString GetName () {returnname; }       Public voidsetName (String name) { This. Name =name; }       PublicString getaddress () {returnaddress; }       Public voidsetaddress (String address) { This. Address =address; }                 }     2 >Reflection Test Packagecom.whbs.test; ImportJava.lang.reflect.Field; ImportJava.lang.reflect.Method; ImportCom.whbs.bean.UserBean;  Public classTest1 { Public Static voidMain (string[] args)throwsException {/** Method of listing Classes 1*/         //String ClassPath = "Com.whbs.bean.UserBean"; //Class cla = Test1.class.getClassLoader (). LoadClass (ClassPath); //Object ob = Cla.newinstance ();                /** Method of listing Classes 2*/UserBean Bean=NewUserBean (); Bean.setid (100); Bean.setaddress (Wuhan); //Get Class objectClass USERCLA =(Class) Bean.getclass (); /** Get all the property collections in the class*/field[] FS=Usercla.getdeclaredfields ();  for(inti = 0; i < fs.length; i++) {Field F=Fs[i]; F.setaccessible (true);//set some properties to be accessibleObject val = f.get (Bean);//get the value of this propertySystem.out.println ("Name:" +f.getname () + "\ t value =" +val); String type= F.gettype (). toString ();//get the type of this property           if(Type.endswith ("String") {System.out.println (F.gettype () )+ "\ T is string"); F.set (Bean,"12");//set a value for a property}Else if(Type.endswith ("int") | | type.endswith ("Integer")) {System.out.println (F.gettype () )+ "\ t is int"); F.set (Bean,12);//set a value for a property}Else{System.out.println (F.gettype ()+ "\ T"); }                     }                         /** Get the method in the class*/method[] Methods=Usercla.getmethods ();  for(inti = 0; i < methods.length; i++) {Method method=Methods[i]; if(Method.getname (). StartsWith ("Get") {System.out.print ("MethodName:" +method.getname () + "\ T"); System.out.println ("Value:" +method.invoke (bean));//get the value of the Get method           }         }      }     }  

Java reflection Gets the value of the property and sets the value of the property

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.