瀏覽代碼

Merge utils

Lortseam 4 年之前
父節點
當前提交
88f9db2be7

+ 2 - 6
lib/src/main/java/me/lortseam/completeconfig/CompleteConfig.java

@@ -8,12 +8,12 @@ import me.lortseam.completeconfig.extensions.CompleteConfigExtension;
 import me.lortseam.completeconfig.extensions.Extension;
 import me.lortseam.completeconfig.extensions.GuiExtension;
 import me.lortseam.completeconfig.extensions.clothbasicmath.ClothBasicMathExtension;
+import me.lortseam.completeconfig.util.ReflectionUtils;
 import net.fabricmc.api.EnvType;
 import net.fabricmc.loader.api.FabricLoader;
 import net.fabricmc.loader.api.entrypoint.EntrypointContainer;
 import org.apache.commons.lang3.ClassUtils;
 
-import java.lang.reflect.Constructor;
 import java.lang.reflect.InvocationTargetException;
 import java.util.*;
 
@@ -54,11 +54,7 @@ public final class CompleteConfig {
     private static void registerExtension(Class<? extends Extension> extension) {
         if(Collections.disjoint(ClassUtils.getAllInterfaces(extension), validExtensionTypes)) return;
         try {
-            Constructor<? extends Extension> constructor = extension.getDeclaredConstructor();
-            if (!constructor.isAccessible()) {
-                constructor.setAccessible(true);
-            }
-            registerExtension(constructor.newInstance());
+            registerExtension(ReflectionUtils.instantiateClass(extension));
         } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
             logger.error("[CompleteConfig] Failed to instantiate extension " + extension, e);
         }

+ 2 - 6
lib/src/main/java/me/lortseam/completeconfig/data/BaseCollection.java

@@ -7,10 +7,10 @@ import me.lortseam.completeconfig.data.structure.DataPart;
 import me.lortseam.completeconfig.data.structure.ParentDataPart;
 import me.lortseam.completeconfig.data.text.TranslationIdentifier;
 import me.lortseam.completeconfig.exception.IllegalAnnotationTargetException;
+import me.lortseam.completeconfig.util.ReflectionUtils;
 import net.minecraft.text.Text;
 import org.apache.commons.lang3.ArrayUtils;
 
-import java.lang.reflect.Constructor;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Modifier;
 import java.util.Arrays;
@@ -77,11 +77,7 @@ abstract class BaseCollection implements ParentDataPart {
                 return false;
             }).map(nestedClass -> {
                 try {
-                    Constructor<? extends ConfigContainer> constructor = (Constructor<? extends ConfigContainer>) nestedClass.getDeclaredConstructor();
-                    if (!constructor.isAccessible()) {
-                        constructor.setAccessible(true);
-                    }
-                    return constructor.newInstance();
+                    return (ConfigContainer) ReflectionUtils.instantiateClass(nestedClass);
                 } catch (NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) {
                     throw new RuntimeException("Failed to instantiate nested class " + nestedClass, e);
                 }

+ 5 - 6
lib/src/main/java/me/lortseam/completeconfig/data/Entry.java

@@ -15,8 +15,7 @@ import me.lortseam.completeconfig.data.structure.Identifiable;
 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 me.lortseam.completeconfig.util.ReflectionUtils;
 import net.minecraft.text.Text;
 import net.minecraft.text.TextColor;
 import org.apache.commons.lang3.StringUtils;
@@ -61,7 +60,7 @@ public class Entry<T> implements DataPart, Identifiable {
                 ConfigEntry.BoundedDouble bounds = origin.getAnnotation(ConfigEntry.BoundedDouble.class);
                 return new BoundedEntry<>(origin, bounds.min(), bounds.max());
             }),
-            Transformation.builder().byType(type -> Enum.class.isAssignableFrom(TypeUtils.getTypeClass(type))).byAnnotation(ConfigEntry.Enum.class, true).transforms(EnumEntry::new),
+            Transformation.builder().byType(type -> Enum.class.isAssignableFrom(ReflectionUtils.getTypeClass(type))).byAnnotation(ConfigEntry.Enum.class, true).transforms(EnumEntry::new),
             Transformation.builder().byAnnotation(ConfigEntry.Color.class).transforms(ColorEntry::new),
             Transformation.builder().byType(TextColor.class).transforms(origin -> new ColorEntry<>(origin, false))
     );
