-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInstanceMethodHandler.kt
More file actions
217 lines (186 loc) · 8.21 KB
/
InstanceMethodHandler.kt
File metadata and controls
217 lines (186 loc) · 8.21 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package com.study.proxy.impl.handler
import com.study.proxy.impl.util.ClassNameProvider
import com.study.proxy.impl.util.TypeUtil
import org.objectweb.asm.*
import org.objectweb.asm.Type
import java.lang.reflect.*
import java.util.concurrent.atomic.AtomicInteger
class InstanceMethodHandler(val classWriter: ClassWriter, val methods: List<Method>) : Handler {
private val intConstInstructionGenerator = IntConstInstructionGenerator()
private val handlerFieldName = "h"
private val toPrimitiveMethodName: (Type) -> String = { type ->
when (type) {
Type.BOOLEAN_TYPE -> "booleanValue"
Type.CHAR_TYPE -> "charValue"
Type.SHORT_TYPE -> "shortValue"
Type.BYTE_TYPE -> "byteValue"
Type.INT_TYPE -> "intValue"
Type.FLOAT_TYPE -> "floatValue"
Type.LONG_TYPE -> "longValue"
Type.DOUBLE_TYPE -> "doubleValue"
else -> {
throw RuntimeException("Unexpected type: $type")
}
}
}
/**
* Below methods are processed
* 1. {@link java.lang.Object#hashCode()}
* 2. {@link java.lang.Object#equals(java.lang.Object)}
* 3. {@link java.lang.Object#toString()}
* 4. Declared non-static methods in the specified interface
*/
override fun process() {
val cnt = AtomicInteger()
methods.forEach { method ->
val methodVisitor = buildMethodVisitor(method)
val exceptionClasses = computeUniqueCatchList(method.exceptionTypes)
methodVisitor.visitCode()
val label0 = Label()
val label1 = Label()
val label2 = Label()
if (!exceptionClasses.isEmpty()) {
for (exceptionClass in exceptionClasses) {
methodVisitor.visitTryCatchBlock(label0, label1, label1, Type.getInternalName(exceptionClass))
}
methodVisitor.visitTryCatchBlock(label0, label1, label2, Type.getInternalName(Throwable::class.java))
methodVisitor.visitLabel(label0)
}
methodVisitor.visitVarInsn(Opcodes.ALOAD, 0)
methodVisitor.visitFieldInsn(
Opcodes.GETFIELD,
Type.getInternalName(
Proxy::class.java
), handlerFieldName,
Type.getDescriptor(InvocationHandler::class.java)
)
methodVisitor.visitVarInsn(Opcodes.ALOAD, 0)
val methodFieldName = "m" + cnt.getAndIncrement()
methodVisitor.visitFieldInsn(
Opcodes.GETSTATIC, ClassNameProvider.internalName(), methodFieldName,
Type.getDescriptor(Method::class.java)
)
val types = Type.getArgumentTypes(method)
if (types.size == 0) {
methodVisitor.visitInsn(Opcodes.ACONST_NULL)
} else {
generateObjectArray(methodVisitor, types)
}
methodVisitor.visitMethodInsn(
Opcodes.INVOKEINTERFACE,
Type.getInternalName(
InvocationHandler::class.java
),
"invoke",
"(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;",
true
)
val returnType = Type.getReturnType(method)
if (returnType == Type.VOID_TYPE) {
methodVisitor.visitInsn(Opcodes.POP)
methodVisitor.visitInsn(Opcodes.RETURN)
} else if (TypeUtil.forPrimitive(returnType)) {
methodVisitor.visitTypeInsn(Opcodes.CHECKCAST, TypeUtil.toWrapperType(returnType).internalName)
methodVisitor.visitMethodInsn(
Opcodes.INVOKEVIRTUAL, TypeUtil.toWrapperType(returnType).internalName,
toPrimitiveMethodName(returnType), "()" + returnType.descriptor, false
)
methodVisitor.visitInsn(TypeUtil.buildPrimitiveTypeInfo(returnType).returnOpcode)
} else {
methodVisitor.visitTypeInsn(Opcodes.CHECKCAST, returnType.internalName)
methodVisitor.visitInsn(Opcodes.ARETURN)
}
// if (!exceptionClasses.isEmpty()) {
methodVisitor.visitLabel(label1)
methodVisitor.visitInsn(Opcodes.ATHROW)
methodVisitor.visitLabel(label2)
methodVisitor.visitVarInsn(Opcodes.ASTORE, 1)
methodVisitor.visitTypeInsn(Opcodes.NEW, Type.getInternalName(UndeclaredThrowableException::class.java))
methodVisitor.visitInsn(Opcodes.DUP)
methodVisitor.visitVarInsn(Opcodes.ALOAD, 1)
methodVisitor.visitMethodInsn(
Opcodes.INVOKESPECIAL, Type.getInternalName(
UndeclaredThrowableException::class.java
),
"<init>", "(Ljava/lang/Throwable;)V", false
)
methodVisitor.visitInsn(Opcodes.ATHROW)
// }
// Maxs computed by ClassWriter.COMPUTE_FRAMES, these arguments ignored
methodVisitor.visitMaxs(-1, -1)
methodVisitor.visitEnd()
}
}
private fun buildMethodVisitor(method: Method): MethodVisitor {
var accessFlags = Modifier.PUBLIC or Modifier.FINAL
if (method.isVarArgs) {
accessFlags = accessFlags or Opcodes.ACC_VARARGS
}
return classWriter.visitMethod(
accessFlags, method.name, Type.getMethodDescriptor(method),
null, // signature is always null
convert(method.exceptionTypes)
)
}
private fun convert(exceptionTypes: Array<Class<*>>): Array<String> {
return exceptionTypes.map { Type.getInternalName(it) }.toTypedArray()
}
private fun generateObjectArray(methodVisitor: MethodVisitor, types: Array<Type>) {
val length = types.size
intConstInstructionGenerator.generate(methodVisitor, length)
methodVisitor.visitTypeInsn(Opcodes.ANEWARRAY, Type.getInternalName(Object::class.java))
var delta = 0
for (i in 0 until length) {
methodVisitor.visitInsn(Opcodes.DUP)
intConstInstructionGenerator.generate(methodVisitor, i)
val localVariableTableIndex = 1 + i + delta
val type = types[i]
if (TypeUtil.forPrimitive(type)) {
val loadOpcode = TypeUtil.buildPrimitiveTypeInfo(type).loadOpcode
methodVisitor.visitVarInsn(loadOpcode, localVariableTableIndex)
val wrapperType = TypeUtil.toWrapperType(type)
methodVisitor.visitMethodInsn(
Opcodes.INVOKESTATIC, wrapperType.internalName, "valueOf",
"(${type.descriptor})${wrapperType.descriptor}", false
)
} else {
methodVisitor.visitVarInsn(Opcodes.ALOAD, localVariableTableIndex)
}
if (type == Type.LONG_TYPE || type == Type.DOUBLE_TYPE) {
delta++
}
methodVisitor.visitInsn(Opcodes.AASTORE)
}
}
/**
* The code is based on {@link java.lang.reflect.ProxyGenerator#computeUniqueCatchList(java.lang.Class[])}
*/
private fun computeUniqueCatchList(exceptions: Array<Class<*>>): List<Class<*>> {
for (ex in exceptions) {
if (ex == Throwable::class.java) {
return emptyList()
}
}
var uniqueList = mutableListOf<Class<*>>()
// unique exceptions to catch
uniqueList.add(Error::class.java) // always catch/rethrow these
uniqueList.add(RuntimeException::class.java)
for (ex in exceptions) {
/*
* Compare this exception against the current list of
* exceptions that need to be caught:
*/
if (uniqueList.any { it.isAssignableFrom(ex) }) {
continue
}
/*
* if a subclass of this exception is on the list
* to catch, then remove it
*/
val newResult = uniqueList.stream().filter { !ex.isAssignableFrom(it) }.toList()
uniqueList = newResult.toMutableList()
uniqueList.add(ex)
}
return uniqueList
}
}