Browse Source

Improved exceptions

Lortseam 5 years ago
parent
commit
0e5d92402c

+ 5 - 3
src/main/java/me/lortseam/completeconfig/CompleteConfig.java

@@ -1,7 +1,8 @@
 package me.lortseam.completeconfig;
 
 import com.google.common.base.CaseFormat;
-import me.lortseam.completeconfig.entry.Entry;
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
 import me.lortseam.completeconfig.entry.GuiProvider;
 import me.lortseam.completeconfig.entry.GuiRegistry;
 import me.shedaniel.clothconfig2.api.ConfigEntryBuilder;
@@ -11,13 +12,14 @@ import java.util.HashSet;
 import java.util.Optional;
 import java.util.Set;
 
-public class CompleteConfig {
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class CompleteConfig {
 
     private static final Set<ConfigManager> managers = new HashSet<>();
 
     public static ConfigManager register(String modID) {
         if (getManager(modID).isPresent()) {
-            throw new RuntimeException("There is already registered a manager for this mod ID!");
+            throw new IllegalArgumentException("A manager with this mod ID is already registered");
         }
         ConfigManager manager = new ConfigManager(modID);
         registerDefaultGuiProviders(manager.getGuiRegistry());

+ 20 - 16
src/main/java/me/lortseam/completeconfig/ConfigManager.java

@@ -13,6 +13,9 @@ import me.lortseam.completeconfig.api.ConfigEntrySaveConsumer;
 import me.lortseam.completeconfig.collection.Collection;
 import me.lortseam.completeconfig.entry.Entry;
 import me.lortseam.completeconfig.entry.GuiRegistry;
+import me.lortseam.completeconfig.exception.IllegalAnnotationParameterException;
+import me.lortseam.completeconfig.exception.IllegalAnnotationTargetException;
+import me.lortseam.completeconfig.exception.IllegalReturnValueException;
 import me.lortseam.completeconfig.saveconsumer.SaveConsumer;
 import me.lortseam.completeconfig.serialization.CollectionsDeserializer;
 import me.lortseam.completeconfig.serialization.EntrySerializer;
@@ -35,7 +38,6 @@ import java.nio.file.Paths;
 import java.util.*;
 import java.util.stream.Collectors;
 
-//TODO: Sortierung der Categories, Subcategories und Entrys (Nach Registrierungsreihenfolge oder Alphabet; allgemein und für jeden Container einzeln?)
 public class ConfigManager {
 
     @Getter(AccessLevel.PACKAGE)
@@ -54,12 +56,14 @@ public class ConfigManager {
     }
 
     private JsonElement load() {
-        if(!Files.exists(jsonPath)) return JsonNull.INSTANCE;
-        try {
-            return new Gson().fromJson(new FileReader(jsonPath.toString()), JsonElement.class);
-        } catch (FileNotFoundException e) {
-            throw new RuntimeException(e);
+        if(Files.exists(jsonPath)) {
+            try {
+                return new Gson().fromJson(new FileReader(jsonPath.toString()), JsonElement.class);
+            } catch (FileNotFoundException e) {
+                throw new RuntimeException(e);
+            }
         }
+        return JsonNull.INSTANCE;
     }
 
     private LinkedHashMap<String, Entry> getContainerEntries(ConfigEntryContainer container) {
@@ -88,7 +92,7 @@ public class ConfigManager {
                     } else {
                         Entry entry = fieldClassEntries.get(fieldName);
                         if (entry == null) {
-                            throw new RuntimeException("Could not find field " + fieldName + " in " + fieldClass + " of save consumer method " + method);
+                            throw new IllegalAnnotationParameterException("Could not find field " + fieldName + " in " + fieldClass + " requested by save consumer method " + method);
                         }
                         entry.addSaveConsumer(method, container);
                     }
@@ -111,26 +115,26 @@ public class ConfigManager {
                 if (field.isAnnotationPresent(ConfigEntry.TranslationKey.class)) {
                     String customTranslationKey = field.getDeclaredAnnotation(ConfigEntry.TranslationKey.class).value();
                     if (StringUtils.isBlank(customTranslationKey)) {
-                        throw new RuntimeException("Translation key for entry field " + field + " was blank!");
+                        throw new IllegalAnnotationParameterException("Translation key for entry field " + field + " must not be blank");
                     }
                     builder.setCustomTranslationKey(customTranslationKey);
                 }
                 if (field.isAnnotationPresent(ConfigEntry.Integer.Bounded.class)) {
                     if (field.getType() != Integer.TYPE) {
-                        throw new RuntimeException("Cannot apply integer bound to non integer field " + field + "!");
+                        throw new IllegalAnnotationTargetException("Cannot apply integer bound to non integer field " + field);
                     }
                     ConfigEntry.Integer.Bounded bounds = field.getDeclaredAnnotation(ConfigEntry.Integer.Bounded.class);
                     builder.setBounds(bounds.min(), bounds.max());
                 } else if (field.isAnnotationPresent(ConfigEntry.Long.Bounded.class)) {
                     if (field.getType() != Long.TYPE) {
-                        throw new RuntimeException("Cannot apply long bound to non long field " + field + "!");
+                        throw new IllegalAnnotationTargetException("Cannot apply long bound to non long field " + field);
                     }
                     ConfigEntry.Long.Bounded bounds = field.getDeclaredAnnotation(ConfigEntry.Long.Bounded.class);
                     builder.setBounds(bounds.min(), bounds.max());
                 }
                 Entry<?> entry = builder.build();
                 if (guiRegistry.getProvider(entry) == null) {
-                    throw new RuntimeException("Could not find gui provider for field type " + entry.getType());
+                    throw new UnsupportedOperationException("Could not find gui provider for field type " + entry.getType());
                 }
                 String fieldName = field.getName();
                 saveConsumers.removeIf(saveConsumer -> {
@@ -144,7 +148,7 @@ public class ConfigManager {
             });
             if (!saveConsumers.isEmpty()) {
                 SaveConsumer saveConsumer = saveConsumers.iterator().next();
-                throw new RuntimeException("Could not find field " + saveConsumer.getFieldName() + " of save consumer method " + saveConsumer.getMethod());
+                throw new IllegalAnnotationParameterException("Could not find field " + saveConsumer.getFieldName() + " in " + clazz + " requested by save consumer method " + saveConsumer.getMethod());
             }
             clazzEntries.putAll(entries);
             entries = clazzEntries;
@@ -169,10 +173,10 @@ public class ConfigManager {
     private void registerCategory(LinkedHashMap<String, Collection> configMap, ConfigCategory category, boolean applyJson) {
         String categoryID = category.getConfigCategoryID();
         if (StringUtils.isBlank(categoryID)) {
-            throw new RuntimeException("Category ID of " + category.getClass() + " was null or blank!");
+            throw new IllegalReturnValueException("Category ID of " + category.getClass() + " must not be null or blank");
         }
         if (configMap.containsKey(categoryID)) {
-            throw new RuntimeException("Duplicate category ID found: " + categoryID);
+            throw new IllegalStateException("Duplicate category ID found: " + categoryID);
         }
         Collection collection = new me.lortseam.completeconfig.collection.Collection();
         configMap.put(categoryID, collection);
@@ -191,7 +195,7 @@ public class ConfigManager {
 
     private void registerContainer(Collection collection, ConfigEntryContainer container) {
         if (!findEntries(config, container.getClass()).isEmpty()) {
-            throw new RuntimeException("An instance of " + container.getClass() + " is already registered!");
+            throw new UnsupportedOperationException("An instance of " + container.getClass() + " is already registered");
         }
         collection.getEntries().putAll(getContainerEntries(container));
         ConfigEntryContainer[] containers = ArrayUtils.addAll(Arrays.stream(container.getClass().getDeclaredFields()).filter(field -> {
@@ -203,7 +207,7 @@ public class ConfigManager {
             }
             if (field.isAnnotationPresent(ConfigEntryContainer.Transitive.class)) {
                 if (!ConfigEntryContainer.class.isAssignableFrom(field.getType())) {
-                    throw new RuntimeException("@ConfigEntryContainer.Transitive is not applicable on field type " + field.getType());
+                    throw new IllegalAnnotationTargetException("Transitive entry " + field + " must implement ConfigEntryContainer");
                 }
                 return true;
             }

+ 1 - 2
src/main/java/me/lortseam/completeconfig/collection/Collection.java

@@ -6,10 +6,9 @@ import me.lortseam.completeconfig.entry.Entry;
 
 import java.util.LinkedHashMap;
 
-@NoArgsConstructor
 public class Collection {
 
-    //TODO: Entries und Collections werden auch in der Config Json Datei gespeichtert, wenn sie leer sind
+    //TODO: Do not save empty maps in json file
     @Getter
     private final LinkedHashMap<String, Entry> entries = new LinkedHashMap<>();
     @Getter

+ 1 - 1
src/main/java/me/lortseam/completeconfig/entry/Entry.java

@@ -101,7 +101,7 @@ public class Entry<T> {
 
     public void addSaveConsumer(Method method, ConfigEntryContainer parentObject) {
         if (method.getParameterCount() != 1 || method.getParameterTypes()[0] != type) {
-            throw new IllegalArgumentException("Save consumer method " + method + " has wrong parameter type(s)!");
+            throw new IllegalArgumentException("Save consumer method " + method + " has wrong parameter type(s)");
         }
         if (!method.isAccessible()) {
             method.setAccessible(true);

+ 0 - 2
src/main/java/me/lortseam/completeconfig/entry/GuiRegistry.java

@@ -3,8 +3,6 @@ package me.lortseam.completeconfig.entry;
 import java.lang.reflect.Field;
 import java.util.LinkedHashMap;
 import java.util.Map;
-import java.util.function.BiPredicate;
-import java.util.function.Predicate;
 
 public class GuiRegistry {
 

+ 9 - 0
src/main/java/me/lortseam/completeconfig/exception/IllegalAnnotationParameterException.java

@@ -0,0 +1,9 @@
+package me.lortseam.completeconfig.exception;
+
+public class IllegalAnnotationParameterException extends RuntimeException {
+
+    public IllegalAnnotationParameterException(String message) {
+        super(message);
+    }
+
+}

+ 9 - 0
src/main/java/me/lortseam/completeconfig/exception/IllegalAnnotationTargetException.java

@@ -0,0 +1,9 @@
+package me.lortseam.completeconfig.exception;
+
+public class IllegalAnnotationTargetException extends RuntimeException {
+
+    public IllegalAnnotationTargetException(String message) {
+        super(message);
+    }
+
+}

+ 9 - 0
src/main/java/me/lortseam/completeconfig/exception/IllegalReturnValueException.java

@@ -0,0 +1,9 @@
+package me.lortseam.completeconfig.exception;
+
+public class IllegalReturnValueException extends RuntimeException {
+
+    public IllegalReturnValueException(String message) {
+        super(message);
+    }
+
+}

+ 0 - 2
src/main/java/me/lortseam/completeconfig/serialization/EntrySerializer.java

@@ -3,13 +3,11 @@ package me.lortseam.completeconfig.serialization;
 import com.google.gson.JsonElement;
 import com.google.gson.JsonSerializationContext;
 import com.google.gson.JsonSerializer;
-import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
 import me.lortseam.completeconfig.entry.Entry;
 
 import java.lang.reflect.Type;
 
-@NoArgsConstructor
 public class EntrySerializer implements JsonSerializer<Entry> {
 
     public static final Type TYPE = Entry.class;