Skip to content

Commit a129ebe

Browse files
committed
Adds check for resources opened by our bean mapping
1 parent 641b68d commit a129ebe

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

JSONObject.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.json;
22

3+
import java.io.Closeable;
4+
35
/*
46
Copyright (c) 2002 JSON.org
57
@@ -1412,8 +1414,10 @@ private void populateMap(Object bean) {
14121414
.getDeclaredMethods();
14131415
for (final Method method : methods) {
14141416
final int modifiers = method.getModifiers();
1415-
if (Modifier.isPublic(modifiers) && !Modifier.isStatic(modifiers)
1416-
&& method.getParameterTypes().length == 0 && !method.isBridge()
1417+
if (Modifier.isPublic(modifiers)
1418+
&& !Modifier.isStatic(modifiers)
1419+
&& method.getParameterTypes().length == 0
1420+
&& !method.isBridge()
14171421
&& method.getReturnType() != Void.TYPE ) {
14181422
final String name = method.getName();
14191423
String key;
@@ -1427,7 +1431,8 @@ private void populateMap(Object bean) {
14271431
} else {
14281432
continue;
14291433
}
1430-
if (key.length() > 0 && Character.isUpperCase(key.charAt(0))) {
1434+
if (key.length() > 0
1435+
&& Character.isUpperCase(key.charAt(0))) {
14311436
if (key.length() == 1) {
14321437
key = key.toLowerCase(Locale.ROOT);
14331438
} else if (!Character.isUpperCase(key.charAt(1))) {
@@ -1439,6 +1444,14 @@ private void populateMap(Object bean) {
14391444
final Object result = method.invoke(bean);
14401445
if (result != null) {
14411446
this.map.put(key, wrap(result));
1447+
// we don't use the result anywhere outside of wrap
1448+
// if it's a resource we should be sure to close it after calling toString
1449+
if(result instanceof Closeable) {
1450+
try {
1451+
((Closeable)result).close();
1452+
} catch (IOException ignore) {
1453+
}
1454+
}
14421455
}
14431456
} catch (IllegalAccessException ignore) {
14441457
} catch (IllegalArgumentException ignore) {

0 commit comments

Comments
 (0)