Skip to content

Commit 643b251

Browse files
author
John J. Aylward
committed
Updates for populateMap based on discussion in stleary#279 and stleary#264
1 parent 5024f2d commit 643b251

File tree

1 file changed

+29
-26
lines changed

1 file changed

+29
-26
lines changed

JSONObject.java

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ of this software and associated documentation files (the "Software"), to deal
2828
import java.io.StringWriter;
2929
import java.io.Writer;
3030
import java.lang.reflect.Field;
31+
import java.lang.reflect.InvocationTargetException;
3132
import java.lang.reflect.Method;
3233
import java.lang.reflect.Modifier;
3334
import java.math.BigDecimal;
@@ -1397,39 +1398,41 @@ private void populateMap(Object bean) {
13971398

13981399
Method[] methods = includeSuperClass ? klass.getMethods() : klass
13991400
.getDeclaredMethods();
1400-
for (int i = 0; i < methods.length; i += 1) {
1401-
try {
1402-
Method method = methods[i];
1403-
if (Modifier.isPublic(method.getModifiers())) {
1404-
String name = method.getName();
1405-
String key = "";
1406-
if (name.startsWith("get")) {
1407-
if ("getClass".equals(name)
1408-
|| "getDeclaringClass".equals(name)) {
1409-
key = "";
1410-
} else {
1411-
key = name.substring(3);
1412-
}
1413-
} else if (name.startsWith("is")) {
1414-
key = name.substring(2);
1401+
for (final Method method : methods) {
1402+
final int modifiers = method.getModifiers();
1403+
if (Modifier.isPublic(modifiers) && !Modifier.isStatic(modifiers)
1404+
&& method.getParameterTypes().length == 0 && !method.isBridge()
1405+
&& method.getReturnType() != Void.TYPE ) {
1406+
final String name = method.getName();
1407+
String key;
1408+
if (name.startsWith("get")) {
1409+
if ("getClass".equals(name) || "getDeclaringClass".equals(name)) {
1410+
continue;
1411+
}
1412+
key = name.substring(3);
1413+
} else if (name.startsWith("is")) {
1414+
key = name.substring(2);
1415+
} else {
1416+
continue;
1417+
}
1418+
if (key.length() > 0 && Character.isUpperCase(key.charAt(0))) {
1419+
if (key.length() == 1) {
1420+
key = key.toLowerCase(Locale.ROOT);
1421+
} else if (!Character.isUpperCase(key.charAt(1))) {
1422+
key = key.substring(0, 1).toLowerCase(Locale.ROOT)
1423+
+ key.substring(1);
14151424
}
1416-
if (key.length() > 0
1417-
&& Character.isUpperCase(key.charAt(0))
1418-
&& method.getParameterTypes().length == 0) {
1419-
if (key.length() == 1) {
1420-
key = key.toLowerCase(Locale.ROOT);
1421-
} else if (!Character.isUpperCase(key.charAt(1))) {
1422-
key = key.substring(0, 1).toLowerCase(Locale.ROOT)
1423-
+ key.substring(1);
1424-
}
14251425

1426-
Object result = method.invoke(bean, (Object[]) null);
1426+
try {
1427+
final Object result = method.invoke(bean);
14271428
if (result != null) {
14281429
this.map.put(key, wrap(result));
14291430
}
1431+
} catch (IllegalAccessException ignore) {
1432+
} catch (IllegalArgumentException ignore) {
1433+
} catch (InvocationTargetException ignore) {
14301434
}
14311435
}
1432-
} catch (Exception ignore) {
14331436
}
14341437
}
14351438
}

0 commit comments

Comments
 (0)