From d5d82cdb8706387e2075688ee9f55f1c8e052a50 Mon Sep 17 00:00:00 2001 From: marilynel Date: Sun, 20 Jul 2025 11:31:29 -0800 Subject: [PATCH 1/3] fixing sonarcube issues --- .../java/org/json/junit/JSONObjectTest.java | 37 ++++++++++++------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 52a5240..3c34368 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -4000,19 +4000,24 @@ public class JSONObjectTest { @Test public void test_strictModeWithMisCasedBooleanOrNullValue(){ JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration().withStrictMode(); - try{ - JSONObject j1 = new JSONObject("{\"a\":True}", jsonParserConfiguration); + new JSONObject("{\"a\":True}", jsonParserConfiguration); fail("Expected an exception"); - } catch (JSONException e) { } + } catch (JSONException e) { + // No action, expected outcome + } try{ - JSONObject j2 = new JSONObject("{\"a\":TRUE}", jsonParserConfiguration); + new JSONObject("{\"a\":TRUE}", jsonParserConfiguration); fail("Expected an exception"); - } catch (JSONException e) { } + } catch (JSONException e) { + // No action, expected outcome + } try{ - JSONObject j2 = new JSONObject("{\"a\":nUlL}", jsonParserConfiguration); + new JSONObject("{\"a\":nUlL}", jsonParserConfiguration); fail("Expected an exception"); - } catch (JSONException e) { } + } catch (JSONException e) { + // No action, expected outcome + } } @Test @@ -4021,17 +4026,23 @@ public class JSONObjectTest { // Parsing the following objects should fail try{ - JSONObject j3 = new JSONObject("{true : 3}", jsonParserConfiguration); + new JSONObject("{true : 3}", jsonParserConfiguration); fail("Expected an exception"); - } catch (JSONException e) { } + } catch (JSONException e) { + // No action, expected outcome + } try{ - JSONObject j4 = new JSONObject("{TRUE : 3}", jsonParserConfiguration); + new JSONObject("{TRUE : 3}", jsonParserConfiguration); fail("Expected an exception"); - } catch (JSONException e) { } + } catch (JSONException e) { + // No action, expected outcome + } try{ - JSONObject j5 = new JSONObject("{1 : 3}", jsonParserConfiguration); + new JSONObject("{1 : 3}", jsonParserConfiguration); fail("Expected an exception"); - } catch (JSONException e) { } + } catch (JSONException e) { + // No action, expected outcome + } } From 7fc41a6c0ee191c0d201fd95252328238a4555fd Mon Sep 17 00:00:00 2001 From: marilynel Date: Sun, 20 Jul 2025 11:58:30 -0800 Subject: [PATCH 2/3] addressing cognitive complextity --- src/main/java/org/json/JSONObject.java | 84 ++++++++++++++++++-------- 1 file changed, 58 insertions(+), 26 deletions(-) diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index 10fed95..e2674e3 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -1785,46 +1785,78 @@ public class JSONObject { populateMap(bean, Collections.newSetFromMap(new IdentityHashMap()), jsonParserConfiguration); } + /** + * Convert a bean into a json object + * @param bean object tobe converted + * @param objectsRecord set of all objects for this method + * @param jsonParserConfiguration json parser settings + */ private void populateMap(Object bean, Set objectsRecord, JSONParserConfiguration jsonParserConfiguration) { Class klass = bean.getClass(); // If klass is a System class then set includeSuperClass to false. - boolean includeSuperClass = klass.getClassLoader() != null; - - Method[] methods = includeSuperClass ? klass.getMethods() : klass.getDeclaredMethods(); + Method[] methods = getMethods(klass); for (final Method method : methods) { if (isValidMethod(method)) { final String key = getKeyNameFromMethod(method); if (key != null && !key.isEmpty()) { - try { - final Object result = method.invoke(bean); - if (result != null || jsonParserConfiguration.isUseNativeNulls()) { - // check cyclic dependency and throw error if needed - // the wrap and populateMap combination method is - // itself DFS recursive - if (objectsRecord.contains(result)) { - throw recursivelyDefinedObjectException(key); - } - - objectsRecord.add(result); - - testValidity(result); - this.map.put(key, wrap(result, objectsRecord)); - - objectsRecord.remove(result); - - closeClosable(result); - } - } catch (IllegalAccessException ignore) { - } catch (IllegalArgumentException ignore) { - } catch (InvocationTargetException ignore) { - } + processMethod(bean, objectsRecord, jsonParserConfiguration, method, key); } } } } + /** + * Processes method into json object entry if appropriate + * @param bean object being processed (owns the method) + * @param objectsRecord set of all objects for this method + * @param jsonParserConfiguration json parser settings + * @param method method being processed + * @param key name of the method + */ + private void processMethod(Object bean, Set objectsRecord, JSONParserConfiguration jsonParserConfiguration, + Method method, String key) { + try { + final Object result = method.invoke(bean); + if (result != null || jsonParserConfiguration.isUseNativeNulls()) { + // check cyclic dependency and throw error if needed + // the wrap and populateMap combination method is + // itself DFS recursive + if (objectsRecord.contains(result)) { + throw recursivelyDefinedObjectException(key); + } + + objectsRecord.add(result); + + testValidity(result); + this.map.put(key, wrap(result, objectsRecord)); + + objectsRecord.remove(result); + + closeClosable(result); + } + } catch (IllegalAccessException ignore) { + // ignore exception + } catch (IllegalArgumentException ignore) { + // ignore exception + } catch (InvocationTargetException ignore) { + // ignore exception + } + } + + /** + * This is a convenience method to simplify populate maps + * @param klass the name of the object being checked + * @return methods of klass + */ + private static Method[] getMethods(Class klass) { + boolean includeSuperClass = klass.getClassLoader() != null; + + Method[] methods = includeSuperClass ? klass.getMethods() : klass.getDeclaredMethods(); + return methods; + } + private static boolean isValidMethodName(String name) { return !"getClass".equals(name) && !"getDeclaringClass".equals(name); } From e762629bcc37e4e543e6f53655430b0224a58e28 Mon Sep 17 00:00:00 2001 From: marilynel Date: Sun, 20 Jul 2025 12:04:51 -0800 Subject: [PATCH 3/3] oops one more sonarcube issue lol --- src/main/java/org/json/JSONObject.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index e2674e3..ee61d47 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -1853,8 +1853,7 @@ public class JSONObject { private static Method[] getMethods(Class klass) { boolean includeSuperClass = klass.getClassLoader() != null; - Method[] methods = includeSuperClass ? klass.getMethods() : klass.getDeclaredMethods(); - return methods; + return includeSuperClass ? klass.getMethods() : klass.getDeclaredMethods(); } private static boolean isValidMethodName(String name) {