Transferred from: http://blog.sina.com.cn/s/blog_3c62c21f01011xci.html
Method using the field and method under these two packages.
Import Java.lang.reflect.Field;
Import Java.lang.reflect.Method;
public static void Getobjectvalue (Object object) throws Exception {
All entity classes in our project inherit Basedomain (all entity base classes: This class is just serialization)
You don't need to get rid of yourself.
if (Object! = null&& Object instanceof basedomain) {//if (object!=null)----begin
//Get the class
CLASS<?>CLZ = Object.getclass ();
//Gets all properties of the entity class, returns the field array
Field[]fields = Clz.getdeclaredfields ();
for (Fieldfield:fields) {//--for () begin
System.out.println (Field.getgenerictype ());//Print all property types for this class
//If the type is string
if (Field.getgenerictype (). toString (). Equals (
"Classjava.lang.String") {//if type is a class type, it is preceded by "class", followed by the class name
//Gettet method to get the attribute
METHODM = (Method) object.getclass (). GetMethod (
"Get" + getmethodname (Field.getname ()));
Stringval = (String) m.invoke (object);//Call getter method to get property value
if (val! = null) {
System.out.println ("StringType:" + val);
}
}
If the type is an integer
if (Field.getgenerictype (). toString (). Equals (
"Classjava.lang.Integer")) {
METHODM = (Method) object.getclass (). GetMethod (
"Get" + getmethodname (Field.getname ()));
Integerval = (Integer) m.invoke (object);
if (val! = null) {
System.out.println ("Integertype:" + val);
}
}
If the type is double
if (Field.getgenerictype (). toString (). Equals (
"Classjava.lang.Double")) {
METHODM = (Method) object.getclass (). GetMethod (
"Get" + getmethodname (Field.getname ()));
Doubleval = (Double) m.invoke (object);
if (val! = null) {
System.out.println ("Doubletype:" + val);
}
}
If the type is Boolean is the encapsulated class
if (Field.getgenerictype (). toString (). Equals (
"Classjava.lang.Boolean")) {
METHODM = (Method) object.getclass (). GetMethod (
Field.getname ());
Booleanval = (Boolean) m.invoke (object);
if (val! = null) {
System.out.println ("Booleantype:" + val);
}
}
//If the type is a Boolean base data type It's a bit of a name, if the definition name is isxxx, it's all isxxx.
// Reflection cannot find the specific name of the Getter
if (Field.getgenerictype (). toString (). Equals ("Boolean")) {
&NBSP;&NBSP;&NBSP;&NBSP;METHODM = (Method) object.getclass (). GetMethod (
field.getname ());
booleanval = (Boolean) m.invoke (object);
if (val! = null) {
system.out.println (" Booleantype: "+ val");
}
}
//If the type is Date
if (Field.getgenerictype (). toString (). equals (
"Classjava.util.Date")) {
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;METHODM = (Method) object.getclass (). GetMethod (
"Get" + getmethodname (Field.getname ()));
dateval = (Date) m.invoke (object);
if (val! = null) {
system.out.println (" Datetype: "+ val");
}
}
If the type is short
if (Field.getgenerictype (). toString (). Equals (
"Classjava.lang.Short")) {
METHODM = (Method) object.getclass (). GetMethod (
"Get" + getmethodname (Field.getname ()));
Shortval = (short) m.invoke (object);
if (val! = null) {
System.out.println ("Shorttype:" + val);
}
}
If you need additional types, do your own expansion
}//for ()--end
}//if (object!=null)----End
}
Capitalize the first letter of a string, the highest efficiency,
private static String Getmethodname (stringfildename) throws exception{
byte[] Items =fildename.getbytes ();
Items[0] = (byte) ((char) items[0]-' a ' + ' a ');
return new String (items);
}
All the property types and values of the class are obtained through the Java reflection mechanism.