2
2
3
3
import java .io .IOException ;
4
4
import java .lang .annotation .Annotation ;
5
- import java .lang .IllegalAccessException ;
6
- import java .lang .InstantiationException ;
7
5
import java .lang .reflect .Array ;
8
6
import java .lang .reflect .Constructor ;
9
7
import java .lang .reflect .InvocationTargetException ;
@@ -68,12 +66,7 @@ final class Decoder {
68
66
69
67
private final NodeCache .Loader cacheLoader = this ::decode ;
70
68
71
- public <T > T decode (int offset , Class <T > cls )
72
- throws IOException ,
73
- InstantiationException ,
74
- IllegalAccessException ,
75
- InvocationTargetException ,
76
- NoSuchMethodException {
69
+ public <T > T decode (int offset , Class <T > cls ) throws IOException {
77
70
if (offset >= this .buffer .capacity ()) {
78
71
throw new InvalidDatabaseException (
79
72
"The MaxMind DB file's data section contains bad data: "
@@ -84,12 +77,7 @@ public <T> T decode(int offset, Class<T> cls)
84
77
return cls .cast (decode (cls , null ));
85
78
}
86
79
87
- private <T > T decode (CacheKey <T > key )
88
- throws IOException ,
89
- InstantiationException ,
90
- IllegalAccessException ,
91
- InvocationTargetException ,
92
- NoSuchMethodException {
80
+ private <T > T decode (CacheKey <T > key ) throws IOException {
93
81
int offset = key .getOffset ();
94
82
if (offset >= this .buffer .capacity ()) {
95
83
throw new InvalidDatabaseException (
@@ -103,11 +91,7 @@ private <T> T decode(CacheKey<T> key)
103
91
}
104
92
105
93
private <T > Object decode (Class <T > cls , java .lang .reflect .Type genericType )
106
- throws IOException ,
107
- InstantiationException ,
108
- IllegalAccessException ,
109
- InvocationTargetException ,
110
- NoSuchMethodException {
94
+ throws IOException {
111
95
int ctrlByte = 0xFF & this .buffer .get ();
112
96
113
97
Type type = Type .fromControlByte (ctrlByte );
@@ -173,11 +157,7 @@ private <T> Object decodeByType(
173
157
int size ,
174
158
Class <T > cls ,
175
159
java .lang .reflect .Type genericType
176
- ) throws IOException ,
177
- InstantiationException ,
178
- IllegalAccessException ,
179
- InvocationTargetException ,
180
- NoSuchMethodException {
160
+ ) throws IOException {
181
161
switch (type ) {
182
162
case MAP :
183
163
return this .decodeMap (size , cls , genericType );
@@ -302,12 +282,7 @@ private <T, V> List<V> decodeArray(
302
282
int size ,
303
283
Class <T > cls ,
304
284
Class <V > elementClass
305
- ) throws IOException ,
306
- InstantiationException ,
307
- IllegalAccessException ,
308
- InvocationTargetException ,
309
- DeserializationException ,
310
- NoSuchMethodException {
285
+ ) throws IOException {
311
286
if (!List .class .isAssignableFrom (cls ) && !cls .equals (Object .class )) {
312
287
throw new DeserializationException ();
313
288
}
@@ -316,11 +291,22 @@ private <T, V> List<V> decodeArray(
316
291
if (cls .equals (List .class ) || cls .equals (Object .class )) {
317
292
array = new ArrayList <>(size );
318
293
} else {
319
- Constructor <T > constructor = cls .getConstructor (Integer .TYPE );
294
+ Constructor <T > constructor ;
295
+ try {
296
+ constructor = cls .getConstructor (Integer .TYPE );
297
+ } catch (NoSuchMethodException e ) {
298
+ throw new DeserializationException ("No constructor found for the List: " + e );
299
+ }
320
300
Object [] parameters = {size };
321
- @ SuppressWarnings ("unchecked" )
322
- List <V > array2 = (List <V >) constructor .newInstance (parameters );
323
- array = array2 ;
301
+ try {
302
+ @ SuppressWarnings ("unchecked" )
303
+ List <V > array2 = (List <V >) constructor .newInstance (parameters );
304
+ array = array2 ;
305
+ } catch (InstantiationException |
306
+ IllegalAccessException |
307
+ InvocationTargetException e ) {
308
+ throw new DeserializationException ("Error creating list: " + e );
309
+ }
324
310
}
325
311
326
312
for (int i = 0 ; i < size ; i ++) {
@@ -335,11 +321,7 @@ private <T> Object decodeMap(
335
321
int size ,
336
322
Class <T > cls ,
337
323
java .lang .reflect .Type genericType
338
- ) throws IOException ,
339
- InstantiationException ,
340
- IllegalAccessException ,
341
- InvocationTargetException ,
342
- NoSuchMethodException {
324
+ ) throws IOException {
343
325
if (Map .class .isAssignableFrom (cls ) || cls .equals (Object .class )) {
344
326
Class <?> valueClass = Object .class ;
345
327
if (genericType instanceof ParameterizedType ) {
@@ -365,20 +347,27 @@ private <T, V> Map<String, V> decodeMapIntoMap(
365
347
Class <T > cls ,
366
348
int size ,
367
349
Class <V > valueClass
368
- ) throws IOException ,
369
- InstantiationException ,
370
- IllegalAccessException ,
371
- InvocationTargetException ,
372
- NoSuchMethodException {
350
+ ) throws IOException {
373
351
Map <String , V > map ;
374
352
if (cls .equals (Map .class ) || cls .equals (Object .class )) {
375
353
map = new HashMap <>(size );
376
354
} else {
377
- Constructor <T > constructor = cls .getConstructor (Integer .TYPE );
355
+ Constructor <T > constructor ;
356
+ try {
357
+ constructor = cls .getConstructor (Integer .TYPE );
358
+ } catch (NoSuchMethodException e ) {
359
+ throw new DeserializationException ("No constructor found for the Map: " + e );
360
+ }
378
361
Object [] parameters = {size };
379
- @ SuppressWarnings ("unchecked" )
380
- Map <String , V > map2 = (Map <String , V >) constructor .newInstance (parameters );
381
- map = map2 ;
362
+ try {
363
+ @ SuppressWarnings ("unchecked" )
364
+ Map <String , V > map2 = (Map <String , V >) constructor .newInstance (parameters );
365
+ map = map2 ;
366
+ } catch (InstantiationException |
367
+ IllegalAccessException |
368
+ InvocationTargetException e ) {
369
+ throw new DeserializationException ("Error creating map: " + e );
370
+ }
382
371
}
383
372
384
373
for (int i = 0 ; i < size ; i ++) {
@@ -391,11 +380,7 @@ private <T, V> Map<String, V> decodeMapIntoMap(
391
380
}
392
381
393
382
private <T > Object decodeMapIntoObject (int size , Class <T > cls )
394
- throws IOException ,
395
- InstantiationException ,
396
- IllegalAccessException ,
397
- InvocationTargetException ,
398
- NoSuchMethodException {
383
+ throws IOException {
399
384
CachedConstructor <T > cachedConstructor = this .constructors .get (cls );
400
385
Constructor <T > constructor ;
401
386
Class <?>[] parameterTypes ;
@@ -448,11 +433,17 @@ private <T> Object decodeMapIntoObject(int size, Class<T> cls)
448
433
);
449
434
}
450
435
451
- return constructor .newInstance (parameters );
436
+ try {
437
+ return constructor .newInstance (parameters );
438
+ } catch (InstantiationException |
439
+ IllegalAccessException |
440
+ InvocationTargetException e ) {
441
+ throw new DeserializationException ("Error creating object: " + e );
442
+ }
452
443
}
453
444
454
445
private static <T > Constructor <T > findConstructor (Class <T > cls )
455
- throws ConstructorNotFoundException {
446
+ throws ConstructorNotFoundException {
456
447
Constructor <?>[] constructors = cls .getConstructors ();
457
448
for (Constructor <?> constructor : constructors ) {
458
449
if (constructor .getAnnotation (MaxMindDbConstructor .class ) == null ) {
0 commit comments