更新一下配置类
This commit is contained in:
@@ -1,33 +1,20 @@
|
||||
package com.ilummc.tlib.inject;
|
||||
|
||||
import com.google.common.io.Files;
|
||||
import com.ilummc.tlib.TLib;
|
||||
import com.ilummc.tlib.annotations.Config;
|
||||
import me.skymc.taboolib.fileutils.ConfigUtils;
|
||||
import org.apache.commons.lang3.Validate;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.yaml.snakeyaml.DumperOptions;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.apache.commons.lang3.Validate;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.configuration.serialization.ConfigurationSerializable;
|
||||
import org.bukkit.configuration.serialization.ConfigurationSerialization;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.yaml.snakeyaml.DumperOptions;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.io.Files;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
import com.ilummc.tlib.TLib;
|
||||
import com.ilummc.tlib.annotations.Config;
|
||||
import com.ilummc.tlib.bean.Property;
|
||||
|
||||
public class TConfigInjector {
|
||||
|
||||
@@ -83,9 +70,10 @@ public class TConfigInjector {
|
||||
try {
|
||||
Config config = clazz.getAnnotation(Config.class);
|
||||
Validate.notNull(config);
|
||||
return new GsonBuilder().disableHtmlEscaping().excludeFieldsWithModifiers(config.excludeModifiers())
|
||||
.create().fromJson(new Gson().toJson(new Yaml()
|
||||
.dump(Files.toString(new File(plugin.getDataFolder(), config.name()), Charset.forName(config.charset())))), clazz);
|
||||
return ConfigUtils.confToObj(
|
||||
ConfigUtils.mapToConf(
|
||||
ConfigUtils.yamlToMap(
|
||||
Files.toString(new File(plugin.getDataFolder(), config.name()), Charset.forName(config.charset())))), clazz);
|
||||
} catch (NullPointerException e) {
|
||||
TLib.getTLib().getLogger().warn("插件 " + plugin + " 的配置类 " + clazz.getSimpleName() + " 加载失败:没有 @Config 注解或文件不存在");
|
||||
return null;
|
||||
@@ -103,7 +91,7 @@ public class TConfigInjector {
|
||||
try {
|
||||
Config config = object.getClass().getAnnotation(Config.class);
|
||||
Validate.notNull(config);
|
||||
return new Serializer(new LinkedHashMap<>(), object, config.excludeModifiers()).get();
|
||||
return ConfigUtils.objToConf(object).getValues(false);
|
||||
} catch (NullPointerException e) {
|
||||
TLib.getTLib().getLogger().warn("插件 " + plugin + " 的配置类 " + object.getClass().getSimpleName() + " 序列化失败:没有 @Config 注解");
|
||||
} catch (Exception e) {
|
||||
@@ -128,64 +116,4 @@ public class TConfigInjector {
|
||||
Files.write(arr, target);
|
||||
}
|
||||
|
||||
private static final List<Class<?>> primitiveType = Lists.newArrayList(Integer.class,
|
||||
Double.class, Float.class, Boolean.class, Short.class, Byte.class, Character.class, Long.class, String.class);
|
||||
|
||||
private static class Serializer {
|
||||
|
||||
private HashMap<String, Object> map;
|
||||
private Object o;
|
||||
private int modifiers;
|
||||
|
||||
private Serializer(HashMap<String, Object> map, Object o, int modifiers) {
|
||||
this.map = map;
|
||||
this.o = o;
|
||||
this.modifiers = modifiers;
|
||||
}
|
||||
|
||||
private HashMap<String, Object> get() {
|
||||
for (Field field : o.getClass().getDeclaredFields()) {
|
||||
if ((field.getModifiers() & modifiers) == 0 && !field.isSynthetic())
|
||||
try {
|
||||
SerializedName node = field.getAnnotation(SerializedName.class);
|
||||
if (!field.isAccessible()) field.setAccessible(true);
|
||||
Object obj = field.get(o);
|
||||
map.put(node == null ? field.getName() : node.value(), serialize(obj));
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
private Object serialize(Object o) {
|
||||
try {
|
||||
if (o.getClass().isPrimitive() || primitiveType.contains(o.getClass())) {
|
||||
return o;
|
||||
} else if (o.getClass().isArray()) {
|
||||
return ImmutableList.copyOf(((Object[]) o));
|
||||
} else if (o instanceof Collection) {
|
||||
return ((Collection) o).stream().map(this::serialize).collect(Collectors.toList());
|
||||
} else if (o instanceof Map) {
|
||||
Map map = new LinkedHashMap<>();
|
||||
((Map) o).forEach((o1, o2) -> map.put(o1, serialize(o2)));
|
||||
return map;
|
||||
} else if (o instanceof ConfigurationSerializable) {
|
||||
Map map = new LinkedHashMap<>();
|
||||
map.put(ConfigurationSerialization.SERIALIZED_TYPE_KEY,
|
||||
ConfigurationSerialization.getAlias((Class<? extends ConfigurationSerializable>) o.getClass()));
|
||||
map.putAll(((ConfigurationSerializable) o).serialize());
|
||||
return map;
|
||||
} else if (o instanceof Property) {
|
||||
return serialize(((Property) o).get());
|
||||
} else {
|
||||
return new Serializer(new HashMap<>(), o, modifiers).get();
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,25 +1,18 @@
|
||||
package com.ilummc.tlib.util;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
|
||||
import org.objectweb.asm.ClassReader;
|
||||
import org.objectweb.asm.ClassWriter;
|
||||
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
import com.ilummc.tlib.TLib;
|
||||
import com.ilummc.tlib.util.asm.AsmAnalyser;
|
||||
|
||||
import org.objectweb.asm.ClassReader;
|
||||
import org.objectweb.asm.ClassWriter;
|
||||
import sun.reflect.Reflection;
|
||||
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@ThreadSafe
|
||||
public class Ref {
|
||||
|
||||
@@ -70,7 +63,20 @@ public class Ref {
|
||||
public static Optional<Class<?>> getCallerClass(int depth) {
|
||||
return Optional.ofNullable(CallerClass.impl.getCallerClass(depth + 1));
|
||||
}
|
||||
|
||||
|
||||
public static String getSerializedName(Field field) {
|
||||
return field.isAnnotationPresent(SerializedName.class) ? field.getAnnotation(SerializedName.class).value() : field.getName();
|
||||
}
|
||||
|
||||
public static Optional<Field> getFieldBySerializedName(Class<?> clazz, String name) {
|
||||
for (Field field : Ref.getDeclaredFields(clazz, 0, false)) {
|
||||
if (field.isAnnotationPresent(SerializedName.class))
|
||||
if (field.getAnnotation(SerializedName.class).value().equals(name)) return Optional.of(field);
|
||||
else if (field.getName().equals(name)) return Optional.of(field);
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
private static abstract class CallerClass {
|
||||
|
||||
private static CallerClass impl;
|
||||
@@ -87,9 +93,9 @@ public class Ref {
|
||||
abstract Class<?> getCallerClass(int i);
|
||||
|
||||
private static class ReflectionImpl extends CallerClass {
|
||||
|
||||
@SuppressWarnings({ "deprecation", "restriction" })
|
||||
@Override
|
||||
|
||||
@SuppressWarnings({"deprecation", "restriction"})
|
||||
@Override
|
||||
Class<?> getCallerClass(int i) {
|
||||
return Reflection.getCallerClass(i);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user