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:
parent
9013313c7a
commit
b514702598
@ -5,6 +5,7 @@ import java.lang.reflect.Field;
|
|||||||
import java.lang.reflect.InvocationTargetException;
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.lang.reflect.Modifier;
|
import java.lang.reflect.Modifier;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
import org.bukkit.ChatColor;
|
import org.bukkit.ChatColor;
|
||||||
@ -67,32 +68,21 @@ public abstract class AbstractInjectConfig {
|
|||||||
* @throws NoSuchMethodException
|
* @throws NoSuchMethodException
|
||||||
* @throws SecurityException
|
* @throws SecurityException
|
||||||
*/
|
*/
|
||||||
private Object convertType(Class<?> type, String path, Object value) throws IllegalAccessException, IllegalArgumentException, InstantiationException, InvocationTargetException, NoSuchMethodException, SecurityException {
|
private Object convertType(Class type, String path, Object value) throws IllegalAccessException, IllegalArgumentException, InstantiationException, InvocationTargetException, NoSuchMethodException, SecurityException {
|
||||||
Object result = InjectParse.parse(type, config, path);
|
switch (type.getName()) {
|
||||||
return result == null ? hanldeDefault(type, path, value) : result;
|
case "java.lang.Integer":
|
||||||
}
|
return Integer.valueOf(value.toString());
|
||||||
|
}
|
||||||
/**
|
if (type.isEnum()) {
|
||||||
* 默认类型处理流程
|
try {
|
||||||
*
|
return Enum.valueOf(type, value.toString());
|
||||||
* @param path
|
} catch (IllegalArgumentException | NullPointerException ex) {
|
||||||
* 路径
|
throw new ConfigParseException(String.format("值 %s 无效! %s 有效值为 %s", value, type.getSimpleName(), Arrays.toString(type.getEnumConstants())));
|
||||||
* @param field
|
}
|
||||||
* 字段
|
}
|
||||||
* @param value
|
if (InjectConfigurationSection.class.isAssignableFrom(type)) {
|
||||||
* 值
|
|
||||||
* @return 解析后的Value
|
|
||||||
* @throws IllegalAccessException
|
|
||||||
* @throws IllegalArgumentException
|
|
||||||
* @throws InstantiationException
|
|
||||||
* @throws InvocationTargetException
|
|
||||||
* @throws NoSuchMethodException
|
|
||||||
* @throws SecurityException
|
|
||||||
*/
|
|
||||||
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)) {
|
if (config.isConfigurationSection(path)) {
|
||||||
Constructor<?> constructor = field.getDeclaredConstructor(ConfigurationSection.class);
|
Constructor<?> constructor = type.getDeclaredConstructor(ConfigurationSection.class);
|
||||||
constructor.setAccessible(true);
|
constructor.setAccessible(true);
|
||||||
return constructor.newInstance(config.getConfigurationSection(path));
|
return constructor.newInstance(config.getConfigurationSection(path));
|
||||||
}
|
}
|
||||||
@ -118,8 +108,8 @@ public abstract class AbstractInjectConfig {
|
|||||||
* @throws IllegalArgumentException
|
* @throws IllegalArgumentException
|
||||||
* @throws IllegalAccessException
|
* @throws IllegalAccessException
|
||||||
*/
|
*/
|
||||||
private void hanldeValue(String path, Field field, Object value) throws IllegalAccessException, IllegalArgumentException, InstantiationException, InvocationTargetException, NoSuchMethodException, SecurityException, ConfigParseException {
|
private void hanldeValue(Field field, String path, Object value) throws IllegalAccessException, IllegalArgumentException, InstantiationException, InvocationTargetException, NoSuchMethodException, SecurityException, ConfigParseException {
|
||||||
Class<?> type = field.getType();
|
Class type = field.getType();
|
||||||
if (!type.equals(value.getClass())) {
|
if (!type.equals(value.getClass())) {
|
||||||
value = convertType(type, path, value);
|
value = convertType(type, path, value);
|
||||||
}
|
}
|
||||||
@ -134,7 +124,7 @@ public abstract class AbstractInjectConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 配置注入后的初始化操作(对象初始化也要在此处)
|
* 配置注入后的初始化操作(非注入对象初始化也要在此处)
|
||||||
*/
|
*/
|
||||||
protected void init() {
|
protected void init() {
|
||||||
}
|
}
|
||||||
@ -224,7 +214,7 @@ public abstract class AbstractInjectConfig {
|
|||||||
* 字段
|
* 字段
|
||||||
*/
|
*/
|
||||||
protected void setField(String path, Field field) {
|
protected void setField(String path, Field field) {
|
||||||
Object value = config.get(path);
|
Object value = InjectParse.parse(field.getType(), config, path);
|
||||||
try {
|
try {
|
||||||
Default def = field.getAnnotation(Default.class);
|
Default def = field.getAnnotation(Default.class);
|
||||||
if (value == null && def != null) {
|
if (value == null && def != null) {
|
||||||
@ -237,7 +227,7 @@ public abstract class AbstractInjectConfig {
|
|||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
hanldeValue(path, field, value);
|
hanldeValue(field, path, 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.d(ex);
|
Log.d(ex);
|
||||||
|
@ -3,7 +3,6 @@ package pw.yumc.YumCore.config.inject;
|
|||||||
import java.text.DateFormat;
|
import java.text.DateFormat;
|
||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -33,15 +32,7 @@ public class InjectParse {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static Object parse(Class clazz, ConfigurationSection config, String path) {
|
public static Object parse(Class clazz, ConfigurationSection config, String path) {
|
||||||
if (clazz.isEnum()) {
|
return allparse.containsKey(clazz) ? allparse.get(clazz).parse(config, path) : config.get(path);
|
||||||
String value = config.getString(path);
|
|
||||||
try {
|
|
||||||
return Enum.valueOf(clazz, value);
|
|
||||||
} catch (IllegalArgumentException ex) {
|
|
||||||
throw new ConfigParseException(String.format("%s 不是 %s 有效值为 %s", value, clazz.getSimpleName(), Arrays.toString(clazz.getEnumConstants())));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return allparse.containsKey(clazz) ? allparse.get(clazz).parse(config, path) : null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user