Skip to content

Commit 7352848

Browse files
committed
HHH-19918 Avoid reflection when instantiating known FormatMapper
1 parent 5f2857f commit 7352848

File tree

1 file changed

+8
-37
lines changed

1 file changed

+8
-37
lines changed

hibernate-core/src/main/java/org/hibernate/type/format/jackson/JacksonIntegration.java

Lines changed: 8 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -41,67 +41,38 @@ private static boolean ableToLoadJacksonOSONFactory() {
4141

4242
public static @Nullable FormatMapper getXMLJacksonFormatMapperOrNull(FormatMapperCreationContext creationContext) {
4343
return JACKSON_XML_AVAILABLE
44-
? createFormatMapper( "org.hibernate.type.format.jackson.JacksonXmlFormatMapper", creationContext )
44+
? new JacksonXmlFormatMapper( creationContext )
4545
: null;
4646
}
4747

4848
public static @Nullable FormatMapper getJsonJacksonFormatMapperOrNull(FormatMapperCreationContext creationContext) {
4949
return JACKSON_JSON_AVAILABLE
50-
? createFormatMapper( "org.hibernate.type.format.jackson.JacksonJsonFormatMapper", creationContext )
50+
? new JacksonJsonFormatMapper( creationContext )
5151
: null;
5252
}
5353
public static @Nullable FormatMapper getOsonJacksonFormatMapperOrNull(FormatMapperCreationContext creationContext) {
5454
return JACKSON_OSON_AVAILABLE
55-
? createFormatMapper( "org.hibernate.type.format.jackson.JacksonOsonFormatMapper", creationContext )
55+
? new JacksonOsonFormatMapper( creationContext )
5656
: null;
5757
}
5858

5959
public static @Nullable FormatMapper getXMLJacksonFormatMapperOrNull(boolean legacyFormat) {
60-
if ( JACKSON_XML_AVAILABLE ) {
61-
try {
62-
final Class<?> formatMapperClass = JacksonIntegration.class.getClassLoader()
63-
.loadClass( "org.hibernate.type.format.jackson.JacksonXmlFormatMapper" );
64-
return (FormatMapper) formatMapperClass.getDeclaredConstructor( boolean.class )
65-
.newInstance( legacyFormat );
66-
}
67-
catch (Exception e) {
68-
throw new RuntimeException( "Couldn't instantiate Jackson XML FormatMapper", e );
69-
}
70-
}
71-
return null;
60+
return JACKSON_XML_AVAILABLE
61+
? new JacksonXmlFormatMapper( legacyFormat )
62+
: null;
7263
}
7364

7465
public static @Nullable FormatMapper getJsonJacksonFormatMapperOrNull() {
7566
return JACKSON_JSON_AVAILABLE
76-
? createFormatMapper( "org.hibernate.type.format.jackson.JacksonJsonFormatMapper", null )
67+
? new JacksonJsonFormatMapper()
7768
: null;
7869
}
7970
public static @Nullable FormatMapper getOsonJacksonFormatMapperOrNull() {
8071
return JACKSON_OSON_AVAILABLE
81-
? createFormatMapper( "org.hibernate.type.format.jackson.JacksonOsonFormatMapper", null )
72+
? new JacksonOsonFormatMapper()
8273
: null;
8374
}
8475

85-
private static FormatMapper createFormatMapper(String className, @Nullable FormatMapperCreationContext creationContext) {
86-
try {
87-
if ( creationContext == null ) {
88-
final Class<?> formatMapperClass = JacksonIntegration.class.getClassLoader()
89-
.loadClass( className );
90-
return (FormatMapper) formatMapperClass.getDeclaredConstructor().newInstance();
91-
}
92-
else {
93-
return (FormatMapper) creationContext.getBootstrapContext()
94-
.getClassLoaderAccess()
95-
.classForName( className )
96-
.getDeclaredConstructor( FormatMapperCreationContext.class )
97-
.newInstance( creationContext );
98-
}
99-
}
100-
catch (Exception e) {
101-
throw new RuntimeException( "Couldn't instantiate Jackson FormatMapper", e );
102-
}
103-
}
104-
10576
/**
10677
* Checks that Oracle OSON extension available
10778
*

0 commit comments

Comments
 (0)