1
0
mirror of https://e.coding.net/circlecloud/YumCore.git synced 2024-11-22 01:48:50 +00:00

fix: 修复默认值添加错误

Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
502647092 2016-09-26 00:35:16 +08:00
parent bbedb39b8c
commit a6f9faffa4

View File

@ -5,7 +5,8 @@ import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Collections; import java.util.ArrayList;
import java.util.HashMap;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
@ -47,7 +48,7 @@ public abstract class AbstractInjectConfig {
*/ */
public void inject(final ConfigurationSection config, final boolean save) { public void inject(final ConfigurationSection config, final boolean save) {
if (config == null) { if (config == null) {
Log.warning("尝试注入 ConfigurationSection 为 Null 的数据!"); Log.w("尝试%s ConfigurationSection 为 Null 的数据!", save ? "保存" : "读取");
return; return;
} }
this.config = config; this.config = config;
@ -75,8 +76,9 @@ public abstract class AbstractInjectConfig {
* @param config * @param config
* 配置文件区 * 配置文件区
*/ */
public void save(final ConfigurationSection config) { public ConfigurationSection save(final ConfigurationSection config) {
inject(config, true); inject(config, true);
return config;
} }
/** /**
@ -86,12 +88,17 @@ public abstract class AbstractInjectConfig {
* 字段 * 字段
* @param value * @param value
* *
* @throws IllegalAccessException
* @throws IllegalArgumentException
*/ */
private void applyDefault(final Field field, Object value) { private void applyDefault(final Field field, Object value) throws IllegalArgumentException, IllegalAccessException {
switch (field.getType().getName()) { switch (field.getType().getName()) {
case "java.util.List": case "java.util.List":
value = Collections.emptyList(); value = new ArrayList<>();
case "java.util.Map":
value = new HashMap<>();
} }
field.set(this, value);
} }
/** /**
@ -118,6 +125,8 @@ public abstract class AbstractInjectConfig {
return df.parse((String) value); return df.parse((String) value);
case "java.util.List": case "java.util.List":
return config.getList(path); return config.getList(path);
case "java.util.Map":
return config.getConfigurationSection(path).getValues(false);
default: default:
return hanldeDefault(type, path, value); return hanldeDefault(type, path, value);
} }
@ -159,26 +168,24 @@ public abstract class AbstractInjectConfig {
* 字段 * 字段
* @param value * @param value
* *
* @throws ParseException
* @throws SecurityException
* @throws NoSuchMethodException
* @throws InvocationTargetException
* @throws InstantiationException
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/ */
private void hanldeValue(final String path, final Field field, Object value) { private void hanldeValue(final String path, final Field field, Object value) throws IllegalAccessException, IllegalArgumentException, InstantiationException, InvocationTargetException, NoSuchMethodException, SecurityException, ParseException {
try { final Class<?> type = field.getType();
final Class<?> type = field.getType(); if (!type.equals(value.getClass())) {
if (!type.equals(value.getClass())) { value = convertType(type, path, value);
value = convertType(type, path, value);
}
if (type.equals(String.class)) {
value = ChatColor.translateAlternateColorCodes('&', (String) value);
}
field.set(this, value);
} catch (final IllegalArgumentException ex) {
Log.w(INJECT_TYPE_ERROR, path, field.getType().getName(), value.getClass().getName());
Log.debug(ex);
} catch (final ParseException e) {
Log.w(DATE_PARSE_ERROR, path, DATE_FORMAT, value);
} catch (final InstantiationException | InvocationTargetException | NoSuchMethodException | SecurityException | IllegalAccessException ex) {
Log.w(INJECT_ERROR, ex.getClass().getName(), ex.getMessage());
Log.debug(ex);
} }
if (type.equals(String.class)) {
value = ChatColor.translateAlternateColorCodes('&', (String) value);
}
field.set(this, value);
} }
/** /**
@ -208,17 +215,27 @@ public abstract class AbstractInjectConfig {
*/ */
protected void setField(final String path, final Field field) { protected void setField(final String path, final Field field) {
Object value = config.get(path); Object value = config.get(path);
final Default def = field.getAnnotation(Default.class); try {
if (value == null && def != null) { final Default def = field.getAnnotation(Default.class);
value = def.value(); if (value == null && def != null) {
} value = def.value();
if (value == null) {
if (field.getAnnotation(Nullable.class) == null) {
Log.w(PATH_NOT_FOUND, path);
applyDefault(field, value);
} }
return; if (value == null) {
if (field.getAnnotation(Nullable.class) == null) {
Log.w(PATH_NOT_FOUND, path);
applyDefault(field, value);
}
return;
}
hanldeValue(path, field, value);
} catch (final IllegalArgumentException ex) {
Log.w(INJECT_TYPE_ERROR, path, field.getType().getName(), value.getClass().getName());
Log.debug(ex);
} catch (final ParseException e) {
Log.w(DATE_PARSE_ERROR, path, DATE_FORMAT, value);
} catch (final InstantiationException | InvocationTargetException | NoSuchMethodException | SecurityException | IllegalAccessException ex) {
Log.w(INJECT_ERROR, ex.getClass().getName(), ex.getMessage());
Log.debug(ex);
} }
hanldeValue(path, field, value);
} }
} }