-
Notifications
You must be signed in to change notification settings - Fork 22
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
iProdigy
merged 3 commits into
pajlads:master
from
iProdigy:fix/import-death-exception-config
Jan 13, 2024
Merged
fix: import death safe exceptions #406
iProdigy
merged 3 commits into
pajlads:master
from
iProdigy:fix/import-death-exception-config
Jan 13, 2024
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iProdigy
commented
Jan 13, 2024
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 |
There was a problem hiding this comment.
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) {
Felanbird
approved these changes
Jan 13, 2024
pajlada
approved these changes
Jan 13, 2024
There was a problem hiding this 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Closes #405