1
0
mirror of https://e.coding.net/circlecloud/YumCore.git synced 2024-12-25 07:08:52 +00:00

feat: 添加配置忽略保存功能

This commit is contained in:
502647092 2016-10-24 00:33:42 +08:00
parent b481b7ee9e
commit 17c75d923e
2 changed files with 83 additions and 58 deletions

View File

@ -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 {
}

View File

@ -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());