/** * Attempts to locate {@link XmlRegistry} for the XML type. Usually, a class named ObjectFactory is located * in the same package as the type we're trying to marshall. This method simply locates this class and * instantiates it if found. * * @param type type class * @return object factory instance */ public static Object findObjectFactory(Class<?> type) { try { Class<?> factoryClass = AbstractJAXBContextFinder.findDefaultObjectFactoryClass(type); if (factoryClass != null && factoryClass.isAnnotationPresent(XmlRegistry.class)) { return factoryClass.newInstance(); } else { throw new JAXBMarshalException(Messages.MESSAGES.validXmlRegistryCouldNotBeLocated()); } } catch (InstantiationException | IllegalAccessException e) { throw new JAXBMarshalException(e); } }