From 22e4c0a68ef86aa306776f5d6fb8cee3ca9b704b Mon Sep 17 00:00:00 2001 From: "David M. Lloyd" Date: Wed, 10 Jan 2024 13:58:53 -0600 Subject: [PATCH] Fix casting issue In IntelliJ, the existing combination of raw types, casting, and lambdas often produces spurious compilation errors. This PR introduces a solution which hides the unsafe cast in a single method, and also uses `Map.putIfAbsent` to avoid constructing a lambda for this case. --- .../java/io/smallrye/config/SmallRyeConfig.java | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/implementation/src/main/java/io/smallrye/config/SmallRyeConfig.java b/implementation/src/main/java/io/smallrye/config/SmallRyeConfig.java index 49b10dd35..c7982a24a 100644 --- a/implementation/src/main/java/io/smallrye/config/SmallRyeConfig.java +++ b/implementation/src/main/java/io/smallrye/config/SmallRyeConfig.java @@ -615,10 +615,21 @@ public T convert(String value, Class asType) { return value != null ? requireConverter(asType).convert(value) : null; } - @SuppressWarnings({ "unchecked", "rawtypes" }) private Converter> getOptionalConverter(Class asType) { - return optionalConverters.computeIfAbsent(asType, - clazz -> newOptionalConverter(requireConverter((Class) clazz))); + Converter> converter = recast(optionalConverters.get(asType)); + if (converter == null) { + converter = newOptionalConverter(requireConverter(asType)); + Converter> appearing = recast(optionalConverters.putIfAbsent(asType, recast(converter))); + if (appearing != null) { + converter = appearing; + } + } + return converter; + } + + @SuppressWarnings("unchecked") + private static T recast(Object obj) { + return (T) obj; } @Deprecated // binary-compatibility bridge method for Quarkus