|
@@ -15,37 +15,24 @@
|
|
|
*/
|
|
|
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 com.baomidou.mybatisplus.core.toolkit.StringPool;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
import java.lang.invoke.SerializedLambda;
|
|
|
-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 = null;
|
|
|
- try {
|
|
|
- Class<SerializedLambda> aClass = SerializedLambda.class;
|
|
|
- fieldCapturingClass = ReflectionKit.setAccessible(aClass.getDeclaredField("capturingClass"));
|
|
|
- } catch (Throwable e) {
|
|
|
- // ignore
|
|
|
- }
|
|
|
- FIELD_CAPTURING_CLASS = fieldCapturingClass;
|
|
|
- }
|
|
|
-
|
|
|
private final SerializedLambda lambda;
|
|
|
|
|
|
- public ReflectLambdaMeta(SerializedLambda lambda) {
|
|
|
+ private final ClassLoader classLoader;
|
|
|
+
|
|
|
+ public ReflectLambdaMeta(SerializedLambda lambda, ClassLoader classLoader) {
|
|
|
this.lambda = lambda;
|
|
|
+ this.classLoader = classLoader;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -57,19 +44,7 @@ public class ReflectLambdaMeta implements LambdaMeta {
|
|
|
public Class<?> getInstantiatedClass() {
|
|
|
String instantiatedMethodType = lambda.getInstantiatedMethodType();
|
|
|
String instantiatedType = instantiatedMethodType.substring(2, instantiatedMethodType.indexOf(StringPool.SEMICOLON)).replace(StringPool.SLASH, StringPool.DOT);
|
|
|
- return ClassUtils.toClassConfident(instantiatedType, getCapturingClassClassLoader());
|
|
|
- }
|
|
|
-
|
|
|
- private ClassLoader getCapturingClassClassLoader() {
|
|
|
- // 如果反射失败,使用默认的 classloader
|
|
|
- if (FIELD_CAPTURING_CLASS == null) {
|
|
|
- return null;
|
|
|
- }
|
|
|
- try {
|
|
|
- return ((Class<?>) FIELD_CAPTURING_CLASS.get(lambda)).getClassLoader();
|
|
|
- } catch (IllegalAccessException e) {
|
|
|
- throw new MybatisPlusException(e);
|
|
|
- }
|
|
|
+ return ClassUtils.toClassConfident(instantiatedType, this.classLoader);
|
|
|
}
|
|
|
|
|
|
}
|