From 17c75d923e67df85a86df4a3693f1a3d7a5acaa2 Mon Sep 17 00:00:00 2001 From: 502647092 Date: Mon, 24 Oct 2016 00:33:42 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E5=BF=BD=E7=95=A5=E4=BF=9D=E5=AD=98=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../YumCore/config/annotation/NotSave.java | 19 +++ .../config/inject/AbstractInjectConfig.java | 122 +++++++++--------- 2 files changed, 83 insertions(+), 58 deletions(-) create mode 100644 src/main/java/pw/yumc/YumCore/config/annotation/NotSave.java diff --git a/src/main/java/pw/yumc/YumCore/config/annotation/NotSave.java b/src/main/java/pw/yumc/YumCore/config/annotation/NotSave.java new file mode 100644 index 0000000..343410e --- /dev/null +++ b/src/main/java/pw/yumc/YumCore/config/annotation/NotSave.java @@ -0,0 +1,19 @@ +package pw.yumc.YumCore.config.annotation; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * 忽略配置保存 + * + * @author 喵♂呜 + * @since 2016年10月24日 上午00:11 + */ +@Target(ElementType.FIELD) +@Documented +@Retention(RetentionPolicy.RUNTIME) +public @interface NotSave { +} 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 b021003..075b0b8 100644 --- a/src/main/java/pw/yumc/YumCore/config/inject/AbstractInjectConfig.java +++ b/src/main/java/pw/yumc/YumCore/config/inject/AbstractInjectConfig.java @@ -14,6 +14,7 @@ import pw.yumc.YumCore.bukkit.Log; import pw.yumc.YumCore.commands.exception.CommandParseException; import pw.yumc.YumCore.config.annotation.ConfigNode; import pw.yumc.YumCore.config.annotation.Default; +import pw.yumc.YumCore.config.annotation.NotSave; import pw.yumc.YumCore.config.annotation.Nullable; import pw.yumc.YumCore.config.exception.ConfigParseException; @@ -29,59 +30,6 @@ public abstract class AbstractInjectConfig { private static String PATH_NOT_FOUND = "配置节点 %s 丢失 将使用默认值!"; private ConfigurationSection config; - /** - * 注入配置数据 - * - * @param config - * 配置区 - */ - public void inject(ConfigurationSection config) { - inject(config, false); - } - - /** - * 注入配置数据 - * - * @param config - * 配置区 - * @param save - * 是否为保存 - */ - public void inject(ConfigurationSection config, boolean save) { - if (config == null) { - Log.w("尝试%s ConfigurationSection 为 Null 的数据!", save ? "保存" : "读取"); - return; - } - this.config = config; - for (Field field : getClass().getDeclaredFields()) { - if (Modifier.isTransient(field.getModifiers()) || field.getType().isPrimitive()) { - continue; - } - ConfigNode node = field.getAnnotation(ConfigNode.class); - String path = field.getName(); - if (node != null && !node.value().isEmpty()) { - path = node.value(); - } - field.setAccessible(true); - if (save) { - setConfig(path, field); - } else { - setField(path, field); - } - } - } - - /** - * 自动化保存 - * - * @param config - * 配置文件区 - */ - public ConfigurationSection save(ConfigurationSection config) { - inject(config, true); - return config; - } - /** * 添加默认值 * @@ -144,9 +92,9 @@ 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; @@ -181,6 +129,59 @@ public abstract class AbstractInjectConfig { Log.d("设置字段 %s 为 %s ", field.getName(), value); } + /** + * 注入配置数据 + * + * @param config + * 配置区 + */ + public void inject(ConfigurationSection config) { + inject(config, false); + } + + /** + * 注入配置数据 + * + * @param config + * 配置区 + * @param save + * 是否为保存 + */ + public void inject(ConfigurationSection config, boolean save) { + if (config == null) { + Log.w("尝试%s ConfigurationSection 为 Null 的数据!", save ? "保存" : "读取"); + return; + } + this.config = config; + for (Field field : getClass().getDeclaredFields()) { + if (Modifier.isTransient(field.getModifiers()) || field.getType().isPrimitive()) { + continue; + } + ConfigNode node = field.getAnnotation(ConfigNode.class); + String path = field.getName(); + if (node != null && !node.value().isEmpty()) { + path = node.value(); + } + field.setAccessible(true); + if (save) { + setConfig(path, field); + } else { + setField(path, field); + } + } + } + + /** + * 自动化保存 + * + * @param config + * 配置文件区 + */ + public ConfigurationSection save(ConfigurationSection config) { + inject(config, true); + return config; + } + /** * 通用保存流程 * @@ -191,7 +192,9 @@ public abstract class AbstractInjectConfig { */ protected void setConfig(String path, Field field) { try { - config.set(path, field.get(this)); + if (field.getAnnotation(NotSave.class) == null) { + config.set(path, field.get(this)); + } } catch (IllegalArgumentException | IllegalAccessException e) { Log.w(INJECT_ERROR, e.getClass().getName(), e.getMessage()); Log.debug(e); @@ -222,7 +225,10 @@ public abstract class AbstractInjectConfig { } 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());