com.kaka.numerical.Parser Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kaka-core Show documentation
Show all versions of kaka-core Show documentation
An event driven framework
The newest version!
package com.kaka.numerical;
import com.kaka.numerical.NumericField.BiConverter;
import com.kaka.numerical.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.Map;
import static com.kaka.util.ReflectUtils.getFieldValue;
import static com.kaka.util.ReflectUtils.setFieldValue;
/**
* 配置文件解析器
*
* @author zkpursuit
*/
abstract public class Parser {
/**
* 为对象的字段赋值
*
* @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) {
throw new IllegalArgumentException(ex);
}
return;
}
String[] elements = att.elements();
Class extends FieldConverter> converterClass = att.converter();
FieldConverter converter = converterClass == FieldConverter.class ? null : ReflectUtils.newInstance(converterClass);
if (converter == null) {
String value = analyzer.getContent(elements[0].trim().replaceAll(" ", ""));
setFieldValue(object, field, value);
} else if (converter instanceof BiConverter> biConverter) {
Object fieldValue = getFieldValue(object, field);
Class> fieldClass = field.getType();
for (int i = 0; i < elements.length; i++) {
String element = elements[i].trim();
String value = analyzer.getContent(element);
Object resultValue = biConverter.transform(element, value, i, elements.length, object, field);
if (fieldValue == null && resultValue != null && (resultValue.getClass() == fieldClass || fieldClass.isAssignableFrom(resultValue.getClass()))) {
setFieldValue(object, field, resultValue);
fieldValue = resultValue;
}
}
} else {
Converter> siConverter = (Converter>) converter;
int fieldType = 0; //字段类型,1为列表,2为map
Class> filedTypeClass = field.getType();
Object fieldValue = getFieldValue(object, field);
if (Collection.class.isAssignableFrom(filedTypeClass)) {
fieldType = 1;
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);
}
} else if (Map.class.isAssignableFrom(filedTypeClass)) {
fieldType = 2;
}
for (String element : elements) {
element = element.trim();
String value = analyzer.getContent(element);
if (fieldType == 1) {
Object resultValue = siConverter.transform(value);
if (resultValue != null && fieldValue != null) {
Collection