Map conversion object, Object transform map, list<object> conversion list<map>, list<map> conversion list<object>, Object copy
Package cn.framework.util; Import Java.lang.reflect.Field;
Import java.lang.reflect.InvocationTargetException;
Import Java.lang.reflect.Method;
Import Java.math.BigDecimal;
Import Java.text.DateFormat;
Import java.text.ParseException;
Import Java.text.SimpleDateFormat;
Import java.util.ArrayList;
Import java.util.Collection;
Import Java.util.HashMap;
Import Java.util.Iterator;
Import java.util.List;
Import Java.util.Map;
Import Javax.servlet.http.HttpServletRequest;
/**
* Object class Advanced Operations
*
* @author Jonas
*
*/
@SuppressWarnings ({"Rawtypes", "Unchecked"})
public class Objectutil {
/**
* Two objects of the same type overwrite (oldobj overwrite newobj,isnullreplace when oldobj field value is empty overwrite, true overwrite, false not overwrite)
*
* @author Jonas
* @date 2014-11-28 10:28:54
* @param NEWOBJ
* @param oldobj
* @param isnullreplace
* @throws illegalaccessexception
* @throws IllegalArgumentException
* @throws InvocationTargetException
*/
public static void Objectreplace (object newObj, Object Oldobj, Boolean isnullreplace) throws Illegalaccessexception, Ille Galargumentexception, InvocationTargetException {
if (newObj!= null && oldobj!= null) {
Class _class1 = Newobj.getclass ();
Class _class2 = Oldobj.getclass ();
field[] fields = _class1.getdeclaredfields ();
for (Field field:fields) {
Method GetMethod = GetMethod (_class2, field, ' get ');
if (GetMethod!= null) {
Object value = Getmethod.invoke (oldobj, NULL);
if (!isnullreplace && value = null) {
Continue
}
Method Setmethod = GetMethod (_class1, field, "set");
Setmethod.invoke (newObj, value);
}
}
}
}
/**
* Two objects of the same type (Oldobj overlay newobj,isnullreplacelist with empty face value and fields to be overwritten)
*
* @author Jonas
* @date 2014-11-28 10:28:54
* @param NEWOBJ
* @param oldobj
* @param isnullreplace
* @throws illegalaccessexception
* @throws IllegalArgumentException
* @throws InvocationTargetException
*/
public static void Objectreplace (object newObj, Object Oldobj, list<string> isnullreplacelist)
Throws Illegalaccessexception, IllegalArgumentException, invocationtargetexception {
if (newObj!= null && oldobj!= null) {
if (isnullreplacelist = = null) {
Isnullreplacelist = new arraylist<string> ();
}
Class _class1 = Newobj.getclass ();
Class _class2 = Oldobj.getclass ();
if (_class1.getname (). Equals (_class2.getname ())) {
field[] fields = _class1.getdeclaredfields ();
for (Field field:fields) {
Method GetMethod = GetMethod (_class1, field, ' get ');
if (GetMethod!= null) {
Object value = Getmethod.invoke (oldobj, NULL);
if (!isnullreplacelist.contains (Field.getname ()) && value = = null) {
Continue
}
Method Setmethod = GetMethod (_class1, field, "set");
Setmethod.invoke (newObj, value);
}
}
}
}
}
/**
* Convert list<t> inside objects into list<map>
*
* @author Jonas
* @date 2014-11-28 10:30:17
* @param list
* @return
* @throws IllegalArgumentException
* @throws illegalaccessexception
* @throws ParseException
* @throws InvocationTargetException
*/
public static list<map<string, object>> listtomap (List list) throws IllegalArgumentException, Illegalaccessexception, ParseException, invocationtargetexception {
list<map<string, object>> listmap = null;
for (Object object:list) {
if (Listmap = = null) {
Listmap = new arraylist<map<string, object>> ();
}
map<string, object> map = Objecttomap (Object, NULL);
if (map!= null &&!map.isempty ()) {
Listmap.add (map);
}
}
return listmap;
}
public static map<string, object> Objecttomap (Object object) throws IllegalArgumentException, Illegalaccessexception, InvocationTargetException, parseexception {
Class _class = Object.getclass ();
field[] fields = _class.getdeclaredfields ();
map<string, object> map = null;
for (Field field:fields) {
if (map = = null) {
Map = new hashmap<string, object> ();
}
method is = GetMethod (_class, field, "get");
if (method!= null) {
SetFieldValue (object, field, map, method, True);
}
}
return map;
}
public static void Listmapvaluetostring (List<map> listmap) throws IllegalArgumentException, Illegalaccessexception, ParseException, invocationtargetexception {
if (Listmap = = null) {
Return
}
for (Map Map:listmap) {
Mapvaluetostring (map);
}
}
public static void Listmapvaluetostrings (List<map<string, object>> Listmap) throws IllegalArgumentException, Illegalaccessexception, ParseException, invocationtargetexception {
if (Listmap = = null) {
Return
}
for (Map Map:listmap) {
Mapvaluetostring (map);
}
}
public static map<string, object> Objecttomap (Object object, List<string> ignorefieldlist) {
Class _class = Object.getclass ();
field[] fields = _class.getdeclaredfields ();
map<string, object> map = null;
for (Field field:fields) {
if (map = = null) {
Map = new hashmap<string, object> ();
}
String fieldName = Field.getname ();
if (ignorefieldlist!= null && ignorefieldlist.contains (fieldName)) {
Continue
}
method is = GetMethod (_class, FieldName, "get");
if (method!= null) {
try {
SetFieldValue (object, field, map, method, True);
catch (Exception e) {
E.printstacktrace ();
}
}
}
return map;
}
/**
* Convert list<t> inside objects into list<map>
*
* @author Jonas
* @date 2014-11-28 10:30:17
* @param list
* @return
* @throws IllegalArgumentException
* @throws illegalaccessexception
* @throws ParseException
* @throws InvocationTargetException
*/
public static list<map<string, object>> listtomap (list list, list<string> ignorefieldlist)
Throws IllegalArgumentException, Illegalaccessexception, ParseException, invocationtargetexception {
list<map<string, object>> listmap = null;
for (Object object:list) {
if (Listmap = = null) {
Listmap = new arraylist<map<string, object>> ();
}
map<string, object> map = Objecttomap (Object, ignorefieldlist);
if (map!= null &&!map.isempty ()) {
Listmap.add (map);
}
}
return listmap;
}
public static void Mapvaluetostring (map map) {
if (map!= null) {
For (Object Key:map.keySet ()) {
Object value = Map.get (key);
if (value!= null) {
Map.put (Key, value.tostring ());
}
}
}
}
public static list<map> Listtomapbymethod (List list) throws IllegalArgumentException, Illegalaccessexception, ParseException, InvocationTargetException {
List<map> listmap = null;
for (Object object:list) {
if (Listmap = = null) {
Listmap = new arraylist<map> ();
}
Map map = Objecttomapbymethod (object, NULL);
if (map!= null &&!map.isempty ()) {
Listmap.add (map);
}
}
return listmap;
}
public static list<map<string, object>> Listtomapbymethod (list list, list<string> ignorefieldlist)
Throws IllegalArgumentException, Illegalaccessexception, ParseException, invocationtargetexception {
list<map<string, object>> listmap = null;
for (Object object:list) {
if (Listmap = = null) {
Listmap = new arraylist<map<string, object>> ();
}
map<string, object> map = Objecttomapbymethod (Object, ignorefieldlist);
if (map!= null &&!map.isempty ()) {
Listmap.add (map);
}
}
return listmap;
}
public static map<string, object> Objecttomapbymethod (Object object, List<string> ignorefieldlist)
Throws IllegalArgumentException, Illegalaccessexception, InvocationTargetException, ParseException, SecurityException {
Class _class = Object.getclass ();
Method[] methods = _class.getmethods ();
Map map = null;
For (method Method:methods) {
if (map = = null) {
Map = new HashMap ();
}
String methodname = Method.getname ();
if (ignorefieldlist!= null && ignorefieldlist.contains (methodname)) {
Continue
}
String prefix = methodname.substring (0, 3);
String suffix = methodname.substring (3, Methodname.length ());
if ("Get". Equals (prefix)) {
SetFieldValue (object, suffix, map, method, True);
else if ("Set". Equals (prefix)) {
Method _method = null;
try {
_method = _class.getmethod ("get" + suffix, null);
catch (Nosuchmethodexception e) {
}
if (_method!= null) {
SetFieldValue (object, suffix, map, _method, true);
}
} else {
SetFieldValue (object, methodname, Map, method, True);
}
}
return map;
}
/**
* Remove a space from the string field value inside the object
*
* @author Jonas
* @date 2014-11-28 10:32:54
* @param Object
* @return
* @throws illegalaccessexception
* @throws IllegalArgumentException
* @throws InvocationTargetException
* @throws ParseException
*/
public static object Objectfieldtrim (Object object) throws Illegalaccessexception, IllegalArgumentException, InvocationTargetException, ParseException {
Class _class = Object.getclass ();
field[] fields = _class.getdeclaredfields ();
for (Field field:fields) {
method is = GetMethod (_class, field, "get");
Object value = Method.invoke (object, NULL);
if (value!= null) {
if (Field.gettype (). GetName (). Equals ("java.lang.String")) {
SetFieldValue (object, field, Value.tostring (). Trim ());
} else {
SetFieldValue (object, field, value.tostring ());
}
}
}
return object;
}
/**
* Gets the object get or Set method
*
* @author Jonas
* @date 2014-11-28 10:34:56
* @param _class
* @param field
* @param prefix
* @return
*/
public static method GetMethod (Class _class, Field field, String prefix) {
Method[] methods = _class.getdeclaredmethods ();
For (method Method:methods) {
String methodname = Method.getname ();
String fieldName = Field.getname ();
String _fieldname = prefix + fieldName;
if (Methodname.touppercase (). Equals (_fieldname.touppercase ())) {
return method;
}
}
return null;
}
/**
* Gets the object get or Set method
*
* @author Jonas
* @date 2014-11-28 10:34:56
* @param _class
* @param field
* @param prefix
* @return
*/
public static method GetMethod (Class _class, String fieldName, string prefix) {
Method[] methods = _class.getdeclaredmethods ();
For (method Method:methods) {
String methodname = Method.getname ();
String _fieldname = prefix + fieldName;
if (Methodname.touppercase (). Equals (_fieldname.touppercase ())) {
return method;
}
}
return null;
}
private static void SetFieldValue (Object obj, Field field, String value) throws IllegalArgumentException, Illegalaccessex Ception, ParseException {
Field.setaccessible (TRUE);
String FieldType = Field.gettype (). GetName ();
if (Fieldtype.equals ("java.lang.String")) {
Field.set (obj, value);
else if (fieldtype.equals ("Java.lang.Integer") | | fieldtype.equals ("int")) {
Field.set (obj, integer.valueof (value));
else if (fieldtype.equals ("Java.lang.Long") | | fieldtype.equals ("Long") {
Field.set (obj, long.valueof (value));
else if (fieldtype.equals ("Java.lang.Float") | | fieldtype.equals ("Float")) {
Field.set (obj, float.valueof (value));
else if (fieldtype.equals ("java.lang.Double") | | fieldtype.equals ("Double") {
Field.set (obj, double.valueof (value));
else if (fieldtype.equals ("Java.util.Date")) {
DateFormat df = new SimpleDateFormat ("Yyyy-mm-dd");
Field.set (obj, Df.parse (value));
else if (fieldtype.equals ("Java.math.BigDecimal")) {
Field.set (obj, new BigDecimal (value));
else if (fieldtype.equals ("Java.lang.Boolean") | | fieldtype.equals ("Boolean") {
Field.set (obj, boolean.valueof (value));
else if (fieldtype.equals ("Java.lang.Byte") | | fieldtype.equals ("Byte") {
Field.set (obj, byte.valueof (value));
else if (fieldtype.equals ("Java.lang.Short") | | fieldtype.equals ("short")) {
Field.set (obj, short.valueof (value));
}
}
private static void SetFieldValue (Object obj, field field, map map, Method method, Boolean Islegalmethod)
Throws IllegalArgumentException, Illegalaccessexception, ParseException, invocationtargetexception {
Field.setaccessible (TRUE);
String fieldName = Field.getname ();
SetFieldValue (obj, fieldName, Map, method, Islegalmethod);
}
private static void SetFieldValue (Object obj, String fieldName, Map Map, Method method, Boolean Islegalmethod)
Throws IllegalArgumentException, Illegalaccessexception, ParseException, invocationtargetexception {
if (map!= null && map.containskey (Fieldname.touppercase ())) {
FieldName = Obj.getclass (). Getsimplename () + fieldName;
}
class[] _c = Method.getparametertypes ();
Class returncls = Method.getreturntype ();
if (Islegalmethod) {
if ((_c = NULL | | _c.length = 0) && isbasictype (Returncls.getname ())) {
Map.put (Fieldname.touppercase (), method.invoke (obj, null));
}
} else {
Map.put (Fieldname.touppercase (), method.invoke (obj, null));
}
}
public static Boolean Isbasictype (String typeName) {
if (Typename.equals ("java.lang.String") | | | typename.equals ("java.lang.Integer") | | typename.equals ("int") | | Typename.equals ("Java.lang.Long") | | Typename.equals ("Long")
|| Typename.equals ("Java.lang.Float") | | Typename.equals ("float") | | Typename.equals ("java.lang.Double") | | Typename.equals ("Double")
|| Typename.equals ("Java.util.Date") | | Typename.equals ("Java.math.BigDecimal") | | Typename.equals ("Java.lang.Boolean") | | Typename.equals ("boolean")
|| Typename.equals ("Java.lang.Byte") | | Typename.equals ("byte")