The java reflection mechanism is used to obtain all attribute types and values of the class.

Source: Internet
Author: User

The field and method under the two packages are used.

Import java. lang. reflect. Field;

Import java. lang. reflect. Method;

 

Public static void getObjectValue (Object object) throws Exception {
// All object classes of our project inherit BaseDomain
(ALL object base classes: this class is serialized)
// Remove unnecessary items.
If (object! = Null
& Object instanceof BaseDomain)
{// If (object! = Null) ---- begin
// Obtain the class
Class <?>
Clz = object. getClass ();
//
Obtains all attributes of the object class and returns the Field array.
Field []
Fields = clz. getDeclaredFields ();

For (Field
Field: fields) {// -- for () begin
System. out. println (field. getGenericType (); // print all attribute types of the class

//
If the type is String
If
(Field. getGenericType (). toString (). equals (
"Class
Java. lang. String ") {// if the type is of the class type, it contains" class "first, followed by the class name
//
Gettet method for getting this property

Method
M = (Method) object. getClass (). getMethod (
"Get"
+ GetMethodName (field. getName ()));

String
Val = (String) m. invoke (object); // call the getter method to obtain the attribute value
If
(Val! = Null ){
System. out. println ("String
Type: "+ val );
}

}

//
If the data type is Integer
If
(Field. getGenericType (). toString (). equals (
"Class
Java. lang. Integer ")){
Method
M = (Method) object. getClass (). getMethod (
"Get"
+ GetMethodName (field. getName ()));
Integer
Val = (Integer) m. invoke (object );
If
(Val! = Null ){
System. out. println ("Integer
Type: "+ val );
}

}

//
If the data type is Double
If
(Field. getGenericType (). toString (). equals (
"Class
Java. lang. Double ")){
Method
M = (Method) object. getClass (). getMethod (
"Get"
+ GetMethodName (field. getName ()));
Double
Val = (Double) m. invoke (object );
If
(Val! = Null ){
System. out. println ("Double
Type: "+ val );
}

}

//
If the type is Boolean, it is an encapsulation class.
If
(Field. getgenerictype (). tostring (). Equals (
"Class
Java. Lang. boolean ")){
Method
M = (method) object. getclass (). getmethod (
Field. getname ());

Boolean
Val = (Boolean) M. Invoke (object );
If
(Val! = NULL ){
System. Out. println ("Boolean
Type: "+ val );
}

}

//
If the data type is Boolean, the basic data type is different. Here we can say that if the definition name is isxxx, all data types are isxxx.
//
The specific getter name cannot be found for reflection.
If
(Field. getgenerictype (). tostring (). Equals ("Boolean ")){
Method
M = (method) object. getclass (). getmethod (
Field. getname ());

Boolean
Val = (Boolean) M. Invoke (object );
If
(Val! = NULL ){
System. Out. println ("Boolean
Type: "+ val );
}

}

//
If the data type is date
If
(Field. getgenerictype (). tostring (). Equals (
"Class
Java. util. Date ")){
Method
M = (Method) object. getClass (). getMethod (
"Get"
+ GetMethodName (field. getName ()));
Date
Val = (Date) m. invoke (object );
If
(Val! = Null ){
System. out. println ("Date
Type: "+ val );
}

}

//
If the type is Short
If
(Field. getGenericType (). toString (). equals (
"Class
Java. lang. Short ")){
Method
M = (method) object. getclass (). getmethod (
"Get"
+ Getmethodname (field. getname ()));
Short
Val = (short) M. Invoke (object );
If
(Val! = NULL ){
System. Out. println ("short
Type: "+ val );
}

}

//
If you need other types, perform expansion on your own.

} // ()
-- End

} // If (object! = NULL
) ---- End
}

// Upper the first letter of a string with the highest efficiency,
Private static String getMethodName (String
FildeName) throws Exception {
Byte [] items =
FildeName. getBytes ();
Items [0] = (byte) (char)
Items [0]-'A' + 'A ');
Return new String (items );
}

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.