[config-reflect] Utilize commons lamda gen
This commit is contained in:
parent
e15aef38c1
commit
5c1b2586a8
|
@ -15,7 +15,7 @@ curseforge_id=482600
|
|||
curseforge_optional_dependencies=fabric-api
|
||||
|
||||
fabric_version=0.60.0+1.19.2
|
||||
commons_version=2022.9.25+10-22-41
|
||||
commons_version=2022.9.26+17-56-16
|
||||
modmenu_version=4.0.6
|
||||
|
||||
asm_version=9.4
|
||||
|
|
|
@ -54,22 +54,14 @@ public record ConfigHolderImpl(io.gitlab.jfronny.libjf.config.api.v1.ConfigHolde
|
|||
}
|
||||
builder.addPreset(ReflectiveConfigBuilderImpl.CONFIG_PRESET_DEFAULT, ConfigCategory::reset);
|
||||
for (Method method : klazz.getMethods()) {
|
||||
if (method.isAnnotationPresent(Preset.class)) {
|
||||
builder.addPreset(builder.getTranslationPrefix() + method.getName(), c -> {
|
||||
try {
|
||||
method.invoke(null);
|
||||
} catch (InvocationTargetException | IllegalAccessException e) {
|
||||
LibJf.LOGGER.error("Could not apply preset", e);
|
||||
}
|
||||
});
|
||||
} else if (method.isAnnotationPresent(Verifier.class)) {
|
||||
builder.addVerifier(c -> {
|
||||
try {
|
||||
method.invoke(null);
|
||||
} catch (IllegalAccessException | InvocationTargetException e) {
|
||||
LibJf.LOGGER.error("Could not run verifier", e);
|
||||
}
|
||||
});
|
||||
try {
|
||||
if (method.isAnnotationPresent(Preset.class)) {
|
||||
builder.addPreset(builder.getTranslationPrefix() + method.getName(), ReflectiveConfigBuilderImpl.<ConfigCategory>staticToConsumer(klazz, method).addHandler(e -> LibJf.LOGGER.error("Could not apply preset", e)));
|
||||
} else if (method.isAnnotationPresent(Verifier.class)) {
|
||||
builder.addVerifier(ReflectiveConfigBuilderImpl.<ConfigCategory>staticToConsumer(klazz, method).addHandler(e -> LibJf.LOGGER.error("Could not run verifier", e)));
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
LibJf.LOGGER.error("Could not process method " + method.getName() + " of config class " + klazz.getName());
|
||||
}
|
||||
}
|
||||
for (Class<?> category : klazz.getClasses()) {
|
||||
|
|
|
@ -1,15 +1,17 @@
|
|||
package io.gitlab.jfronny.libjf.config.impl.reflect;
|
||||
|
||||
import io.gitlab.jfronny.commons.reflect.Reflect;
|
||||
import io.gitlab.jfronny.commons.throwable.ThrowingConsumer;
|
||||
import io.gitlab.jfronny.libjf.LibJf;
|
||||
import io.gitlab.jfronny.libjf.config.api.v1.*;
|
||||
import io.gitlab.jfronny.libjf.config.api.v1.dsl.CategoryBuilder;
|
||||
import io.gitlab.jfronny.libjf.config.api.v1.dsl.ConfigBuilder;
|
||||
import io.gitlab.jfronny.libjf.config.api.v1.reflect.ReflectiveConfigBuilder;
|
||||
import io.gitlab.jfronny.libjf.config.api.v1.type.Type;
|
||||
import io.gitlab.jfronny.libjf.config.impl.AuxiliaryMetadata;
|
||||
import io.gitlab.jfronny.libjf.config.impl.dsl.DslEntryInfo;
|
||||
|
||||
import java.lang.reflect.*;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Objects;
|
||||
|
||||
public class ReflectiveConfigBuilderImpl implements ReflectiveConfigBuilder {
|
||||
|
@ -37,22 +39,14 @@ public class ReflectiveConfigBuilderImpl implements ReflectiveConfigBuilder {
|
|||
}
|
||||
builder.addPreset(CONFIG_PRESET_DEFAULT, ConfigCategory::reset);
|
||||
for (Method method : configClass.getMethods()) {
|
||||
if (method.isAnnotationPresent(Preset.class)) {
|
||||
builder.addPreset(builder.getTranslationPrefix() + method.getName(), c -> {
|
||||
try {
|
||||
method.invoke(null);
|
||||
} catch (InvocationTargetException | IllegalAccessException e) {
|
||||
LibJf.LOGGER.error("Could not apply preset", e);
|
||||
}
|
||||
});
|
||||
} else if (method.isAnnotationPresent(Verifier.class)) {
|
||||
builder.addVerifier(c -> {
|
||||
try {
|
||||
method.invoke(null);
|
||||
} catch (InvocationTargetException | IllegalAccessException e) {
|
||||
LibJf.LOGGER.error("Could not run verifier", e);
|
||||
}
|
||||
});
|
||||
try {
|
||||
if (method.isAnnotationPresent(Preset.class)) {
|
||||
builder.addPreset(builder.getTranslationPrefix() + method.getName(), ReflectiveConfigBuilderImpl.<ConfigCategory>staticToConsumer(configClass, method).addHandler(e -> LibJf.LOGGER.error("Could not apply preset", e)));
|
||||
} else if (method.isAnnotationPresent(Verifier.class)) {
|
||||
builder.addVerifier(ReflectiveConfigBuilderImpl.<ConfigCategory>staticToConsumer(configClass, method).addHandler(e -> LibJf.LOGGER.error("Could not run verifier", e)));
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
LibJf.LOGGER.error("Could not process method " + method.getName() + " of config class " + configClass.getName());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -66,4 +60,9 @@ public class ReflectiveConfigBuilderImpl implements ReflectiveConfigBuilder {
|
|||
|
||||
return builder;
|
||||
}
|
||||
|
||||
public static <T> ThrowingConsumer<T, Throwable> staticToConsumer(Class<?> klazz, Method method) throws Throwable {
|
||||
Runnable rn = Reflect.staticProcedure(klazz, method.getName());
|
||||
return c -> rn.run();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue