diff --git a/common/build.gradle b/common/build.gradle index fc7450e7..da0c23e1 100644 --- a/common/build.gradle +++ b/common/build.gradle @@ -12,7 +12,7 @@ repositories { dependencies { implementation "com.squareup.retrofit2:retrofit:2.11.0" api "com.squareup.retrofit2:converter-gson:2.11.0" - api 'com.google.code.gson:gson:2.10.1' + api 'com.google.code.gson:gson:2.11.0' testImplementation "org.junit.jupiter:junit-jupiter:5.11.0" testImplementation "org.mockito:mockito-core:5.13.0" } diff --git a/common/src/main/java/com/lucaskjaerozhang/wikitext_parser/common/metadata/WikiConstants.java b/common/src/main/java/com/lucaskjaerozhang/wikitext_parser/common/metadata/WikiConstants.java index d79eaa32..37f58981 100644 --- a/common/src/main/java/com/lucaskjaerozhang/wikitext_parser/common/metadata/WikiConstants.java +++ b/common/src/main/java/com/lucaskjaerozhang/wikitext_parser/common/metadata/WikiConstants.java @@ -15,10 +15,12 @@ public class WikiConstants { "Failed to load constants file %s: %s"; private static final Gson gson = new Gson(); - private static final Set BEHAVIOR_SWITCHES = readSetConstant("behavior_switches.json"); + private static final Set BEHAVIOR_SWITCHES = + readSetConstant("behavior_switches.json", String.class); private static final Set LANGUAGE_CODES = readSetOfStringsConstantToLowercase("language_codes.json"); - private static final Map NAMESPACES = readMapConstant("namespaces.json"); + private static final Map NAMESPACES = + readMapConstant("namespaces.json", String.class, String.class); private static final Set WIKIS = readSetOfStringsConstantToLowercase("wikis.json"); public static boolean isBehaviorSwitch(String behaviorSwitch) { @@ -39,10 +41,13 @@ public static boolean isWiki(String wiki) { return WIKIS.contains(wiki.toLowerCase(Locale.ROOT)); } - private static Map readMapConstant(String filename) { + private static Map readMapConstant( + String filename, Class keyClass, Class valueClass) { try { String constantString = readStringConstant(filename); - return gson.fromJson(constantString, new TypeToken>() {}.getType()); + TypeToken> token = + (TypeToken>) TypeToken.getParameterized(Map.class, keyClass, valueClass); + return gson.fromJson(constantString, token); } catch (Exception e) { throw new IllegalStateException( String.format(CONSTANT_LOADING_ERROR_MESSAGE, filename, e.getMessage())); @@ -50,16 +55,16 @@ private static Map readMapConstant(String filename) { } private static Set readSetOfStringsConstantToLowercase(String filename) { - return readSetConstant(filename).stream() - .map(String.class::cast) + return readSetConstant(filename, String.class).stream() .map(String::toLowerCase) .collect(Collectors.toSet()); } - private static Set readSetConstant(String filename) { + private static Set readSetConstant(String filename, Class clazz) { try { String constantString = readStringConstant(filename); - return gson.fromJson(constantString, new TypeToken>() {}.getType()); + TypeToken> token = (TypeToken>) TypeToken.getParameterized(Set.class, clazz); + return gson.fromJson(constantString, token); } catch (Exception e) { throw new IllegalStateException( String.format(CONSTANT_LOADING_ERROR_MESSAGE, filename, e.getMessage()));