Skip to content

Instantly share code, notes, and snippets.

@C0BR4cH
Created February 9, 2018 14:29
Show Gist options
  • Save C0BR4cH/6129b1758e8957eaa860f268c5e74705 to your computer and use it in GitHub Desktop.
Save C0BR4cH/6129b1758e8957eaa860f268c5e74705 to your computer and use it in GitHub Desktop.
Getting reasonable exception messages for com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException
public static <T extends Object> T deserialize(Path xmlFile, Class<T> clazz) {
try {
JAXBContext context = JAXBContext.newInstance(clazz);
Unmarshaller unmarshaller = context.createUnmarshaller();
return (T) unmarshaller.unmarshal(xmlFile.toFile());
} catch (Exception e) {
String message = "Error while deserializing XML.";
// Let's make some fancy magic to get understandable Exception messages!
if (e instanceof IllegalAnnotationsException) {
Field field;
try {
field = IllegalAnnotationsException.class.getDeclaredField("errors");
field.setAccessible(true);
Object value = field.get(e);
if (value instanceof List) {
message += "\r\n" + Arrays.toString(((List)value).toArray());
}
} catch (Exception ex) {
// Ignore
}
}
Logging.error(message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment