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

Updates for populateMap based on discussion in #279 and #264

This commit is contained in:
John J. Aylward 2017-07-07 20:48:42 -04:00
parent 5024f2d210
commit 643b25140f

View file

@ -28,6 +28,7 @@ import java.io.IOException;
import java.io.StringWriter; import java.io.StringWriter;
import java.io.Writer; import java.io.Writer;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.math.BigDecimal; import java.math.BigDecimal;
@ -1397,25 +1398,24 @@ public class JSONObject {
Method[] methods = includeSuperClass ? klass.getMethods() : klass Method[] methods = includeSuperClass ? klass.getMethods() : klass
.getDeclaredMethods(); .getDeclaredMethods();
for (int i = 0; i < methods.length; i += 1) { for (final Method method : methods) {
try { final int modifiers = method.getModifiers();
Method method = methods[i]; if (Modifier.isPublic(modifiers) && !Modifier.isStatic(modifiers)
if (Modifier.isPublic(method.getModifiers())) { && method.getParameterTypes().length == 0 && !method.isBridge()
String name = method.getName(); && method.getReturnType() != Void.TYPE ) {
String key = ""; final String name = method.getName();
String key;
if (name.startsWith("get")) { if (name.startsWith("get")) {
if ("getClass".equals(name) if ("getClass".equals(name) || "getDeclaringClass".equals(name)) {
|| "getDeclaringClass".equals(name)) { continue;
key = "";
} else {
key = name.substring(3);
} }
key = name.substring(3);
} else if (name.startsWith("is")) { } else if (name.startsWith("is")) {
key = name.substring(2); key = name.substring(2);
} else {
continue;
} }
if (key.length() > 0 if (key.length() > 0 && Character.isUpperCase(key.charAt(0))) {
&& Character.isUpperCase(key.charAt(0))
&& method.getParameterTypes().length == 0) {
if (key.length() == 1) { if (key.length() == 1) {
key = key.toLowerCase(Locale.ROOT); key = key.toLowerCase(Locale.ROOT);
} else if (!Character.isUpperCase(key.charAt(1))) { } else if (!Character.isUpperCase(key.charAt(1))) {
@ -1423,13 +1423,16 @@ public class JSONObject {
+ key.substring(1); + key.substring(1);
} }
Object result = method.invoke(bean, (Object[]) null); try {
final Object result = method.invoke(bean);
if (result != null) { if (result != null) {
this.map.put(key, wrap(result)); this.map.put(key, wrap(result));
} }
} catch (IllegalAccessException ignore) {
} catch (IllegalArgumentException ignore) {
} catch (InvocationTargetException ignore) {
} }
} }
} catch (Exception ignore) {
} }
} }
} }