In Java, iterate through all the attributes of an object

Source: Internet
Author: User
This article is reproduced from (http://www.cnblogs.com/137913828S2/archive/2012/07/10/2584774.html) in Java, Traverse entity class properties and types, attribute values

public static void Testreflect (Object model) throws Nosuchmethodexception, Illegalaccessexception, IllegalArgumentException, invocationtargetexception{
field[] field = Model.getclass (). Getdeclaredfields (); Gets all properties of the entity class, returns a field array
for (int j=0; j<field.length; j + +) {//Traverse all properties
String name = Field[j].getname (); Gets the name of the property

System.out.println ("attribute name:" +name);
Name = name.substring (0,1). toUpperCase () +name.substring (1); Capitalize the first character of a property to facilitate the construction of the Get,set method
String type = Field[j].getgenerictype (). toString (); Get the type of the property
if (Type.equals ("Class java.lang.String")) {//if type is a class type, preceded by "class" followed by the class name
Method M = Model.getclass (). GetMethod ("get" +name);
String value = (string) m.invoke (model); Call getter Method 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 ());
}
}
}
}

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.