gistfile1.txt public static void getObjectValue(Object object) throws Exception { if (object != null object instanceof BaseDomain) {//if (object!=null ) ----begin // 拿到类 Class clz = object.getClass(); // 获取实体类的所有属性,
public static void getObjectValue(Object object) throws Exception { if (object != null && object instanceof BaseDomain) {//if (object!=null ) ----begin // 拿到类 Class clz = object.getClass(); // 获取实体类的所有属性,返回Field数组 Field[] fields = clz.getDeclaredFields(); for (Field field : fields) {// --for() begin //打印该类的所有属性类型 System.out.println(field.getGenericType()); // String if (field.getGenericType().toString().equals( "class java.lang.String")) { // 拿到该属性的get,set方法 Method m = (Method) object.getClass().getMethod( "get" + getMethodName(field.getName())); // 调用getter方法获取属性值 String val = (String) m.invoke(object); if (val != null) { System.out.println("String type:" + val); } } //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); } } // 如果类型是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); } } } //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); } } //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); } } } } } }