Skip to content

Commit 2f7d702

Browse files
authored
Merge pull request #63 from cryptimeleon/exceptionsImprove
Improved exception handling
2 parents f61bccf + 707ee52 commit 2f7d702

6 files changed

Lines changed: 48 additions & 17 deletions

File tree

src/main/java/org/cryptimeleon/math/serialization/RepresentableRepresentation.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,13 @@ public Object recreateRepresentable() {
7777
} catch (ClassNotFoundException e) {
7878
throw new IllegalArgumentException("Cannot find class " + representedTypeName, e);
7979
} catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException
80-
| IllegalArgumentException | InvocationTargetException e) {
81-
throw new IllegalArgumentException("Don't know how to handle Representable type '" + representedTypeName
82-
+ "'", e);
80+
| IllegalArgumentException e) {
81+
throw new IllegalArgumentException("Error instantiating '" + representedTypeName + "' from representation", e);
82+
} catch (InvocationTargetException e) {
83+
if (e.getCause() instanceof RuntimeException)
84+
throw (RuntimeException) e.getCause();
85+
else
86+
throw new IllegalArgumentException(e);
8387
}
8488
}
8589

src/main/java/org/cryptimeleon/math/serialization/annotations/ReprUtil.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,9 +329,14 @@ private static RepresentationRestorer callMethods(Object baseObject, String[] pa
329329
String methodToCall = parsedRestorerString[i];
330330
try {
331331
currentObject = currentObject.getClass().getMethod(methodToCall).invoke(currentObject);
332-
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
332+
} catch (IllegalAccessException | NoSuchMethodException e) {
333333
e.printStackTrace();
334334
throw new IllegalArgumentException("Cannot call desired method "+methodToCall+" on "+currentObject.getClass().getName(), e);
335+
} catch (InvocationTargetException e) {
336+
if (e.getCause() instanceof RuntimeException)
337+
throw (RuntimeException) e.getCause();
338+
else
339+
throw new RuntimeException("An error occured during invocation of "+methodToCall+ " on "+currentObject.getClass(), e);
335340
}
336341
}
337342

src/main/java/org/cryptimeleon/math/serialization/annotations/internal/ListAndSetRepresentationHandler.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,19 +95,29 @@ public Object deserializeFromRepresentation(Representation repr,
9595
//Try to call default constructor to create collection.
9696
try {
9797
result = (Collection) collectionType.getConstructor().newInstance();
98-
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
98+
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException e) {
9999
// if the type is an interface, we try the defined fallback classes
100100
// for example, fall back to ArrayList if the type is List
101101
for (Class<?> fallback : supportedFallbackClasses) {
102102
try {
103103
if (collectionType.isAssignableFrom(fallback))
104104
result = (Collection) fallback.getConstructor().newInstance();
105-
} catch (InstantiationException | NoSuchMethodException | IllegalAccessException
106-
| InvocationTargetException ex) {
105+
} catch (InstantiationException | NoSuchMethodException | IllegalAccessException ex) {
107106
throw new RuntimeException("Cannot instantiate type "+collectionType.getName());
107+
} catch (InvocationTargetException ex) {
108+
if (e.getCause() instanceof RuntimeException)
109+
throw (RuntimeException) ex.getCause();
110+
else
111+
throw new RuntimeException("An error occured during invocation of the constructor of "+fallback.getSimpleName(), ex);
108112
}
109113
}
114+
} catch (InvocationTargetException e) {
115+
if (e.getCause() instanceof RuntimeException)
116+
throw (RuntimeException) e.getCause();
117+
else
118+
throw new RuntimeException("An error occured during invocation of the constructor of "+collectionType.getSimpleName(), e);
110119
}
120+
111121
if (result == null)
112122
throw new RuntimeException("Cannot instantiate type "+collectionType.getName());
113123

src/main/java/org/cryptimeleon/math/serialization/annotations/internal/MapRepresentationHandler.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,17 @@ public Object deserializeFromRepresentation(Representation repr, Function<String
117117
// Try to call default constructor to create collection.
118118
try {
119119
result = (Map) mapType.getConstructor().newInstance();
120-
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException
121-
| InvocationTargetException e) {
120+
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException e ) {
122121
// if the type has no constructor, fall back to LinkedHashMap
123122
if (mapType.isAssignableFrom(LinkedHashMap.class))
124123
result = new LinkedHashMap<>();
124+
} catch (InvocationTargetException e) {
125+
if (e.getCause() instanceof RuntimeException)
126+
throw (RuntimeException) e.getCause();
127+
else
128+
throw new RuntimeException("An error occured during invocation of the constructor of "+mapType.getSimpleName(), e);
125129
}
130+
126131
if (result == null)
127132
throw new RuntimeException("Cannot instantiate type "+ mapType.getName());
128133

src/test/java/org/cryptimeleon/math/serialization/standalone/StandaloneReprSubTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,10 @@ protected final void test(StandaloneRepresentable object) {
5858
assertNotNull(constructor);
5959
Representation repr = (Representation) clazz.getMethod("getRepresentation").invoke(object);
6060
assertEquals(object, constructor.newInstance(repr));
61-
} catch (IllegalAccessException | InvocationTargetException | InstantiationException | NoSuchMethodException e) {
62-
e.printStackTrace();
63-
fail("An exception occured while serializing/deserializing "+clazz.getName());
61+
} catch (IllegalAccessException | InstantiationException | NoSuchMethodException e) {
62+
fail("An error occured while serializing/deserializing "+clazz.getName(), e);
63+
} catch (InvocationTargetException e) {
64+
fail("An exception was thrown while serializing/deserializing "+clazz.getName(), e.getCause());
6465
}
6566
}
6667

@@ -78,8 +79,7 @@ public final Set<Class<? extends StandaloneRepresentable>> runTests() {
7879
} catch (IllegalAccessException e) {
7980
fail(e);
8081
} catch (InvocationTargetException e) {
81-
e.printStackTrace();
82-
fail("Exception thrown during execution of "+getClass().getName()+"::"+method.getName(), e);
82+
fail("Exception thrown during execution of "+getClass().getName()+"::"+method.getName(), e.getCause());
8383
}
8484
}
8585

