22
33import java .io .IOException ;
44import java .lang .annotation .Annotation ;
5- import java .lang .IllegalAccessException ;
6- import java .lang .InstantiationException ;
75import java .lang .reflect .Array ;
86import java .lang .reflect .Constructor ;
97import java .lang .reflect .InvocationTargetException ;
@@ -68,12 +66,7 @@ final class Decoder {
6866
6967 private final NodeCache .Loader cacheLoader = this ::decode ;
7068
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 {
7770 if (offset >= this .buffer .capacity ()) {
7871 throw new InvalidDatabaseException (
7972 "The MaxMind DB file's data section contains bad data: "
@@ -84,12 +77,7 @@ public <T> T decode(int offset, Class<T> cls)
8477 return cls .cast (decode (cls , null ));
8578 }
8679
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 {
9381 int offset = key .getOffset ();
9482 if (offset >= this .buffer .capacity ()) {
9583 throw new InvalidDatabaseException (
@@ -103,11 +91,7 @@ private <T> T decode(CacheKey<T> key)
10391 }
10492
10593 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 {
11195 int ctrlByte = 0xFF & this .buffer .get ();
11296
11397 Type type = Type .fromControlByte (ctrlByte );
@@ -173,11 +157,7 @@ private <T> Object decodeByType(
173157 int size ,
174158 Class <T > cls ,
175159 java .lang .reflect .Type genericType
176- ) throws IOException ,
177- InstantiationException ,
178- IllegalAccessException ,
179- InvocationTargetException ,
180- NoSuchMethodException {
160+ ) throws IOException {
181161 switch (type ) {
182162 case MAP :
183163 return this .decodeMap (size , cls , genericType );
@@ -302,12 +282,7 @@ private <T, V> List<V> decodeArray(
302282 int size ,
303283 Class <T > cls ,
304284 Class <V > elementClass
305- ) throws IOException ,
306- InstantiationException ,
307- IllegalAccessException ,
308- InvocationTargetException ,
309- DeserializationException ,
310- NoSuchMethodException {
285+ ) throws IOException {
311286 if (!List .class .isAssignableFrom (cls ) && !cls .equals (Object .class )) {
312287 throw new DeserializationException ();
313288 }
@@ -316,11 +291,22 @@ private <T, V> List<V> decodeArray(
316291 if (cls .equals (List .class ) || cls .equals (Object .class )) {
317292 array = new ArrayList <>(size );
318293 } 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+ }
320300 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+ }
324310 }
325311
326312 for (int i = 0 ; i < size ; i ++) {
@@ -335,11 +321,7 @@ private <T> Object decodeMap(
335321 int size ,
336322 Class <T > cls ,
337323 java .lang .reflect .Type genericType
338- ) throws IOException ,
339- InstantiationException ,
340- IllegalAccessException ,
341- InvocationTargetException ,
342- NoSuchMethodException {
324+ ) throws IOException {
343325 if (Map .class .isAssignableFrom (cls ) || cls .equals (Object .class )) {
344326 Class <?> valueClass = Object .class ;
345327 if (genericType instanceof ParameterizedType ) {
@@ -365,20 +347,27 @@ private <T, V> Map<String, V> decodeMapIntoMap(
365347 Class <T > cls ,
366348 int size ,
367349 Class <V > valueClass
368- ) throws IOException ,
369- InstantiationException ,
370- IllegalAccessException ,
371- InvocationTargetException ,
372- NoSuchMethodException {
350+ ) throws IOException {
373351 Map <String , V > map ;
374352 if (cls .equals (Map .class ) || cls .equals (Object .class )) {
375353 map = new HashMap <>(size );
376354 } 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+ }
378361 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+ }
382371 }
383372
384373 for (int i = 0 ; i < size ; i ++) {
@@ -391,11 +380,7 @@ private <T, V> Map<String, V> decodeMapIntoMap(
391380 }
392381
393382 private <T > Object decodeMapIntoObject (int size , Class <T > cls )
394- throws IOException ,
395- InstantiationException ,
396- IllegalAccessException ,
397- InvocationTargetException ,
398- NoSuchMethodException {
383+ throws IOException {
399384 CachedConstructor <T > cachedConstructor = this .constructors .get (cls );
400385 Constructor <T > constructor ;
401386 Class <?>[] parameterTypes ;
@@ -448,11 +433,17 @@ private <T> Object decodeMapIntoObject(int size, Class<T> cls)
448433 );
449434 }
450435
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+ }
452443 }
453444
454445 private static <T > Constructor <T > findConstructor (Class <T > cls )
455- throws ConstructorNotFoundException {
446+ throws ConstructorNotFoundException {
456447 Constructor <?>[] constructors = cls .getConstructors ();
457448 for (Constructor <?> constructor : constructors ) {
458449 if (constructor .getAnnotation (MaxMindDbConstructor .class ) == null ) {
0 commit comments