|
@@ -3,6 +3,7 @@ package com.baomidou.mybatisplus.core.toolkit.support;
|
|
|
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.ClassUtils;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.ReflectionKit;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
import java.lang.invoke.SerializedLambda;
|
|
|
import java.lang.reflect.Field;
|
|
@@ -10,16 +11,21 @@ import java.lang.reflect.Field;
|
|
|
/**
|
|
|
* Created by hcl at 2021/5/14
|
|
|
*/
|
|
|
+@Slf4j
|
|
|
public class ReflectLambdaMeta implements LambdaMeta {
|
|
|
private static final Field FIELD_CAPTURING_CLASS;
|
|
|
|
|
|
static {
|
|
|
+ Field fieldCapturingClass;
|
|
|
try {
|
|
|
Class<SerializedLambda> aClass = SerializedLambda.class;
|
|
|
- FIELD_CAPTURING_CLASS = ReflectionKit.setAccessible(aClass.getDeclaredField("capturingClass"));
|
|
|
- } catch (NoSuchFieldException e) {
|
|
|
- throw new MybatisPlusException(e);
|
|
|
+ fieldCapturingClass = ReflectionKit.setAccessible(aClass.getDeclaredField("capturingClass"));
|
|
|
+ } catch (Throwable e) {
|
|
|
+ // 解决高版本 jdk 的问题 gitee: https://gitee.com/baomidou/mybatis-plus/issues/I4A7I5
|
|
|
+ log.warn(e.getMessage());
|
|
|
+ fieldCapturingClass = null;
|
|
|
}
|
|
|
+ FIELD_CAPTURING_CLASS = fieldCapturingClass;
|
|
|
}
|
|
|
|
|
|
private final SerializedLambda lambda;
|
|
@@ -37,12 +43,16 @@ public class ReflectLambdaMeta implements LambdaMeta {
|
|
|
public Class<?> getInstantiatedClass() {
|
|
|
String instantiatedMethodType = lambda.getInstantiatedMethodType();
|
|
|
String instantiatedType = instantiatedMethodType.substring(2, instantiatedMethodType.indexOf(';')).replace('/', '.');
|
|
|
- return ClassUtils.toClassConfident(instantiatedType, getCapturingClass().getClassLoader());
|
|
|
+ return ClassUtils.toClassConfident(instantiatedType, getCapturingClassClassLoader());
|
|
|
}
|
|
|
|
|
|
- public Class<?> getCapturingClass() {
|
|
|
+ private ClassLoader getCapturingClassClassLoader() {
|
|
|
+ // 如果反射失败,使用默认的 classloader
|
|
|
+ if (FIELD_CAPTURING_CLASS == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
try {
|
|
|
- return (Class<?>) FIELD_CAPTURING_CLASS.get(lambda);
|
|
|
+ return ((Class<?>) FIELD_CAPTURING_CLASS.get(lambda)).getClassLoader();
|
|
|
} catch (IllegalAccessException e) {
|
|
|
throw new MybatisPlusException(e);
|
|
|
}
|