瀏覽代碼

Auto-detect setter listeners

Lortseam 4 年之前
父節點
當前提交
5c810860ef
共有 1 個文件被更改,包括 25 次插入10 次删除
  1. 25 10
      src/main/java/me/lortseam/completeconfig/data/EntryMap.java

+ 25 - 10
src/main/java/me/lortseam/completeconfig/data/EntryMap.java

@@ -9,10 +9,12 @@ import me.lortseam.completeconfig.exception.IllegalAnnotationParameterException;
 import me.lortseam.completeconfig.exception.IllegalModifierException;
 import me.lortseam.completeconfig.exception.IllegalReturnTypeException;
 
+import java.lang.reflect.Field;
 import java.lang.reflect.Modifier;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
+import java.util.regex.Pattern;
 
 public class EntryMap extends ConfigMap<Entry> {
 
@@ -44,18 +46,31 @@ public class EntryMap extends ConfigMap<Entry> {
                 clazzEntries.add(entry);
             });
             containerEntries.addAll(0, clazzEntries);
-            Arrays.stream(clazz.getDeclaredMethods()).filter(method -> (clazz == container.getClass() || !Modifier.isStatic(method.getModifiers())) && method.isAnnotationPresent(ConfigEntryListener.class)).forEach(method -> {
-                ConfigEntryListener listener = method.getDeclaredAnnotation(ConfigEntryListener.class);
-                String fieldName = listener.value();
-                if (fieldName.equals("")) {
-                    if (!method.getName().startsWith("set") || method.getName().equals("set")) {
-                        throw new IllegalAnnotationParameterException("Could not detect field name for listener method " + method + ", please provide it inside the annotation");
+            Arrays.stream(clazz.getDeclaredMethods()).filter(method -> {
+                if (clazz != container.getClass() && Modifier.isStatic(method.getModifiers())) {
+                    return false;
+                }
+                return method.isAnnotationPresent(ConfigEntryListener.class) || container.isConfigPOJO() && method.getName().startsWith("set");
+            }).forEach(method -> {
+                String fieldName = null;
+                Class<? extends ConfigEntryContainer> fieldClass = clazz;
+                if (method.isAnnotationPresent(ConfigEntryListener.class)) {
+                    ConfigEntryListener listener = method.getDeclaredAnnotation(ConfigEntryListener.class);
+                    if (!listener.value().equals("")) {
+                        fieldName = listener.value();
                     }
-                    fieldName = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, method.getName().replaceFirst("set", ""));
+                    if (listener.container() != ConfigEntryContainer.class) {
+                        fieldClass = listener.container();
+                    }
+                }
+                if (fieldName == null && fieldClass == clazz && method.getName().startsWith("set")) {
+                    try {
+                        Field field = fieldClass.getDeclaredField(CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, method.getName().replaceFirst(Pattern.quote("set"), "")));
+                        fieldName = field.getName();
+                    } catch (NoSuchFieldException ignore) {}
                 }
-                Class<? extends ConfigEntryContainer> fieldClass = listener.container();
-                if (fieldClass == ConfigEntryContainer.class) {
-                    fieldClass = clazz;
+                if (fieldName == null) {
+                    throw new IllegalAnnotationParameterException("Could not detect field name for listener method " + method);
                 }
                 if (method.getParameterCount() != 1) {
                     throw new IllegalArgumentException("Listener method " + method + " has wrong number of parameters");