Explorar el Código

修复高版本JDK下IDEA执行Evaluate出现反射错误.

https://github.com/baomidou/mybatis-plus/issues/5573
nieqiurong hace 1 año
padre
commit
b2e38d83de

+ 5 - 12
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/support/IdeaProxyLambdaMeta.java

@@ -15,13 +15,11 @@
  */
 package com.baomidou.mybatisplus.core.toolkit.support;
 
-import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
-import com.baomidou.mybatisplus.core.toolkit.ReflectionKit;
 
 import java.lang.invoke.MethodHandle;
+import java.lang.invoke.MethodHandleProxies;
 import java.lang.invoke.MethodHandles;
 import java.lang.reflect.Executable;
-import java.lang.reflect.InvocationHandler;
 import java.lang.reflect.Proxy;
 
 /**
@@ -34,15 +32,10 @@ public class IdeaProxyLambdaMeta implements LambdaMeta {
     private final String name;
 
     public IdeaProxyLambdaMeta(Proxy func) {
-        InvocationHandler handler = Proxy.getInvocationHandler(func);
-        try {
-            MethodHandle dmh = (MethodHandle) ReflectionKit.setAccessible(handler.getClass().getDeclaredField("val$target")).get(handler);
-            Executable executable = MethodHandles.reflectAs(Executable.class, dmh);
-            clazz = executable.getDeclaringClass();
-            name = executable.getName();
-        } catch (IllegalAccessException | NoSuchFieldException e) {
-            throw new MybatisPlusException(e);
-        }
+        MethodHandle dmh = MethodHandleProxies.wrapperInstanceTarget(func);
+        Executable executable = MethodHandles.reflectAs(Executable.class, dmh);
+        clazz = executable.getDeclaringClass();
+        name = executable.getName();
     }
 
     @Override

+ 0 - 3
mybatis-plus-core/src/test/java/com/baomidou/mybatisplus/core/toolkit/support/IdeaProxyLambdaMetaTest.java

@@ -1,8 +1,6 @@
 package com.baomidou.mybatisplus.core.toolkit.support;
 
 import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.condition.EnabledOnJre;
-import org.junit.jupiter.api.condition.JRE;
 
 import java.lang.invoke.MethodHandle;
 import java.lang.invoke.MethodHandleProxies;
@@ -22,7 +20,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
 class IdeaProxyLambdaMetaTest {
 
     @Test
-    @EnabledOnJre(JRE.JAVA_8)
     void test() throws NoSuchMethodException, IllegalAccessException {
         MethodHandles.Lookup lookup = MethodHandles.lookup();
         MethodHandle handle = lookup.findStatic(IdeaProxyLambdaMetaTest.class, "s", MethodType.methodType(int.class));