Skip to content

Commit 90a31a2

Browse files
committed
Refactoring needed for JSONObject.populateMap() #984
1 parent a381060 commit 90a31a2

1 file changed

Lines changed: 37 additions & 37 deletions

File tree

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

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1782,56 +1782,56 @@ private void populateMap(Object bean, Set<Object> objectsRecord, JSONParserConfi
17821782

17831783
Method[] methods = includeSuperClass ? klass.getMethods() : klass.getDeclaredMethods();
17841784
for (final Method method : methods) {
1785-
final int modifiers = method.getModifiers();
1786-
if (Modifier.isPublic(modifiers)
1787-
&& !Modifier.isStatic(modifiers)
1788-
&& method.getParameterTypes().length == 0
1789-
&& !method.isBridge()
1790-
&& method.getReturnType() != Void.TYPE
1791-
&& isValidMethodName(method.getName())) {
1792-
final String key = getKeyNameFromMethod(method);
1785+
final String key = getKeyNameFromMethod(method);
1786+
try {
17931787
if (key != null && !key.isEmpty()) {
1794-
try {
1795-
final Object result = method.invoke(bean);
1796-
if (result != null || jsonParserConfiguration.isUseNativeNulls()) {
1797-
// check cyclic dependency and throw error if needed
1798-
// the wrap and populateMap combination method is
1799-
// itself DFS recursive
1800-
if (objectsRecord.contains(result)) {
1801-
throw recursivelyDefinedObjectException(key);
1802-
}
1803-
1804-
objectsRecord.add(result);
1805-
1806-
testValidity(result);
1807-
this.map.put(key, wrap(result, objectsRecord));
1808-
1809-
objectsRecord.remove(result);
1810-
1811-
// we don't use the result anywhere outside of wrap
1812-
// if it's a resource we should be sure to close it
1813-
// after calling toString
1814-
if (result instanceof Closeable) {
1815-
try {
1816-
((Closeable) result).close();
1817-
} catch (IOException ignore) {
1818-
}
1819-
}
1788+
final Object result = method.invoke(bean);
1789+
if (result != null || jsonParserConfiguration.isUseNativeNulls()) {
1790+
// check cyclic dependency and throw error if needed
1791+
// the wrap and populateMap combination method is
1792+
// itself DFS recursive
1793+
if (objectsRecord.contains(result)) {
1794+
throw recursivelyDefinedObjectException(key);
18201795
}
1821-
} catch (IllegalAccessException ignore) {
1822-
} catch (IllegalArgumentException ignore) {
1823-
} catch (InvocationTargetException ignore) {
1796+
1797+
objectsRecord.add(result);
1798+
1799+
testValidity(result);
1800+
this.map.put(key, wrap(result, objectsRecord));
1801+
1802+
objectsRecord.remove(result);
1803+
1804+
}
1805+
// we don't use the result anywhere outside of wrap
1806+
// if it's a resource we should be sure to close it
1807+
// after calling toString
1808+
if (result instanceof Closeable) {
1809+
((Closeable) result).close();
18241810
}
18251811
}
1812+
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | IOException ignore) {
18261813
}
18271814
}
18281815
}
18291816

1817+
private static boolean isValidGetterMethod(final Method method) {
1818+
final int modifiers = method.getModifiers();
1819+
return Modifier.isPublic(modifiers)
1820+
&& !Modifier.isStatic(modifiers)
1821+
&& method.getParameterTypes().length == 0
1822+
&& !method.isBridge()
1823+
&& method.getReturnType() != Void.TYPE
1824+
&& isValidMethodName(method.getName());
1825+
}
1826+
18301827
private static boolean isValidMethodName(String name) {
18311828
return !"getClass".equals(name) && !"getDeclaringClass".equals(name);
18321829
}
18331830

18341831
private static String getKeyNameFromMethod(Method method) {
1832+
if (!isValidGetterMethod(method)) {
1833+
return null;
1834+
}
18351835
final int ignoreDepth = getAnnotationDepth(method, JSONPropertyIgnore.class);
18361836
if (ignoreDepth > 0) {
18371837
final int forcedNameDepth = getAnnotationDepth(method, JSONPropertyName.class);

0 commit comments

Comments
 (0)