mirror of
https://github.com/CarmJos/EasyConfiguration.git
synced 2026-06-04 18:48:20 +08:00
Compare commits
20 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| fd01d9b7ef | |||
| 0f8383bbf3 | |||
| 1232c7c4da | |||
| 7ac39da4e9 | |||
| bf89f583db | |||
| 03c69ba3a2 | |||
| d9cbd1a283 | |||
| 96e90dd71b | |||
| a9f3d829bd | |||
| 8eefba5159 | |||
| 01e20df559 | |||
| 35398ab741 | |||
| 6f97166192 | |||
| ccd239ad6b | |||
| 69b27476bc | |||
| 0651cac6b0 | |||
| 1a1efad283 | |||
| 3c1ba61b61 | |||
| 1a3e84a787 | |||
| 00228db2c4 |
@@ -30,7 +30,7 @@
|
||||
|
||||
### 示例代码
|
||||
|
||||
您可以 [点击这里](impl/yaml/src/test/java/config/source/DemoConfiguration.java) 查看部分代码演示,更多演示详见 [开发介绍](.documentation/README.md) 。
|
||||
您可以 [点击这里](demo/src/main/java/cc/carm/lib/configuration/demo/DatabaseConfiguration.java) 查看部分代码演示,更多演示详见 [开发介绍](.documentation/README.md) 。
|
||||
|
||||
### 依赖方式
|
||||
|
||||
|
||||
+3
-1
@@ -5,12 +5,14 @@
|
||||
<parent>
|
||||
<artifactId>easyconfiguration-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>3.2.0</version>
|
||||
<version>3.3.3</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<properties>
|
||||
<maven.compiler.source>${java.version}</maven.compiler.source>
|
||||
<maven.compiler.target>${java.version}</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
|
||||
</properties>
|
||||
|
||||
<artifactId>easyconfiguration-core</artifactId>
|
||||
|
||||
@@ -46,7 +46,7 @@ public class ConfigInitializer<T extends ConfigurationProvider<?>> {
|
||||
*/
|
||||
public void initialize(@NotNull Class<? extends ConfigurationRoot> clazz,
|
||||
boolean saveDefaults, boolean loadSubClasses) {
|
||||
initializeClass(
|
||||
initializeStaticClass(
|
||||
clazz, null, null,
|
||||
null, null, null,
|
||||
saveDefaults, loadSubClasses
|
||||
@@ -60,12 +60,61 @@ public class ConfigInitializer<T extends ConfigurationProvider<?>> {
|
||||
}
|
||||
}
|
||||
|
||||
protected void initializeClass(@NotNull Class<?> clazz,
|
||||
@Nullable String parentPath, @Nullable String fieldName,
|
||||
@Nullable ConfigPath fieldPath,
|
||||
@Nullable HeaderComment fieldHeaderComments,
|
||||
@Nullable InlineComment fieldInlineComments,
|
||||
boolean saveDefaults, boolean loadSubClasses) {
|
||||
/**
|
||||
* 初始化指定实例的所有 {@link ConfigValue} 与内部 {@link ConfigurationRoot} 对象。
|
||||
*
|
||||
* @param config 配置文件实例类,须实现 {@link ConfigurationRoot} 。
|
||||
*/
|
||||
public void initialize(@NotNull ConfigurationRoot config) {
|
||||
initialize(config, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化指定实例的所有 {@link ConfigValue} 与内部 {@link ConfigurationRoot} 对象。
|
||||
*
|
||||
* @param config 配置文件实例类,须实现 {@link ConfigurationRoot} 。
|
||||
* @param saveDefaults 是否写入默认值(默认为 true)。
|
||||
*/
|
||||
public void initialize(@NotNull ConfigurationRoot config, boolean saveDefaults) {
|
||||
initializeInstance(
|
||||
config, null, null,
|
||||
null, null, null,
|
||||
saveDefaults
|
||||
);
|
||||
if (saveDefaults) {
|
||||
try {
|
||||
provider.save();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 针对实例类的初始化方法
|
||||
private void initializeInstance(@NotNull ConfigurationRoot root,
|
||||
@Nullable String parentPath, @Nullable String fieldName,
|
||||
@Nullable ConfigPath fieldPath,
|
||||
@Nullable HeaderComment fieldHeaderComments,
|
||||
@Nullable InlineComment fieldInlineComments,
|
||||
boolean saveDefaults) {
|
||||
String path = getClassPath(root.getClass(), parentPath, fieldName, fieldPath);
|
||||
this.provider.setHeaderComment(path, getClassHeaderComments(root.getClass(), fieldHeaderComments));
|
||||
if (path != null) this.provider.setInlineComment(path, readInlineComments(fieldInlineComments));
|
||||
|
||||
for (Field field : root.getClass().getDeclaredFields()) {
|
||||
initializeField(root, field, path, saveDefaults, false);
|
||||
}
|
||||
}
|
||||
|
||||
// 针对静态类的初始化方法
|
||||
private void initializeStaticClass(@NotNull Class<?> clazz,
|
||||
@Nullable String parentPath, @Nullable String fieldName,
|
||||
@Nullable ConfigPath fieldPath,
|
||||
@Nullable HeaderComment fieldHeaderComments,
|
||||
@Nullable InlineComment fieldInlineComments,
|
||||
boolean saveDefaults, boolean loadSubClasses) {
|
||||
if (!ConfigurationRoot.class.isAssignableFrom(clazz)) return; // 只解析继承了 ConfigurationRoot 的类
|
||||
String path = getClassPath(clazz, parentPath, fieldName, fieldPath);
|
||||
this.provider.setHeaderComment(path, getClassHeaderComments(clazz, fieldHeaderComments));
|
||||
if (path != null) this.provider.setInlineComment(path, readInlineComments(fieldInlineComments));
|
||||
@@ -77,7 +126,7 @@ public class ConfigInitializer<T extends ConfigurationProvider<?>> {
|
||||
if (!loadSubClasses) return;
|
||||
Class<?>[] classes = clazz.getDeclaredClasses();
|
||||
for (int i = classes.length - 1; i >= 0; i--) { // 逆向加载,保持顺序。
|
||||
initializeClass(
|
||||
initializeStaticClass(
|
||||
classes[i], path, classes[i].getSimpleName(),
|
||||
null, null, null,
|
||||
saveDefaults, true
|
||||
@@ -85,11 +134,12 @@ public class ConfigInitializer<T extends ConfigurationProvider<?>> {
|
||||
}
|
||||
}
|
||||
|
||||
private void initializeField(@NotNull Class<?> source, @NotNull Field field, @Nullable String parent,
|
||||
boolean saveDefaults, boolean loadSubClasses) {
|
||||
private void initializeField(@NotNull Object source, @NotNull Field field,
|
||||
@Nullable String parent, boolean saveDefaults, boolean loadSubClasses) {
|
||||
try {
|
||||
field.setAccessible(true);
|
||||
Object object = field.get(source);
|
||||
|
||||
if (object instanceof ConfigValue<?>) {
|
||||
initializeValue(
|
||||
(ConfigValue<?>) object, getFieldPath(field, parent),
|
||||
@@ -97,8 +147,18 @@ public class ConfigInitializer<T extends ConfigurationProvider<?>> {
|
||||
field.getAnnotation(InlineComment.class),
|
||||
saveDefaults
|
||||
);
|
||||
} else if (object instanceof Class<?>) {
|
||||
initializeClass(
|
||||
} else if (source instanceof ConfigurationRoot && object instanceof ConfigurationRoot) {
|
||||
// 当且仅当 源字段与字段 均为ConfigurationRoot实例时,才对目标字段进行下一步初始化加载。
|
||||
initializeInstance(
|
||||
(ConfigurationRoot) object, parent, field.getName(),
|
||||
field.getAnnotation(ConfigPath.class),
|
||||
field.getAnnotation(HeaderComment.class),
|
||||
field.getAnnotation(InlineComment.class),
|
||||
saveDefaults
|
||||
);
|
||||
} else if (source instanceof Class<?> && object instanceof Class<?>) {
|
||||
// 当且仅当 源字段与字段 均为静态类时,才对目标字段进行下一步初始化加载。
|
||||
initializeStaticClass(
|
||||
(Class<?>) object, parent, field.getName(),
|
||||
field.getAnnotation(ConfigPath.class),
|
||||
field.getAnnotation(HeaderComment.class),
|
||||
@@ -106,6 +166,11 @@ public class ConfigInitializer<T extends ConfigurationProvider<?>> {
|
||||
saveDefaults, loadSubClasses
|
||||
);
|
||||
}
|
||||
|
||||
// 以上判断实现以下规范:
|
||||
// - 实例类中仅加载 ConfigValue实例 与 ConfigurationRoot实例
|
||||
// - 静态类中仅加载 静态ConfigValue实例 与 静态ConfigurationRoot类
|
||||
|
||||
} catch (IllegalAccessException ignored) {
|
||||
}
|
||||
}
|
||||
@@ -142,7 +207,7 @@ public class ConfigInitializer<T extends ConfigurationProvider<?>> {
|
||||
return value.length() > 0 ? value : null;
|
||||
}
|
||||
|
||||
protected static @Nullable String getClassPath(@NotNull Class<?> clazz,
|
||||
protected static @Nullable String getClassPath(@Nullable Class<?> clazz,
|
||||
@Nullable String parentPath,
|
||||
@Nullable String filedName,
|
||||
@Nullable ConfigPath fieldAnnotation) {
|
||||
@@ -150,11 +215,13 @@ public class ConfigInitializer<T extends ConfigurationProvider<?>> {
|
||||
boolean fromRoot = false;
|
||||
|
||||
// 先获取 Class 对应的路径注解 其优先度最高。
|
||||
ConfigPath clazzAnnotation = clazz.getAnnotation(ConfigPath.class);
|
||||
if (clazzAnnotation != null) {
|
||||
fromRoot = clazzAnnotation.root();
|
||||
if (clazzAnnotation.value().length() > 0) {
|
||||
return (fromRoot ? "" : parent) + clazzAnnotation.value();
|
||||
if (clazz != null) {
|
||||
ConfigPath clazzAnnotation = clazz.getAnnotation(ConfigPath.class);
|
||||
if (clazzAnnotation != null) {
|
||||
fromRoot = clazzAnnotation.root();
|
||||
if (clazzAnnotation.value().length() > 0) {
|
||||
return (fromRoot ? "" : parent) + clazzAnnotation.value();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public abstract class AbstractConfigBuilder<T, B extends AbstractConfigBuilder<T, B, P>, P extends ConfigurationProvider<?>> {
|
||||
|
||||
@@ -61,4 +62,8 @@ public abstract class AbstractConfigBuilder<T, B extends AbstractConfigBuilder<T
|
||||
return getThis();
|
||||
}
|
||||
|
||||
public @NotNull B defaults(@NotNull Supplier<@Nullable T> defaultValueSupplier) {
|
||||
return defaults(defaultValueSupplier.get());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import cc.carm.lib.configuration.core.value.type.ConfiguredMap;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class SourceMapBuilder<M extends Map<K, V>, S, K, V> extends CommonConfigBuilder<M, SourceMapBuilder<M, S, K, V>> {
|
||||
@@ -51,6 +52,12 @@ public class SourceMapBuilder<M extends Map<K, V>, S, K, V> extends CommonConfig
|
||||
);
|
||||
}
|
||||
|
||||
public @NotNull SourceMapBuilder<M, S, K, V> defaults(@NotNull Consumer<M> factory) {
|
||||
M map = supplier.get();
|
||||
factory.accept(map);
|
||||
return defaults(map);
|
||||
}
|
||||
|
||||
public @NotNull SourceMapBuilder<M, S, K, V> parseKey(@NotNull ConfigDataFunction<String, K> parser) {
|
||||
this.keyParser = parser;
|
||||
return this;
|
||||
|
||||
@@ -50,6 +50,25 @@ public interface ConfigValueParser<T, R> {
|
||||
@Contract(pure = true)
|
||||
static <V> @NotNull ConfigValueParser<Object, V> castObject(Class<V> valueClass) {
|
||||
return (input, defaultValue) -> {
|
||||
|
||||
if (Number.class.isAssignableFrom(valueClass)) {
|
||||
if (Long.class.isAssignableFrom(valueClass)) {
|
||||
input = longValue().parse(input, (Long) defaultValue);
|
||||
} else if (Integer.class.isAssignableFrom(valueClass)) {
|
||||
input = intValue().parse(input, (Integer) defaultValue);
|
||||
} else if (Float.class.isAssignableFrom(valueClass)) {
|
||||
input = floatValue().parse(input, (Float) defaultValue);
|
||||
} else if (Double.class.isAssignableFrom(valueClass)) {
|
||||
input = doubleValue().parse(input, (Double) defaultValue);
|
||||
} else if (Byte.class.isAssignableFrom(valueClass)) {
|
||||
input = byteValue().parse(input, (Byte) defaultValue);
|
||||
} else if (Short.class.isAssignableFrom(valueClass)) {
|
||||
input = shortValue().parse(input, (Short) defaultValue);
|
||||
}
|
||||
} else if (Boolean.class.isAssignableFrom(valueClass)) {
|
||||
input = booleanValue().parse(input, (Boolean) defaultValue);
|
||||
}
|
||||
|
||||
if (valueClass.isInstance(input)) return valueClass.cast(input);
|
||||
else throw new IllegalArgumentException("Cannot cast value to " + valueClass.getName());
|
||||
};
|
||||
|
||||
+72
-2
@@ -2,6 +2,8 @@ package cc.carm.lib.configuration.core.source;
|
||||
|
||||
import cc.carm.lib.configuration.core.ConfigInitializer;
|
||||
import cc.carm.lib.configuration.core.ConfigurationRoot;
|
||||
import cc.carm.lib.configuration.core.value.ConfigValue;
|
||||
import cc.carm.lib.configuration.core.value.impl.CachedConfigValue;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.annotations.Unmodifiable;
|
||||
@@ -17,23 +19,54 @@ public abstract class ConfigurationProvider<W extends ConfigurationWrapper<?>> {
|
||||
this.updateTime = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
/**
|
||||
* 得到配置文件的更新(最后加载)时间。
|
||||
*
|
||||
* @return 更新时间
|
||||
*/
|
||||
public long getUpdateTime() {
|
||||
return updateTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* 用于 {@link CachedConfigValue} 判断缓存值是否过期(即缓存的时间早于配置文件的最后加载时间)。
|
||||
*
|
||||
* @param time 缓存值时的时间戳
|
||||
* @return 缓存值是否过期
|
||||
*/
|
||||
public boolean isExpired(long time) {
|
||||
return getUpdateTime() > time;
|
||||
}
|
||||
|
||||
/**
|
||||
* 得到配置文件的原生功能类。
|
||||
*
|
||||
* @return 原生类
|
||||
*/
|
||||
public abstract @NotNull W getConfiguration();
|
||||
|
||||
/**
|
||||
* 重载当前配置文件。(将不会保存已修改的内容)
|
||||
*
|
||||
* @throws Exception 当重载出现错误时抛出
|
||||
*/
|
||||
public void reload() throws Exception {
|
||||
onReload(); // 调用重写的Reload方法
|
||||
this.updateTime = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
/**
|
||||
* 将当前对配置文件的更改进行保存。
|
||||
*
|
||||
* @throws Exception 当保存出现错误时抛出
|
||||
*/
|
||||
public abstract void save() throws Exception;
|
||||
|
||||
/**
|
||||
* 针对于不同配置文件类型所执行的重载操作。
|
||||
*
|
||||
* @throws Exception 当操作出现错误时抛出。
|
||||
*/
|
||||
protected abstract void onReload() throws Exception;
|
||||
|
||||
public abstract @Nullable ConfigurationComments getComments();
|
||||
@@ -60,16 +93,53 @@ public abstract class ConfigurationProvider<W extends ConfigurationWrapper<?>> {
|
||||
|
||||
public abstract @NotNull ConfigInitializer<? extends ConfigurationProvider<W>> getInitializer();
|
||||
|
||||
/**
|
||||
* 初始化指定类以及其子类的所有 {@link ConfigValue} 对象。
|
||||
*
|
||||
* @param configClazz 配置文件类,须继承于 {@link ConfigurationRoot} 。
|
||||
*/
|
||||
public void initialize(Class<? extends ConfigurationRoot> configClazz) {
|
||||
initialize(configClazz, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化指定类以及其子类的所有 {@link ConfigValue} 对象。
|
||||
*
|
||||
* @param configClazz 配置文件类,须继承于 {@link ConfigurationRoot} 。
|
||||
* @param saveDefaults 是否写入默认值(默认为 true)。
|
||||
*/
|
||||
public void initialize(Class<? extends ConfigurationRoot> configClazz, boolean saveDefaults) {
|
||||
getInitializer().initialize(configClazz, saveDefaults);
|
||||
this.getInitializer().initialize(configClazz, saveDefaults);
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化指定类的所有 {@link ConfigValue} 对象。
|
||||
*
|
||||
* @param configClazz 配置文件类,须继承于 {@link ConfigurationRoot} 。
|
||||
* @param saveDefaults 是否写入默认值(默认为 true)。
|
||||
* @param loadSubClasses 是否加载内部子类(默认为 true)。
|
||||
*/
|
||||
public void initialize(Class<? extends ConfigurationRoot> configClazz, boolean saveDefaults, boolean loadSubClasses) {
|
||||
getInitializer().initialize(configClazz, saveDefaults, loadSubClasses);
|
||||
this.getInitializer().initialize(configClazz, saveDefaults, loadSubClasses);
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化指定实例的所有 {@link ConfigValue} 与内部 {@link ConfigurationRoot} 对象。
|
||||
*
|
||||
* @param config 配置文件实例类,须实现 {@link ConfigurationRoot} 。
|
||||
*/
|
||||
public void initialize(@NotNull ConfigurationRoot config) {
|
||||
this.getInitializer().initialize(config, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化指定实例的所有 {@link ConfigValue} 与内部 {@link ConfigurationRoot} 对象。
|
||||
*
|
||||
* @param config 配置文件实例类,须实现 {@link ConfigurationRoot} 。
|
||||
* @param saveDefaults 是否写入默认值(默认为 true)。
|
||||
*/
|
||||
public void initialize(@NotNull ConfigurationRoot config, boolean saveDefaults) {
|
||||
this.getInitializer().initialize(config, saveDefaults);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package config.offset;
|
||||
package cc.carm.test.config.offset;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
//package config.offset;
|
||||
//
|
||||
//
|
||||
//import sun.misc.Unsafe;
|
||||
//
|
||||
//import java.lang.reflect.Field;
|
||||
//import java.lang.reflect.Modifier;
|
||||
//import java.util.Collections;
|
||||
//import java.util.LinkedList;
|
||||
//import java.util.List;
|
||||
//
|
||||
///**
|
||||
// * @author Chris2018998
|
||||
// */
|
||||
//public class OffsetUtil {
|
||||
// private static Unsafe unsafe;
|
||||
//
|
||||
// static {
|
||||
// try {
|
||||
// // 获取 Unsafe 内部的私有的实例化单例对象
|
||||
// Field field = Unsafe.class.getDeclaredField("theUnsafe");
|
||||
// // 无视权限
|
||||
// field.setAccessible(true);
|
||||
// unsafe = (Unsafe) field.get(null);
|
||||
// } catch (NoSuchFieldException | IllegalAccessException e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
//// try {
|
||||
//// unsafe = AccessController.doPrivileged((PrivilegedExceptionAction<Unsafe>) () -> {
|
||||
//// Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
|
||||
//// theUnsafe.setAccessible(true);
|
||||
//// return (Unsafe) theUnsafe.get(null);
|
||||
//// });
|
||||
//// } catch (Throwable e) {
|
||||
//// System.err.println("Unable to load unsafe");
|
||||
//// }
|
||||
// }
|
||||
//
|
||||
// public static List<FieldOffset> getClassMemberOffset(Class<?> beanClass) {
|
||||
// List<FieldOffset> offsetsList = new LinkedList<>();
|
||||
// for (Field field : beanClass.getDeclaredFields()) {
|
||||
// FieldOffset fieldOffset = new FieldOffset(field);
|
||||
// offsetsList.add(fieldOffset);
|
||||
// if (Modifier.isStatic(field.getModifiers()))
|
||||
// fieldOffset.setOffsetValue(unsafe.staticFieldOffset(field));
|
||||
// else
|
||||
// fieldOffset.setOffsetValue(unsafe.objectFieldOffset(field));
|
||||
// Class<?> fieldType = field.getType();
|
||||
// if (!fieldType.getName().startsWith("java")) {
|
||||
// Field[] subfields = fieldType.getDeclaredFields();
|
||||
// if (subfields.length > 0) {
|
||||
// fieldOffset.setSubFieldOffsetList(getClassMemberOffset(fieldType));
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// Collections.sort(offsetsList);
|
||||
// return offsetsList;
|
||||
// }
|
||||
//
|
||||
//
|
||||
//}
|
||||
//
|
||||
@@ -0,0 +1,52 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>easyconfiguration-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>3.3.3</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<properties>
|
||||
<maven.compiler.source>${java.version}</maven.compiler.source>
|
||||
<maven.compiler.target>${java.version}</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
|
||||
</properties>
|
||||
<artifactId>easyconfiguration-demo</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>${project.parent.groupId}</groupId>
|
||||
<artifactId>easyconfiguration-core</artifactId>
|
||||
<version>${project.parent.version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-source-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
+3
-2
@@ -1,4 +1,4 @@
|
||||
package config.source;
|
||||
package cc.carm.lib.configuration.demo;
|
||||
|
||||
import cc.carm.lib.configuration.core.ConfigurationRoot;
|
||||
import cc.carm.lib.configuration.core.annotation.ConfigPath;
|
||||
@@ -12,7 +12,8 @@ public class DatabaseConfiguration extends ConfigurationRoot {
|
||||
@ConfigPath("driver")
|
||||
@HeaderComment({
|
||||
"数据库驱动配置,请根据数据库类型设置。",
|
||||
"- MySQL: com.mysql.cj.jdbc.Driver",
|
||||
"- MySQL(旧): com.mysql.jdbc.Driver",
|
||||
"- MySQL(新): com.mysql.cj.jdbc.Driver",
|
||||
"- MariaDB(推荐): org.mariadb.jdbc.Driver",
|
||||
})
|
||||
protected static final ConfigValue<String> DRIVER_NAME = ConfiguredValue.of(
|
||||
@@ -0,0 +1,78 @@
|
||||
package cc.carm.lib.configuration.demo.tests;
|
||||
|
||||
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
||||
import cc.carm.lib.configuration.demo.tests.model.TestModel;
|
||||
import cc.carm.lib.configuration.demo.tests.conf.DemoConfiguration;
|
||||
import cc.carm.lib.configuration.demo.tests.conf.TestConfiguration;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class ConfigurationTest {
|
||||
|
||||
public static void testDemo(ConfigurationProvider<?> provider) {
|
||||
provider.initialize(DemoConfiguration.class);
|
||||
|
||||
System.out.println("----------------------------------------------------");
|
||||
|
||||
System.out.println("Test Number: ");
|
||||
|
||||
System.out.println("before: " + DemoConfiguration.TEST_NUMBER.get());
|
||||
DemoConfiguration.TEST_NUMBER.set((long) (Long.MAX_VALUE * Math.random()));
|
||||
System.out.println("after: " + DemoConfiguration.TEST_NUMBER.get());
|
||||
|
||||
System.out.println("> Test Value:");
|
||||
System.out.println("before: " + DemoConfiguration.Sub.UUID_CONFIG_VALUE.get());
|
||||
DemoConfiguration.Sub.UUID_CONFIG_VALUE.set(UUID.randomUUID());
|
||||
System.out.println("after: " + DemoConfiguration.Sub.UUID_CONFIG_VALUE.get());
|
||||
|
||||
System.out.println("> Test List:");
|
||||
DemoConfiguration.Sub.That.OPERATORS.getNotNull().forEach(System.out::println);
|
||||
List<UUID> operators = IntStream.range(0, 5).mapToObj(i -> UUID.randomUUID()).collect(Collectors.toList());
|
||||
DemoConfiguration.Sub.That.OPERATORS.set(operators);
|
||||
|
||||
System.out.println("> Test Section:");
|
||||
System.out.println(DemoConfiguration.MODEL_TEST.get());
|
||||
DemoConfiguration.MODEL_TEST.set(TestModel.random());
|
||||
|
||||
System.out.println("> Test Maps:");
|
||||
DemoConfiguration.USERS.getNotNull().forEach((k, v) -> System.out.println(k + ": " + v));
|
||||
LinkedHashMap<Integer, UUID> data = new LinkedHashMap<>();
|
||||
for (int i = 1; i <= 5; i++) {
|
||||
data.put(i, UUID.randomUUID());
|
||||
}
|
||||
DemoConfiguration.USERS.set(data);
|
||||
System.out.println("----------------------------------------------------");
|
||||
}
|
||||
|
||||
public static void testInner(ConfigurationProvider<?> provider) {
|
||||
|
||||
TestConfiguration TEST = new TestConfiguration();
|
||||
|
||||
provider.initialize(TEST, true);
|
||||
|
||||
System.out.println("> Test Inner value before:");
|
||||
System.out.println(TEST.INNER.INNER_VALUE.getNotNull());
|
||||
|
||||
double after = Math.random() * 200D;
|
||||
System.out.println("> Test Inner value -> " + after);
|
||||
TEST.INNER.INNER_VALUE.set(after);
|
||||
|
||||
System.out.println("> Test Inner value after:");
|
||||
System.out.println(TEST.INNER.INNER_VALUE.getNotNull());
|
||||
|
||||
}
|
||||
|
||||
public static void save(ConfigurationProvider<?> provider) {
|
||||
try {
|
||||
provider.save();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+7
-9
@@ -1,4 +1,4 @@
|
||||
package config.source;
|
||||
package cc.carm.lib.configuration.demo.tests.conf;
|
||||
|
||||
import cc.carm.lib.configuration.core.ConfigInitializer;
|
||||
import cc.carm.lib.configuration.core.ConfigurationRoot;
|
||||
@@ -10,7 +10,7 @@ import cc.carm.lib.configuration.core.value.type.ConfiguredList;
|
||||
import cc.carm.lib.configuration.core.value.type.ConfiguredMap;
|
||||
import cc.carm.lib.configuration.core.value.type.ConfiguredSection;
|
||||
import cc.carm.lib.configuration.core.value.type.ConfiguredValue;
|
||||
import config.model.TestModel;
|
||||
import cc.carm.lib.configuration.demo.tests.model.TestModel;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -27,16 +27,15 @@ public class DemoConfiguration extends ConfigurationRoot {
|
||||
@ConfigPath(root = true)
|
||||
protected static final ConfigValue<Double> VERSION = ConfiguredValue.of(Double.class, 1.0D);
|
||||
|
||||
@ConfigPath(root = true)
|
||||
public static final ConfigValue<Long> TEST_NUMBER = ConfiguredValue.of(Long.class, 1000000L);
|
||||
|
||||
// 支持通过 Class<?> 变量标注子配置,一并注册。
|
||||
// 注意: 若对应类也有注解,则优先使用类的注解。
|
||||
@ConfigPath("impl-test") //支持通过注解修改子配置的主路径,若不修改则以变量名自动生成。
|
||||
@ConfigPath("other-class-config") //支持通过注解修改子配置的主路径,若不修改则以变量名自动生成。
|
||||
@HeaderComment({"", "Something..."}) // 支持给子路径直接打注释
|
||||
@InlineComment("InlineComments for class path")
|
||||
public static final Class<?> IMPL = ImplConfiguration.class;
|
||||
|
||||
// 子配置文件
|
||||
@ConfigPath("database")
|
||||
public static final Class<?> DB_CONFIG = DatabaseConfiguration.class;
|
||||
public static final Class<?> OTHER = OtherConfiguration.class;
|
||||
|
||||
@ConfigPath("user") // 通过注解规定配置文件中的路径,若不进行注解则以变量名自动生成。
|
||||
@HeaderComment({"", "Section类型数据测试"}) // 通过注解给配置添加注释。
|
||||
@@ -76,7 +75,6 @@ public class DemoConfiguration extends ConfigurationRoot {
|
||||
.build();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
package cc.carm.lib.configuration.demo.tests.conf;
|
||||
|
||||
public class OtherConfiguration {
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package cc.carm.lib.configuration.demo.tests.conf;
|
||||
|
||||
import cc.carm.lib.configuration.core.ConfigurationRoot;
|
||||
import cc.carm.lib.configuration.core.annotation.ConfigPath;
|
||||
import cc.carm.lib.configuration.core.annotation.HeaderComment;
|
||||
import cc.carm.lib.configuration.core.annotation.InlineComment;
|
||||
import cc.carm.lib.configuration.core.value.ConfigValue;
|
||||
import cc.carm.lib.configuration.core.value.type.ConfiguredSection;
|
||||
import cc.carm.lib.configuration.core.value.type.ConfiguredValue;
|
||||
import cc.carm.lib.configuration.demo.tests.model.TestModel;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class TestConfiguration extends ConfigurationRoot {
|
||||
|
||||
public final TestInnerConfiguration INNER = new TestInnerConfiguration();
|
||||
|
||||
public final ConfigValue<Double> CLASS_VALUE = ConfiguredValue.of(Double.class, 1.0D);
|
||||
|
||||
@ConfigPath("test.user") // 通过注解规定配置文件中的路径,若不进行注解则以变量名自动生成。
|
||||
@HeaderComment({"", "Section类型数据测试"}) // 通过注解给配置添加注释。
|
||||
@InlineComment("Section数据也支持InlineComment注释")
|
||||
public final ConfigValue<TestModel> TEST_MODEL = ConfiguredSection
|
||||
.builder(TestModel.class)
|
||||
.defaults(new TestModel("Carm", UUID.randomUUID()))
|
||||
.parseValue((section, defaultValue) -> TestModel.deserialize(section))
|
||||
.serializeValue(TestModel::serialize).build();
|
||||
|
||||
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package cc.carm.lib.configuration.demo.tests.conf;
|
||||
|
||||
import cc.carm.lib.configuration.core.ConfigurationRoot;
|
||||
import cc.carm.lib.configuration.core.annotation.HeaderComment;
|
||||
import cc.carm.lib.configuration.core.value.ConfigValue;
|
||||
import cc.carm.lib.configuration.core.value.type.ConfiguredValue;
|
||||
|
||||
@HeaderComment("Inner Test")
|
||||
public class TestInnerConfiguration extends ConfigurationRoot {
|
||||
|
||||
public final ConfigValue<Double> INNER_VALUE = ConfiguredValue.of(Double.class, 1.0D);
|
||||
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package config.model;
|
||||
package cc.carm.lib.configuration.demo.tests.model;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package config.model;
|
||||
package cc.carm.lib.configuration.demo.tests.model;
|
||||
|
||||
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
+11
-2
@@ -5,13 +5,15 @@
|
||||
<parent>
|
||||
<artifactId>easyconfiguration-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>3.2.0</version>
|
||||
<version>3.3.3</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<properties>
|
||||
<maven.compiler.source>${java.version}</maven.compiler.source>
|
||||
<maven.compiler.target>${java.version}</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
|
||||
</properties>
|
||||
<artifactId>easyconfiguration-json</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
@@ -25,10 +27,17 @@
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>${project.parent.groupId}</groupId>
|
||||
<artifactId>easyconfiguration-demo</artifactId>
|
||||
<version>${project.parent.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>2.9.1</version>
|
||||
<version>2.10.1</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
|
||||
@@ -1,67 +1,28 @@
|
||||
package config;
|
||||
|
||||
import cc.carm.lib.configuration.EasyConfiguration;
|
||||
import cc.carm.lib.configuration.demo.tests.ConfigurationTest;
|
||||
import cc.carm.lib.configuration.json.JSONConfigProvider;
|
||||
import config.model.TestModel;
|
||||
import config.source.DemoConfiguration;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class JSONConfigTest {
|
||||
|
||||
protected final JSONConfigProvider provider = EasyConfiguration.from("target/config.yml", "config.yml");
|
||||
protected final JSONConfigProvider provider = EasyConfiguration.from("target/config.json", "config.json");
|
||||
|
||||
|
||||
@Test
|
||||
public void onTest() {
|
||||
|
||||
provider.initialize(DemoConfiguration.class);
|
||||
ConfigurationTest.testDemo(this.provider);
|
||||
ConfigurationTest.testInner(this.provider);
|
||||
|
||||
testDemo();
|
||||
System.out.println("----------------------------------------------------");
|
||||
provider.getConfiguration().getValues(true).forEach((k, v) -> System.out.println(k + ": " + v));
|
||||
System.out.println("----------------------------------------------------");
|
||||
provider.getConfiguration().getValues(false).forEach((k, v) -> System.out.println(k + ": " + v));
|
||||
System.out.println("----------------------------------------------------");
|
||||
|
||||
try {
|
||||
provider.save();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void testDemo() {
|
||||
System.out.println("----------------------------------------------------");
|
||||
|
||||
System.out.println("> Test Value:");
|
||||
System.out.println("before: " + DemoConfiguration.Sub.UUID_CONFIG_VALUE.get());
|
||||
DemoConfiguration.Sub.UUID_CONFIG_VALUE.set(UUID.randomUUID());
|
||||
System.out.println("after: " + DemoConfiguration.Sub.UUID_CONFIG_VALUE.get());
|
||||
|
||||
System.out.println("> Test List:");
|
||||
DemoConfiguration.Sub.That.OPERATORS.getNotNull().forEach(System.out::println);
|
||||
List<UUID> operators = IntStream.range(0, 5).mapToObj(i -> UUID.randomUUID()).collect(Collectors.toList());
|
||||
DemoConfiguration.Sub.That.OPERATORS.set(operators);
|
||||
|
||||
System.out.println("> Test Section:");
|
||||
System.out.println(DemoConfiguration.MODEL_TEST.get());
|
||||
DemoConfiguration.MODEL_TEST.set(TestModel.random());
|
||||
|
||||
System.out.println("> Test Maps:");
|
||||
DemoConfiguration.USERS.getNotNull().forEach((k, v) -> System.out.println(k + ": " + v));
|
||||
LinkedHashMap<Integer, UUID> data = new LinkedHashMap<>();
|
||||
for (int i = 1; i <= 5; i++) {
|
||||
data.put(i, UUID.randomUUID());
|
||||
}
|
||||
DemoConfiguration.USERS.set(data);
|
||||
System.out.println("----------------------------------------------------");
|
||||
ConfigurationTest.save(this.provider);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,39 +0,0 @@
|
||||
package config.model;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.TestOnly;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class SomeModel extends AbstractModel {
|
||||
|
||||
public final int num;
|
||||
|
||||
public SomeModel(@NotNull String name, int num) {
|
||||
super(name);
|
||||
this.num = num;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SomeModel{" +
|
||||
"name='" + name + '\'' +
|
||||
", num=" + num +
|
||||
'}';
|
||||
}
|
||||
|
||||
public @NotNull Map<String, Object> serialize() {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("name", name);
|
||||
map.put("num", num);
|
||||
return map;
|
||||
}
|
||||
|
||||
@TestOnly
|
||||
public static SomeModel deserialize(Map<String, ?> args) {
|
||||
return new SomeModel((String) args.get("name"), (Integer) args.get("num"));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
package config.source;
|
||||
|
||||
import cc.carm.lib.configuration.core.ConfigurationRoot;
|
||||
import cc.carm.lib.configuration.core.annotation.HeaderComment;
|
||||
import cc.carm.lib.configuration.core.annotation.ConfigPath;
|
||||
import cc.carm.lib.configuration.core.value.ConfigValue;
|
||||
import cc.carm.lib.configuration.core.value.type.ConfiguredValue;
|
||||
|
||||
@HeaderComment({"数据库配置", " 用于提供数据库连接,进行数据库操作。"})
|
||||
public class DatabaseConfiguration extends ConfigurationRoot {
|
||||
|
||||
@ConfigPath("driver")
|
||||
protected static final ConfigValue<String> DRIVER_NAME = ConfiguredValue.of(
|
||||
String.class, "com.mysql.cj.jdbc.Driver"
|
||||
);
|
||||
|
||||
protected static final ConfigValue<String> HOST = ConfiguredValue.of(String.class, "127.0.0.1");
|
||||
protected static final ConfigValue<Integer> PORT = ConfiguredValue.of(Integer.class, 3306);
|
||||
|
||||
protected static final ConfigValue<String> DATABASE = ConfiguredValue.of(String.class, "minecraft");
|
||||
protected static final ConfigValue<String> USERNAME = ConfiguredValue.of(String.class, "root");
|
||||
protected static final ConfigValue<String> PASSWORD = ConfiguredValue.of(String.class, "password");
|
||||
|
||||
protected static final ConfigValue<String> EXTRA = ConfiguredValue.of(String.class, "?useSSL=false");
|
||||
|
||||
protected static String buildJDBC() {
|
||||
return String.format("jdbc:mysql://%s:%s/%s%s", HOST.get(), PORT.get(), DATABASE.get(), EXTRA.get());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,65 +0,0 @@
|
||||
package config.source;
|
||||
|
||||
import cc.carm.lib.configuration.core.ConfigurationRoot;
|
||||
import cc.carm.lib.configuration.core.annotation.ConfigPath;
|
||||
import cc.carm.lib.configuration.core.value.ConfigValue;
|
||||
import cc.carm.lib.configuration.core.value.type.ConfiguredList;
|
||||
import cc.carm.lib.configuration.core.value.type.ConfiguredMap;
|
||||
import cc.carm.lib.configuration.core.value.type.ConfiguredSection;
|
||||
import cc.carm.lib.configuration.core.value.type.ConfiguredValue;
|
||||
import config.model.TestModel;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
|
||||
public class DemoConfiguration extends ConfigurationRoot {
|
||||
|
||||
@ConfigPath(root = true)
|
||||
protected static final ConfigValue<Double> VERSION = ConfiguredValue.of(Double.class, 1.0D);
|
||||
|
||||
// 可以直接写静态内部类,并通过 Class<?> 声明。
|
||||
public static final Class<?> SUB_TEST = Sub.class;
|
||||
|
||||
@ConfigPath("user") // 通过注解规定配置文件中的路径,若不进行注解则以变量名自动生成。
|
||||
public static final ConfigValue<TestModel> MODEL_TEST = ConfiguredSection
|
||||
.builder(TestModel.class)
|
||||
.defaults(new TestModel("Carm", UUID.randomUUID()))
|
||||
.parseValue((section, defaultValue) -> TestModel.deserialize(section))
|
||||
.serializeValue(TestModel::serialize).build();
|
||||
|
||||
// 子配置文件
|
||||
@ConfigPath("database")
|
||||
public static final Class<?> DB_CONFIG = DatabaseConfiguration.class;
|
||||
|
||||
public static final ConfigValue<Map<Integer, UUID>> USERS = ConfiguredMap
|
||||
.builder(Integer.class, UUID.class).fromString()
|
||||
.parseKey(Integer::parseInt)
|
||||
.parseValue(v -> Objects.requireNonNull(UUID.fromString(v)))
|
||||
.build();
|
||||
|
||||
|
||||
public static class Sub extends ConfigurationRoot {
|
||||
|
||||
@ConfigPath(value = "uuid-value", root = true)
|
||||
public static final ConfigValue<UUID> UUID_CONFIG_VALUE = ConfiguredValue
|
||||
.builder(UUID.class).fromString()
|
||||
.parseValue((data, defaultValue) -> UUID.fromString(data))
|
||||
.build();
|
||||
|
||||
public static final Class<?> NOTHING = That.class;
|
||||
|
||||
public static class That extends ConfigurationRoot {
|
||||
|
||||
public static final ConfigValue<List<UUID>> OPERATORS = ConfiguredList
|
||||
.builder(UUID.class).fromString()
|
||||
.parseValue(s -> Objects.requireNonNull(UUID.fromString(s)))
|
||||
.build();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+5
-3
@@ -5,13 +5,17 @@
|
||||
<parent>
|
||||
<artifactId>easyconfiguration-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>3.2.0</version>
|
||||
<version>3.3.3</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<properties>
|
||||
<maven.compiler.source>${java.version}</maven.compiler.source>
|
||||
<maven.compiler.target>${java.version}</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
|
||||
|
||||
<maven.deploy.skip>true</maven.deploy.skip>
|
||||
</properties>
|
||||
<artifactId>easyconfiguration-sql</artifactId>
|
||||
|
||||
@@ -24,8 +28,6 @@
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
+11
-2
@@ -5,13 +5,15 @@
|
||||
<parent>
|
||||
<artifactId>easyconfiguration-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>3.2.0</version>
|
||||
<version>3.3.3</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<properties>
|
||||
<maven.compiler.source>${java.version}</maven.compiler.source>
|
||||
<maven.compiler.target>${java.version}</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
|
||||
</properties>
|
||||
|
||||
<artifactId>easyconfiguration-yaml</artifactId>
|
||||
@@ -26,10 +28,17 @@
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>${project.parent.groupId}</groupId>
|
||||
<artifactId>easyconfiguration-demo</artifactId>
|
||||
<version>${project.parent.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.bspfsystems</groupId>
|
||||
<artifactId>yamlconfiguration</artifactId>
|
||||
<version>1.2.1</version>
|
||||
<version>1.3.1</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
|
||||
@@ -1,79 +1,45 @@
|
||||
package config;
|
||||
|
||||
import cc.carm.lib.configuration.EasyConfiguration;
|
||||
import cc.carm.lib.configuration.demo.tests.ConfigurationTest;
|
||||
import cc.carm.lib.configuration.demo.tests.model.AbstractModel;
|
||||
import cc.carm.lib.configuration.yaml.YAMLConfigProvider;
|
||||
import config.model.AbstractModel;
|
||||
import config.model.AnyModel;
|
||||
import config.model.SomeModel;
|
||||
import config.model.TestModel;
|
||||
import config.source.DemoConfiguration;
|
||||
import config.source.ImplConfiguration;
|
||||
import config.source.ModelConfiguration;
|
||||
import org.bspfsystems.yamlconfiguration.serialization.ConfigurationSerialization;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class DemoConfigTest {
|
||||
|
||||
static {
|
||||
ConfigurationSerialization.registerClass(TestModel.class);
|
||||
ConfigurationSerialization.registerClass(SomeModel.class);
|
||||
ConfigurationSerialization.registerClass(AnyModel.class);
|
||||
}
|
||||
|
||||
protected final YAMLConfigProvider provider = EasyConfiguration.from("target/config.yml", "config.yml");
|
||||
|
||||
@Test
|
||||
public void onTest() {
|
||||
provider.initialize(DemoConfiguration.class);
|
||||
ConfigurationTest.testDemo(this.provider);
|
||||
ConfigurationTest.testInner(this.provider);
|
||||
|
||||
testDemo();
|
||||
testSerialization();
|
||||
|
||||
try {
|
||||
provider.save();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
testSerialization(this.provider);
|
||||
|
||||
ConfigurationTest.save(this.provider);
|
||||
}
|
||||
|
||||
|
||||
public static void testDemo() {
|
||||
public static void testSerialization(YAMLConfigProvider provider) {
|
||||
provider.initialize(ModelConfiguration.class);
|
||||
System.out.println("----------------------------------------------------");
|
||||
|
||||
System.out.println("> Test Value:");
|
||||
System.out.println("before: " + DemoConfiguration.Sub.UUID_CONFIG_VALUE.get());
|
||||
DemoConfiguration.Sub.UUID_CONFIG_VALUE.set(UUID.randomUUID());
|
||||
System.out.println("after: " + DemoConfiguration.Sub.UUID_CONFIG_VALUE.get());
|
||||
AbstractModel someModel = ModelConfiguration.SOME_MODEL.get();
|
||||
if (someModel != null) System.out.println(someModel.getName());
|
||||
|
||||
System.out.println("> Test List:");
|
||||
DemoConfiguration.Sub.That.OPERATORS.getNotNull().forEach(System.out::println);
|
||||
List<UUID> operators = IntStream.range(0, 5).mapToObj(i -> UUID.randomUUID()).collect(Collectors.toList());
|
||||
DemoConfiguration.Sub.That.OPERATORS.set(operators);
|
||||
AbstractModel anyModel = ModelConfiguration.ANY_MODEL.get();
|
||||
if (anyModel != null) System.out.println(anyModel.getName());
|
||||
|
||||
System.out.println("> Test Section:");
|
||||
System.out.println(DemoConfiguration.MODEL_TEST.get());
|
||||
DemoConfiguration.MODEL_TEST.set(TestModel.random());
|
||||
|
||||
System.out.println("> Test Maps:");
|
||||
DemoConfiguration.USERS.getNotNull().forEach((k, v) -> System.out.println(k + ": " + v));
|
||||
LinkedHashMap<Integer, UUID> data = new LinkedHashMap<>();
|
||||
for (int i = 1; i <= 5; i++) {
|
||||
data.put(i, UUID.randomUUID());
|
||||
}
|
||||
DemoConfiguration.USERS.set(data);
|
||||
System.out.println("----------------------------------------------------");
|
||||
}
|
||||
|
||||
public static void testSerialization() {
|
||||
System.out.println("----------------------------------------------------");
|
||||
AbstractModel model = ImplConfiguration.TEST.get();
|
||||
if (model != null) {
|
||||
System.out.println(model.getName());
|
||||
}
|
||||
System.out.println("----------------------------------------------------");
|
||||
}
|
||||
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
package config;
|
||||
|
||||
import config.offset.FieldOffset;
|
||||
import config.offset.OffsetUtil;
|
||||
import config.source.DemoConfiguration;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class OffsetTest {
|
||||
|
||||
// @Test
|
||||
public void test() {
|
||||
//
|
||||
output(OffsetUtil.getClassMemberOffset(DemoConfiguration.class));
|
||||
output(OffsetUtil.getClassMemberOffset(DemoConfiguration.Sub.class));
|
||||
|
||||
}
|
||||
|
||||
protected static void output(List<FieldOffset> fieldOffsets) {
|
||||
for (FieldOffset fieldOffset : fieldOffsets) {
|
||||
System.out.println(fieldOffset.getOffsetValue() + " -> " + fieldOffset);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
package config.model;
|
||||
|
||||
import org.bspfsystems.yamlconfiguration.serialization.ConfigurationSerializable;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public abstract class AbstractModel implements ConfigurationSerializable {
|
||||
|
||||
protected final @NotNull String name;
|
||||
|
||||
public AbstractModel(@NotNull String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public @NotNull String getName() {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package config.model;
|
||||
|
||||
import cc.carm.lib.configuration.demo.tests.model.AbstractModel;
|
||||
import org.bspfsystems.yamlconfiguration.serialization.ConfigurationSerializable;
|
||||
import org.bspfsystems.yamlconfiguration.serialization.SerializableAs;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.TestOnly;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
@SerializableAs("AnyModel")
|
||||
public class AnyModel extends AbstractModel implements ConfigurationSerializable {
|
||||
|
||||
public final boolean bool;
|
||||
|
||||
public AnyModel(@NotNull String name, boolean bool) {
|
||||
super(name);
|
||||
this.bool = bool;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "AnyModel{" +
|
||||
"name='" + name + '\'' +
|
||||
", bool=" + bool +
|
||||
'}';
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Map<String, Object> serialize() {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("name", name);
|
||||
map.put("state", bool);
|
||||
return map;
|
||||
}
|
||||
|
||||
public static AnyModel random() {
|
||||
return new AnyModel(UUID.randomUUID().toString().substring(0, 5), Math.random() > 0.5);
|
||||
}
|
||||
|
||||
|
||||
@TestOnly
|
||||
public static AnyModel deserialize(Map<String, ?> args) {
|
||||
return new AnyModel((String) args.get("name"), (Boolean) args.get("state"));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package config.model;
|
||||
|
||||
import cc.carm.lib.configuration.demo.tests.model.AbstractModel;
|
||||
import org.bspfsystems.yamlconfiguration.serialization.ConfigurationSerializable;
|
||||
import org.bspfsystems.yamlconfiguration.serialization.SerializableAs;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -7,6 +8,7 @@ import org.jetbrains.annotations.TestOnly;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
@SerializableAs("SomeModel")
|
||||
public class SomeModel extends AbstractModel implements ConfigurationSerializable {
|
||||
@@ -34,6 +36,11 @@ public class SomeModel extends AbstractModel implements ConfigurationSerializabl
|
||||
return map;
|
||||
}
|
||||
|
||||
public static SomeModel random() {
|
||||
return new SomeModel(UUID.randomUUID().toString().substring(0, 5), (int) (Math.random() * 1000));
|
||||
}
|
||||
|
||||
|
||||
@TestOnly
|
||||
public static SomeModel deserialize(Map<String, ?> args) {
|
||||
return new SomeModel((String) args.get("name"), (Integer) args.get("num"));
|
||||
|
||||
@@ -1,72 +0,0 @@
|
||||
package config.model;
|
||||
|
||||
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
|
||||
import org.bspfsystems.yamlconfiguration.serialization.ConfigurationSerializable;
|
||||
import org.bspfsystems.yamlconfiguration.serialization.SerializableAs;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.TestOnly;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
@SerializableAs("TestModel")
|
||||
public class TestModel extends AbstractModel implements ConfigurationSerializable {
|
||||
|
||||
public UUID uuid;
|
||||
|
||||
public TestModel(String name, UUID uuid) {
|
||||
super(name);
|
||||
this.uuid = uuid;
|
||||
}
|
||||
|
||||
public void setUuid(UUID uuid) {
|
||||
this.uuid = uuid;
|
||||
}
|
||||
|
||||
public UUID getUuid() {
|
||||
return uuid;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Map<String, Object> serialize() {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("name", name);
|
||||
Map<String, Object> map2 = new HashMap<>();
|
||||
map2.put("uuid", uuid.toString());
|
||||
map.put("info", map2);
|
||||
return map;
|
||||
}
|
||||
|
||||
public static TestModel deserialize(ConfigurationWrapper<?> section) {
|
||||
String name = section.getString("name");
|
||||
if (name == null) throw new NullPointerException("name is null");
|
||||
String uuidString = section.getString("info.uuid");
|
||||
if (uuidString == null) throw new NullPointerException("uuid is null");
|
||||
return new TestModel(name, UUID.fromString(uuidString));
|
||||
}
|
||||
|
||||
@TestOnly
|
||||
@SuppressWarnings("unchecked")
|
||||
public static TestModel deserialize(Map<String, ?> args) {
|
||||
String name = (String) args.get("name");
|
||||
if (name == null) throw new NullPointerException("name is null");
|
||||
Map<String, ?> map = (Map<String, ?>) args.get("info");
|
||||
String uuidString = (String) map.get("uuid");
|
||||
if (uuidString == null) throw new NullPointerException("uuid is null");
|
||||
return new TestModel(name, UUID.fromString(uuidString));
|
||||
}
|
||||
|
||||
public static TestModel random() {
|
||||
return new TestModel(UUID.randomUUID().toString().substring(0, 5), UUID.randomUUID());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "TestUser{" +
|
||||
"name='" + name + '\'' +
|
||||
", uuid=" + uuid +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
package config.offset;
|
||||
|
||||
|
||||
import sun.misc.Unsafe;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Chris2018998
|
||||
*/
|
||||
public class OffsetUtil {
|
||||
private static Unsafe unsafe;
|
||||
|
||||
static {
|
||||
try {
|
||||
// 获取 Unsafe 内部的私有的实例化单例对象
|
||||
Field field = Unsafe.class.getDeclaredField("theUnsafe");
|
||||
// 无视权限
|
||||
field.setAccessible(true);
|
||||
unsafe = (Unsafe) field.get(null);
|
||||
} catch (NoSuchFieldException | IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
// try {
|
||||
// unsafe = AccessController.doPrivileged((PrivilegedExceptionAction<Unsafe>) () -> {
|
||||
// Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
|
||||
// theUnsafe.setAccessible(true);
|
||||
// return (Unsafe) theUnsafe.get(null);
|
||||
// });
|
||||
// } catch (Throwable e) {
|
||||
// System.err.println("Unable to load unsafe");
|
||||
// }
|
||||
}
|
||||
|
||||
public static List<FieldOffset> getClassMemberOffset(Class<?> beanClass) {
|
||||
List<FieldOffset> offsetsList = new LinkedList<>();
|
||||
for (Field field : beanClass.getDeclaredFields()) {
|
||||
FieldOffset fieldOffset = new FieldOffset(field);
|
||||
offsetsList.add(fieldOffset);
|
||||
if (Modifier.isStatic(field.getModifiers()))
|
||||
fieldOffset.setOffsetValue(unsafe.staticFieldOffset(field));
|
||||
else
|
||||
fieldOffset.setOffsetValue(unsafe.objectFieldOffset(field));
|
||||
Class<?> fieldType = field.getType();
|
||||
if (!fieldType.getName().startsWith("java")) {
|
||||
Field[] subfields = fieldType.getDeclaredFields();
|
||||
if (subfields.length > 0) {
|
||||
fieldOffset.setSubFieldOffsetList(getClassMemberOffset(fieldType));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Collections.sort(offsetsList);
|
||||
return offsetsList;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
package config.source;
|
||||
|
||||
import cc.carm.lib.configuration.core.ConfigurationRoot;
|
||||
import cc.carm.lib.configuration.core.annotation.ConfigPath;
|
||||
import cc.carm.lib.configuration.core.value.ConfigValue;
|
||||
import cc.carm.lib.configuration.yaml.value.ConfiguredSerializable;
|
||||
import config.model.AbstractModel;
|
||||
import config.model.TestModel;
|
||||
|
||||
@ConfigPath(root = true)
|
||||
public class ImplConfiguration extends ConfigurationRoot {
|
||||
|
||||
public static final ConfigValue<? extends AbstractModel> TEST = ConfiguredSerializable.of(
|
||||
TestModel.class, TestModel.random()
|
||||
);
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package config.source;
|
||||
|
||||
import cc.carm.lib.configuration.core.ConfigurationRoot;
|
||||
import cc.carm.lib.configuration.core.annotation.ConfigPath;
|
||||
import cc.carm.lib.configuration.core.value.ConfigValue;
|
||||
import cc.carm.lib.configuration.demo.tests.model.AbstractModel;
|
||||
import cc.carm.lib.configuration.yaml.value.ConfiguredSerializable;
|
||||
import config.model.AnyModel;
|
||||
import config.model.SomeModel;
|
||||
|
||||
@ConfigPath("model-test")
|
||||
public class ModelConfiguration extends ConfigurationRoot {
|
||||
|
||||
public static final ConfigValue<? extends AbstractModel> SOME_MODEL = ConfiguredSerializable.of(
|
||||
SomeModel.class, SomeModel.random()
|
||||
);
|
||||
|
||||
public static final ConfigValue<? extends AbstractModel> ANY_MODEL = ConfiguredSerializable.of(
|
||||
AnyModel.class, AnyModel.random()
|
||||
);
|
||||
|
||||
}
|
||||
@@ -15,9 +15,10 @@
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<artifactId>easyconfiguration-parent</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
<version>3.2.0</version>
|
||||
<version>3.3.3</version>
|
||||
<modules>
|
||||
<module>core</module>
|
||||
<module>demo</module>
|
||||
<module>impl/yaml</module>
|
||||
<module>impl/json</module>
|
||||
<module>impl/sql</module>
|
||||
@@ -108,7 +109,7 @@
|
||||
<dependency>
|
||||
<groupId>org.jetbrains</groupId>
|
||||
<artifactId>annotations</artifactId>
|
||||
<version>23.0.0</version>
|
||||
<version>24.0.0</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
@@ -204,7 +205,7 @@
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>3.2.2</version>
|
||||
<version>3.3.0</version>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
@@ -224,7 +225,7 @@
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>3.3.0</version>
|
||||
<version>3.4.1</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
|
||||
Reference in New Issue
Block a user