1
0
Fork 0
mirror of https://github.com/ethauvin/JSON-java.git synced 2025-06-17 07:50:52 -07:00

Adds check for resources opened by our bean mapping

This commit is contained in:
John J. Aylward 2017-07-09 17:36:36 -04:00
parent 641b68dd55
commit a129ebe8e4

View file

@ -1,5 +1,7 @@
package org.json;
import java.io.Closeable;
/*
Copyright (c) 2002 JSON.org
@ -1412,8 +1414,10 @@ public class JSONObject {
.getDeclaredMethods();
for (final Method method : methods) {
final int modifiers = method.getModifiers();
if (Modifier.isPublic(modifiers) && !Modifier.isStatic(modifiers)
&& method.getParameterTypes().length == 0 && !method.isBridge()
if (Modifier.isPublic(modifiers)
&& !Modifier.isStatic(modifiers)
&& method.getParameterTypes().length == 0
&& !method.isBridge()
&& method.getReturnType() != Void.TYPE ) {
final String name = method.getName();
String key;
@ -1427,7 +1431,8 @@ public class JSONObject {
} else {
continue;
}
if (key.length() > 0 && Character.isUpperCase(key.charAt(0))) {
if (key.length() > 0
&& Character.isUpperCase(key.charAt(0))) {
if (key.length() == 1) {
key = key.toLowerCase(Locale.ROOT);
} else if (!Character.isUpperCase(key.charAt(1))) {
@ -1439,6 +1444,14 @@ public class JSONObject {
final Object result = method.invoke(bean);
if (result != null) {
this.map.put(key, wrap(result));
// we don't use the result anywhere outside of wrap
// if it's a resource we should be sure to close it after calling toString
if(result instanceof Closeable) {
try {
((Closeable)result).close();
} catch (IOException ignore) {
}
}
}
} catch (IllegalAccessException ignore) {
} catch (IllegalArgumentException ignore) {