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

fix: import death safe exceptions #406

Merged
merged 3 commits into from
Jan 13, 2024

Conversation

iProdigy
Copy link
Member

Closes #405

Type rawType = ((ParameterizedType) type).getRawType();
if (rawType instanceof Class && Collection.class.isAssignableFrom((Class<?>) rawType)) {
try {
return gson.fromJson(gson.toJson(in), type); // TODO: make more efficient
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

optimizing this is probably not worth

--- a/src/main/java/dinkplugin/util/ConfigUtil.java	(revision 7e708a656c522fe9f2dc74c83899d56cae416f8c)
+++ b/src/main/java/dinkplugin/util/ConfigUtil.java	(revision 08d37fe24e4204ed5f10ec297c4b774b6f6aeb09)
@@ -2,6 +2,7 @@
 
 import com.google.gson.Gson;
 import lombok.experimental.UtilityClass;
+import lombok.extern.slf4j.Slf4j;
 import net.runelite.api.Client;
 import net.runelite.api.widgets.ComponentID;
 import net.runelite.api.widgets.Widget;
@@ -16,9 +17,12 @@
 import java.time.Duration;
 import java.time.Instant;
 import java.util.Collection;
+import java.util.EnumSet;
+import java.util.Set;
 import java.util.regex.Pattern;
 import java.util.stream.Stream;
 
+@Slf4j
 @UtilityClass
 public class ConfigUtil {
 
@@ -102,17 +106,35 @@
         }
 
         if (in instanceof Collection && type instanceof ParameterizedType) {
-            Type rawType = ((ParameterizedType) type).getRawType();
+            ParameterizedType pType = (ParameterizedType) type;
+            Type rawType = pType.getRawType();
             if (rawType instanceof Class && Collection.class.isAssignableFrom((Class<?>) rawType)) {
+                Type[] args = pType.getActualTypeArguments();
+                if (args != null && args.length == 1) {
+                    Type eType = args[0];
+                    if (eType instanceof Class && Enum.class.isAssignableFrom((Class<?>) eType)) {
+                        //noinspection unchecked,rawtypes
+                        return convert((Class<? extends Enum>) eType, (Collection<?>) in);
+                    }
+                }
+            }
+        }
+
+        return null;
+    }
+
+    private <E extends Enum<E>> Set<E> convert(Class<E> enumClass, Iterable<?> values) {
+        Set<E> set = EnumSet.noneOf(enumClass);
+        for (Object value : values) {
+            if (value instanceof String) {
                 try {
-                    return gson.fromJson(gson.toJson(in), type); // TODO: make more efficient
-                } catch (Exception e) {
-                    return null;
+                    set.add(Enum.valueOf(enumClass, (String) value));
+                } catch (IllegalArgumentException e) {
+                    log.debug("Encountered unknown enum value for {}: {}", enumClass.getSimpleName(), value, e);
                 }
             }
         }
-
-        return null;
+        return set;
     }
 
     public boolean isSettingsOpen(@NotNull Client client) {

@iProdigy iProdigy marked this pull request as ready for review January 13, 2024 05:29
@iProdigy iProdigy requested review from pajlada and Felanbird January 13, 2024 05:29
Copy link
Member

@pajlada pajlada left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks fine to me, I haven't tested it

@iProdigy iProdigy merged commit e9626b4 into pajlads:master Jan 13, 2024
4 checks passed
@iProdigy iProdigy deleted the fix/import-death-exception-config branch January 13, 2024 10:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Import Command Does Not Update Safe Death Exceptions
3 participants