Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,13 @@ public Object recreateRepresentable() {
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException("Cannot find class " + representedTypeName, e);
} catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException
| IllegalArgumentException | InvocationTargetException e) {
throw new IllegalArgumentException("Don't know how to handle Representable type '" + representedTypeName
+ "'", e);
| IllegalArgumentException e) {
throw new IllegalArgumentException("Error instantiating '" + representedTypeName + "' from representation", e);
} catch (InvocationTargetException e) {
if (e.getCause() instanceof RuntimeException)
throw (RuntimeException) e.getCause();
else
throw new IllegalArgumentException(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,9 +329,14 @@ private static RepresentationRestorer callMethods(Object baseObject, String[] pa
String methodToCall = parsedRestorerString[i];
try {
currentObject = currentObject.getClass().getMethod(methodToCall).invoke(currentObject);
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
} catch (IllegalAccessException | NoSuchMethodException e) {
e.printStackTrace();
throw new IllegalArgumentException("Cannot call desired method "+methodToCall+" on "+currentObject.getClass().getName(), e);
} catch (InvocationTargetException e) {
if (e.getCause() instanceof RuntimeException)
throw (RuntimeException) e.getCause();
else
throw new RuntimeException("An error occured during invocation of "+methodToCall+ " on "+currentObject.getClass(), e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,19 +95,29 @@ public Object deserializeFromRepresentation(Representation repr,
//Try to call default constructor to create collection.
try {
result = (Collection) collectionType.getConstructor().newInstance();
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException e) {
// if the type is an interface, we try the defined fallback classes
// for example, fall back to ArrayList if the type is List
for (Class<?> fallback : supportedFallbackClasses) {
try {
if (collectionType.isAssignableFrom(fallback))
result = (Collection) fallback.getConstructor().newInstance();
} catch (InstantiationException | NoSuchMethodException | IllegalAccessException
| InvocationTargetException ex) {
} catch (InstantiationException | NoSuchMethodException | IllegalAccessException ex) {
throw new RuntimeException("Cannot instantiate type "+collectionType.getName());
} catch (InvocationTargetException ex) {
if (e.getCause() instanceof RuntimeException)
throw (RuntimeException) ex.getCause();
else
throw new RuntimeException("An error occured during invocation of the constructor of "+fallback.getSimpleName(), ex);
}
}
} catch (InvocationTargetException e) {
if (e.getCause() instanceof RuntimeException)
throw (RuntimeException) e.getCause();
else
throw new RuntimeException("An error occured during invocation of the constructor of "+collectionType.getSimpleName(), e);
}

if (result == null)
throw new RuntimeException("Cannot instantiate type "+collectionType.getName());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,17 @@ public Object deserializeFromRepresentation(Representation repr, Function<String
// Try to call default constructor to create collection.
try {
result = (Map) mapType.getConstructor().newInstance();
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException
| InvocationTargetException e) {
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException e ) {
// if the type has no constructor, fall back to LinkedHashMap
if (mapType.isAssignableFrom(LinkedHashMap.class))
result = new LinkedHashMap<>();
} catch (InvocationTargetException e) {
if (e.getCause() instanceof RuntimeException)
throw (RuntimeException) e.getCause();
else
throw new RuntimeException("An error occured during invocation of the constructor of "+mapType.getSimpleName(), e);
}

if (result == null)
throw new RuntimeException("Cannot instantiate type "+ mapType.getName());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@ protected final void test(StandaloneRepresentable object) {
assertNotNull(constructor);
Representation repr = (Representation) clazz.getMethod("getRepresentation").invoke(object);
assertEquals(object, constructor.newInstance(repr));
} catch (IllegalAccessException | InvocationTargetException | InstantiationException | NoSuchMethodException e) {
e.printStackTrace();
fail("An exception occured while serializing/deserializing "+clazz.getName());
} catch (IllegalAccessException | InstantiationException | NoSuchMethodException e) {
fail("An error occured while serializing/deserializing "+clazz.getName(), e);
} catch (InvocationTargetException e) {
fail("An exception was thrown while serializing/deserializing "+clazz.getName(), e.getCause());
}
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,14 @@ protected Stream<? extends Arguments> provideStandaloneReprSubTests() {
.map(clazz -> {
try {
return clazz.getDeclaredConstructor().newInstance();
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException e) {
e.printStackTrace();
return null;
} catch (InvocationTargetException e) {
if (e.getCause() instanceof RuntimeException)
throw (RuntimeException) e.getCause();
else
throw new RuntimeException("An exception was thrown in the constructor of "+clazz.getSimpleName(), e);
}
})
.filter(Objects::nonNull)
Expand All @@ -85,8 +90,10 @@ public void testClassesWithTrivialConstructor() {
.forEach(clazz -> {
try {
test(clazz.getConstructor().newInstance());
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException e) {
fail(e);
} catch (InvocationTargetException e) {
fail("Exception was thrown during constructor invocation of "+clazz.getSimpleName(), e.getCause());
}
});
}
Expand All @@ -102,7 +109,7 @@ public void checkForUntestedClasses() {
classesToTest.removeIf(c -> c.isInterface() || Modifier.isAbstract(c.getModifiers()) || !c.getPackage().getName().startsWith(packageToTest));

for (Class<? extends StandaloneRepresentable> notTestedClass : classesToTest) {
System.err.println(notTestedClass.getName() + " implements StandaloneRepresentable was not tested by StandaloneTest. You need to define a StandaloneSubTest for it.");
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.");
}

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