-
-
Notifications
You must be signed in to change notification settings - Fork 147
/
DefaultMethodHandler.kt
131 lines (116 loc) · 5.41 KB
/
DefaultMethodHandler.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* Copyright 2018-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.ktorm.entity
import java.lang.invoke.MethodHandle
import java.lang.invoke.MethodHandles
import java.lang.invoke.MethodHandles.Lookup
import java.lang.invoke.MethodHandles.Lookup.*
import java.lang.reflect.Constructor
import java.lang.reflect.InvocationTargetException
import java.lang.reflect.Method
import java.util.*
internal class DefaultMethodHandler(
private val javaDefaultMethodHandle: MethodHandle? = null,
private val kotlinDefaultImplMethod: Method? = null
) {
fun invoke(proxy: Any, args: Array<out Any>?): Any? {
if (javaDefaultMethodHandle != null) {
if (args == null) {
return javaDefaultMethodHandle.bindTo(proxy).invokeWithArguments()
} else {
return javaDefaultMethodHandle.bindTo(proxy).invokeWithArguments(*args)
}
}
if (kotlinDefaultImplMethod != null) {
if (args == null) {
return kotlinDefaultImplMethod.invoke0(null, proxy)
} else {
return kotlinDefaultImplMethod.invoke0(null, proxy, *args)
}
}
throw AssertionError("Non-abstract method in an interface must be a JVM default method or have DefaultImpls")
}
companion object {
private val handlersCache = Collections.synchronizedMap(WeakHashMap<Method, DefaultMethodHandler>())
private val privateLookupIn: Method?
private val lookupConstructor: Constructor<Lookup>?
init {
privateLookupIn = initPrivateLookupInMethod()
lookupConstructor = if (privateLookupIn == null) initLookupConstructor() else null
}
@Suppress("SwallowedException")
private fun initPrivateLookupInMethod(): Method? {
try {
return MethodHandles::class.java.getMethod("privateLookupIn", Class::class.java, Lookup::class.java)
} catch (e: NoSuchMethodException) {
// MethodHandles.privateLookupIn(Class, MethodHandles.Lookup) doesn't exist in JDK 1.8.
return null
}
}
private fun initLookupConstructor(): Constructor<Lookup>? {
try {
// This branch only runs in JDK 1.8, so the reflection operation (setAccessible) is safe.
val c = Lookup::class.java.getDeclaredConstructor(Class::class.java, Int::class.javaPrimitiveType)
c.isAccessible = true
return c
} catch (e: NoSuchMethodException) {
val msg = "" +
"Cannot find constructor MethodHandles.Lookup(Class, int), " +
"please ensure you are using JDK 1.8 or above."
throw IllegalStateException(msg, e)
}
}
@Suppress("SwallowedException")
private fun unreflectSpecial(method: Method): MethodHandle {
// For JDK 9 or above.
if (privateLookupIn != null) {
val lookup = privateLookupIn.invoke0(null, method.declaringClass, MethodHandles.lookup()) as Lookup
return lookup.unreflectSpecial(method, method.declaringClass)
}
// For JDK 1.8.
if (lookupConstructor != null) {
try {
val allModes = PUBLIC or PRIVATE or PROTECTED or PACKAGE
val lookup = lookupConstructor.newInstance(method.declaringClass, allModes)
return lookup.unreflectSpecial(method, method.declaringClass)
} catch (e: InvocationTargetException) {
throw e.targetException
}
}
// Throws error for JDK version lower than 1.8.
val msg = "" +
"Cannot find constructor MethodHandles.Lookup(Class, int), " +
"please ensure you are using JDK 1.8 or above."
throw AssertionError(msg)
}
fun forMethod(method: Method): DefaultMethodHandler {
// Workaround for the compiler bug, see https://youtrack.jetbrains.com/issue/KT-34826
@Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN", "UNCHECKED_CAST")
val cache = handlersCache as java.util.Map<Method, DefaultMethodHandler>
return cache.computeIfAbsent(method) {
if (method.isDefault) {
val handle = unreflectSpecial(method)
DefaultMethodHandler(javaDefaultMethodHandle = handle)
} else {
val classLoader = method.declaringClass.classLoader
val cls = Class.forName(method.declaringClass.name + "\$DefaultImpls", true, classLoader)
val impl = cls.getMethod(method.name, method.declaringClass, *method.parameterTypes)
DefaultMethodHandler(kotlinDefaultImplMethod = impl)
}
}
}
}
}