Skip to content

Commit

Permalink
Avoid interception of generic Exception
Browse files Browse the repository at this point in the history
Overall, restore a good practice of catching the desired exception types
explicitly, in order to avoid accidental interception of exceptions that
can be meaningful to API caller.

In particular, do not hide subtypes of IOException which are good
indicators of broken data. See GitHub issue protostuff#291 for more details.

In order to simplify the enforcement of this good practice - use
Accessor implementations wherever possible.
  • Loading branch information
mirlord committed Sep 15, 2020
1 parent 1acb072 commit 2b559af
Show file tree
Hide file tree
Showing 9 changed files with 494 additions and 413 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package io.protostuff.runtime;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;

Expand Down Expand Up @@ -385,7 +386,19 @@ public T newInstance()
{
return constructor.newInstance((Object[]) null);
}
catch (Exception e)
catch (InstantiationException e)
{
throw new RuntimeException(e);
}
catch (IllegalArgumentException e)
{
throw new RuntimeException(e);
}
catch (InvocationTargetException e)
{
throw new RuntimeException(e);
}
catch (IllegalAccessException e)
{
throw new RuntimeException(e);
}
Expand All @@ -411,7 +424,15 @@ public T newInstance()
return (T) newInstanceFromObjectInputStream.invoke(null, clazz,
Object.class);
}
catch (Exception e)
catch (IllegalArgumentException e)
{
throw new RuntimeException(e);
}
catch (InvocationTargetException e)
{
throw new RuntimeException(e);
}
catch (IllegalAccessException e)
{
throw new RuntimeException(e);
}
Expand All @@ -437,7 +458,15 @@ public T newInstance()
return (T) newInstanceFromObjectStreamClass.invoke(null, clazz,
(int) objectConstructorId);
}
catch (Exception e)
catch (IllegalArgumentException e)
{
throw new RuntimeException(e);
}
catch (InvocationTargetException e)
{
throw new RuntimeException(e);
}
catch (IllegalAccessException e)
{
throw new RuntimeException(e);
}
Expand All @@ -463,7 +492,15 @@ public T newInstance()
return (T) newInstanceFromObjectStreamClass.invoke(null, clazz,
objectConstructorId);
}
catch (Exception e)
catch (IllegalArgumentException e)
{
throw new RuntimeException(e);
}
catch (InvocationTargetException e)
{
throw new RuntimeException(e);
}
catch (IllegalAccessException e)
{
throw new RuntimeException(e);
}
Expand Down
12 changes: 10 additions & 2 deletions protostuff-runtime/src/main/java/io/protostuff/runtime/EnumIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,11 @@ static Class<?> getKeyTypeFromEnumMap(Object enumMap)
{
return (Class<?>) __keyTypeFromEnumMap.get(enumMap);
}
catch (Exception e)
catch (IllegalArgumentException e)
{
throw new RuntimeException(e);
}
catch (IllegalAccessException e)
{
throw new RuntimeException(e);
}
Expand All @@ -104,7 +108,11 @@ static Class<?> getElementTypeFromEnumSet(Object enumSet)
{
return (Class<?>) __elementTypeFromEnumSet.get(enumSet);
}
catch (Exception e)
catch (IllegalArgumentException e)
{
throw new RuntimeException(e);
}
catch (IllegalAccessException e)
{
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.IOException;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
Expand Down Expand Up @@ -1895,9 +1896,25 @@ protected static <T> T createMessageInstance(Class<T> clazz)
constructor.setAccessible(true);
return constructor.newInstance();
}
catch (Exception e1)
catch (NoSuchMethodException ne)
{
throw new RuntimeException(e);
throw new RuntimeException(ne);
}
catch (IllegalArgumentException ne)
{
throw new RuntimeException(ne);
}
catch (IllegalAccessException ne)
{
throw new RuntimeException(ne);
}
catch (InvocationTargetException ne)
{
throw new RuntimeException(ne);
}
catch (InstantiationException ne)
{
throw new RuntimeException(ne);
}
}
catch (InstantiationException e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,11 @@ static void writeNonPublicCollectionTo(Output output, Object value,
{
element = fSingletonSet_element.get(value);
}
catch (Exception e)
catch (IllegalAccessException e)
{
throw new RuntimeException(e);
}
catch (IllegalArgumentException e)
{
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -629,7 +633,11 @@ static void writeNonPublicCollectionTo(Output output, Object value,
{
m = fSetFromMap_m.get(value);
}
catch (Exception e)
catch (IllegalAccessException e)
{
throw new RuntimeException(e);
}
catch (IllegalArgumentException e)
{
throw new RuntimeException(e);
}
Expand All @@ -649,7 +657,11 @@ static void writeNonPublicCollectionTo(Output output, Object value,
{
element = fCopiesList_element.get(value);
}
catch (Exception e)
catch (IllegalAccessException e)
{
throw new RuntimeException(e);
}
catch (IllegalArgumentException e)
{
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -733,7 +745,11 @@ private static void writeUnmodifiableCollectionTo(Output output,
{
c = fUnmodifiableCollection_c.get(value);
}
catch (Exception e)
catch (IllegalAccessException e)
{
throw new RuntimeException(e);
}
catch (IllegalArgumentException e)
{
throw new RuntimeException(e);
}
Expand All @@ -751,7 +767,11 @@ private static void writeSynchronizedCollectionTo(Output output,
c = fSynchronizedCollection_c.get(value);
mutex = fSynchronizedCollection_mutex.get(value);
}
catch (Exception e)
catch (IllegalAccessException e)
{
throw new RuntimeException(e);
}
catch (IllegalArgumentException e)
{
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -780,7 +800,11 @@ private static void writeCheckedCollectionTo(Output output, Object value,
c = fCheckedCollection_c.get(value);
type = fCheckedCollection_type.get(value);
}
catch (Exception e)
catch (IllegalAccessException e)
{
throw new RuntimeException(e);
}
catch (IllegalArgumentException e)
{
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -861,7 +885,11 @@ static Object readObjectFrom(Input input, Schema<?> schema, Object owner,
{
fSingletonSet_element.set(collection, element);
}
catch (Exception e)
catch (IllegalAccessException e)
{
throw new RuntimeException(e);
}
catch (IllegalArgumentException e)
{
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -901,7 +929,11 @@ static Object readObjectFrom(Input input, Schema<?> schema, Object owner,
{
fSingletonList_element.set(collection, element);
}
catch (Exception e)
catch (IllegalAccessException e)
{
throw new RuntimeException(e);
}
catch (IllegalArgumentException e)
{
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -930,7 +962,11 @@ static Object readObjectFrom(Input input, Schema<?> schema, Object owner,
fSetFromMap_m.set(collection, m);
fSetFromMap_s.set(collection, ((Map<?, ?>) m).keySet());
}
catch (Exception e)
catch (IllegalAccessException e)
{
throw new RuntimeException(e);
}
catch (IllegalArgumentException e)
{
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -964,7 +1000,11 @@ static Object readObjectFrom(Input input, Schema<?> schema, Object owner,
{
fCopiesList_n.setInt(collection, n);
}
catch (Exception e)
catch (IllegalAccessException e)
{
throw new RuntimeException(e);
}
catch (IllegalArgumentException e)
{
throw new RuntimeException(e);
}
Expand All @@ -985,7 +1025,11 @@ static Object readObjectFrom(Input input, Schema<?> schema, Object owner,
fCopiesList_n.setInt(collection, n);
fCopiesList_element.set(collection, element);
}
catch (Exception e)
catch (IllegalAccessException e)
{
throw new RuntimeException(e);
}
catch (IllegalArgumentException e)
{
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -1135,7 +1179,11 @@ private static Object readUnmodifiableCollectionFrom(Input input,
if (list)
fUnmodifiableList_list.set(collection, c);
}
catch (Exception e)
catch (IllegalAccessException e)
{
throw new RuntimeException(e);
}
catch (IllegalArgumentException e)
{
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -1170,7 +1218,11 @@ private static Object readSynchronizedCollectionFrom(Input input,
if (list)
fSynchronizedList_list.set(collection, c);
}
catch (Exception e)
catch (IllegalAccessException e)
{
throw new RuntimeException(e);
}
catch (IllegalArgumentException e)
{
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -1211,7 +1263,11 @@ private static Object readCheckedCollectionFrom(Input input,
if (list)
fCheckedList_list.set(collection, c);
}
catch (Exception e)
catch (IllegalAccessException e)
{
throw new RuntimeException(e);
}
catch (IllegalArgumentException e)
{
throw new RuntimeException(e);
}
Expand Down
Loading

0 comments on commit 2b559af

Please sign in to comment.