2020import java .lang .invoke .MethodHandles ;
2121import java .lang .invoke .MethodHandles .Lookup ;
2222import java .lang .invoke .MethodType ;
23- import java .lang .reflect .Constructor ;
2423import java .lang .reflect .InvocationHandler ;
2524import java .lang .reflect .InvocationTargetException ;
2625import java .lang .reflect .Method ;
3635public class MapperProxy <T > implements InvocationHandler , Serializable {
3736
3837 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 ;
4238 private static final Method privateLookupInMethod ;
4339 private final SqlSession sqlSession ;
4440 private final Class <T > mapperInterface ;
@@ -51,29 +47,13 @@ public MapperProxy(SqlSession sqlSession, Class<T> mapperInterface, Map<Method,
5147 }
5248
5349 static {
54- Method privateLookupIn ;
5550 try {
56- privateLookupIn = MethodHandles .class .getMethod ("privateLookupIn" , Class .class , MethodHandles .Lookup .class );
51+ privateLookupInMethod = MethodHandles .class .getMethod ("privateLookupIn" , Class .class , MethodHandles .Lookup .class );
5752 } 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 );
5956 }
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 ;
7757 }
7858
7959 @ Override
@@ -95,12 +75,8 @@ private MapperMethodInvoker cachedInvoker(Method method) throws Throwable {
9575 return new PlainMethodInvoker (new MapperMethod (mapperInterface , method , sqlSession .getConfiguration ()));
9676 }
9777 try {
98- if (privateLookupInMethod == null ) {
99- return new DefaultMethodInvoker (getMethodHandleJava8 (method ));
100- }
10178 return new DefaultMethodInvoker (getMethodHandleJava9 (method ));
102- } catch (IllegalAccessException | InstantiationException | InvocationTargetException
103- | NoSuchMethodException e ) {
79+ } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e ) {
10480 throw new RuntimeException (e );
10581 }
10682 });
@@ -118,12 +94,6 @@ private MethodHandle getMethodHandleJava9(Method method)
11894 declaringClass );
11995 }
12096
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-
12797 interface MapperMethodInvoker {
12898 Object invoke (Object proxy , Method method , Object [] args , SqlSession sqlSession ) throws Throwable ;
12999 }
0 commit comments