com.kaka.numerical.Parser Maven / Gradle / Ivy
package com.kaka.numerical;
import com.kaka.numerical.annotation.NumericField;
import com.kaka.numerical.annotation.NumericField.Converter;
import com.kaka.util.ArrayUtils;
import com.kaka.util.ReflectUtils;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
import static com.kaka.util.ReflectUtils.getFieldValue;
import static com.kaka.util.ReflectUtils.setFieldValue;
/**
* 配置文件解析器
*
* @author zkpursuit
*/
abstract public class Parser {
/**
* 日志记录
*/
private static final Logger logger = Logger.getLogger(Parser.class.getTypeName());
/**
* 为对象的字段赋值
*
* @param 对象限定类型
* @param object 对象
* @param field 对象字段
* @param analyzer 赋值分析器
* @throws Exception 解析异常
*/
protected void doParse(T object, Field field, IAnalyzer analyzer) throws Exception {
NumericField att = field.getAnnotation(NumericField.class);
if (att == null) {
String value = analyzer.getContent(field.getName());
try {
setFieldValue(object, field, value);
} catch (Exception ex) {
logger.log(Level.SEVERE, ex.getMessage(), ex);
}
return;
}
String[] eles = att.elements();
Class extends Converter> converterClass = att.converter();
Converter> converter = converterClass == Converter.class ? null : ReflectUtils.newInstance(converterClass);
boolean isCollectionField = false;
Class> filedTypeClass = field.getType();
Object fieldValue = getFieldValue(object, field);
if (Collection.class.isAssignableFrom(filedTypeClass)) {
isCollectionField = true;
if (fieldValue == null) {
if (filedTypeClass.isInterface() || Modifier.isAbstract(filedTypeClass.getModifiers())) {
if (java.util.SortedSet.class.isAssignableFrom(filedTypeClass)) {
fieldValue = new java.util.TreeSet<>();
} else if (java.util.LinkedHashSet.class.isAssignableFrom(filedTypeClass)) {
fieldValue = new java.util.LinkedHashSet<>();
} else if (java.util.Set.class.isAssignableFrom(filedTypeClass)) {
fieldValue = new java.util.HashSet<>();
} else if (java.util.Stack.class.isAssignableFrom(filedTypeClass)) {
fieldValue = new java.util.Stack<>();
} else if (java.util.LinkedList.class.isAssignableFrom(filedTypeClass)) {
fieldValue = new java.util.LinkedList<>();
} else if (java.util.Queue.class.isAssignableFrom(filedTypeClass)) {
fieldValue = new java.util.LinkedList<>();
} else {
fieldValue = new java.util.ArrayList<>();
}
} else {
Constructor>[] constructors = filedTypeClass.getConstructors();
for (Constructor> constructor : constructors) {
int modifier = constructor.getModifiers();
if (!Modifier.isPublic(modifier)) {
continue;
}
int paramCount = constructor.getParameterCount();
if (paramCount > 0) {
continue;
}
fieldValue = constructor.newInstance();
break;
}
}
setFieldValue(object, field, fieldValue);
}
}
for (String confColName : eles) {
confColName = confColName.trim().replaceAll(" ", "");
String value = analyzer.getContent(confColName);
if (isCollectionField) {
Object resultValue = converter != null ? converter.transform(value) : null;
if (resultValue != null && fieldValue != null) {
Collection