mirror of
https://e.coding.net/circlecloud/YumCore.git
synced 2024-11-22 01:48:50 +00:00
feat: 优化配置解析流程
Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
parent
1c85776b8a
commit
828bd1be88
@ -1,6 +1,5 @@
|
||||
package pw.yumc.YumCore.config.inject;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Modifier;
|
||||
@ -50,39 +49,6 @@ public abstract class AbstractInjectConfig {
|
||||
field.set(this, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换字段值类型
|
||||
*
|
||||
* @param type
|
||||
* 字段类型
|
||||
* @param path
|
||||
* 配置路径
|
||||
* @param value
|
||||
* 字段值
|
||||
* @return 转换后的值
|
||||
* @throws IllegalAccessException
|
||||
* @throws IllegalArgumentException
|
||||
* @throws InstantiationException
|
||||
* @throws InvocationTargetException
|
||||
* @throws NoSuchMethodException
|
||||
* @throws SecurityException
|
||||
*/
|
||||
private Object convertType(Class type, String path, Object value) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
|
||||
try {
|
||||
return type.getDeclaredMethod("valueOf", String.class).invoke(null, value);
|
||||
} catch (NoSuchMethodException | IllegalArgumentException ignored) {
|
||||
}
|
||||
if (InjectConfigurationSection.class.isAssignableFrom(type)) {
|
||||
if (config.isConfigurationSection(path)) {
|
||||
Constructor<?> constructor = type.getDeclaredConstructor(ConfigurationSection.class);
|
||||
constructor.setAccessible(true);
|
||||
return constructor.newInstance(config.getConfigurationSection(path));
|
||||
}
|
||||
Log.w(INJECT_TYPE_ERROR, path, ConfigurationSection.class.getName(), value.getClass().getName());
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理值
|
||||
*
|
||||
@ -103,7 +69,7 @@ public abstract class AbstractInjectConfig {
|
||||
private void hanldeValue(Field field, String path, Object value) throws IllegalAccessException, IllegalArgumentException, InstantiationException, InvocationTargetException, NoSuchMethodException, SecurityException, ConfigParseException {
|
||||
Class type = field.getType();
|
||||
if (!type.equals(value.getClass())) {
|
||||
value = convertType(type, path, value);
|
||||
value = InjectParse.parse(type, config, path);
|
||||
}
|
||||
if (type.equals(String.class)) {
|
||||
value = ChatColor.translateAlternateColorCodes('&', String.valueOf(value));
|
||||
@ -206,18 +172,19 @@ public abstract class AbstractInjectConfig {
|
||||
* 字段
|
||||
*/
|
||||
protected void setField(String path, Field field) {
|
||||
Object value = InjectParse.parse(field.getType(), config, path);
|
||||
Object value = null;
|
||||
try {
|
||||
if (!config.contains(path)) {
|
||||
Default def = field.getAnnotation(Default.class);
|
||||
if (value == null && def != null) {
|
||||
if (def != null) {
|
||||
value = def.value();
|
||||
}
|
||||
if (value == null) {
|
||||
if (field.getAnnotation(Nullable.class) == null) {
|
||||
} else if (field.getAnnotation(Nullable.class) == null) {
|
||||
Log.w(PATH_NOT_FOUND, path);
|
||||
applyDefault(field);
|
||||
}
|
||||
return;
|
||||
if (value == null) { return; }
|
||||
} else {
|
||||
value = config.get(path);
|
||||
}
|
||||
hanldeValue(field, path, value);
|
||||
} catch (IllegalArgumentException ex) {
|
||||
|
@ -1,5 +1,7 @@
|
||||
package pw.yumc.YumCore.config.inject;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.text.DateFormat;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
@ -31,8 +33,21 @@ public class InjectParse {
|
||||
new DateFormatParse();
|
||||
}
|
||||
|
||||
public static Object parse(Class clazz, ConfigurationSection config, String path) {
|
||||
return allparse.containsKey(clazz) ? allparse.get(clazz).parse(config, path) : config.get(path);
|
||||
public static Object parse(Class type, ConfigurationSection config, String path) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
|
||||
if (allparse.containsKey(type)) { return allparse.get(type).parse(config, path); }
|
||||
Object value = config.get(path);
|
||||
try {
|
||||
return type.getDeclaredMethod("valueOf", String.class).invoke(null, value);
|
||||
} catch (NoSuchMethodException | IllegalArgumentException ignored) {
|
||||
}
|
||||
if (InjectConfigurationSection.class.isAssignableFrom(type)) {
|
||||
if (config.isConfigurationSection(path)) {
|
||||
Constructor<?> constructor = type.getDeclaredConstructor(ConfigurationSection.class);
|
||||
constructor.setAccessible(true);
|
||||
return constructor.newInstance(config.getConfigurationSection(path));
|
||||
}
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user