Skip to content

Commit 7fc41a6

Browse files
committed
addressing cognitive complextity
1 parent d5d82cd commit 7fc41a6

File tree

1 file changed

+55
-23
lines changed

1 file changed

+55
-23
lines changed

src/main/java/org/json/JSONObject.java

Lines changed: 55 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1785,46 +1785,78 @@ private void populateMap(Object bean, JSONParserConfiguration jsonParserConfigur
17851785
populateMap(bean, Collections.newSetFromMap(new IdentityHashMap<Object, Boolean>()), jsonParserConfiguration);
17861786
}
17871787

1788+
/**
1789+
* Convert a bean into a json object
1790+
* @param bean object tobe converted
1791+
* @param objectsRecord set of all objects for this method
1792+
* @param jsonParserConfiguration json parser settings
1793+
*/
17881794
private void populateMap(Object bean, Set<Object> objectsRecord, JSONParserConfiguration jsonParserConfiguration) {
17891795
Class<?> klass = bean.getClass();
17901796

17911797
// If klass is a System class then set includeSuperClass to false.
17921798

1793-
boolean includeSuperClass = klass.getClassLoader() != null;
1794-
1795-
Method[] methods = includeSuperClass ? klass.getMethods() : klass.getDeclaredMethods();
1799+
Method[] methods = getMethods(klass);
17961800
for (final Method method : methods) {
17971801
if (isValidMethod(method)) {
17981802
final String key = getKeyNameFromMethod(method);
17991803
if (key != null && !key.isEmpty()) {
1800-
try {
1801-
final Object result = method.invoke(bean);
1802-
if (result != null || jsonParserConfiguration.isUseNativeNulls()) {
1803-
// check cyclic dependency and throw error if needed
1804-
// the wrap and populateMap combination method is
1805-
// itself DFS recursive
1806-
if (objectsRecord.contains(result)) {
1807-
throw recursivelyDefinedObjectException(key);
1808-
}
1804+
processMethod(bean, objectsRecord, jsonParserConfiguration, method, key);
1805+
}
1806+
}
1807+
}
1808+
}
18091809

1810-
objectsRecord.add(result);
1810+
/**
1811+
* Processes method into json object entry if appropriate
1812+
* @param bean object being processed (owns the method)
1813+
* @param objectsRecord set of all objects for this method
1814+
* @param jsonParserConfiguration json parser settings
1815+
* @param method method being processed
1816+
* @param key name of the method
1817+
*/
1818+
private void processMethod(Object bean, Set<Object> objectsRecord, JSONParserConfiguration jsonParserConfiguration,
1819+
Method method, String key) {
1820+
try {
1821+
final Object result = method.invoke(bean);
1822+
if (result != null || jsonParserConfiguration.isUseNativeNulls()) {
1823+
// check cyclic dependency and throw error if needed
1824+
// the wrap and populateMap combination method is
1825+
// itself DFS recursive
1826+
if (objectsRecord.contains(result)) {
1827+
throw recursivelyDefinedObjectException(key);
1828+
}
18111829

1812-
testValidity(result);
1813-
this.map.put(key, wrap(result, objectsRecord));
1830+
objectsRecord.add(result);
18141831

1815-
objectsRecord.remove(result);
1832+
testValidity(result);
1833+
this.map.put(key, wrap(result, objectsRecord));
18161834

1817-
closeClosable(result);
1818-
}
1819-
} catch (IllegalAccessException ignore) {
1820-
} catch (IllegalArgumentException ignore) {
1821-
} catch (InvocationTargetException ignore) {
1822-
}
1823-
}
1835+
objectsRecord.remove(result);
1836+
1837+
closeClosable(result);
18241838
}
1839+
} catch (IllegalAccessException ignore) {
1840+
// ignore exception
1841+
} catch (IllegalArgumentException ignore) {
1842+
// ignore exception
1843+
} catch (InvocationTargetException ignore) {
1844+
// ignore exception
18251845
}
18261846
}
18271847

1848+
/**
1849+
* This is a convenience method to simplify populate maps
1850+
* @param klass the name of the object being checked
1851+
* @return methods of klass
1852+
*/
1853+
private static Method[] getMethods(Class<?> klass) {
1854+
boolean includeSuperClass = klass.getClassLoader() != null;
1855+
1856+
Method[] methods = includeSuperClass ? klass.getMethods() : klass.getDeclaredMethods();
1857+
return methods;
1858+
}
1859+
18281860
private static boolean isValidMethodName(String name) {
18291861
return !"getClass".equals(name) && !"getDeclaringClass".equals(name);
18301862
}

0 commit comments

Comments
 (0)