mirror of
https://github.com/stleary/JSON-java.git
synced 2025-08-03 03:15:32 -04:00
addressing cognitive complextity
This commit is contained in:
parent
d5d82cdb87
commit
7fc41a6c0e
@ -1785,46 +1785,78 @@ public class JSONObject {
|
|||||||
populateMap(bean, Collections.newSetFromMap(new IdentityHashMap<Object, Boolean>()), jsonParserConfiguration);
|
populateMap(bean, Collections.newSetFromMap(new IdentityHashMap<Object, Boolean>()), 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<Object> objectsRecord, JSONParserConfiguration jsonParserConfiguration) {
|
private void populateMap(Object bean, Set<Object> objectsRecord, JSONParserConfiguration jsonParserConfiguration) {
|
||||||
Class<?> klass = bean.getClass();
|
Class<?> klass = bean.getClass();
|
||||||
|
|
||||||
// If klass is a System class then set includeSuperClass to false.
|
// If klass is a System class then set includeSuperClass to false.
|
||||||
|
|
||||||
boolean includeSuperClass = klass.getClassLoader() != null;
|
Method[] methods = getMethods(klass);
|
||||||
|
|
||||||
Method[] methods = includeSuperClass ? klass.getMethods() : klass.getDeclaredMethods();
|
|
||||||
for (final Method method : methods) {
|
for (final Method method : methods) {
|
||||||
if (isValidMethod(method)) {
|
if (isValidMethod(method)) {
|
||||||
final String key = getKeyNameFromMethod(method);
|
final String key = getKeyNameFromMethod(method);
|
||||||
if (key != null && !key.isEmpty()) {
|
if (key != null && !key.isEmpty()) {
|
||||||
try {
|
processMethod(bean, objectsRecord, jsonParserConfiguration, method, key);
|
||||||
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) {
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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<Object> 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) {
|
private static boolean isValidMethodName(String name) {
|
||||||
return !"getClass".equals(name) && !"getDeclaringClass".equals(name);
|
return !"getClass".equals(name) && !"getDeclaringClass".equals(name);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user