1
0
Fork 0
mirror of https://github.com/ethauvin/JSON-java.git synced 2025-06-17 07:50:52 -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,39 +1398,41 @@ 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();
if (name.startsWith("get")) { String key;
if ("getClass".equals(name) if (name.startsWith("get")) {
|| "getDeclaringClass".equals(name)) { if ("getClass".equals(name) || "getDeclaringClass".equals(name)) {
key = ""; continue;
} 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 && Character.isUpperCase(key.charAt(0))) {
if (key.length() == 1) {
key = key.toLowerCase(Locale.ROOT);
} else if (!Character.isUpperCase(key.charAt(1))) {
key = key.substring(0, 1).toLowerCase(Locale.ROOT)
+ key.substring(1);
} }
if (key.length() > 0
&& Character.isUpperCase(key.charAt(0))
&& method.getParameterTypes().length == 0) {
if (key.length() == 1) {
key = key.toLowerCase(Locale.ROOT);
} else if (!Character.isUpperCase(key.charAt(1))) {
key = key.substring(0, 1).toLowerCase(Locale.ROOT)
+ 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) {
} }
} }
} }