This is an automated email from the ASF dual-hosted git repository.

orpiske pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new 722289460042 (chores): fix SonarCloud S6201 in camel-util-json
722289460042 is described below

commit 722289460042608aeb73e1ca9418c29c21441de6
Author: Otavio Rodolfo Piske <[email protected]>
AuthorDate: Fri Mar 27 15:58:05 2026 +0000

    (chores): fix SonarCloud S6201 in camel-util-json
    
    Claude Code on behalf of Otavio R. Piske
    
    Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
---
 .../java/org/apache/camel/util/json/JsonArray.java | 32 ++++-----
 .../org/apache/camel/util/json/JsonObject.java     | 80 +++++++++++-----------
 .../java/org/apache/camel/util/json/Jsoner.java    | 68 ++++++++----------
 3 files changed, 84 insertions(+), 96 deletions(-)

diff --git 
a/tooling/camel-util-json/src/main/java/org/apache/camel/util/json/JsonArray.java
 
b/tooling/camel-util-json/src/main/java/org/apache/camel/util/json/JsonArray.java
index fa1ed51f1f95..c8be1b0ed517 100644
--- 
a/tooling/camel-util-json/src/main/java/org/apache/camel/util/json/JsonArray.java
+++ 
b/tooling/camel-util-json/src/main/java/org/apache/camel/util/json/JsonArray.java
@@ -112,9 +112,9 @@ public class JsonArray extends ArrayList<Object> implements 
Jsonable {
         } else if (returnable instanceof Number) {
             /* A number can be used to construct a BigDecimal. */
             returnable = new BigDecimal(returnable.toString());
-        } else if (returnable instanceof String) {
+        } else if (returnable instanceof String str) {
             /* A number can be used to construct a BigDecimal. */
-            returnable = new BigDecimal((String) returnable);
+            returnable = new BigDecimal(str);
         }
         return (BigDecimal) returnable;
     }
