From b4d7f14d494a696594c92c92763110dfaee6d085 Mon Sep 17 00:00:00 2001 From: 502647092 Date: Fri, 28 Oct 2016 13:26:28 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=AD=98=E5=9C=A8=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 502647092 --- .../pw/yumc/YumCore/config/FileConfig.java | 7 ++++--- .../config/inject/AbstractInjectConfig.java | 20 ++++++++----------- .../YumCore/config/inject/InjectParse.java | 4 ++-- 3 files changed, 14 insertions(+), 17 deletions(-) diff --git a/src/main/java/pw/yumc/YumCore/config/FileConfig.java b/src/main/java/pw/yumc/YumCore/config/FileConfig.java index 93740dd..3eed865 100644 --- a/src/main/java/pw/yumc/YumCore/config/FileConfig.java +++ b/src/main/java/pw/yumc/YumCore/config/FileConfig.java @@ -560,9 +560,9 @@ public class FileConfig extends AbstractConfig { /** * 更新配置文件 * - * @param newcfg + * @param newCfg * 新配置文件 - * @param oldcfg + * @param oldCfg * 旧配置文件 * @return 更新以后的配置文件 */ @@ -599,7 +599,8 @@ public class FileConfig extends AbstractConfig { Object var = oldCfg.get(string); // 需要进行节点检查 还有类型检查 不同类型情况下 使用新配置 if (var != null && !(var instanceof MemorySection)) { - if (!newCfg.get(string).getClass().equals(var.getClass())) { + Object newVer = newCfg.get(string); + if (newVer != null && !newVer.getClass().equals(var.getClass())) { Log.w("警告! 旧数据类型与新配置类型不匹配!"); } Log.debug(String.format(CONFIG_UPDATE_VALUE, string, var)); 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 7cd76e1..92c68bf 100644 --- a/src/main/java/pw/yumc/YumCore/config/inject/AbstractInjectConfig.java +++ b/src/main/java/pw/yumc/YumCore/config/inject/AbstractInjectConfig.java @@ -35,17 +35,18 @@ public abstract class AbstractInjectConfig { * * @param field * 字段 - * @param value - * 值 * @throws IllegalAccessException * @throws IllegalArgumentException */ - private void applyDefault(Field field, Object value) throws IllegalArgumentException, IllegalAccessException { + private void applyDefault(Field field) throws IllegalArgumentException, IllegalAccessException { + Object value = null; switch (field.getType().getName()) { case "java.util.List": value = new ArrayList<>(); + break; case "java.util.Map": value = new HashMap<>(); + break; } field.set(this, value); } @@ -92,9 +93,7 @@ public abstract class AbstractInjectConfig { */ private Object hanldeDefault(Class field, String path, Object value) throws IllegalAccessException, IllegalArgumentException, InstantiationException, InvocationTargetException, NoSuchMethodException, SecurityException { if (InjectConfigurationSection.class.isAssignableFrom(field)) { - if (config.isConfigurationSection(path)) { return field - .getConstructor(ConfigurationSection.class) - .newInstance(config.getConfigurationSection(path)); } + if (config.isConfigurationSection(path)) { return field.getConstructor(ConfigurationSection.class).newInstance(config.getConfigurationSection(path)); } Log.w(INJECT_TYPE_ERROR, path, ConfigurationSection.class.getName(), value.getClass().getName()); } return value; @@ -123,7 +122,7 @@ public abstract class AbstractInjectConfig { value = convertType(type, path, value); } if (type.equals(String.class)) { - value = ChatColor.translateAlternateColorCodes('&', (String) value); + value = ChatColor.translateAlternateColorCodes('&', String.valueOf(value)); } field.set(this, value); Log.d("设置字段 %s 为 %s ", field.getName(), value); @@ -219,16 +218,13 @@ public abstract class AbstractInjectConfig { if (value == null) { if (field.getAnnotation(Nullable.class) == null) { Log.w(PATH_NOT_FOUND, path); - applyDefault(field, value); + applyDefault(field); } return; } hanldeValue(path, field, value); } catch (IllegalArgumentException ex) { - Log.w(INJECT_TYPE_ERROR, - path, - field.getType().getName(), - value != null ? value.getClass().getName() : "空指针"); + Log.w(INJECT_TYPE_ERROR, path, field.getType().getName(), value != null ? value.getClass().getName() : "空指针"); Log.debug(ex); } catch (ConfigParseException e) { Log.w(e.getMessage()); 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 d0a8964..97985cc 100644 --- a/src/main/java/pw/yumc/YumCore/config/inject/InjectParse.java +++ b/src/main/java/pw/yumc/YumCore/config/inject/InjectParse.java @@ -84,7 +84,7 @@ public class InjectParse { } } - public static interface Parse { - public FC parse(ConfigurationSection config, String path); + public interface Parse { + FC parse(ConfigurationSection config, String path); } }