Skip to content

Commit b38e206

Browse files
committed
refactored complex complex conditional
1 parent 20ea046 commit b38e206

File tree

1 file changed

+36
-35
lines changed

1 file changed

+36
-35
lines changed

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

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1756,56 +1756,57 @@ private void populateMap(Object bean, Set<Object> objectsRecord) {
17561756
Class<?> klass = bean.getClass();
17571757

17581758
// If klass is a System class then set includeSuperClass to false.
1759-
17601759
boolean includeSuperClass = klass.getClassLoader() != null;
17611760

17621761
Method[] methods = includeSuperClass ? klass.getMethods() : klass.getDeclaredMethods();
17631762
for (final Method method : methods) {
1764-
final int modifiers = method.getModifiers();
1765-
if (Modifier.isPublic(modifiers)
1766-
&& !Modifier.isStatic(modifiers)
1767-
&& method.getParameterTypes().length == 0
1768-
&& !method.isBridge()
1769-
&& method.getReturnType() != Void.TYPE
1770-
&& isValidMethodName(method.getName())) {
1763+
if (isEligibleMethod(method)) {
17711764
final String key = getKeyNameFromMethod(method);
1772-
if (key != null && !key.isEmpty()) {
1765+
if (isValidKey(key)) {
17731766
try {
17741767
final Object result = method.invoke(bean);
17751768
if (result != null) {
1776-
// check cyclic dependency and throw error if needed
1777-
// the wrap and populateMap combination method is
1778-
// itself DFS recursive
1779-
if (objectsRecord.contains(result)) {
1780-
throw recursivelyDefinedObjectException(key);
1781-
}
1782-
1783-
objectsRecord.add(result);
1784-
1785-
testValidity(result);
1786-
this.map.put(key, wrap(result, objectsRecord));
1787-
1788-
objectsRecord.remove(result);
1789-
1790-
// we don't use the result anywhere outside of wrap
1791-
// if it's a resource we should be sure to close it
1792-
// after calling toString
1793-
if (result instanceof Closeable) {
1794-
try {
1795-
((Closeable) result).close();
1796-
} catch (IOException ignore) {
1797-
}
1798-
}
1769+
processResult(bean, objectsRecord, key, result);
17991770
}
1800-
} catch (IllegalAccessException ignore) {
1801-
} catch (IllegalArgumentException ignore) {
1802-
} catch (InvocationTargetException ignore) {
1771+
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ignore) {
18031772
}
18041773
}
18051774
}
18061775
}
18071776
}
18081777

1778+
private boolean isEligibleMethod(Method method) {
1779+
final int modifiers = method.getModifiers();
1780+
return Modifier.isPublic(modifiers)
1781+
&& !Modifier.isStatic(modifiers)
1782+
&& method.getParameterTypes().length == 0
1783+
&& !method.isBridge()
1784+
&& method.getReturnType() != Void.TYPE
1785+
&& isValidMethodName(method.getName());
1786+
}
1787+
1788+
private boolean isValidKey(String key) {
1789+
return key != null && !key.isEmpty();
1790+
}
1791+
1792+
private void processResult(Object bean, Set<Object> objectsRecord, String key, Object result) throws IllegalAccessException, InvocationTargetException {
1793+
if (objectsRecord.contains(result)) {
1794+
throw recursivelyDefinedObjectException(key);
1795+
}
1796+
1797+
objectsRecord.add(result);
1798+
testValidity(result);
1799+
this.map.put(key, wrap(result, objectsRecord));
1800+
objectsRecord.remove(result);
1801+
1802+
if (result instanceof Closeable) {
1803+
try {
1804+
((Closeable) result).close();
1805+
} catch (IOException ignore) {
1806+
}
1807+
}
1808+
}
1809+
18091810
private static boolean isValidMethodName(String name) {
18101811
return !"getClass".equals(name) && !"getDeclaringClass".equals(name);
18111812
}

0 commit comments

Comments
 (0)