Skip to content

Commit

Permalink
Config framework.
Browse files Browse the repository at this point in the history
  • Loading branch information
cao-awa committed May 23, 2024
1 parent 0f84c1a commit d46a0eb
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ class ConfigFramework : ReflectionFramework() {

val inTemplateFileKey = if (useTemplate.equals("")) field.name else useTemplate.value

val creatingWithTemplate = {
val creatingWithTemplate = { parameterized: Boolean ->
// 当依赖是数据而不是Entry

// 读取模板获得默认值
Expand All @@ -633,34 +633,46 @@ class ConfigFramework : ReflectionFramework() {
template, parentTemplate,
currentKey, inTemplateFileKey,
argType,
field
field,
parameterized
)

val argActualizedType =
if (parameterized)
toClass((argType as ParameterizedType).rawType)
else
toClass(argType)


// 当值存在时则设定
if (value != null) {
checkType(
toClass(argType), value
argActualizedType, value
) { fetchField(configEntry, "value")[configEntry] = it }
}
}

val creatingWithTemplateNoParameterized = { creatingWithTemplate(false) }

postProcessing(
target,
configEntry,
argType,
configChain,
// 处理泛型的方式和处理普通数据一样
{ creatingWithTemplate() },
{
// 处理泛型数据
creatingWithTemplate(true)
},
{
// 当依赖是Entry而不是数据

// 处理此配置的依赖
// 配置对象内所有字段都应为ConfigEntry<KalmiaConfig>
// 配置对象内所有字段都应为ConfigEntry<LiliumConfig>
val config = configEntry.get() as KalmiaConfig
createConfig(config, configEntry.key()!!, template ?: getTemplate(config), configChain)
},
// 处理普通数据
creatingWithTemplate
creatingWithTemplateNoParameterized
)
}
}
Expand All @@ -672,10 +684,15 @@ class ConfigFramework : ReflectionFramework() {
currentKey: String,
inTemplateFileKey: String,
argType: Type,
field: Field
field: Field,
parameterized: Boolean
): Any? {
return if (template != null) {
val requiredType = toClass(argType)
val requiredType =
if (parameterized)
toClass((argType as ParameterizedType).rawType)
else
toClass(argType)

// 首先从当前配置模板中获取
val fetchedTemplate = fetchField(template, field.name)[template]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,21 +215,45 @@ public static Class<?> toClass(Type type) {
return (Class<?>) type;
}

public static void checkTypeExact(Class<?> target, Object object, Consumer<Object> consumer) {
if (object == null) {
return;
}
if (target != object.getClass()) {
Object casted = Manipulate.action(() -> SPECIALLY_CAST.get(object.getClass())
.get(target)
.apply(object))
.get();
if (casted == null) {
throw new ClassCastException("The parameter has specified '" + target + "' but got '" + object.getClass() + "'");
}
consumer.accept(casted);
} else {
consumer.accept(object);
}
}

public static void checkType(Class<?> target, Object object, Consumer<Object> consumer) {
if (object == null) {
return;
}
if (target != object.getClass()) {
Object casted = Manipulate.supply(() -> SPECIALLY_CAST.get(object.getClass())
Object casted = Manipulate.action(() -> SPECIALLY_CAST.get(object.getClass())
.get(target)
.apply(object))
.get();
if (casted == null) {
if (target.isAssignableFrom(object.getClass())) {
casted = object;
}
}
if (casted == null) {
throw new ClassCastException("The parameter has specified '" + target + "' but got '" + object.getClass() + "'");
}
consumer.accept(casted);
} else {
consumer.accept(object);
}
consumer.accept(object);
}

public static <T> T checkOrDiscard(Class<?> target, T object) {
Expand All @@ -239,13 +263,20 @@ public static <T> T checkOrDiscard(Class<?> target, T object) {
return object;
}

public static <T> T checkOrDiscard(Class<?> target, T object, Class<?> except) {
public static <T> T checkOrDiscardExact(Class<?> target, T object, Class<?> except) {
if ((object == null || (target != object.getClass() && object.getClass() != except))) {
return null;
}
return object;
}

public static <T> T checkOrDiscard(Class<?> target, T object, Class<?> except) {
if ((object == null || (! target.isAssignableFrom(object.getClass()) && ! except.isAssignableFrom(object.getClass())))) {
return null;
}
return object;
}

public static Type getArgType(Field field) {
return getArgType(field.getGenericType());
}
Expand Down

0 comments on commit d46a0eb

Please sign in to comment.