BeanUtils
public class BeanUtils {
/** * Whether a member variable ignores judgment */
public static boolean isIgnore(Field field) {
if (field.isAnnotationPresent(Ignore.class)) {
Ignore ignore = field.getAnnotation(Ignore.class);
return ignore.ignore();
}
return false;
}
/** * object transfer set */
public static Map<String, String> beanToMapString(Object object) {
HashMap<String, String> map = newHashMap<>(); Class<? > aClass = object.getClass(); Field[] fields = aClass.getFields();for (Field field : fields) {
field.setAccessible(true);
try {
String key = field.getName();
Object value = field.get(object);
if(value ! =null) {
map.put(key, value.toString());
} else {
map.put(key, null); }}catch(IllegalAccessException e) { e.printStackTrace(); }}return map;
}
/** * object transfer set */
public static Map<String, Object> beanToMap(Object object) {
HashMap<String, Object> map = newHashMap<>(); Class<? > aClass = object.getClass(); Field[] fields = aClass.getFields();for (Field field : fields) {
field.setAccessible(true);
try {
String key = field.getName();
Object value = field.get(object);
map.put(key, value);
} catch(IllegalAccessException e) { e.printStackTrace(); }}return map;
}
/** * object transfer set */
public static Object mapToBean(Map<String, Object> map, Class clazz) {
try {
Object object = clazz.newInstance();
for (Map.Entry<String, Object> entry : map.entrySet()) {
String key = entry.getKey();
Object value = entry.getKey();
for (Field field : clazz.getFields()) {
field.setAccessible(true);
if(field.getName().equals(key)) { field.set(object, value); }}}return object;
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
/** * a deep clone of a collection in which objects must implement the serialization interface **@paramSRC Source data *@returnClone object */
public static Object clone(Object src) {
Object dest = null;
try {
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(byteOut);
out.writeObject(src);
ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
ObjectInputStream in = new ObjectInputStream(byteIn);
//noinspection unchecked
dest = in.readObject();
} catch (Exception e) {
Logger.e("BeanUtils", e.getMessage());
}
return dest;
}
public static void formatValue(Object object) {
formatNumber(object);
formatString(object);
}
/** * An object whose type is Number and value is NULL is formatted as 0 */
public static void formatNumber(Object object) {
if(object ! =null) { Class<? > aClass = object.getClass(); Field[] fields = aClass.getFields();for (Field field : fields) {
field.setAccessible(true);
if (isIgnore(field)) continue; Class<? > type = field.getType();try {
Object value = field.get(object);
if (value == null) {
if (type == Byte.class) {
field.set(object, (byte) 0);
}
if (type == Short.class) {
field.set(object, (short) 0);
}
if (type == Integer.class) {
field.set(object, (int) 0);
}
if (type == Long.class) {
field.set(object, (long) 0);
}
if (type == Float.class) {
field.set(object, (float) 0);
}
if (type == Double.class) {
field.set(object, (double) 0); }}}catch(Exception e) { e.printStackTrace(); }}}}/** * objects whose type is String and value is NULL are formatted as "" */
public static void formatString(Object object) {
if(object ! =null) { Class<? > aClass = object.getClass(); Field[] fields = aClass.getFields();for (Field field : fields) {
field.setAccessible(true);
if (isIgnore(field)) continue; Class<? > type = field.getType();if (type == String.class) {
try {
Object value = field.get(object);
if (value == null) {
field.set(object, ""); }}catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
}
Copy the code