How to traverse the attributes and data types and property values of an entity class in Java

Source: Internet
Author: User

1  Packagecom.walkerjava.test; 2    3  ImportJava.lang.reflect.Field; 4  Importjava.lang.reflect.InvocationTargetException; 5  ImportJava.lang.reflect.Method; 6  Importjava.util.Date; 7    8  /*** 9 * Traverse the attributes and data types of an entity class and attribute valuesTen   *   One   * @authorLibaozhen A * @date 2013-1-4 a.m. 10:25:02 - * @company -   * @versionv1.3 the   * @seeRelated Classes -   * @sinceRelated/Version -   */   -   Public classReflecttest { +           Public Static voidReflecttest (Object model)throwsNosuchmethodexception, - illegalaccessexception, IllegalArgumentException, + InvocationTargetException { A                  //gets all properties of the entity class, returns the field array atfield[] field =Model.getclass (). Getdeclaredfields ();  -                  //Traverse All Properties -                   for(intj = 0; J < Field.length; J + +) {   -                          //gets the name of the property -String name =Field[j].getname ();  -                          //capitalize the first character of a property to make it easy to construct a Get,set method inName = name.substring (0, 1). toUpperCase () + name.substring (1);  -                          //gets the type of the property toString type =Field[j].getgenerictype (). toString ();  +                          //if type is a class type, it is preceded by "class", followed by the class name -System.out.println ("property is:" +name);  the                          if(Type.equals ("Class java.lang.String"))) {   *Method m = Model.getclass (). GetMethod ("get" +name);  $                                  //Call getter method to get property valuePanax NotoginsengString value =(String) M.invoke (model);  -SYSTEM.OUT.PRINTLN ("Data type: String");  the                                  if(Value! =NULL) {   +System.out.println ("Property value is:" +value);  A}Else {   theSystem.out.println ("Property value is: null");  +                                  }   -                          }   $                          if(Type.equals ("Class Java.lang.Integer"))) {   $Method m = Model.getclass (). GetMethod ("get" +name);  -Integer value =(Integer) M.invoke (model);  -SYSTEM.OUT.PRINTLN ("Data type: Integer");  the                                  if(Value! =NULL) {   -System.out.println ("Property value is:" +value); Wuyi}Else {   theSystem.out.println ("Property value is: null");  -                                  }   Wu                          }   -                          if(Type.equals ("Class Java.lang.Short"))) {   AboutMethod m = Model.getclass (). GetMethod ("get" +name);  $Short value =(short) m.invoke (model);  -SYSTEM.OUT.PRINTLN ("Data type: Short");  -                                  if(Value! =NULL) {   -System.out.println ("Property value is:" +value);  A}Else {   +System.out.println ("Property value is: null");  the                                  }   -                          }   $                          if(Type.equals ("Class java.lang.Double"))) {   theMethod m = Model.getclass (). GetMethod ("get" +name);  theDouble value =(Double) M.invoke (model);  theSYSTEM.OUT.PRINTLN ("Data type: Double");  the                                  if(Value! =NULL) {   -System.out.println ("Property value is:" +value);  in}Else {   theSystem.out.println ("Property value is: null");  the                                  }   About                          }   the                          if(Type.equals ("Class Java.lang.Boolean"))) {   theMethod m = Model.getclass (). GetMethod ("get" +name);  theBoolean value =(Boolean) M.invoke (model);  +SYSTEM.OUT.PRINTLN ("Data type: Boolean");  -                                  if(Value! =NULL) {   theSystem.out.println ("Property value is:" +value); Bayi}Else {   theSystem.out.println ("Property value is: null");  the                                  }   -                          }   -                          if(Type.equals ("Class Java.util.Date"))) {   theMethod m = Model.getclass (). GetMethod ("get" +name);  theDate value =(Date) M.invoke (model);  theSYSTEM.OUT.PRINTLN ("Data type: Date");  the                                  if(Value! =NULL) {   -System.out.println ("Property value is:" +value);  the}Else {   theSystem.out.println ("Property value is: null");  the                                  }  94                          }   the                  }   the          }   the}

How to traverse the attributes and data types and property values of an entity class in Java

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.