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

merge/1/MERGE
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 20161024 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());