@@ -101,8 +100,8 @@ public class Entry<T> implements DataPart, Identifiable {
         if (!field.isAccessible()) {
             field.setAccessible(true);
         }
-        type = TypeUtils.getFieldType(origin.getField());
-        typeClass = (Class<T>) TypeUtils.getTypeClass(type);
+        type = ReflectionUtils.getFieldType(origin.getField());
+        typeClass = (Class<T>) ReflectionUtils.getTypeClass(type);
         parentObject = origin.getParentObject();
         parentTranslation = origin.getParentTranslation();
         this.valueModifier = valueModifier;
@@ -153,7 +152,7 @@ public class Entry<T> implements DataPart, Identifiable {
 
     private void set(T value) {
         try {
-            Optional<Method> writeMethod = PropertyUtils.getWriteMethod(field);
+            Optional<Method> writeMethod = ReflectionUtils.getWriteMethod(field);
             if (writeMethod.isPresent()) {
                 writeMethod.get().invoke(isStatic() ? null : parentObject, value);
             } else {

+ 2 - 2
lib/src/main/java/me/lortseam/completeconfig/data/entry/EntryOrigin.java

@@ -4,7 +4,7 @@ import lombok.Getter;
 import lombok.RequiredArgsConstructor;
 import me.lortseam.completeconfig.api.ConfigContainer;
 import me.lortseam.completeconfig.data.text.TranslationIdentifier;
-import me.lortseam.completeconfig.util.TypeUtils;
+import me.lortseam.completeconfig.util.ReflectionUtils;
 
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Field;
@@ -23,7 +23,7 @@ public final class EntryOrigin {
     private final TranslationIdentifier parentTranslation;
 
     public Type getType() {
-        return TypeUtils.getFieldType(field);
+        return ReflectionUtils.getFieldType(field);
     }
 
     public <A extends Annotation> A getAnnotation(Class<A> annotationType) {

+ 21 - 4
lib/src/main/java/me/lortseam/completeconfig/util/PropertyUtils.java → lib/src/main/java/me/lortseam/completeconfig/util/ReflectionUtils.java

@@ -1,22 +1,39 @@
 package me.lortseam.completeconfig.util;
 
 import com.google.common.collect.MoreCollectors;
+import com.google.common.reflect.TypeToken;
+import io.leangen.geantyref.GenericTypeReflector;
 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.lang.reflect.*;
 import java.util.Arrays;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Optional;
 
 @UtilityClass
-public final class PropertyUtils {
+public final class ReflectionUtils {
 
-    private final Map<Field, Method> writeMethodsCache = new HashMap<>();
+    private static final Map<Field, Method> writeMethodsCache = new HashMap<>();
+
+    public static Class<?> getTypeClass(Type type) {
+        return TypeToken.of(type).getRawType();
+    }
+
+    public static Type getFieldType(Field field) {
+        return GenericTypeReflector.getExactFieldType(field, field.getDeclaringClass());
+    }
+
+    public static <T> T instantiateClass(Class<T> clazz) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
+        Constructor<T> constructor = clazz.getDeclaredConstructor();
+        if (!constructor.isAccessible()) {
+            constructor.setAccessible(true);
+        }
+        return constructor.newInstance();
+    }
 
     public static Optional<Method> getWriteMethod(Field field) throws IntrospectionException {
         if (writeMethodsCache.containsKey(field)) {

+ 0 - 21
lib/src/main/java/me/lortseam/completeconfig/util/TypeUtils.java

@@ -1,21 +0,0 @@
-package me.lortseam.completeconfig.util;
-
-import com.google.common.reflect.TypeToken;
-import io.leangen.geantyref.GenericTypeReflector;
-import lombok.experimental.UtilityClass;
-
-import java.lang.reflect.Field;
-import java.lang.reflect.Type;
-
-@UtilityClass
-public final class TypeUtils {
-
-    public static Type getFieldType(Field field) {
-        return GenericTypeReflector.getExactFieldType(field, field.getDeclaringClass());
-    }
-
-    public static Class<?> getTypeClass(Type type) {
-        return TypeToken.of(type).getRawType();
-    }
-
-}