Created
February 9, 2018 14:29
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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