src/test/java/org/cryptimeleon/math/serialization/standalone/StandaloneReprTest.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,14 @@ protected Stream<? extends Arguments> provideStandaloneReprSubTests() {
6868
.map(clazz -> {
6969
try {
7070
return clazz.getDeclaredConstructor().newInstance();
71-
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
71+
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException e) {
7272
e.printStackTrace();
7373
return null;
74+
} catch (InvocationTargetException e) {
75+
if (e.getCause() instanceof RuntimeException)
76+
throw (RuntimeException) e.getCause();
77+
else
78+
throw new RuntimeException("An exception was thrown in the constructor of "+clazz.getSimpleName(), e);
7479
}
7580
})
7681
.filter(Objects::nonNull)
@@ -85,8 +90,10 @@ public void testClassesWithTrivialConstructor() {
8590
.forEach(clazz -> {
8691
try {
8792
test(clazz.getConstructor().newInstance());
88-
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
93+
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException e) {
8994
fail(e);
95+
} catch (InvocationTargetException e) {
96+
fail("Exception was thrown during constructor invocation of "+clazz.getSimpleName(), e.getCause());
9097
}
9198
});
9299
}
@@ -102,7 +109,7 @@ public void checkForUntestedClasses() {
102109
classesToTest.removeIf(c -> c.isInterface() || Modifier.isAbstract(c.getModifiers()) || !c.getPackage().getName().startsWith(packageToTest));
103110

104111
for (Class<? extends StandaloneRepresentable> notTestedClass : classesToTest) {
105-
System.err.println(notTestedClass.getName() + " implements StandaloneRepresentable was not tested by StandaloneTest. You need to define a StandaloneSubTest for it.");
112+
System.err.println(notTestedClass.getName() + " implements StandaloneRepresentable but was not tested by StandaloneTest (or the test failed). You need to define a StandaloneReprSubTest for it.");
106113
}
107114

108115
assertTrue(classesToTest.isEmpty(), "Missing (or failed) StandaloneRepresentation tests for "+classesToTest.stream().map(Class::getSimpleName).sorted().collect(Collectors.joining(", ")));

0 commit comments

Comments
 (0)