/**
* 设置缓存
*
* @param clazz 带设置的Class
*/
private static void setPropertyCache(Class<?> clazz) {
String mapperClassName = clazz.getName();
// 获取映射类的字段
List fields = getClassFields(clazz);
for (Field field : fields) {
Map<String, PropertyMapper> propertyMapperMap = Maps.newHashMap();
if (field.isAnnotationPresent(PropertyMapper.class)) {
PropertyMapper propertyMapper = field.getAnnotation(PropertyMapper.class);
String key;
if (!StringUtils.isEmpty(propertyMapper.sourceReference())) {
key = propertyMapper.sourceReference();
} else {
key = propertyMapper.sourceClass().getName();
}
propertyMapperMap.put(key, propertyMapper);
}
if (field.isAnnotationPresent(PropertyMappers.class)) {
PropertyMappers propertyMappers = field.getAnnotation(PropertyMappers.class);
PropertyMapper[] value = propertyMappers.value();
for (PropertyMapper propertyMapper : value) {
String key;
if (!StringUtils.isEmpty(propertyMapper.sourceReference())) {
key = propertyMapper.sourceReference();
} else {
key = propertyMapper.sourceClass().getName();
}
if (!propertyMapperMap.containsKey(key)) {
propertyMapperMap.put(key, propertyMapper);
}
}
}
for (Map.Entry<String, PropertyMapper> mapperEntry : propertyMapperMap.entrySet()) {
PropertyMapper propertyMapper = mapperEntry.getValue();
Class<?> targetClass;
String targetClassName;
if (!StringUtils.isEmpty(propertyMapper.sourceReference())) {
targetClassName = propertyMapper.sourceReference();
try {
targetClass = Class.forName(targetClassName);
} catch (ClassNotFoundException e) {
continue;
}
} else {
targetClass = propertyMapper.sourceClass();
targetClassName = targetClass.getName();
}
String targetProperty = propertyMapper.property();
Map<String, String> mapperMap = getMap(targetClass, clazz, fields.size());
mapperMap.put(mapperClassName + CHARACTER_VERTICAL_LINE + field.getName(),
targetProperty);
Map<String, String> targetMap = getMap(clazz, targetClass, fields.size());
targetMap.put(targetClassName + CHARACTER_VERTICAL_LINE + targetProperty,
field.getName());
}
}
}
/**
* 获取缓存
*
* @param sourceClass 源
* @param targetClass 目标
* @return java.util.Map
*/
private static Map<String, String> getPropertyCache(Class<?> sourceClass, Class<?> targetClass) {
String classKey = sourceClass.getName() + CHARACTER_VERTICAL_LINE + targetClass.getName();
if (propertyCache.containsKey(classKey)) {
return propertyCache.get(classKey);
}
setPropertyCache(sourceClass);
setPropertyCache(targetClass);
return propertyCache.get(classKey);
}