mirror of
https://github.com/CarmJos/EasyConfiguration.git
synced 2026-06-04 18:48:20 +08:00
[1.0.6] 版本修复
- `[F]` 修复protected类型参数无法被正常初始化的问题。
This commit is contained in:
@@ -18,18 +18,26 @@ public class ConfigInitializer {
|
||||
}
|
||||
|
||||
public static void initialize(ConfigurationProvider provider, Class<? extends ConfigurationRoot> rootClazz, boolean saveDefault) {
|
||||
ConfigPath sectionAnnotation = rootClazz.getAnnotation(ConfigPath.class);
|
||||
|
||||
String rootSection = null;
|
||||
|
||||
ConfigPath sectionAnnotation = rootClazz.getAnnotation(ConfigPath.class);
|
||||
if (sectionAnnotation != null && sectionAnnotation.value().length() > 1) {
|
||||
rootSection = sectionAnnotation.value();
|
||||
}
|
||||
|
||||
if (rootSection != null) {
|
||||
//Not usable for null section.
|
||||
ConfigComment comments = rootClazz.getAnnotation(ConfigComment.class);
|
||||
if (comments != null && comments.value().length > 0) {
|
||||
provider.setComments(rootSection, comments.value());
|
||||
}
|
||||
}
|
||||
|
||||
for (Class<?> innerClass : rootClazz.getDeclaredClasses()) {
|
||||
initSection(provider, rootSection, innerClass, saveDefault);
|
||||
}
|
||||
|
||||
for (Field field : rootClazz.getFields()) {
|
||||
for (Field field : rootClazz.getDeclaredFields()) {
|
||||
initValue(provider, rootSection, rootClazz, field, saveDefault);
|
||||
}
|
||||
|
||||
@@ -52,21 +60,22 @@ public class ConfigInitializer {
|
||||
provider.setComments(parentSection, comments.value());
|
||||
}
|
||||
|
||||
for (Field field : clazz.getFields()) initValue(provider, section, clazz, field, saveDefault);
|
||||
for (Field field : clazz.getDeclaredFields()) initValue(provider, section, clazz, field, saveDefault);
|
||||
for (Class<?> innerClass : clazz.getDeclaredClasses()) initSection(provider, section, innerClass, saveDefault);
|
||||
|
||||
}
|
||||
|
||||
private static void initValue(ConfigurationProvider provider, String parentSection, Class<?> clazz, Field field, boolean saveDefault) {
|
||||
try {
|
||||
field.setAccessible(true);
|
||||
Object object = field.get(clazz);
|
||||
if (object instanceof ConfigValue<?>) {
|
||||
initializeValue(
|
||||
provider, (ConfigValue<?>) object,
|
||||
provider, (ConfigValue<?>) object, saveDefault,
|
||||
getSectionPath(field.getName(), parentSection, field.getAnnotation(ConfigPath.class)),
|
||||
Optional.ofNullable(field.getAnnotation(ConfigComment.class))
|
||||
.map(ConfigComment::value).orElse(new String[0]),
|
||||
saveDefault);
|
||||
.map(ConfigComment::value).orElse(new String[0])
|
||||
);
|
||||
}
|
||||
} catch (IllegalAccessException ignored) {
|
||||
}
|
||||
@@ -74,7 +83,7 @@ public class ConfigInitializer {
|
||||
|
||||
|
||||
public static void initializeValue(@NotNull ConfigurationProvider provider, @NotNull ConfigValue<?> value,
|
||||
@NotNull String path, @NotNull String[] comments, boolean saveDefault) {
|
||||
boolean saveDefault, @NotNull String path, @NotNull String[] comments) {
|
||||
value.initialize(provider, path, comments);
|
||||
if (saveDefault && value.getDefaultValue() != null && !provider.getConfiguration().contains(path)) {
|
||||
value.setDefault();
|
||||
@@ -84,16 +93,31 @@ public class ConfigInitializer {
|
||||
public static String getSectionPath(@NotNull String name,
|
||||
@Nullable String parentSection,
|
||||
@Nullable ConfigPath pathAnnotation) {
|
||||
String parent = parentSection != null ? parentSection + "." : "";
|
||||
if (pathAnnotation != null && pathAnnotation.value().length() > 0) {
|
||||
return parent + pathAnnotation.value();
|
||||
} else {
|
||||
return parent + getSectionName(name);
|
||||
@NotNull String parent = parentSection != null ? parentSection + "." : "";
|
||||
@NotNull String path = getSectionName(name);
|
||||
boolean root = false;
|
||||
if (pathAnnotation != null) {
|
||||
if (pathAnnotation.value().length() > 0) path = pathAnnotation.value();
|
||||
root = pathAnnotation.root();
|
||||
}
|
||||
return (root ? "" : parent) + path;
|
||||
}
|
||||
|
||||
public static String getSectionName(String codeName) {
|
||||
return codeName.toLowerCase().replace("_", "-");
|
||||
/**
|
||||
* 得到指定元素的配置名称。
|
||||
* 采用 全小写,以“-”链接 的命名规则。
|
||||
*
|
||||
* @param name 源名称
|
||||
* @return 全小写,以“-”链接 的 路径名称
|
||||
*/
|
||||
public static String getSectionName(String name) {
|
||||
return name.replaceAll("[A-Z]", "-$0") // 将驼峰转换为蛇形;
|
||||
.replaceAll("-(.*)", "$1") // 若首字母也为大写,则也会被转换,需要去掉第一个横线
|
||||
.replaceAll("_-([A-Z])", "_$1") // 因为命名中可能包含 _,因此需要被特殊处理一下
|
||||
.replaceAll("([a-z])-([A-Z])", "$1_$2") // 然后将非全大写命名的内容进行转换
|
||||
.replace("-", "") // 移除掉多余的横线
|
||||
.replace("_", "-") // 将下划线替换为横线
|
||||
.toLowerCase(); // 最后转为全小写
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,14 +1,34 @@
|
||||
package cc.carm.lib.configuration.core.annotation;
|
||||
|
||||
import cc.carm.lib.configuration.core.ConfigInitializer;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* 用于标记对应类或参数的配置路径
|
||||
*/
|
||||
@Target({ElementType.TYPE, ElementType.FIELD})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface ConfigPath {
|
||||
|
||||
String value();
|
||||
/**
|
||||
* 指定路径的值。
|
||||
* 若不指定,则会通过 {@link ConfigInitializer#getSectionName(String)}自动生成当前路径的值。
|
||||
*
|
||||
* @return 路径的值
|
||||
*/
|
||||
String value() default "";
|
||||
|
||||
/**
|
||||
* 是否从根节点开始。
|
||||
* <br>若为 false,则会自动添加上一个路径(如果有)到本节点的路径。
|
||||
* <br>若为 true,则会从根节点开始直接设置本路径。
|
||||
*
|
||||
* @return 是否不继承上一路径,直接从根路径为开始
|
||||
*/
|
||||
boolean root() default false;
|
||||
|
||||
}
|
||||
|
||||
+12
-7
@@ -22,17 +22,22 @@ public abstract class FileConfigProvider extends ConfigurationProvider {
|
||||
}
|
||||
|
||||
public void initializeFile(@Nullable String sourcePath) throws IOException {
|
||||
if (getFile().exists()) return;
|
||||
if (!getFile().getParentFile().exists() && !getFile().getParentFile().mkdirs()) {
|
||||
if (this.file.exists()) return;
|
||||
|
||||
File parent = this.file.getParentFile();
|
||||
if (parent != null && !parent.exists() && !parent.mkdirs()) {
|
||||
throw new IOException("Failed to create directory " + file.getParentFile().getAbsolutePath());
|
||||
}
|
||||
if (!getFile().createNewFile()) {
|
||||
|
||||
if (!this.file.createNewFile()) {
|
||||
throw new IOException("Failed to create file " + file.getAbsolutePath());
|
||||
}
|
||||
if (sourcePath == null) return;
|
||||
try {
|
||||
saveResource(sourcePath, true);
|
||||
} catch (Exception ignored) {
|
||||
|
||||
if (sourcePath != null) {
|
||||
try {
|
||||
saveResource(sourcePath, true);
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
package cc.carm.lib.configuration.core.value.type;
|
||||
|
||||
import cc.carm.lib.configuration.core.builder.ConfigBuilder;
|
||||
import cc.carm.lib.configuration.core.builder.list.ConfigListBuilder;
|
||||
import cc.carm.lib.configuration.core.function.ConfigDataFunction;
|
||||
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
||||
import cc.carm.lib.configuration.core.value.CachedConfigValue;
|
||||
import cc.carm.lib.configuration.core.value.ConfigValue;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
import cc.carm.lib.configuration.core.ConfigInitializer;
|
||||
import org.junit.Test;
|
||||
|
||||
public class NameTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void onTest() {
|
||||
|
||||
System.out.println(ConfigInitializer.getSectionName("LoveGames")); // -> love-games
|
||||
System.out.println(ConfigInitializer.getSectionName("EASY_GAME")); // -> easy-game
|
||||
System.out.println(ConfigInitializer.getSectionName("F")); //-? f
|
||||
System.out.println(ConfigInitializer.getSectionName("Test123123")); // -? test123123123
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user