Skip to content

Commit 82432f0

Browse files
authored
Merge pull request #1000 from marilynel/master
fixing sonarcube issues
2 parents 0a9364e + e762629 commit 82432f0

File tree

2 files changed

+78
-36
lines changed

2 files changed

+78
-36
lines changed

src/main/java/org/json/JSONObject.java

Lines changed: 54 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1785,46 +1785,77 @@ private void populateMap(Object bean, JSONParserConfiguration jsonParserConfigur
17851785
populateMap(bean, Collections.newSetFromMap(new IdentityHashMap<Object, Boolean>()), jsonParserConfiguration);
17861786
}
17871787

1788+
/**
1789+
* Convert a bean into a json object
1790+
* @param bean object tobe converted
1791+
* @param objectsRecord set of all objects for this method
1792+
* @param jsonParserConfiguration json parser settings
1793+
*/
17881794
private void populateMap(Object bean, Set<Object> objectsRecord, JSONParserConfiguration jsonParserConfiguration) {
17891795
Class<?> klass = bean.getClass();
17901796

17911797
// If klass is a System class then set includeSuperClass to false.
17921798

1793-
boolean includeSuperClass = klass.getClassLoader() != null;
1794-
1795-
Method[] methods = includeSuperClass ? klass.getMethods() : klass.getDeclaredMethods();
1799+
Method[] methods = getMethods(klass);
17961800
for (final Method method : methods) {
17971801
if (isValidMethod(method)) {
17981802
final String key = getKeyNameFromMethod(method);
17991803
if (key != null && !key.isEmpty()) {
1800-
try {
1801-
final Object result = method.invoke(bean);
1802-
if (result != null || jsonParserConfiguration.isUseNativeNulls()) {
1803-
// check cyclic dependency and throw error if needed
1804-
// the wrap and populateMap combination method is
1805-
// itself DFS recursive
1806-
if (objectsRecord.contains(result)) {
1807-
throw recursivelyDefinedObjectException(key);
1808-
}
1804+
processMethod(bean, objectsRecord, jsonParserConfiguration, method, key);
1805+
}
1806+
}
1807+
}
1808+
}
18091809

1810-
objectsRecord.add(result);
1810+
/**
1811+
* Processes method into json object entry if appropriate
1812+
* @param bean object being processed (owns the method)
1813+
* @param objectsRecord set of all objects for this method
1814+
* @param jsonParserConfiguration json parser settings
1815+
* @param method method being processed
1816+
* @param key name of the method
1817+
*/
1818+
private void processMethod(Object bean, Set<Object> objectsRecord, JSONParserConfiguration jsonParserConfiguration,
1819+
Method method, String key) {
1820+
try {
1821+
final Object result = method.invoke(bean);
1822+
if (result != null || jsonParserConfiguration.isUseNativeNulls()) {
1823+
// check cyclic dependency and throw error if needed
1824+
// the wrap and populateMap combination method is
1825+
// itself DFS recursive
1826+
if (objectsRecord.contains(result)) {
1827+
throw recursivelyDefinedObjectException(key);
1828+
}
18111829

1812-
testValidity(result);
1813-
this.map.put(key, wrap(result, objectsRecord));
1830+
objectsRecord.add(result);
18141831

1815-
objectsRecord.remove(result);
1832+
testValidity(result);
1833+
this.map.put(key, wrap(result, objectsRecord));
18161834

1817-
closeClosable(result);
1818-
}
1819-
} catch (IllegalAccessException ignore) {
1820-
} catch (IllegalArgumentException ignore) {
1821-
} catch (InvocationTargetException ignore) {
1822-
}
1823-
}
1835+
objectsRecord.remove(result);
1836+
1837+
closeClosable(result);
18241838
}
1839+
} catch (IllegalAccessException ignore) {
1840+
// ignore exception
1841+
} catch (IllegalArgumentException ignore) {
1842+
// ignore exception
1843+
} catch (InvocationTargetException ignore) {
1844+
// ignore exception
18251845
}
18261846
}
18271847

1848+
/**
1849+
* This is a convenience method to simplify populate maps
1850+
* @param klass the name of the object being checked
1851+
* @return methods of klass
1852+
*/
1853+
private static Method[] getMethods(Class<?> klass) {
1854+
boolean includeSuperClass = klass.getClassLoader() != null;
1855+
1856+
return includeSuperClass ? klass.getMethods() : klass.getDeclaredMethods();
1857+
}
1858+
18281859
private static boolean isValidMethodName(String name) {
18291860
return !"getClass".equals(name) && !"getDeclaringClass".equals(name);
18301861
}

src/test/java/org/json/junit/JSONObjectTest.java

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4000,19 +4000,24 @@ public void testStrictModeJSONTokener_expectException(){
40004000
@Test
40014001
public void test_strictModeWithMisCasedBooleanOrNullValue(){
40024002
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration().withStrictMode();
4003-
40044003
try{
4005-
JSONObject j1 = new JSONObject("{\"a\":True}", jsonParserConfiguration);
4004+
new JSONObject("{\"a\":True}", jsonParserConfiguration);
40064005
fail("Expected an exception");
4007-
} catch (JSONException e) { }
4006+
} catch (JSONException e) {
4007+
// No action, expected outcome
4008+
}
40084009
try{
4009-
JSONObject j2 = new JSONObject("{\"a\":TRUE}", jsonParserConfiguration);
4010+
new JSONObject("{\"a\":TRUE}", jsonParserConfiguration);
40104011
fail("Expected an exception");
4011-
} catch (JSONException e) { }
4012+
} catch (JSONException e) {
4013+
// No action, expected outcome
4014+
}
40124015
try{
4013-
JSONObject j2 = new JSONObject("{\"a\":nUlL}", jsonParserConfiguration);
4016+
new JSONObject("{\"a\":nUlL}", jsonParserConfiguration);
40144017
fail("Expected an exception");
4015-
} catch (JSONException e) { }
4018+
} catch (JSONException e) {
4019+
// No action, expected outcome
4020+
}
40164021
}
40174022

40184023
@Test
@@ -4021,17 +4026,23 @@ public void test_strictModeWithInappropriateKey(){
40214026

40224027
// Parsing the following objects should fail
40234028
try{
4024-
JSONObject j3 = new JSONObject("{true : 3}", jsonParserConfiguration);
4029+
new JSONObject("{true : 3}", jsonParserConfiguration);
40254030
fail("Expected an exception");
4026-
} catch (JSONException e) { }
4031+
} catch (JSONException e) {
4032+
// No action, expected outcome
4033+
}
40274034
try{
4028-
JSONObject j4 = new JSONObject("{TRUE : 3}", jsonParserConfiguration);
4035+
new JSONObject("{TRUE : 3}", jsonParserConfiguration);
40294036
fail("Expected an exception");
4030-
} catch (JSONException e) { }
4037+
} catch (JSONException e) {
4038+
// No action, expected outcome
4039+
}
40314040
try{
4032-
JSONObject j5 = new JSONObject("{1 : 3}", jsonParserConfiguration);
4041+
new JSONObject("{1 : 3}", jsonParserConfiguration);
40334042
fail("Expected an exception");
4034-
} catch (JSONException e) { }
4043+
} catch (JSONException e) {
4044+
// No action, expected outcome
4045+
}
40354046

40364047
}
40374048

0 commit comments

Comments
 (0)