com.fhs.trans.manager.ClassInfo Maven / Gradle / Ivy
package com.fhs.trans.manager;
import com.fhs.core.trans.anno.Trans;
import com.fhs.core.trans.anno.UnTrans;
import com.fhs.core.trans.util.ReflectUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.*;
/**
* 类缓存
*
* @author user
* @date 2020-05-19 11:14:15
*/
public class ClassInfo implements Serializable {
private static final long serialVersionUID = 1L;
public static final Logger LOGGER = LoggerFactory.getLogger(ClassInfo.class);
private Class> clazz;
private Field idField;
private String[] transTypes;
private String[] unTransTypes;
private Map fieldMap = new HashMap();
/**
* 获取翻译字段 key 翻译的类型比如字典的类型为dict
*/
private Map> transFieldMap = new HashMap>();
private Map> unTransFieldMap = new HashMap>();
public ClassInfo() {
super();
}
public ClassInfo(Class> clazz) throws InstantiationException, IllegalAccessException {
super();
this.clazz = clazz;
getClazzFieldMap();
}
public Class> getClazz() {
return clazz;
}
public void setClazz(Class> clazz) {
this.clazz = clazz;
}
public Field getIdField() {
return idField;
}
public void setIdField(Field idField) {
this.idField = idField;
}
public Map getFieldMap() {
return fieldMap;
}
public void setFieldMap(Map fieldMap) {
this.fieldMap = fieldMap;
}
/**
* 获取需要翻译的类型
*
* @return 需要翻译的类型
*/
public String[] getTransTypes() {
return transTypes;
}
/**
* 获取需要翻译的字段
*
* @param type 翻译类型
* @return 字段集合
*/
public List getTransField(String type) {
return new ArrayList<>(transFieldMap.get(type));
}
private void getClazzFieldMap() throws InstantiationException, IllegalAccessException {
// PO 类和其祖先类声明的字段名称集合
List declaredFields = ReflectUtils.getAllField(clazz.newInstance());
Set transTypeSet = new HashSet<>();
Set unTransTypeSet = new HashSet<>();
int mod = 0;
// 循环遍历所有的属性进行判断
for (Field field : declaredFields) {
mod = field.getModifiers();
// 如果是 static, final, volatile, transient 的字段,则直接跳过
if (Modifier.isStatic(mod) || Modifier.isFinal(mod) || Modifier.isVolatile(mod)) {
continue;
}
Trans trans = field.getAnnotation(Trans.class);
if (trans != null) {
if (trans.type() == null) {
LOGGER.warn("类 {} 属性 [{}] type为空。", clazz.getName(), field.getName());
} else {
transTypeSet.add(trans.type());
List fieldList = transFieldMap.get(trans.type());
fieldList = fieldList != null ? fieldList : new ArrayList();
fieldList.add(field);
transFieldMap.put(trans.type(), fieldList);
}
}
UnTrans untrans = field.getAnnotation(UnTrans.class);
if (untrans != null) {
if (untrans.type() == null) {
LOGGER.warn("类 {} 属性 [{}] type为空。", clazz.getName(), field.getName());
} else {
unTransTypeSet.add(untrans.type());
List fieldList = unTransFieldMap.get(untrans.type());
fieldList = fieldList != null ? fieldList : new ArrayList();
fieldList.add(field);
unTransFieldMap.put(untrans.type(), fieldList);
}
}
}
this.transTypes = new String[transTypeSet.size()];
this.unTransTypes = new String[unTransTypeSet.size()];
transTypeSet.toArray(transTypes);
unTransTypeSet.toArray(unTransTypes);
}
public Map> getTransFieldMap() {
return transFieldMap;
}
public void setTransFieldMap(Map> transFieldMap) {
this.transFieldMap = transFieldMap;
}
public void setTransTypes(String[] transTypes) {
this.transTypes = transTypes;
}
public String[] getUnTransTypes() {
return unTransTypes;
}
public void setUnTransTypes(String[] unTransTypes) {
this.unTransTypes = unTransTypes;
}
public Map> getUnTransFieldMap() {
return unTransFieldMap;
}
public void setUnTransFieldMap(Map> unTransFieldMap) {
this.unTransFieldMap = unTransFieldMap;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy