diff --git a/hibernate-core/src/main/java/org/hibernate/type/jackson/JacksonIntegration.java b/hibernate-core/src/main/java/org/hibernate/type/jackson/JacksonIntegration.java index c50b5be3bc..cf9101ef45 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/jackson/JacksonIntegration.java +++ b/hibernate-core/src/main/java/org/hibernate/type/jackson/JacksonIntegration.java @@ -10,24 +10,23 @@ import org.hibernate.type.FormatMapper; public final class JacksonIntegration { - // Implementation note: we rely on the following two fields to be folded as constants + // Implementation note: we rely on the following fields to be folded as constants // when GraalVM native image is initializing them. - private static final boolean JACKSON_AVAILABLE = ableToLoadJackson(); - private static final JacksonXmlFormatMapper XML_FORMAT_MAPPER = JACKSON_AVAILABLE ? new JacksonXmlFormatMapper() : null; - private static final JacksonJsonFormatMapper JSON_FORMAT_MAPPER = JACKSON_AVAILABLE ? new JacksonJsonFormatMapper() : null; + private static final boolean JACKSON_XML_AVAILABLE = ableToLoadJacksonXMLMapper(); + private static final boolean JACKSON_JSON_AVAILABLE = ableToLoadJacksonJSONMapper(); + private static final JacksonXmlFormatMapper XML_FORMAT_MAPPER = JACKSON_XML_AVAILABLE ? new JacksonXmlFormatMapper() : null; + private static final JacksonJsonFormatMapper JSON_FORMAT_MAPPER = JACKSON_JSON_AVAILABLE ? new JacksonJsonFormatMapper() : null; private JacksonIntegration() { //To not be instantiated: static helpers only } - private static boolean ableToLoadJackson() { - try { - JacksonIntegration.class.getClassLoader().loadClass( "com.fasterxml.jackson.dataformat.xml.XmlMapper" ); - return true; - } - catch (ClassNotFoundException | LinkageError e) { - return false; - } + private static boolean ableToLoadJacksonJSONMapper() { + return canLoad( "com.fasterxml.jackson.databind.ObjectMapper" ); + } + + private static boolean ableToLoadJacksonXMLMapper() { + return canLoad( "com.fasterxml.jackson.dataformat.xml.XmlMapper" ); } public static FormatMapper getXMLJacksonFormatMapperOrNull() { @@ -37,4 +36,18 @@ public final class JacksonIntegration { public static FormatMapper getJsonJacksonFormatMapperOrNull() { return JSON_FORMAT_MAPPER; } + + private static boolean canLoad(String name) { + try { + //N.B. intentionally not using the context classloader + // as we're storing these in static references; + // IMO it's reasonable to expect that such dependencies are made reachable from the ORM classloader. + // (we can change this if it's more problematic than expected). + JacksonIntegration.class.getClassLoader().loadClass( name ); + return true; + } + catch (ClassNotFoundException | LinkageError e) { + return false; + } + } } diff --git a/hibernate-core/src/main/java/org/hibernate/type/jakartajson/JakartaJsonIntegration.java b/hibernate-core/src/main/java/org/hibernate/type/jakartajson/JakartaJsonIntegration.java index aa2ea82762..e967d91998 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/jakartajson/JakartaJsonIntegration.java +++ b/hibernate-core/src/main/java/org/hibernate/type/jakartajson/JakartaJsonIntegration.java @@ -21,7 +21,11 @@ public final class JakartaJsonIntegration { private static boolean ableToLoadJakartaJsonB() { try { - JakartaJsonIntegration.class.getClassLoader().loadClass( "jakarta.json.bind.Jsonb" ); + //N.B. intentionally not using the context classloader + // as we're storing these in static references; + // IMO it's reasonable to expect that such dependencies are made reachable from the ORM classloader. + // (we can change this if it's more problematic than expected). + JakartaJsonIntegration.class.getClassLoader().loadClass( "jakarta.json.bind.JsonbBuilder" ); return true; } catch (ClassNotFoundException | LinkageError e) {