mirror of
https://e.coding.net/circlecloud/YumCore.git
synced 2024-11-24 02:08:48 +00:00
feat: 使用反射valueOf转换类型
Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
parent
839698160c
commit
2cad6e713a
@ -5,7 +5,6 @@ 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;
|
||||||
@ -68,17 +67,10 @@ 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 NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
|
||||||
switch (type.getName()) {
|
try {
|
||||||
case "java.lang.Integer":
|
return type.getDeclaredMethod("valueOf", String.class).invoke(null, value);
|
||||||
return Integer.valueOf(value.toString());
|
} catch (NoSuchMethodException | IllegalArgumentException ignored) {
|
||||||
}
|
|
||||||
if (type.isEnum()) {
|
|
||||||
try {
|
|
||||||
return Enum.valueOf(type, value.toString());
|
|
||||||
} catch (IllegalArgumentException | NullPointerException ex) {
|
|
||||||
throw new ConfigParseException(String.format("值 %s 无效! %s 有效值为 %s", value, type.getSimpleName(), Arrays.toString(type.getEnumConstants())));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (InjectConfigurationSection.class.isAssignableFrom(type)) {
|
if (InjectConfigurationSection.class.isAssignableFrom(type)) {
|
||||||
if (config.isConfigurationSection(path)) {
|
if (config.isConfigurationSection(path)) {
|
||||||
|
Loading…
Reference in New Issue
Block a user