Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

opti reflection again. #16

Merged
merged 19 commits into from
Oct 5, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package band.kessoku.lib.api.base.reflect;

import java.lang.reflect.Member;
import java.lang.reflect.Modifier;

public final class ModifiersUtil {
private ModifiersUtil() {
}

/* === Common === */

public static boolean isPublic(Member field) {
return Modifier.isPublic(field.getModifiers());
}

public static boolean isStatic(Member field) {
return Modifier.isStatic(field.getModifiers());
}

/* === Common Combo === */

public static boolean isPublicAndStatic(Member field) {
return isPublic(field) && isStatic(field);
}

public static boolean isPublicOrStatic(Member field, boolean shouldPublic, boolean shouldStatic) {
return shouldPublic == isPublic(field) && shouldStatic == isStatic(field);
}

/* === Common End === */

/* === Data Object === */

public static boolean isVolatile(Member field) {
return Modifier.isVolatile(field.getModifiers());
}

public static boolean isTransient(Member field) {
return Modifier.isTransient(field.getModifiers());
}

/* === Data Object End === */

/* === Low Usage Tools === */

public static boolean isFinal(Member field) {
return Modifier.isFinal(field.getModifiers());
}

public static boolean isProtected(Member field) {
return Modifier.isProtected(field.getModifiers());
}

/* === Low Usage Tools End === */
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package band.kessoku.lib.api.base.reflect;

import java.lang.reflect.Field;
import java.util.Arrays;

public final class ReflectUtil {
private ReflectUtil() {
}

public static boolean isAssignableFrom(Field field, Class<?>... clazzs) {
var flag = Arrays.stream(clazzs).anyMatch(clazz -> !field.getType().isAssignableFrom(clazz));
return !flag;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import java.util.function.Consumer;

import band.kessoku.lib.api.KessokuLib;
import band.kessoku.lib.api.base.reflect.ModifiersUtil;
import band.kessoku.lib.api.base.reflect.ReflectUtil;
import band.kessoku.lib.api.config.annotations.Comment;
import band.kessoku.lib.api.config.annotations.Comments;
import band.kessoku.lib.api.config.annotations.Name;
Expand Down Expand Up @@ -83,7 +85,9 @@ public boolean load() {
// Check the value is public and not static
try {
Field field = this.getClass().getField(key);
if (!Modifier.isPublic(field.getModifiers()) || Modifier.isStatic(field.getModifiers())) continue;
if (!ModifiersUtil.isPublicOrStatic(field, true, false)) {
continue;
}
value = (ConfigValue) ReflectionUtil.getFieldValue(field, this);
} catch (NoSuchFieldException e) {
continue;
Expand Down Expand Up @@ -142,12 +146,9 @@ public void reset() {

List<Field> fields = new ArrayList<>();
for (Field declaredField : this.getClass().getDeclaredFields()) {
declaredField.setAccessible(true);

final boolean flag0 = declaredField.getDeclaringClass().isAssignableFrom(ConfigValue.class);
final boolean flag1 = Modifier.isPublic(declaredField.getModifiers());
final boolean flag2 = !Modifier.isStatic(declaredField.getModifiers());
if (flag0 && flag1 && flag2) {
final boolean flag0 = ReflectUtil.isAssignableFrom(declaredField, ConfigValue.class);
final boolean flag1 = ModifiersUtil.isPublicOrStatic(declaredField, true, false);
if (flag0 && flag1) {
fields.add(declaredField);
}
}
Expand All @@ -166,8 +167,8 @@ public ImmutableList<AbstractConfig> getValidCategories() {
for (Field declaredField : this.getClass().getDeclaredFields()) {
declaredField.setAccessible(true);

final boolean flag0 = declaredField.getDeclaringClass().isAssignableFrom(AbstractConfig.class);
final boolean flag1 = Modifier.isPublic(declaredField.getModifiers());
final boolean flag0 = ReflectUtil.isAssignableFrom(declaredField, AbstractConfig.class);
final boolean flag1 = ModifiersUtil.isPublic(declaredField);
if (flag0 && flag1){
fields.add(declaredField);
}
Expand All @@ -180,15 +181,10 @@ public ImmutableList<AbstractConfig> getValidCategories() {
private ImmutableList<Field> getValidFields() {
ImmutableList.Builder<Field> builder = ImmutableList.builder();
for (Field declaredField : this.getClass().getDeclaredFields()) {
declaredField.setAccessible(true);

final boolean flag0 = declaredField.getDeclaringClass().isAssignableFrom(AbstractConfig.class);
final boolean flag1 = declaredField.getDeclaringClass().isAssignableFrom(ConfigValue.class);
final boolean flag2 = Modifier.isPublic(declaredField.getModifiers());

final var flag = flag0 || flag1;
final boolean flag0 = ReflectUtil.isAssignableFrom(declaredField, AbstractConfig.class, ConfigValue.class);
final boolean flag1 = Modifier.isPublic(declaredField.getModifiers());

if (flag && flag2){
if (flag0 && flag1){
builder.add(declaredField);
}
}
Expand All @@ -198,7 +194,7 @@ private ImmutableList<Field> getValidFields() {
private Map<String, ValueWithComment> serialize() {
ImmutableMap.Builder<String, ValueWithComment> builder = ImmutableMap.builder();
for (Field field : this.getValidFields()) {
field.setAccessible(true);
ReflectionUtil.makeAccessible(field);

final String name = field.isAnnotationPresent(Name.class) ? field.getAnnotation(Name.class).value() : field.getName();
final String[] comments = field.isAnnotationPresent(Comments.class) ? (String[]) Arrays.stream(field.getAnnotation(Comments.class).value()).map(Comment::value).toArray() : new String[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,13 @@ private Json5Builder.ObjectBean toBean(Map<String, AbstractConfig.ValueWithComme
Json5Builder builder = new Json5Builder();
Json5Builder.ObjectBean objectBean = builder.getObjectBean();
value.forEach((s, valueWithComment) -> {
for (String comment : valueWithComment.comments()) objectBean.addNote(comment);
if (valueWithComment.object() instanceof Map<?, ?>)
for (String comment : valueWithComment.comments()) {
objectBean.addNote(comment);
}

if (valueWithComment.object() instanceof Map<?, ?>) {
objectBean.addBean(s, this.toBean((Map<String, AbstractConfig.ValueWithComment>) valueWithComment.object()));
}
});
return objectBean;
}
Expand Down