AsmMethodBoost优化
This commit is contained in:
@@ -121,6 +121,23 @@ public abstract class AsmMethodBoost<T> {
|
||||
public void doAfterMethods(
|
||||
DynBytesClassLoader classLoader, ClassWriter cw, String newDynName, String fieldPrefix) {}
|
||||
|
||||
/**
|
||||
* 处理所有动态方法后调用
|
||||
*
|
||||
* @param classLoader ClassLoader
|
||||
* @param cw 动态字节码Writer
|
||||
* @param mv 构造函数MethodVisitor
|
||||
* @param newDynName 动态新类名
|
||||
* @param fieldPrefix 动态字段的前缀
|
||||
* @param remote 是否远程模式
|
||||
*/
|
||||
public void doConstructorMethod(
|
||||
DynBytesClassLoader classLoader,
|
||||
ClassWriter cw,
|
||||
MethodVisitor mv,
|
||||
String newDynName,
|
||||
String fieldPrefix,
|
||||
boolean remote) {}
|
||||
/**
|
||||
* 实例对象进行操作,通常用于给动态的字段赋值
|
||||
*
|
||||
@@ -336,6 +353,21 @@ public abstract class AsmMethodBoost<T> {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doConstructorMethod(
|
||||
DynBytesClassLoader classLoader,
|
||||
ClassWriter cw,
|
||||
MethodVisitor mv,
|
||||
String newDynName,
|
||||
String fieldPrefix,
|
||||
boolean remote) {
|
||||
for (AsmMethodBoost item : items) {
|
||||
if (item != null) {
|
||||
item.doConstructorMethod(classLoader, cw, mv, newDynName, fieldPrefix, remote);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doInstance(DynBytesClassLoader classLoader, ResourceFactory resourceFactory, T service) {
|
||||
for (AsmMethodBoost item : items) {
|
||||
|
||||
@@ -585,19 +585,22 @@ public abstract class Sncp {
|
||||
fv = cw.visitField(ACC_PRIVATE, FIELDPREFIX + "_mq", Type.getDescriptor(String.class), null, null);
|
||||
fv.visitEnd();
|
||||
}
|
||||
if (methodBoost != null) {
|
||||
createNewMethods(dynLoader, serviceImplClass, methodBoost, new HashSet<>(), cw, newDynName, supDynName);
|
||||
methodBoost.doAfterMethods(dynLoader, cw, newDynName, FIELDPREFIX);
|
||||
}
|
||||
{ // 构造函数
|
||||
mv = new MethodDebugVisitor(cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null));
|
||||
// mv.setDebug(true);
|
||||
mv.visitVarInsn(ALOAD, 0);
|
||||
mv.visitMethodInsn(INVOKESPECIAL, supDynName, "<init>", "()V", false);
|
||||
if (methodBoost != null) {
|
||||
methodBoost.doConstructorMethod(dynLoader, cw, mv, newDynName, FIELDPREFIX, false);
|
||||
}
|
||||
mv.visitInsn(RETURN);
|
||||
mv.visitMaxs(1, 1);
|
||||
mv.visitEnd();
|
||||
}
|
||||
if (methodBoost != null) {
|
||||
createNewMethods(dynLoader, serviceImplClass, methodBoost, new HashSet<>(), cw, newDynName, supDynName);
|
||||
methodBoost.doAfterMethods(dynLoader, cw, newDynName, FIELDPREFIX);
|
||||
}
|
||||
cw.visitEnd();
|
||||
byte[] bytes = cw.toByteArray();
|
||||
Class<?> newClazz = null;
|
||||
@@ -1044,20 +1047,6 @@ public abstract class Sncp {
|
||||
fv = cw.visitField(ACC_PRIVATE, FIELDPREFIX + "_sncp", sncpInfoDesc, null, null);
|
||||
fv.visitEnd();
|
||||
}
|
||||
{ // 构造函数
|
||||
mv = new MethodDebugVisitor(cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null));
|
||||
// mv.setDebug(true);
|
||||
mv.visitVarInsn(ALOAD, 0);
|
||||
mv.visitMethodInsn(
|
||||
INVOKESPECIAL,
|
||||
serviceTypeOrImplClass.isInterface() ? "java/lang/Object" : supDynName,
|
||||
"<init>",
|
||||
"()V",
|
||||
false);
|
||||
mv.visitInsn(RETURN);
|
||||
mv.visitMaxs(1, 1);
|
||||
mv.visitEnd();
|
||||
}
|
||||
{ // init
|
||||
mv = new MethodDebugVisitor(cw.visitMethod(ACC_PUBLIC, "init", "(" + anyValueDesc + ")V", null, null));
|
||||
mv.visitInsn(RETURN);
|
||||
@@ -1238,6 +1227,23 @@ public abstract class Sncp {
|
||||
createNewMethods(dynLoader, serviceTypeOrImplClass, methodBoost, methodKeys, cw, newDynName, supDynName);
|
||||
methodBoost.doAfterMethods(dynLoader, cw, newDynName, FIELDPREFIX);
|
||||
}
|
||||
{ // 构造函数
|
||||
mv = new MethodDebugVisitor(cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null));
|
||||
// mv.setDebug(true);
|
||||
mv.visitVarInsn(ALOAD, 0);
|
||||
mv.visitMethodInsn(
|
||||
INVOKESPECIAL,
|
||||
serviceTypeOrImplClass.isInterface() ? "java/lang/Object" : supDynName,
|
||||
"<init>",
|
||||
"()V",
|
||||
false);
|
||||
if (methodBoost != null) {
|
||||
methodBoost.doConstructorMethod(dynLoader, cw, mv, newDynName, FIELDPREFIX, true);
|
||||
}
|
||||
mv.visitInsn(RETURN);
|
||||
mv.visitMaxs(1, 1);
|
||||
mv.visitEnd();
|
||||
}
|
||||
cw.visitEnd();
|
||||
byte[] bytes = cw.toByteArray();
|
||||
Class<?> newClazz = dynLoader.loadClass(newDynName.replace('/', '.'), bytes);
|
||||
|
||||
Reference in New Issue
Block a user