@@ -302,16 +302,8 @@ public <T extends Object> T getObject(String propertyName, Class<T> c) {
302
302
"Failed to instantiate class object." , e );
303
303
}
304
304
} else {
305
- // POJO.
306
- if (c .isAnonymousClass () || c .isLocalClass ()) {
307
- throw new IllegalArgumentException (
308
- String .format ("%s can't be an anonymous or local class." , c .getName ()));
309
- }
310
-
311
- if (c .isMemberClass () && !Modifier .isStatic (c .getModifiers ())) {
312
- throw new IllegalArgumentException (
313
- String .format ("%s must be static if it's a member class." , c .getName ()));
314
- }
305
+ // POJO
306
+ checkForValidPOJO (c );
315
307
316
308
try {
317
309
return new ObjectMapper ().readValue (jsonObj .toString (), c );
@@ -325,6 +317,16 @@ public <T extends Object> T getObject(String propertyName, Class<T> c) {
325
317
return null ;
326
318
}
327
319
320
+ private static void checkForValidPOJO (Class <?> c ){
321
+ if (c .isAnonymousClass () || c .isLocalClass ()) {
322
+ throw new IllegalArgumentException (
323
+ String .format ("%s can't be an anonymous or local class." , c .getName ()));
324
+ }
325
+ if (c .isMemberClass () && !Modifier .isStatic (c .getModifiers ())) {
326
+ throw new IllegalArgumentException (
327
+ String .format ("%s must be static if it's a member class." , c .getName ()));
328
+ }
329
+ }
328
330
/**
329
331
* Gets an object collection.
330
332
*
@@ -339,13 +341,25 @@ public <T extends Object> Collection<T> getCollection(String propertyName, Class
339
341
JSONArray jsonArray = this .propertyBag .getJSONArray (propertyName );
340
342
Collection <T > result = new ArrayList <T >();
341
343
ObjectMapper mapper = null ;
344
+ boolean isBaseClass = false ;
345
+ boolean isJsonSerializable = false ;
346
+
347
+ // Check once.
348
+ if (Number .class .isAssignableFrom (c ) || String .class .isAssignableFrom (c ) ||
349
+ Boolean .class .isAssignableFrom (c )) {
350
+ isBaseClass = true ;
351
+ } else if (JsonSerializable .class .isAssignableFrom (c )) {
352
+ isJsonSerializable = true ;
353
+ } else {
354
+ checkForValidPOJO (c );
355
+ mapper = new ObjectMapper ();
356
+ }
342
357
343
358
for (int i = 0 ; i < jsonArray .length (); i ++) {
344
- if (Number .class .isAssignableFrom (c ) || String .class .isAssignableFrom (c ) ||
345
- Boolean .class .isAssignableFrom (c )) {
359
+ if (isBaseClass ) {
346
360
// Number, String, Boolean
347
361
result .add (c .cast (jsonArray .get (i )));
348
- } else if (JsonSerializable . class . isAssignableFrom ( c ) ) {
362
+ } else if (isJsonSerializable ) {
349
363
JSONObject jsonObject = jsonArray .getJSONObject (i );
350
364
// JsonSerializable
351
365
try {
@@ -358,14 +372,6 @@ public <T extends Object> Collection<T> getCollection(String propertyName, Class
358
372
} else {
359
373
JSONObject jsonObject = jsonArray .getJSONObject (i );
360
374
// POJO
361
- if (mapper == null ) {
362
- mapper = new ObjectMapper ();
363
- // Checks once.
364
- if (!c .isMemberClass () || !Modifier .isStatic (c .getModifiers ())) {
365
- throw new IllegalArgumentException (
366
- "c must be a member (not an anonymous or local) and static class." );
367
- }
368
- }
369
375
try {
370
376
result .add (mapper .readValue (jsonObject .toString (), c ));
371
377
} catch (IOException e ) {
@@ -440,10 +446,7 @@ public <T extends Object> T toObject(Class<T> c) {
440
446
return c .cast (this .propertyBag );
441
447
} else {
442
448
// POJO
443
- if (!c .isMemberClass () || !Modifier .isStatic (c .getModifiers ())) {
444
- throw new IllegalArgumentException (
445
- "c must be a member (not an anonymous or local) and static class." );
446
- }
449
+ checkForValidPOJO (c );
447
450
try {
448
451
return new ObjectMapper ().readValue (this .toString (), c );
449
452
} catch (IOException e ) {
0 commit comments