20
20
import java .lang .invoke .MethodHandles ;
21
21
import java .lang .invoke .MethodHandles .Lookup ;
22
22
import java .lang .invoke .MethodType ;
23
- import java .lang .reflect .Constructor ;
24
23
import java .lang .reflect .InvocationHandler ;
25
24
import java .lang .reflect .InvocationTargetException ;
26
25
import java .lang .reflect .Method ;
36
35
public class MapperProxy <T > implements InvocationHandler , Serializable {
37
36
38
37
private static final long serialVersionUID = -4724728412955527868L ;
39
- private static final int ALLOWED_MODES = MethodHandles .Lookup .PRIVATE | MethodHandles .Lookup .PROTECTED
40
- | MethodHandles .Lookup .PACKAGE | MethodHandles .Lookup .PUBLIC ;
41
- private static final Constructor <Lookup > lookupConstructor ;
42
38
private static final Method privateLookupInMethod ;
43
39
private final SqlSession sqlSession ;
44
40
private final Class <T > mapperInterface ;
@@ -51,29 +47,13 @@ public MapperProxy(SqlSession sqlSession, Class<T> mapperInterface, Map<Method,
51
47
}
52
48
53
49
static {
54
- Method privateLookupIn ;
55
50
try {
56
- privateLookupIn = MethodHandles .class .getMethod ("privateLookupIn" , Class .class , MethodHandles .Lookup .class );
51
+ privateLookupInMethod = MethodHandles .class .getMethod ("privateLookupIn" , Class .class , MethodHandles .Lookup .class );
57
52
} catch (NoSuchMethodException e ) {
58
- privateLookupIn = null ;
53
+ throw new IllegalStateException (
54
+ "There is no 'privateLookupIn(Class, Lookup)' method in java.lang.invoke.MethodHandles." ,
55
+ e );
59
56
}
60
- privateLookupInMethod = privateLookupIn ;
61
-
62
- Constructor <Lookup > lookup = null ;
63
- if (privateLookupInMethod == null ) {
64
- // JDK 1.8
65
- try {
66
- lookup = MethodHandles .Lookup .class .getDeclaredConstructor (Class .class , int .class );
67
- lookup .setAccessible (true );
68
- } catch (NoSuchMethodException e ) {
69
- throw new IllegalStateException (
70
- "There is neither 'privateLookupIn(Class, Lookup)' nor 'Lookup(Class, int)' method in java.lang.invoke.MethodHandles." ,
71
- e );
72
- } catch (Exception e ) {
73
- lookup = null ;
74
- }
75
- }
76
- lookupConstructor = lookup ;
77
57
}
78
58
79
59
@ Override
@@ -95,12 +75,8 @@ private MapperMethodInvoker cachedInvoker(Method method) throws Throwable {
95
75
return new PlainMethodInvoker (new MapperMethod (mapperInterface , method , sqlSession .getConfiguration ()));
96
76
}
97
77
try {
98
- if (privateLookupInMethod == null ) {
99
- return new DefaultMethodInvoker (getMethodHandleJava8 (method ));
100
- }
101
78
return new DefaultMethodInvoker (getMethodHandleJava9 (method ));
102
- } catch (IllegalAccessException | InstantiationException | InvocationTargetException
103
- | NoSuchMethodException e ) {
79
+ } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e ) {
104
80
throw new RuntimeException (e );
105
81
}
106
82
});
@@ -118,12 +94,6 @@ private MethodHandle getMethodHandleJava9(Method method)
118
94
declaringClass );
119
95
}
120
96
121
- private MethodHandle getMethodHandleJava8 (Method method )
122
- throws IllegalAccessException , InstantiationException , InvocationTargetException {
123
- final Class <?> declaringClass = method .getDeclaringClass ();
124
- return lookupConstructor .newInstance (declaringClass , ALLOWED_MODES ).unreflectSpecial (method , declaringClass );
125
- }
126
-
127
97
interface MapperMethodInvoker {
128
98
Object invoke (Object proxy , Method method , Object [] args , SqlSession sqlSession ) throws Throwable ;
129
99
}
0 commit comments