|
29 | 29 | import org.slf4j.LoggerFactory; |
30 | 30 |
|
31 | 31 | import java.io.IOException; |
| 32 | +import java.lang.reflect.Method; |
32 | 33 | import java.math.BigDecimal; |
33 | 34 | import java.nio.ByteBuffer; |
34 | 35 | import java.nio.ByteOrder; |
@@ -75,6 +76,10 @@ public class MySqlValueConverters extends JdbcValueConverters { |
75 | 76 | public interface ParsingErrorHandler { |
76 | 77 | void error(String message, Exception exception); |
77 | 78 | } |
| 79 | + private static final String METHOD_GET_STATIC_JAVA_ENCODING = "getStaticJavaEncodingForMysqlCharset"; |
| 80 | + private static final String METHOD_GET_JAVA_ENCODING = "getJavaEncodingForMysqlCharset"; |
| 81 | + |
| 82 | + private static final String CLASS_CHARSET_MAPPING = "com.mysql.cj.CharsetMapping"; |
78 | 83 |
|
79 | 84 | private static final Logger LOGGER = LoggerFactory.getLogger(MySqlValueConverters.class); |
80 | 85 |
|
@@ -344,18 +349,37 @@ protected Charset charsetFor(Column column) { |
344 | 349 | return null; |
345 | 350 | } |
346 | 351 |
|
347 | | - // This is a change from the original file, we are using the jdbcClassLoader to invoke the static method. |
| 352 | + // This is a change from the original file, we are using the jdbcClassLoader to invoke the static |
| 353 | + // getJavaEncodingForMysqlCharset or getStaticJavaEncodingForMysqlCharset (since mysql connector 8.0.26) method |
348 | 354 | // Following line is from original file. Instead of getting encoding from CharsetMapping we load the class |
349 | 355 | // using jdbcClassLoader. |
350 | 356 | // String encoding = CharsetMapping.getJavaEncodingForMysqlCharset(mySqlCharsetName); |
351 | 357 | String encoding; |
| 358 | + Class<?> charsetMappingClass = null; |
| 359 | + try { |
| 360 | + charsetMappingClass = jdbcClassLoader.loadClass(CLASS_CHARSET_MAPPING); |
| 361 | + } catch (ClassNotFoundException e) { |
| 362 | + throw new RuntimeException(String.format("Failed to load class %s: %s", CLASS_CHARSET_MAPPING, |
| 363 | + e.getMessage()), e); |
| 364 | + } |
| 365 | + Method getCharsetMethod = null; |
| 366 | + try { |
| 367 | + getCharsetMethod = charsetMappingClass.getMethod(METHOD_GET_JAVA_ENCODING, String.class); |
| 368 | + } catch (NoSuchMethodException e) { |
| 369 | + try { |
| 370 | + getCharsetMethod = charsetMappingClass.getDeclaredMethod(METHOD_GET_STATIC_JAVA_ENCODING, String.class); |
| 371 | + getCharsetMethod.setAccessible(true); |
| 372 | + } catch (NoSuchMethodException noSuchMethodException) { |
| 373 | + throw new RuntimeException(String.format("Failed to find method %s or %s for class %s", |
| 374 | + METHOD_GET_JAVA_ENCODING, METHOD_GET_STATIC_JAVA_ENCODING, |
| 375 | + CLASS_CHARSET_MAPPING)); |
| 376 | + } |
| 377 | + } |
352 | 378 | try { |
353 | | - encoding = (String) jdbcClassLoader.loadClass("com.mysql.cj.CharsetMapping") |
354 | | - .getMethod("getJavaEncodingForMysqlCharset", String.class) |
355 | | - .invoke(null, mySqlCharsetName); |
| 379 | + encoding = (String) getCharsetMethod.invoke(null, mySqlCharsetName); |
356 | 380 | } catch (Exception e) { |
357 | | - throw new RuntimeException("Error while using class loader to invoke 'getJavaEncodingForMysqlCharset' " + |
358 | | - "static method", e); |
| 381 | + throw new RuntimeException(String.format("Error while using class loader to invoke '%s.%s' static method", |
| 382 | + CLASS_CHARSET_MAPPING, getCharsetMethod.getName()), e); |
359 | 383 | } |
360 | 384 | // end change from original file |
361 | 385 |
|
|
0 commit comments