diff --git a/src/main/java/pw/yumc/YumCore/config/inject/AbstractInjectConfig.java b/src/main/java/pw/yumc/YumCore/config/inject/AbstractInjectConfig.java index 812561d..8a7c263 100644 --- a/src/main/java/pw/yumc/YumCore/config/inject/AbstractInjectConfig.java +++ b/src/main/java/pw/yumc/YumCore/config/inject/AbstractInjectConfig.java @@ -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 { - Default def = field.getAnnotation(Default.class); - if (value == null && def != null) { - value = def.value(); - } - if (value == null) { - if (field.getAnnotation(Nullable.class) == null) { + if (!config.contains(path)) { + Default def = field.getAnnotation(Default.class); + if (def != null) { + value = def.value(); + } 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) { diff --git a/src/main/java/pw/yumc/YumCore/config/inject/InjectParse.java b/src/main/java/pw/yumc/YumCore/config/inject/InjectParse.java index 6855b81..b5653a7 100644 --- a/src/main/java/pw/yumc/YumCore/config/inject/InjectParse.java +++ b/src/main/java/pw/yumc/YumCore/config/inject/InjectParse.java @@ -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; } /**