HHH-17579 fix classloader issues with JAXBContext since JDK11

This commit is contained in:
Laurent SCHOELENS 2023-12-19 14:59:44 +01:00 committed by Christian Beikov
parent 037336ea83
commit d1f1e6e8b9
1 changed files with 9 additions and 1 deletions

View File

@ -9,6 +9,7 @@ package org.hibernate.jpamodelgen.util.xml;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URL; import java.net.URL;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ConcurrentMap;
import javax.tools.FileObject; import javax.tools.FileObject;
@ -120,7 +121,14 @@ public class XmlParserHelper {
ContextProvidingValidationEventHandler handler = new ContextProvidingValidationEventHandler(); ContextProvidingValidationEventHandler handler = new ContextProvidingValidationEventHandler();
try { try {
staxEventReader = new JpaNamespaceTransformingEventReader( staxEventReader ); staxEventReader = new JpaNamespaceTransformingEventReader( staxEventReader );
JAXBContext jaxbContext = JAXBContext.newInstance( ObjectFactory.class );
// Class.getClassLoader() may return null if the class was loaded by the bootstrap class loader,
// but since we don't expect the annotation processor to be loaded by that class loader,
// we expect the return to be non-null and hence cast
ClassLoader cl = NullnessUtil.castNonNull( ObjectFactory.class.getClassLoader() );
String packageName = NullnessUtil.castNonNull( ObjectFactory.class.getPackage() ).getName();
JAXBContext jaxbContext = JAXBContext.newInstance( packageName, cl );
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
unmarshaller.setSchema( schema ); unmarshaller.setSchema( schema );
unmarshaller.setEventHandler( handler ); unmarshaller.setEventHandler( handler );