mirror of
https://e.coding.net/circlecloud/YumCore.git
synced 2024-11-22 01:48:50 +00:00
feat: 添加配置忽略保存功能
This commit is contained in:
parent
b481b7ee9e
commit
17c75d923e
19
src/main/java/pw/yumc/YumCore/config/annotation/NotSave.java
Normal file
19
src/main/java/pw/yumc/YumCore/config/annotation/NotSave.java
Normal 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 {
|
||||||
|
}
|
@ -14,6 +14,7 @@ import pw.yumc.YumCore.bukkit.Log;
|
|||||||
import pw.yumc.YumCore.commands.exception.CommandParseException;
|
import pw.yumc.YumCore.commands.exception.CommandParseException;
|
||||||
import pw.yumc.YumCore.config.annotation.ConfigNode;
|
import pw.yumc.YumCore.config.annotation.ConfigNode;
|
||||||
import pw.yumc.YumCore.config.annotation.Default;
|
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.annotation.Nullable;
|
||||||
import pw.yumc.YumCore.config.exception.ConfigParseException;
|
import pw.yumc.YumCore.config.exception.ConfigParseException;
|
||||||
|
|
||||||
@ -29,59 +30,6 @@ public abstract class AbstractInjectConfig {
|
|||||||
private static String PATH_NOT_FOUND = "配置节点 %s 丢失 将使用默认值!";
|
private static String PATH_NOT_FOUND = "配置节点 %s 丢失 将使用默认值!";
|
||||||
private ConfigurationSection config;
|
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 {
|
private Object hanldeDefault(Class<?> field, String path, Object value) throws IllegalAccessException, IllegalArgumentException, InstantiationException, InvocationTargetException, NoSuchMethodException, SecurityException {
|
||||||
if (InjectConfigurationSection.class.isAssignableFrom(field)) {
|
if (InjectConfigurationSection.class.isAssignableFrom(field)) {
|
||||||
if (config.isConfigurationSection(path)) {
|
if (config.isConfigurationSection(path)) { return field
|
||||||
return field.getConstructor(ConfigurationSection.class).newInstance(config.getConfigurationSection(path));
|
.getConstructor(ConfigurationSection.class)
|
||||||
}
|
.newInstance(config.getConfigurationSection(path)); }
|
||||||
Log.w(INJECT_TYPE_ERROR, path, ConfigurationSection.class.getName(), value.getClass().getName());
|
Log.w(INJECT_TYPE_ERROR, path, ConfigurationSection.class.getName(), value.getClass().getName());
|
||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
@ -181,6 +129,59 @@ public abstract class AbstractInjectConfig {
|
|||||||
Log.d("设置字段 %s 为 %s ", field.getName(), value);
|
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) {
|
protected void setConfig(String path, Field field) {
|
||||||
try {
|
try {
|
||||||
config.set(path, field.get(this));
|
if (field.getAnnotation(NotSave.class) == null) {
|
||||||
|
config.set(path, field.get(this));
|
||||||
|
}
|
||||||
} catch (IllegalArgumentException | IllegalAccessException e) {
|
} catch (IllegalArgumentException | IllegalAccessException e) {
|
||||||
Log.w(INJECT_ERROR, e.getClass().getName(), e.getMessage());
|
Log.w(INJECT_ERROR, e.getClass().getName(), e.getMessage());
|
||||||
Log.debug(e);
|
Log.debug(e);
|
||||||
@ -222,7 +225,10 @@ public abstract class AbstractInjectConfig {
|
|||||||
}
|
}
|
||||||
hanldeValue(path, field, value);
|
hanldeValue(path, field, value);
|
||||||
} catch (IllegalArgumentException ex) {
|
} 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);
|
Log.debug(ex);
|
||||||
} catch (ConfigParseException e) {
|
} catch (ConfigParseException e) {
|
||||||
Log.w(e.getMessage());
|
Log.w(e.getMessage());
|
||||||
|
Loading…
Reference in New Issue
Block a user