From 1aa10139a5f7a12075d5b530d355afade644b577 Mon Sep 17 00:00:00 2001 From: 502647092 Date: Tue, 4 Oct 2016 11:03:22 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E5=80=BC=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 502647092 --- .../YumCore/config/AbstractInjectConfig.java | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/main/java/pw/yumc/YumCore/config/AbstractInjectConfig.java b/src/main/java/pw/yumc/YumCore/config/AbstractInjectConfig.java index 2e42479..6f7eabe 100644 --- a/src/main/java/pw/yumc/YumCore/config/AbstractInjectConfig.java +++ b/src/main/java/pw/yumc/YumCore/config/AbstractInjectConfig.java @@ -5,6 +5,8 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Modifier; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.HashMap; import org.bukkit.ChatColor; import org.bukkit.configuration.ConfigurationSection; @@ -79,6 +81,26 @@ public abstract class AbstractInjectConfig { return config; } + /** + * 添加默认值 + * + * @param field + * 字段 + * @param value + * 值 + * @throws IllegalAccessException + * @throws IllegalArgumentException + */ + private void applyDefault(final Field field, Object value) throws IllegalArgumentException, IllegalAccessException { + switch (field.getType().getName()) { + case "java.util.List": + value = new ArrayList<>(); + case "java.util.Map": + value = new HashMap<>(); + } + field.set(this, value); + } + /** * 转换字段值类型 * @@ -104,7 +126,7 @@ public abstract class AbstractInjectConfig { case "java.util.List": return config.getList(path); case "java.util.Map": - return config.getConfigurationSection(path).getValues(false); + return config.getConfigurationSection(path).getValues(true); default: return hanldeDefault(type, path, value); } @@ -155,6 +177,7 @@ public abstract class AbstractInjectConfig { * @throws IllegalAccessException */ private void hanldeValue(final String path, final Field field, Object value) throws IllegalAccessException, IllegalArgumentException, InstantiationException, InvocationTargetException, NoSuchMethodException, SecurityException, ParseException { + final Object origin = field.get(this); final Class type = field.getType(); if (!type.equals(value.getClass())) { value = convertType(type, path, value); @@ -163,7 +186,7 @@ public abstract class AbstractInjectConfig { value = ChatColor.translateAlternateColorCodes('&', (String) value); } field.set(this, value); - + Log.d("设置字段 %s 由 %s 为 %s ", field.getName(), origin, value); } /** @@ -201,6 +224,7 @@ public abstract class AbstractInjectConfig { if (value == null) { if (field.getAnnotation(Nullable.class) == null) { Log.w(PATH_NOT_FOUND, path); + applyDefault(field, value); } return; }