Commit 3c75fae4 authored by yanzg's avatar yanzg

基本操作类

parent b21d4121
package com.yanzuoguang.util.base;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/**
* 字段对应的类型的信息,依赖于字段操作类
*/
public class MethodField {
/**
* 名称
*/
public String name;
/**
* 缩写名称
*/
public String nameSimple;
/**
* 对应的字段
*/
public Field field;
/**
* 对应的获取方法
*/
public Method getMethod;
/**
* 对应的设置方法
*/
public Method setMethod;
}
package com.yanzuoguang.util.base;
import com.yanzuoguang.util.exception.CodeException;
import com.yanzuoguang.util.exception.ExceptionHelper;
import com.yanzuoguang.util.helper.StringHelper;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.*;
/**
* 对象操作类,包含字段反射
*/
public class ObjectHelper {
/**
* 缓存的类型
*/
private static Map<Class<?>, HashMap<String, MethodField>> mapCache = new HashMap<>();
// --------------------------------------------- 类型判断 ---------------------------------------------------------
/**
* 判断类型间是否可以自动转换
*
* @param to 需要转换后的类型
* @param from 转换前的类型
* @return
*/
public static boolean isAutoConvert(Class to, Class from) {
return from != to && (Number.class.isAssignableFrom(to) || to.isEnum() || to == String.class);
}
/**
* 判断from参数是否是to的子类
*
* @param to 父类
* @param from 子类
* @return 是否满足条件
*/
public static boolean isSub(Class to, Class from) {
return from == to || from.isAssignableFrom(to);
}
/**
* 判断值是否能够写入当前类型的对象
*
* @param cls 类型
* @param value 值
* @return 是否能够写入
*/
public static boolean isWriteType(Class<?> cls, Object value) {
if (cls.isEnum() || cls.isAssignableFrom(Number.class)) {
return true;
}
if (cls == String.class || cls == int.class || cls == boolean.class || cls == double.class || cls == float.class) {
return true;
}
boolean isEmpty = StringHelper.isEmpty(value);
if (isEmpty) {
return true;
}
return cls.isAssignableFrom(value.getClass());
}
// --------------------------------------------- 类型判断 ---------------------------------------------------------
/**
* 获取指定对象中某个字段的整形值
*
* @param target 需要处理的对象
* @param field 需要获取的字段的值
* @return 返回值
*/
public static int getInt(Object target, String field) {
return get(Integer.class, target, field);
}
/**
* 获取指定对象中某个字段字符串值
*
* @param target 需要处理的对象
* @param field 需要获取的字段的值
* @return 返回值
*/
public static String getString(Object target, String field) {
return get(String.class, target, field);
}
/**
* 获取指定对象中某个字段的double值
*
* @param target 需要处理的对象
* @param field 需要获取的字段的值
* @return 返回值
*/
public static double getDecimal(Object target, String field) {
return get(Double.class, target, field);
}
/**
* 类型转换(从obj中取出关键词为vName的值 , 转换为cls类型)
*
* @param cls 转换后的类型
* @param target 要转换对象
* @param field 要转换对象的关键词
* @return 返回值
*/
public static <T> T get(Class<T> cls, Object target, String field) {
Object val = get(target, field);
return StringHelper.to(cls, val);
}
/**
* 获取对象中的某个字段值,根据不同对象输出结果
*
* @param target 需要获取的对象
* @param field 需要获取的字段
* @return 获取之后的值
*/
public static Object get(Object target, String field) {
Object ret = null;
if (target == null) {
return ret;
}
Class vType = target.getClass();
if (target instanceof Number || vType.isEnum()) {
ret = null;
} else if (target instanceof Map) {
ret = ((Map) target).get(field);
} else {
ret = getByType(target.getClass(), target, field);
}
return ret;
}
/**
* 获取对象的值(从obj中获取关键词为vName的值)
*
* @param targetCls 要获取的对象的Class类型
* @param target 要获取的对象
* @param field 要获取的对象的关键词
* @return 获取后的值
*/
public static Object getByType(Class<?> targetCls, Object target, String field) {
MethodField item = getMethodField(targetCls, field);
if (item == null) {
return null;
}
try {
if (item.field != null && Modifier.isPublic(item.field.getModifiers())) {
return item.field.get(target);
} else if (item.getMethod != null && Modifier.isPublic(item.getMethod.getModifiers())) {
return item.getMethod.invoke(target);
}
} catch (Exception ex) {
ExceptionHelper.handleException(ObjectHelper.class, ex, field);
}
return null;
}
/**
* 设置对象中某个字段的值,根据不同类型设置不同值
*
* @param target 需要设置的对象
* @param field 需要设置的字段
* @param value 需要设置的值
*/
public static void set(Object target, String field, Object value) {
if (target == null) {
return;
}
Class vType = target.getClass();
if (target instanceof Number || vType.isEnum()) {
return;
} else if (target instanceof Map) {
((Map) target).put(field, value);
} else {
try {
setByType(vType, target, field, value);
} catch (Exception ex) {
throw new CodeException(ex);
}
}
}
/**
* 设置对象的值
*
* @param targetCls 要赋值的类型
* @param target 要赋值的对象
* @param field 要赋值的方法
* @param value 值
*/
public static void setByType(Class<?> targetCls, Object target, String field, Object value) {
MethodField item = getMethodField(targetCls, field);
if (item == null) {
return;
}
try {
if (item.field != null && Modifier.isPublic(item.field.getModifiers())) {
Class toType = item.field.getType();
Object toValue = StringHelper.to(toType, value);
item.field.set(target, toValue);
} else if (item.setMethod != null && Modifier.isPublic(item.setMethod.getModifiers())) {
Class toType = item.setMethod.getParameterTypes()[0];
Object toValue = StringHelper.to(toType, value);
item.setMethod.invoke(target, toValue);
}
} catch (Exception ex) {
ExceptionHelper.handleException(ObjectHelper.class, ex, field);
}
}
// --------------------------------------------- 对象转换、复制、字段复制 ---------------------------------------------------------
/**
* 将对象复制成一个新的对象
*
* @param from 需要复制的对象
* @param <T> 复制的对象的类型
* @return 新的对象
*/
public static <T extends Object> T clone(T from) {
if (from == null) {
return from;
}
Class cls = from.getClass();
try {
Object to = cls.newInstance();
writeWithFromClass(to, from);
return (T) to;
} catch (Exception e) {
throw new CodeException("对象" + cls.getName() + "不能复制", e);
}
}
/***
* 获取缓存中的字段
* @param typeCache
* @param fromName
* @return
*/
private static MethodField getField(HashMap<String, MethodField> typeCache, String fromName) {
String toName = fromName.toLowerCase().replace("_", "");
if (!typeCache.containsKey(toName)) {
MethodField newObj = new MethodField();
typeCache.put(toName, newObj);
newObj.name = fromName;
newObj.nameSimple = toName;
}
return typeCache.get(toName);
}
/**
* 获取实体的字段
*
* @param cls 需要获取的类型
* @return 获取字段之间的对应关系
*/
private static HashMap<String, MethodField> getInitTypeField(Class<?> cls) {
HashMap<String, MethodField> typeCache = new LinkedHashMap<String, MethodField>();
List<Field> fields = new ArrayList<Field>();
List<Method> methods = new ArrayList<Method>();
Class<?> tempClass = cls;
//当父类为null的时候说明到达了最上层的父类(Object类).
while (tempClass != null) {
fields.addAll(Arrays.asList(tempClass.getDeclaredFields()));
methods.addAll(Arrays.asList(tempClass.getDeclaredMethods()));
tempClass = tempClass.getSuperclass(); //得到父类,然后赋给自己
}
// 忽略大小写、忽略下划线 _
for (Field field : fields) {
MethodField obj = getField(typeCache, field.getName());
if (Modifier.isStatic(field.getModifiers())) {
continue;
}
if (obj.field == null) {
obj.field = field;
}
}
for (Method method : methods) {
if (Modifier.isStatic(method.getModifiers())) {
continue;
}
String methodNameSource = method.getName();
String methodNameSimple = methodNameSource.toLowerCase();
if (methodNameSource.equals("getClass")) {
continue;
} else if (methodNameSimple.startsWith("set")) {
if (method.getParameterTypes().length != 1) {
continue;
}
String toName = methodNameSource.substring("set".length());
MethodField obj = getField(typeCache, toName);
if (obj.setMethod == null) {
obj.setMethod = method;
}
} else if (methodNameSimple.startsWith("get")) {
if (method.getReturnType() == null) {
continue;
}
String toName = methodNameSource.substring("get".length());
MethodField obj = getField(typeCache, toName);
if (obj.getMethod == null) {
obj.getMethod = method;
}
} else if (methodNameSimple.startsWith("is")) {
if (method.getReturnType() == null) {
continue;
}
String toName = methodNameSource.substring("is".length());
MethodField obj = getField(typeCache, toName);
if (obj.getMethod == null) {
obj.getMethod = method;
}
}
}
return typeCache;
}
/**
* 获取一个表中的字段
*
* @param type
* @return
*/
public static HashMap<String, MethodField> getTypeField(Class<?> type) {
HashMap<String, MethodField> typeCache;
if (mapCache.containsKey(type)) {
typeCache = mapCache.get(type);
} else {
typeCache = getInitTypeField(type);
mapCache.put(type, typeCache);
}
return typeCache;
}
/**
* 获取字段的名称
*
* @param type
* @param name
* @return
*/
private static MethodField getMethodField(Class<?> type, String name) {
HashMap<String, MethodField> typeCache = getTypeField(type);
name = name.toLowerCase().replace("_", "");
return typeCache.containsKey(name) ? typeCache.get(name) : null;
}
/**
* 将集合的类容转换为对象
*
* @param vToClass 转换后的对象
* @param vTo 转换后的对象的集合
* @param vFrom 要转换的对象
* @param vField 关键词
* @throws Exception 需要跑出的异常
*/
public static <T> void addList(Class<T> vToClass, ArrayList<T> vTo, Object vFrom, String vField) throws InstantiationException, IllegalAccessException {
List vFroms = ObjectHelper.get(List.class, vFrom, vField);
if (vFroms == null || vTo == null) {
return;
}
for (Object vFromItem : vFroms) {
T vToItem = ObjectHelper.convert(vToClass, vFromItem);
vTo.add(vToItem);
}
}
/**
* 将来源类型的对象,转换为目标类型的对象
*
* @param toCls 需要转换后的目标类型
* @param from 来源类型
* @param <T> 目标类型
* @return 转换后的值
* @throws IllegalAccessException 异常信息
* @throws InstantiationException 异常信息
*/
public static <T> T convert(Class<T> toCls, Object from) throws IllegalAccessException, InstantiationException {
T to = toCls.newInstance();
writeWithToClass(to, from);
return to;
}
/**
* 对象转换(一般将map转为javabean)
*
* @param to 转换后的对象
* @param from 要转换的对象
* @return 转换后的值 to
*/
public static Object writeWithToClass(Object to, Object from) {
if (to == null) {
return null;
}
HashMap<String, MethodField> mapField = getInitTypeField(to.getClass());
for (Map.Entry<String, MethodField> field : mapField.entrySet()) {
String name = field.getValue().name;
Object fromValue = ObjectHelper.get(from, name);
ObjectHelper.set(to, name, fromValue);
}
return to;
}
/**
* 根据来源数据,往目标中写入数据
*
* @param to 目标对象
* @param froms 来源对象
*/
public static void writeWithFrom(Object to, Object... froms) {
for (Object model : froms) {
if (model instanceof Map) {
Map map = (Map) model;
for (Object key : map.keySet()) {
ObjectHelper.set(to, StringHelper.toString(key), map.get(key));
}
} else {
ObjectHelper.writeWithFromClass(to, model);
}
}
}
/**
* 根据来源类型的字段,将数据写入目标类型的数据
*
* @param to 目标类型的数据
* @param from 来源类型的数据
* @return 写入之后的值
*/
public static Object writeWithFromClass(Object to, Object from) {
if (from == null) {
return to;
}
HashMap<String, MethodField> mapField = getInitTypeField(from.getClass());
for (Map.Entry<String, MethodField> field : mapField.entrySet()) {
String name = field.getValue().name;
Object fromValue = ObjectHelper.get(from, name);
ObjectHelper.set(to, name, fromValue);
}
return to;
}
/**
* 将对象转换为指定列表
*
* @param cls 需要转换后的结果类型
* @param froms 需要转换后的值
* @param <T> 转换的类型
* @return 转换后的结果
*/
public static <T> ArrayList<T> getList(Class<T> cls, Object froms) {
ArrayList<T> tos = new ArrayList<T>();
if (froms instanceof List) {
List vCodeFrom = (List) ((froms instanceof List) ? froms : null);
for (Object from : vCodeFrom) {
if (StringHelper.isEmpty(from)) {
continue;
}
T to = StringHelper.to(cls, from);
tos.add(to);
}
} else {
if (!StringHelper.isEmpty(froms)) {
T to = StringHelper.to(cls, froms);
tos.add(to);
}
}
return tos;
}
/**
* 将对象转换为数组
*
* @param froms 需要转换的数组
* @return 数组类型
*/
public static <T extends Object> T[] getArray(T... froms) {
return froms;
}
/**
* 动态创建数组
*
* @param cls 数组类型
* @param length 长度
* @param <T> 泛型
* @return 数组长度
*/
public static <T extends Object> T[] createArray(Class<?> cls, int length) {
Object array = Array.newInstance(cls, length);
return (T[]) array;
}
}
......@@ -7,7 +7,7 @@ import com.yanzuoguang.util.exception.ExceptionHelper;
import com.yanzuoguang.util.helper.JSONHelper;
import com.yanzuoguang.util.helper.StringHelper;
import com.yanzuoguang.util.log.Log;
import com.yanzuoguang.util.obj.ObjectHelper;
import com.yanzuoguang.util.base.ObjectHelper;
import com.yanzuoguang.util.thread.ThreadNext;
import com.yanzuoguang.util.vo.LogVo;
import com.yanzuoguang.util.vo.ResponseResult;
......
......@@ -4,7 +4,7 @@ import com.yanzuoguang.dao.BaseDao;
import com.yanzuoguang.dao.DaoConst;
import com.yanzuoguang.util.exception.CodeException;
import com.yanzuoguang.util.helper.StringHelper;
import com.yanzuoguang.util.obj.ObjectHelper;
import com.yanzuoguang.util.base.ObjectHelper;
import com.yanzuoguang.util.vo.InitDao;
import java.util.HashMap;
......
......@@ -5,7 +5,7 @@ import com.yanzuoguang.db.DbExecute;
import com.yanzuoguang.util.cache.MemoryCache;
import com.yanzuoguang.util.exception.CodeException;
import com.yanzuoguang.util.helper.StringHelper;
import com.yanzuoguang.util.obj.ObjectHelper;
import com.yanzuoguang.util.base.ObjectHelper;
import com.yanzuoguang.util.vo.PageSizeData;
import com.yanzuoguang.util.vo.PageSizeReq;
import com.yanzuoguang.util.vo.PageSizeReqVo;
......
......@@ -3,8 +3,8 @@ package com.yanzuoguang.dao.Impl;
import com.yanzuoguang.dao.DaoConst;
import com.yanzuoguang.dao.TableAnnotation;
import com.yanzuoguang.util.helper.StringHelper;
import com.yanzuoguang.util.obj.MethodField;
import com.yanzuoguang.util.obj.ObjectHelper;
import com.yanzuoguang.util.base.MethodField;
import com.yanzuoguang.util.base.ObjectHelper;
import java.util.ArrayList;
import java.util.HashMap;
......
......@@ -2,7 +2,7 @@ package com.yanzuoguang.db.Impl;
import com.yanzuoguang.extend.ConfigDb;
import com.yanzuoguang.util.log.Log;
import com.yanzuoguang.util.obj.ObjectHelper;
import com.yanzuoguang.util.base.ObjectHelper;
import com.yanzuoguang.util.vo.MapRow;
import org.springframework.beans.*;
import org.springframework.dao.DataRetrievalFailureException;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment