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 );
}