Lortseam 4 år sedan
förälder
incheckning
243fb52491

+ 10 - 7
lib/src/main/java/me/lortseam/completeconfig/data/Entry.java

@@ -14,6 +14,7 @@ import me.lortseam.completeconfig.data.structure.DataPart;
 import me.lortseam.completeconfig.data.text.TranslationIdentifier;
 import me.lortseam.completeconfig.exception.IllegalAnnotationParameterException;
 import me.lortseam.completeconfig.extensions.CompleteConfigExtension;
+import me.lortseam.completeconfig.util.PropertyUtils;
 import me.lortseam.completeconfig.util.TypeUtils;
 import net.minecraft.text.Text;
 import net.minecraft.text.TextColor;
@@ -22,8 +23,6 @@ import org.spongepowered.configurate.CommentedConfigurationNode;
 import org.spongepowered.configurate.serialize.SerializationException;
 
 import java.beans.IntrospectionException;
-import java.beans.Introspector;
-import java.beans.PropertyDescriptor;
 import java.lang.reflect.*;
 import java.util.Arrays;
 import java.util.List;
@@ -113,6 +112,10 @@ public class Entry<T> implements DataPart {
         this(origin, null);
     }
 
+    private boolean isStatic() {
+        return Modifier.isStatic(field.getModifiers());
+    }
+
     public T getValue() {
         if (update()) {
             return getValue();
@@ -122,7 +125,7 @@ public class Entry<T> implements DataPart {
 
     private T getFieldValue() {
         try {
-            return (T) Objects.requireNonNull(field.get(parentObject), field.toString());
+            return (T) Objects.requireNonNull(field.get(isStatic() ? null : parentObject), field.toString());
         } catch (IllegalAccessException e) {
             throw new RuntimeException(e);
         }
@@ -149,11 +152,11 @@ public class Entry<T> implements DataPart {
 
     private void set(T value) {
         try {
-            Method writeMethod = new PropertyDescriptor(field.getName(), field.getDeclaringClass()).getWriteMethod();
-            if (writeMethod != null) {
-                writeMethod.invoke(Modifier.isStatic(writeMethod.getModifiers()) ? null : parentObject, value);
+            Optional<Method> writeMethod = PropertyUtils.getWriteMethod(field);
+            if (writeMethod.isPresent()) {
+                writeMethod.get().invoke(isStatic() ? null : parentObject, value);
             } else {
-                field.set(Modifier.isStatic(field.getModifiers()) ? null : parentObject, value);
+                field.set(isStatic() ? null : parentObject, value);
             }
         } catch (IntrospectionException | IllegalAccessException | InvocationTargetException e) {
             throw new RuntimeException("Failed to set entry value", e);

+ 32 - 0
lib/src/main/java/me/lortseam/completeconfig/util/PropertyUtils.java

@@ -0,0 +1,32 @@
+package me.lortseam.completeconfig.util;
+
+import com.google.common.collect.MoreCollectors;
+import lombok.experimental.UtilityClass;
+
+import java.beans.IntrospectionException;
+import java.beans.Introspector;
+import java.beans.PropertyDescriptor;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Optional;
+
+@UtilityClass
+public final class PropertyUtils {
+
+    private final Map<Field, Method> writeMethodsCache = new HashMap<>();
+
+    public static Optional<Method> getWriteMethod(Field field) throws IntrospectionException {
+        if (writeMethodsCache.containsKey(field)) {
+            return Optional.ofNullable(writeMethodsCache.get(field));
+        }
+        Optional<Method> writeMethod = Arrays.stream(Introspector.getBeanInfo(field.getDeclaringClass()).getPropertyDescriptors()).filter(property -> {
+            return property.getName().equals(field.getName());
+        }).collect(MoreCollectors.toOptional()).map(PropertyDescriptor::getWriteMethod);
+        writeMethodsCache.put(field, writeMethod.orElse(null));
+        return writeMethod;
+    }
+
+}