@@ -129,8 +129,8 @@ public class JsonArray extends ArrayList<Object> implements 
Jsonable {
      */
     public Boolean getBoolean(final int index) {
         Object returnable = this.get(index);
-        if (returnable instanceof String) {
-            returnable = Boolean.valueOf((String) returnable);
+        if (returnable instanceof String str) {
+            returnable = Boolean.valueOf(str);
         }
         return (Boolean) returnable;
     }
@@ -151,9 +151,9 @@ public class JsonArray extends ArrayList<Object> implements 
Jsonable {
         if (returnable == null) {
             return null;
         }
-        if (returnable instanceof String) {
+        if (returnable instanceof String str) {
             /* A String can be used to construct a BigDecimal. */
-            returnable = new BigDecimal((String) returnable);
+            returnable = new BigDecimal(str);
         }
         return ((Number) returnable).byteValue();
     }
@@ -194,9 +194,9 @@ public class JsonArray extends ArrayList<Object> implements 
Jsonable {
         if (returnable == null) {
             return null;
         }
-        if (returnable instanceof String) {
+        if (returnable instanceof String str) {
             /* A String can be used to construct a BigDecimal. */
-            returnable = new BigDecimal((String) returnable);
+            returnable = new BigDecimal(str);
         }
         return ((Number) returnable).doubleValue();
     }
@@ -288,9 +288,9 @@ public class JsonArray extends ArrayList<Object> implements 
Jsonable {
         if (returnable == null) {
             return null;
         }
-        if (returnable instanceof String) {
+        if (returnable instanceof String str) {
             /* A String can be used to construct a BigDecimal. */
-            returnable = new BigDecimal((String) returnable);
+            returnable = new BigDecimal(str);
         }
         return ((Number) returnable).floatValue();
     }
@@ -311,9 +311,9 @@ public class JsonArray extends ArrayList<Object> implements 
Jsonable {
         if (returnable == null) {
             return null;
         }
-        if (returnable instanceof String) {
+        if (returnable instanceof String str) {
             /* A String can be used to construct a BigDecimal. */
-            returnable = new BigDecimal((String) returnable);
+            returnable = new BigDecimal(str);
         }
         return ((Number) returnable).intValue();
     }
@@ -334,9 +334,9 @@ public class JsonArray extends ArrayList<Object> implements 
Jsonable {
         if (returnable == null) {
             return null;
         }
-        if (returnable instanceof String) {
+        if (returnable instanceof String str) {
             /* A String can be used to construct a BigDecimal. */
-            returnable = new BigDecimal((String) returnable);
+            returnable = new BigDecimal(str);
         }
         return ((Number) returnable).longValue();
     }
@@ -377,9 +377,9 @@ public class JsonArray extends ArrayList<Object> implements 
Jsonable {
         if (returnable == null) {
             return null;
         }
-        if (returnable instanceof String) {
+        if (returnable instanceof String str) {
             /* A String can be used to construct a BigDecimal. */
-            returnable = new BigDecimal((String) returnable);
+            returnable = new BigDecimal(str);
         }
         return ((Number) returnable).shortValue();
     }
diff --git 
a/tooling/camel-util-json/src/main/java/org/apache/camel/util/json/JsonObject.java
 
b/tooling/camel-util-json/src/main/java/org/apache/camel/util/json/JsonObject.java
index a6be47747d96..7a0421e1c248 100644
--- 
a/tooling/camel-util-json/src/main/java/org/apache/camel/util/json/JsonObject.java
+++ 
b/tooling/camel-util-json/src/main/java/org/apache/camel/util/json/JsonObject.java
@@ -103,9 +103,9 @@ public class JsonObject extends LinkedHashMap<String, 
Object> implements Jsonabl
         if (returnable == null) {
             return null;
         }
-        if (returnable instanceof String) {
+        if (returnable instanceof String str) {
             /* A String can be used to construct a BigDecimal. */
-            returnable = new BigDecimal((String) returnable);
+            returnable = new BigDecimal(str);
         }
         return ((Number) returnable).intValue();
     }
@@ -126,8 +126,8 @@ public class JsonObject extends LinkedHashMap<String, 
Object> implements Jsonabl
      */
     public Boolean pathBoolean(String path) {
         Object returnable = path(path);
-        if (returnable instanceof String) {
-            returnable = Boolean.valueOf((String) returnable);
+        if (returnable instanceof String str) {
+            returnable = Boolean.valueOf(str);
         }
         return (Boolean) returnable;
     }
@@ -165,8 +165,8 @@ public class JsonObject extends LinkedHashMap<String, 
Object> implements Jsonabl
             java.util.Optional<Object> o = doPath(path.substring(0, pos));
             if (o.isPresent()) {
                 answer = o.get();
-                if (answer instanceof Map) {
-                    jo = (Map) answer;
+                if (answer instanceof Map map) {
+                    jo = map;
                 }
             } else {
                 optional = true;
@@ -177,8 +177,8 @@ public class JsonObject extends LinkedHashMap<String, 
Object> implements Jsonabl
             java.util.Optional<Object> o = doPath(path);
             if (o.isPresent()) {
                 answer = o.get();
-                if (answer instanceof Map) {
-                    jo = (Map) answer;
+                if (answer instanceof Map map) {
+                    jo = map;
                 }
             } else {
                 optional = true;
@@ -336,9 +336,9 @@ public class JsonObject extends LinkedHashMap<String, 
Object> implements Jsonabl
         } else if (returnable instanceof Number) {
             /* A number can be used to construct a BigDecimal */
             returnable = new BigDecimal(returnable.toString());
-        } else if (returnable instanceof String) {
+        } else if (returnable instanceof String str) {
             /* A number can be used to construct a BigDecimal */
-            returnable = new BigDecimal((String) returnable);
+            returnable = new BigDecimal(str);
         }
         return (BigDecimal) returnable;
     }
@@ -369,9 +369,9 @@ public class JsonObject extends LinkedHashMap<String, 
Object> implements Jsonabl
         } else if (returnable instanceof Number) {
             /* A number can be used to construct a BigDecimal */
             returnable = new BigDecimal(returnable.toString());
-        } else if (returnable instanceof String) {
+        } else if (returnable instanceof String str) {
             /* A String can be used to construct a BigDecimal */
-            returnable = new BigDecimal((String) returnable);
+            returnable = new BigDecimal(str);
         }
         return (BigDecimal) returnable;
     }
@@ -385,8 +385,8 @@ public class JsonObject extends LinkedHashMap<String, 
Object> implements Jsonabl
      */
     public Boolean getBoolean(final String key) {
         Object returnable = this.get(key);
-        if (returnable instanceof String) {
-            returnable = Boolean.valueOf((String) returnable);
+        if (returnable instanceof String str) {
+            returnable = Boolean.valueOf(str);
         }
         return (Boolean) returnable;
     }
@@ -406,8 +406,8 @@ public class JsonObject extends LinkedHashMap<String, 
Object> implements Jsonabl
         } else {
             return defaultValue;
         }
-        if (returnable instanceof String) {
-            returnable = Boolean.valueOf((String) returnable);
+        if (returnable instanceof String str) {
+            returnable = Boolean.valueOf(str);
         }
         return (Boolean) returnable;
     }
@@ -427,9 +427,9 @@ public class JsonObject extends LinkedHashMap<String, 
Object> implements Jsonabl
         if (returnable == null) {
             return null;
         }
-        if (returnable instanceof String) {
+        if (returnable instanceof String str) {
             /* A String can be used to construct a BigDecimal. */
-            returnable = new BigDecimal((String) returnable);
+            returnable = new BigDecimal(str);
         }
         return ((Number) returnable).byteValue();
     }
@@ -456,9 +456,9 @@ public class JsonObject extends LinkedHashMap<String, 
Object> implements Jsonabl
         if (returnable == null) {
             return null;
         }
-        if (returnable instanceof String) {
+        if (returnable instanceof String str) {
             /* A String can be used to construct a BigDecimal. */
-            returnable = new BigDecimal((String) returnable);
+            returnable = new BigDecimal(str);
         }
         return ((Number) returnable).byteValue();
     }
@@ -521,9 +521,9 @@ public class JsonObject extends LinkedHashMap<String, 
Object> implements Jsonabl
         if (returnable == null) {
             return null;
         }
-        if (returnable instanceof String) {
+        if (returnable instanceof String str) {
             /* A String can be used to construct a BigDecimal. */
-            returnable = new BigDecimal((String) returnable);
+            returnable = new BigDecimal(str);
         }
         return ((Number) returnable).doubleValue();
     }
@@ -550,9 +550,9 @@ public class JsonObject extends LinkedHashMap<String, 
Object> implements Jsonabl
         if (returnable == null) {
             return null;
         }
-        if (returnable instanceof String) {
+        if (returnable instanceof String str) {
             /* A String can be used to construct a BigDecimal. */
-            returnable = new BigDecimal((String) returnable);
+            returnable = new BigDecimal(str);
         }
         return ((Number) returnable).doubleValue();
     }
@@ -725,9 +725,9 @@ public class JsonObject extends LinkedHashMap<String, 
Object> implements Jsonabl
         if (returnable == null) {
             return null;
         }
-        if (returnable instanceof String) {
+        if (returnable instanceof String str) {
             /* A String can be used to construct a BigDecimal. */
-            returnable = new BigDecimal((String) returnable);
+            returnable = new BigDecimal(str);
         }
         return ((Number) returnable).floatValue();
     }
@@ -754,9 +754,9 @@ public class JsonObject extends LinkedHashMap<String, 
Object> implements Jsonabl
         if (returnable == null) {
             return null;
         }
-        if (returnable instanceof String) {
+        if (returnable instanceof String str) {
             /* A String can be used to construct a BigDecimal. */
-            returnable = new BigDecimal((String) returnable);
+            returnable = new BigDecimal(str);
         }
         return ((Number) returnable).floatValue();
     }
@@ -776,9 +776,9 @@ public class JsonObject extends LinkedHashMap<String, 
Object> implements Jsonabl
         if (returnable == null) {
             return null;
         }
-        if (returnable instanceof String) {
+        if (returnable instanceof String str) {
             /* A String can be used to construct a BigDecimal. */
-            returnable = new BigDecimal((String) returnable);
+            returnable = new BigDecimal(str);
         }
         return ((Number) returnable).intValue();
     }
@@ -805,9 +805,9 @@ public class JsonObject extends LinkedHashMap<String, 
Object> implements Jsonabl
         if (returnable == null) {
             return null;
         }
-        if (returnable instanceof String) {
+        if (returnable instanceof String str) {
             /* A String can be used to construct a BigDecimal. */
-            returnable = new BigDecimal((String) returnable);
+            returnable = new BigDecimal(str);
         }
         return ((Number) returnable).intValue();
     }
@@ -827,9 +827,9 @@ public class JsonObject extends LinkedHashMap<String, 
Object> implements Jsonabl
         if (returnable == null) {
             return null;
         }
-        if (returnable instanceof String) {
+        if (returnable instanceof String str) {
             /* A String can be used to construct a BigDecimal. */
-            returnable = new BigDecimal((String) returnable);
+            returnable = new BigDecimal(str);
         }
         return ((Number) returnable).longValue();
     }
@@ -856,9 +856,9 @@ public class JsonObject extends LinkedHashMap<String, 
Object> implements Jsonabl
         if (returnable == null) {
             return null;
         }
-        if (returnable instanceof String) {
+        if (returnable instanceof String str) {
             /* A String can be used to construct a BigDecimal. */
-            returnable = new BigDecimal((String) returnable);
+            returnable = new BigDecimal(str);
         }
         return ((Number) returnable).longValue();
     }
@@ -921,9 +921,9 @@ public class JsonObject extends LinkedHashMap<String, 
Object> implements Jsonabl
         if (returnable == null) {
             return null;
         }
-        if (returnable instanceof String) {
+        if (returnable instanceof String str) {
             /* A String can be used to construct a BigDecimal. */
-            returnable = new BigDecimal((String) returnable);
+            returnable = new BigDecimal(str);
         }
         return ((Number) returnable).shortValue();
     }
@@ -950,9 +950,9 @@ public class JsonObject extends LinkedHashMap<String, 
Object> implements Jsonabl
         if (returnable == null) {
             return null;
         }
-        if (returnable instanceof String) {
+        if (returnable instanceof String str) {
             /* A String can be used to construct a BigDecimal. */
-            returnable = new BigDecimal((String) returnable);
+            returnable = new BigDecimal(str);
         }
         return ((Number) returnable).shortValue();
     }
diff --git 
a/tooling/camel-util-json/src/main/java/org/apache/camel/util/json/Jsoner.java 
b/tooling/camel-util-json/src/main/java/org/apache/camel/util/json/Jsoner.java
index 54d768874461..545e39c1be8e 100644
--- 
a/tooling/camel-util-json/src/main/java/org/apache/camel/util/json/Jsoner.java
+++ 
b/tooling/camel-util-json/src/main/java/org/apache/camel/util/json/Jsoner.java
@@ -289,13 +289,12 @@ public final class Jsoner {
                             break;
                         case DATUM:
                             /* The token ought to be a key. */
-                            if (token.getValue() instanceof String) {
+                            if (token.getValue() instanceof String key) {
                                 /*
                                  * JSON keys are always strings, strings are 
not always
                                  * JSON keys but it is going to be treated as 
one.
                                  * Continue parsing the object.
                                  */
-                                final String key = (String) token.getValue();
                                 valueStack.addLast(key);
                                 stateStack.addLast(currentState);
                                 stateStack.addLast(States.PARSING_ENTRY);
@@ -716,9 +715,9 @@ public final class Jsoner {
                         returnable.append(lexed.getValue());
                         break;
                     default:
-                        if (lexed.getValue() instanceof String) {
+                        if (lexed.getValue() instanceof String s) {
                             returnable.append("\"");
-                            returnable.append(Jsoner.escape((String) 
lexed.getValue()));
+                            returnable.append(Jsoner.escape(s));
                             returnable.append("\"");
                         } else {
                             returnable.append(lexed.getValue());
@@ -845,9 +844,9 @@ public final class Jsoner {
                         
returnable.append(color.color(Yytoken.Types.RIGHT_SQUARE, lexed.getValue()));
                         break;
                     default:
-                        if (lexed.getValue() instanceof String) {
-                            String s = "\"" + Jsoner.escape((String) 
lexed.getValue()) + "\"";
-                            returnable.append(color.color(Yytoken.Types.VALUE, 
s));
+                        if (lexed.getValue() instanceof String s) {
+                            String formatted = "\"" + Jsoner.escape(s) + "\"";
+                            returnable.append(color.color(Yytoken.Types.VALUE, 
formatted));
                         } else {
                             returnable.append(color.color(Yytoken.Types.VALUE, 
lexed.getValue()));
                         }
@@ -941,30 +940,28 @@ public final class Jsoner {
         if (jsonSerializable == null) {
             /* When a null is passed in the word null is supported in JSON. */
             writableDestination.write("null");
-        } else if (jsonSerializable instanceof Jsonable && 
flags.contains(SerializationOptions.ALLOW_JSONABLES)) {
+        } else if (jsonSerializable instanceof Jsonable jsonable && 
flags.contains(SerializationOptions.ALLOW_JSONABLES)) {
             /* Writes the writable as defined by the writable. */
-            ((Jsonable) jsonSerializable).toJson(writableDestination);
-        } else if (jsonSerializable instanceof Enum
+            jsonable.toJson(writableDestination);
+        } else if (jsonSerializable instanceof Enum<?> e
                 && 
flags.contains(SerializationOptions.ALLOW_FULLY_QUALIFIED_ENUMERATIONS)) {
             /*
              * Writes the enum as a special case of string. All enums (unless
              * they implement Jsonable) will be the string literal
              * "${DECLARING_CLASS_NAME}.${ENUM_NAME}" as their value.
              */
-            @SuppressWarnings("rawtypes")
-            final Enum e = (Enum) jsonSerializable;
             writableDestination.write('"');
             writableDestination.write(e.getDeclaringClass().getName());
             writableDestination.write('.');
             writableDestination.write(e.name());
             writableDestination.write('"');
-        } else if (jsonSerializable instanceof String) {
+        } else if (jsonSerializable instanceof String s) {
             /* Make sure the string is properly escaped. */
             writableDestination.write('"');
-            writableDestination.write(Jsoner.escape((String) 
jsonSerializable));
+            writableDestination.write(Jsoner.escape(s));
             writableDestination.write('"');
-        } else if (jsonSerializable instanceof Double) {
-            if (((Double) jsonSerializable).isInfinite() || ((Double) 
jsonSerializable).isNaN()) {
+        } else if (jsonSerializable instanceof Double d) {
+            if (d.isInfinite() || d.isNaN()) {
                 /*
                  * Infinite and not a number are not supported by the JSON
                  * specification, so null is used instead.
@@ -973,8 +970,8 @@ public final class Jsoner {
             } else {
                 writableDestination.write(jsonSerializable.toString());
             }
-        } else if (jsonSerializable instanceof Float) {
-            if (((Float) jsonSerializable).isInfinite() || ((Float) 
jsonSerializable).isNaN()) {
+        } else if (jsonSerializable instanceof Float f) {
+            if (f.isInfinite() || f.isNaN()) {
                 /*
                  * Infinite and not a number are not supported by the JSON
                  * specification, so null is used instead.
@@ -987,11 +984,11 @@ public final class Jsoner {
             writableDestination.write(jsonSerializable.toString());
         } else if (jsonSerializable instanceof Boolean) {
             writableDestination.write(jsonSerializable.toString());
-        } else if (jsonSerializable instanceof Map) {
+        } else if (jsonSerializable instanceof Map<?, ?> map) {
             /* Writes the map in JSON object format. */
             boolean isFirstEntry = true;
             @SuppressWarnings("rawtypes")
-            final Iterator entries = ((Map) 
jsonSerializable).entrySet().iterator();
+            final Iterator entries = map.entrySet().iterator();
             writableDestination.write('{');
             while (entries.hasNext()) {
                 if (isFirstEntry) {
@@ -1006,11 +1003,11 @@ public final class Jsoner {
                 Jsoner.serialize(entry.getValue(), writableDestination, flags);
             }
             writableDestination.write('}');
-        } else if (jsonSerializable instanceof Collection) {
+        } else if (jsonSerializable instanceof Collection<?> collection) {
             /* Writes the collection in JSON array format. */
             boolean isFirstElement = true;
             @SuppressWarnings("rawtypes")
-            final Iterator elements = ((Collection) 
jsonSerializable).iterator();
+            final Iterator elements = collection.iterator();
             writableDestination.write('[');
             while (elements.hasNext()) {
                 if (isFirstElement) {
@@ -1021,9 +1018,8 @@ public final class Jsoner {
                 Jsoner.serialize(elements.next(), writableDestination, flags);
             }
             writableDestination.write(']');
-        } else if (jsonSerializable instanceof byte[]) {
+        } else if (jsonSerializable instanceof byte[] writableArray) {
             /* Writes the array in JSON array format. */
-            final byte[] writableArray = (byte[]) jsonSerializable;
             final int numberOfElements = writableArray.length;
             writableDestination.write('[');
             for (int i = 0; i < numberOfElements; i++) {
@@ -1035,9 +1031,8 @@ public final class Jsoner {
                 }
             }
             writableDestination.write(']');
-        } else if (jsonSerializable instanceof short[]) {
+        } else if (jsonSerializable instanceof short[] writableArray) {
             /* Writes the array in JSON array format. */
-            final short[] writableArray = (short[]) jsonSerializable;
             final int numberOfElements = writableArray.length;
             writableDestination.write('[');
             for (int i = 0; i < numberOfElements; i++) {
@@ -1049,9 +1044,8 @@ public final class Jsoner {
                 }
             }
             writableDestination.write(']');
-        } else if (jsonSerializable instanceof int[]) {
+        } else if (jsonSerializable instanceof int[] writableArray) {
             /* Writes the array in JSON array format. */
-            final int[] writableArray = (int[]) jsonSerializable;
             final int numberOfElements = writableArray.length;
             writableDestination.write('[');
             for (int i = 0; i < numberOfElements; i++) {
@@ -1063,9 +1057,8 @@ public final class Jsoner {
                 }
             }
             writableDestination.write(']');
-        } else if (jsonSerializable instanceof long[]) {
+        } else if (jsonSerializable instanceof long[] writableArray) {
             /* Writes the array in JSON array format. */
-            final long[] writableArray = (long[]) jsonSerializable;
             final int numberOfElements = writableArray.length;
             writableDestination.write('[');
             for (int i = 0; i < numberOfElements; i++) {
@@ -1077,9 +1070,8 @@ public final class Jsoner {
                 }
             }
             writableDestination.write(']');
-        } else if (jsonSerializable instanceof float[]) {
+        } else if (jsonSerializable instanceof float[] writableArray) {
             /* Writes the array in JSON array format. */
-            final float[] writableArray = (float[]) jsonSerializable;
             final int numberOfElements = writableArray.length;
             writableDestination.write('[');
             for (int i = 0; i < numberOfElements; i++) {
@@ -1091,9 +1083,8 @@ public final class Jsoner {
                 }
             }
             writableDestination.write(']');
-        } else if (jsonSerializable instanceof double[]) {
+        } else if (jsonSerializable instanceof double[] writableArray) {
             /* Writes the array in JSON array format. */
-            final double[] writableArray = (double[]) jsonSerializable;
             final int numberOfElements = writableArray.length;
             writableDestination.write('[');
             for (int i = 0; i < numberOfElements; i++) {
@@ -1105,9 +1096,8 @@ public final class Jsoner {
                 }
             }
             writableDestination.write(']');
-        } else if (jsonSerializable instanceof boolean[]) {
+        } else if (jsonSerializable instanceof boolean[] writableArray) {
             /* Writes the array in JSON array format. */
-            final boolean[] writableArray = (boolean[]) jsonSerializable;
             final int numberOfElements = writableArray.length;
             writableDestination.write('[');
             for (int i = 0; i < numberOfElements; i++) {
@@ -1119,9 +1109,8 @@ public final class Jsoner {
                 }
             }
             writableDestination.write(']');
-        } else if (jsonSerializable instanceof char[]) {
+        } else if (jsonSerializable instanceof char[] writableArray) {
             /* Writes the array in JSON array format. */
-            final char[] writableArray = (char[]) jsonSerializable;
             final int numberOfElements = writableArray.length;
             writableDestination.write("[\"");
             for (int i = 0; i < numberOfElements; i++) {
@@ -1133,9 +1122,8 @@ public final class Jsoner {
                 }
             }
             writableDestination.write("\"]");
-        } else if (jsonSerializable instanceof Object[]) {
+        } else if (jsonSerializable instanceof Object[] writableArray) {
             /* Writes the array in JSON array format. */
-            final Object[] writableArray = (Object[]) jsonSerializable;
             final int numberOfElements = writableArray.length;
             writableDestination.write('[');
             for (int i = 0; i < numberOfElements; i++) {

Reply via email to