1. This is a class of direct conversion tools , Can be applied to

* Object conversion
* Object list conversion
* Attribute copy , If and only if the two objects have the same non static property name and the corresponding property type, the property value copy is performed
* Convert object to hash table Object---Map
* take list<object> Convert to List<Map<String, Object>> import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException; import
java.lang.reflect.Method; import java.lang.reflect.Modifier; import
java.util.*; import java.util.concurrent.locks.ReentrantReadWriteLock; import
java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock; /** * @Auther: cjw
* @Date: 2016/7/18 17:22 * @Description: bean Tools */ public class BeanUtil {
private static WriteLock lock = new ReentrantReadWriteLock().writeLock();
private static final Map<Class<?>, Map<String, Field>> METADATA = new
HashMap<>(); /** * Object conversion * * @param source * Source object * @param targetClass * Target class *
@return Returns a new target class object , The property values of the target class object are copied from the source object */ public static <T> T convert(Object
source, Class<T> targetClass) { T target = newInstance(targetClass);
copyProperties(source, target); return target; } /** * Object list conversion * * @param
sources * Source object list * @param targetClass * Target class * @return Returns a new list of target objects ,
The property values of each target class object are copied from the source object */ public static <T> List<T> convert(Collection<?>
sources, Class<T> targetClass) { List<T> targets = new ArrayList<T>(); if
(sources != null && sources.size() > 0) { for (Object source : sources) {
targets.add(convert(source, targetClass)); } } return targets; } /** * Attribute copy ,
If and only if the two objects have the same non static property name and the corresponding property type, the property value copy is performed * * @param source * Source object * @param target
* Target object */ public static void copyProperties(Object source, Object target) {
copyProperties(source, target, true); } /** * Attribute copy ,
If and only if the two objects have the same non static property name and the corresponding property type, the property value copy is performed * * @param source * Source object * @param target
* Target object * @param copyNullProperty * Copy or not null Property value */ public static void
copyProperties(Object source, Object target, boolean copyNullProperty) { if
(source == null) { return ; } Class<?> sourceClass = source.getClass();
Class<?> targetClass = target.getClass(); Map<String, Field> targetFields =
getFieldsMap(targetClass, FieldType.NOT_STATIC); Map<String, Field>
sourceFields = getFieldsMap(sourceClass, FieldType.NOT_STATIC); for (String
name : targetFields.keySet()) { if (sourceFields.containsKey(name)) { Field
sourceField = sourceFields.get(name); Field targetField =
targetFields.get(name); if (targetField.getType() == sourceField.getType()) {
Object value = getPropertyValue(source, sourceField); if (value == null &&
!copyNullProperty) { continue ; } setPropertyValue(target, targetField, value);
} } } } /** * Create an instance of a class * * @param beanClass * class */ public static <T> T
newInstance(Class<T> beanClass) { try { return beanClass.newInstance(); } catch
(Throwable e) { throw new RuntimeException(e); } } /** * Sets the value of an object property * * @param
bean * Target object * @param field * Property name * @param propertyValue * The value of the property */ public
static void setPropertyValue(Object bean, Field field, Object propertyValue) {
try { field.set(bean, propertyValue); } catch (Throwable e) { throw new
RuntimeException(bean.getClass().getName() + " " + field.getName() + " " +
propertyValue, e); } } /** * Gets the value of the target property * * @param bean * Target object * @param field *
Property name * @return */ @SuppressWarnings("unchecked") public static <T> T
getPropertyValue(Object bean, Field field) { try { return (T) field.get(bean);
} catch (Throwable e) { throw new RuntimeException(e); } } /** * Set the value of the property * *
@param bean * Object or class * @param property * Class property or object property name * @param propertyValue * The value of the property
*/ public static void setPropertyValue(Object bean, String property, Object
propertyValue) { if (bean != null) { Class<?> beanClass = null; if (bean
instanceof Class) { beanClass = (Class<?>) bean; } else { beanClass =
bean.getClass(); } Field field = getFieldsMap(beanClass,
FieldType.ALL).get(property); if (field != null) { setPropertyValue(bean,
field, propertyValue); } } } /** * Gets the value of the property * * @param bean * Object or class * @param
property * Class property name or object property name * @return */ public static <T> T
getPropertyValue(Object bean, String property) { if (bean != null) { Class<?>
beanClass = null; if (bean instanceof Class) { beanClass = (Class<?>) bean; }
else { beanClass = bean.getClass(); } Field field = getFieldsMap(beanClass,
FieldType.ALL).get(property); if (field != null) { return
getPropertyValue(bean, field); } } return null; } /** * Gets the type of the property * * @param bean
* Object or class * @param property * Class property name or object property name * @return */ public static Class<?>
getPropertyType(Object bean, String property) { if (bean != null) { Class<?>
beanClass = null; if (bean instanceof Class) { beanClass = (Class<?>) bean; }
else { beanClass = bean.getClass(); } Field field = getFieldsMap(beanClass,
FieldType.ALL).get(property); if (field != null) { return field.getType(); } }
return null; } /** * Convert object to hash table * * @param source * Source object * @return */ public
static Map<String, Object> convertMap(Object source) { return
convertMap(source, true); } /** * Convert object to hash table * * @param source * Source object * @param
convertNullProperty * Is null property converted * @return */ public static Map<String, Object>
convertMap(Object source, boolean convertNullProperty) { Map<String, Object>
map = new HashMap<String, Object>(); if (source != null) { Map<String, Field>
sourceFields = getFieldsMap(source.getClass(), FieldType.NOT_STATIC); for
(String name : sourceFields.keySet()) { Object value = getPropertyValue(source,
sourceFields.get(name)); if (value == null && !convertNullProperty) { continue
; } else { map.put(name, value); } } } return map; } /** * take list<object> Convert to
List<Map<String, Object>> * @param sources * @param convertNullProperty *
@return */ public static List<Map<String, Object>> convertListMap(List<?>
sources, boolean convertNullProperty) { List<Map<String, Object>> maps = new
ArrayList<>(); for (Object object : sources){
maps.add(convertMap(object,convertNullProperty)); } return maps; } /** *
Gets the declared property sheet * * @param beanClass * Target class * @param fieldType * Property type * @return */
private static Map<String, Field> getFieldsMap(Class<?> beanClass, FieldType
fieldType) { Map<String, Field> map = getReferableFieldsMap(beanClass);
Map<String, Field> target = new HashMap<>(); if (map != null && !map.isEmpty())
{ for (String name : map.keySet()) { Field field = map.get(name); switch
(fieldType) { case ALL: target.put(name, field); break; case STATIC: if
((field.getModifiers() & Modifier.STATIC) == Modifier.STATIC) {
target.put(name, field); } break; case NOT_STATIC: if ((field.getModifiers() &
Modifier.STATIC) != Modifier.STATIC) { target.put(name, field); } break; } } }
return target; } /** * Gets the property sheet of the current class declaration * * @param beanClass * Target class * @return */
private static Map<String, Field> getDeclaredFieldsMap(Class<?> beanClass) {
Field[] fields = beanClass.getDeclaredFields(); Map<String, Field> map = new
HashMap<String, Field>(); if (fields != null && fields.length > 0) { for (Field
field : fields) { field.setAccessible(true); map.put(field.getName(), field); }
} return map; } /** * Gets all property sheets of the declaration * * @param beanClass * Target class * @return */
private static Map<String, Field> getReferableFieldsMap(Class<?> beanClass) {
if (!METADATA.containsKey(beanClass)) { try { lock.lock(); if
(!METADATA.containsKey(beanClass)) { Map<String, Field> map = new HashMap<>();
while (beanClass != null) { map.putAll(getDeclaredFieldsMap(beanClass));
beanClass = beanClass.getSuperclass(); } METADATA.put(beanClass, map); } }
finally { lock.unlock(); } } return METADATA.get(beanClass); } enum FieldType {
STATIC, NOT_STATIC, ALL } /** * Calling member methods ( Or class method ), This method requires that the parameters of the called method must not have the basic data type * * @param
object * Specific objects ( Or class ) * @param methodName * Object's member method name ( Or the class method name of the class ) * @param argValues
* A list of parameter values for the method * @return */ public static <E> E invokeMethod(Object object, String
methodName, Object... argValues) { Class<?>[] classes = null; if (argValues !=
null && argValues.length > 0) { int length = argValues.length; classes = new
Class<?>[length]; for (int i = 0; i < length; i++) { classes[i] =
argValues[i].getClass(); } } return invokeMethod(object, methodName, argValues,
classes); } /** * Call the member method of the object ( Class method of or class ) * * @param object * Specific objects ( Or class ) * @param
methodName * Object's member method name ( Or the class method name of the class ) * @param argValues * A list of parameter values for the method * @param
argTypes * A list of parameter types for the method * @return */ @SuppressWarnings("unchecked") public static
<E> E invokeMethod(Object object, String methodName, Object[] argValues,
Class<?>[] argTypes) { try { return (E) getAccessibleMethod(object, methodName,
argTypes).invoke(object, argValues); } catch (IllegalAccessException |
IllegalArgumentException | InvocationTargetException e) { throw new
RuntimeException(e); } } /** * Gets an accessible method object * * @param object * Specific objects ( Or class ) *
@param methodName * Object's member method name ( Or the class method name of the class ) * @param types * A list of parameter types for the method * @return */
private static Method getAccessibleMethod(Object object, String methodName,
Class<?>... types) { Class<?> entityClass = object instanceof Class ?
(Class<?>) object : object.getClass(); while (entityClass != null) { try {
Method target = entityClass.getDeclaredMethod(methodName, types);
target.setAccessible(true); return target; } catch (Throwable e) {} entityClass
= entityClass.getSuperclass(); } return null; } }
 

Technology