Java traversal properties in a class

Source: Internet
Author: User

     Public Static voidTestreflect (Object model)throwsnosuchmethodexception, Illegalaccessexception,
IllegalArgumentException, invocationtargetexception{field[] Field
= Model.getclass (). Getdeclaredfields ();//gets all properties of the entity class, returns the field array for(intj=0; J<field.length; J + +) {//Traverse All PropertiesString name = Field[j].getname ();//gets the name of the propertySystem.out.println ("Attribute name:" +name); Name= Name.substring (0,1). toUpperCase () +name.substring (1);//capitalize the first character of a property to make it easy to construct a Get,set methodString type = Field[j].getgenerictype (). toString ();//gets the type of the property if(Type.equals ("Class java.lang.String")) {//if type is a class type, it is preceded by "class", followed by the class nameMethod m = Model.getclass (). GetMethod ("get" +name); String value= (String) m.invoke (model);//Call getter method to get property value if(Value! =NULL) {System.out.println ("Attribute value:" +value); } } if(Type.equals ("Class Java.lang.Integer")) ) {Method m= Model.getclass (). GetMethod ("get" +name); Integer value=(Integer) M.invoke (model); if(Value! =NULL) {System.out.println ("Attribute value:" +value); } } if(Type.equals ("Class Java.lang.Short")) ) {Method m= Model.getclass (). GetMethod ("get" +name); Short value=(short) m.invoke (model); if(Value! =NULL) {System.out.println ("Attribute value:" +value);
} }
if(Type.equals ("Class java.lang.Double")) ) {Method m= Model.getclass (). GetMethod ("get" +name); Double value=(Double) M.invoke (model); if(Value! =NULL) {System.out.println ("Attribute value:" +value); } } if(Type.equals ("Class Java.lang.Boolean")) ) {Method m= Model.getclass (). GetMethod ("get" +name); Boolean value=(Boolean) M.invoke (model); if(Value! =NULL) {System.out.println ("Attribute value:" +value); } } if(Type.equals ("Class Java.util.Date")) ) {Method m= Model.getclass (). GetMethod ("get" +name); Date value=(Date) M.invoke (model); if(Value! =NULL) {System.out.println ("Attribute value:" +value.tolocalestring ()); } } } }

Java traversal properties in a class

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.