Sncp remote支持字段名
This commit is contained in:
@@ -208,7 +208,7 @@ public abstract class AsmMethodBoost<T> {
|
|||||||
List<AsmMethodParam> params = methodBean.getParams();
|
List<AsmMethodParam> params = methodBean.getParams();
|
||||||
for (int i = 0; i < paramTypes.length; i++) {
|
for (int i = 0; i < paramTypes.length; i++) {
|
||||||
AsmMethodParam param = params.get(i);
|
AsmMethodParam param = params.get(i);
|
||||||
mv.visitLocalVariable(param.getName(), param.getDescription(), param.getSignature(), l0, l2, insns.get(i));
|
mv.visitLocalVariable(param.getName(), param.description(paramTypes[i]), param.signature(paramTypes[i]), l0, l2, insns.get(i));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -307,40 +307,49 @@ public abstract class AsmMethodBoost<T> {
|
|||||||
|
|
||||||
static class MethodParamClassVisitor extends ClassVisitor {
|
static class MethodParamClassVisitor extends ClassVisitor {
|
||||||
|
|
||||||
|
private Class serviceType;
|
||||||
|
|
||||||
private final Map<String, AsmMethodBean> methodBeanMap;
|
private final Map<String, AsmMethodBean> methodBeanMap;
|
||||||
|
|
||||||
public MethodParamClassVisitor(int api, final Map<String, AsmMethodBean> fieldmap) {
|
public MethodParamClassVisitor(int api, Class serviceType, final Map<String, AsmMethodBean> methodBeanMap) {
|
||||||
super(api);
|
super(api);
|
||||||
this.methodBeanMap = fieldmap;
|
this.serviceType = serviceType;
|
||||||
|
this.methodBeanMap = methodBeanMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
|
public MethodVisitor visitMethod(int methodAccess, String methodName, String methodDesc, String methodSignature, String[] methodExceptions) {
|
||||||
if (java.lang.reflect.Modifier.isStatic(access)) {
|
super.visitMethod(api, methodName, methodDesc, methodSignature, methodExceptions);
|
||||||
|
if (java.lang.reflect.Modifier.isStatic(methodAccess)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
String key = name + ":" + desc;
|
String key = methodName + ":" + methodDesc;
|
||||||
if (methodBeanMap.containsKey(key)) {
|
if (methodBeanMap.containsKey(key)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
AsmMethodBean bean = new AsmMethodBean(access, name, desc, signature, exceptions);
|
AsmMethodBean bean = new AsmMethodBean(methodAccess, methodName, methodDesc, methodSignature, methodExceptions);
|
||||||
List<AsmMethodParam> paramList = bean.getParams();
|
List<AsmMethodParam> paramList = bean.getParams();
|
||||||
methodBeanMap.put(key, bean);
|
methodBeanMap.put(key, bean);
|
||||||
return new MethodVisitor(Opcodes.ASM6) {
|
return new MethodVisitor(Opcodes.ASM6) {
|
||||||
@Override
|
@Override
|
||||||
public void visitLocalVariable(String name, String description, String signature, Label start, Label end, int index) {
|
public void visitParameter(String paramName, int paramAccess) {
|
||||||
if (index < 1) {
|
paramList.add(new AsmMethodParam(paramName));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visitLocalVariable(String varName, String varDesc, String varSignature, Label start, Label end, int varIndex) {
|
||||||
|
if (varIndex < 1) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
int size = paramList.size();
|
int size = paramList.size();
|
||||||
//index并不会按顺序执行
|
//index并不会按顺序执行
|
||||||
if (index > size) {
|
if (varIndex > size) {
|
||||||
for (int i = size; i < index; i++) {
|
for (int i = size; i < varIndex; i++) {
|
||||||
paramList.add(new AsmMethodParam(" ", description, signature));
|
paramList.add(new AsmMethodParam(" ", varDesc, varSignature));
|
||||||
}
|
}
|
||||||
paramList.set(index - 1, new AsmMethodParam(name, description, signature));
|
paramList.set(varIndex - 1, new AsmMethodParam(varName, varDesc, varSignature));
|
||||||
}
|
}
|
||||||
paramList.set(index - 1, new AsmMethodParam(name, description, signature));
|
paramList.set(varIndex - 1, new AsmMethodParam(varName, varDesc, varSignature));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -353,11 +362,11 @@ public abstract class AsmMethodBoost<T> {
|
|||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
new ClassReader(Utility.readBytesThenClose(in)).accept(new MethodParamClassVisitor(Opcodes.ASM6, map), 0);
|
new ClassReader(Utility.readBytesThenClose(in)).accept(new MethodParamClassVisitor(Opcodes.ASM6, clazz, map), 0);
|
||||||
} catch (Exception e) { //无需理会
|
} catch (Exception e) { //无需理会
|
||||||
}
|
}
|
||||||
Class superClass = clazz.getSuperclass();
|
Class superClass = clazz.getSuperclass();
|
||||||
if (superClass == Object.class) {
|
if (superClass == null || superClass == Object.class) { //接口的getSuperclass为null
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
return getMethodParamNames(map, superClass);
|
return getMethodParamNames(map, superClass);
|
||||||
|
|||||||
@@ -4,10 +4,11 @@
|
|||||||
package org.redkale.asm;
|
package org.redkale.asm;
|
||||||
|
|
||||||
import org.redkale.convert.json.JsonConvert;
|
import org.redkale.convert.json.JsonConvert;
|
||||||
|
import org.redkale.util.TypeToken;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 存放方法参数的字节信息
|
* 存放方法参数的字节信息
|
||||||
*
|
*
|
||||||
* @see org.redkale.asm.AsmMethodBean
|
* @see org.redkale.asm.AsmMethodBean
|
||||||
* @see org.redkale.asm.AsmMethodBoost
|
* @see org.redkale.asm.AsmMethodBoost
|
||||||
*
|
*
|
||||||
@@ -24,12 +25,24 @@ public class AsmMethodParam {
|
|||||||
public AsmMethodParam() {
|
public AsmMethodParam() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public AsmMethodParam(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
public AsmMethodParam(String name, String description, String signature) {
|
public AsmMethodParam(String name, String description, String signature) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.description = description;
|
this.description = description;
|
||||||
this.signature = signature;
|
this.signature = signature;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String description(java.lang.reflect.Type type) {
|
||||||
|
return description == null ? Type.getDescriptor(TypeToken.typeToClass(type)) : description;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String signature(java.lang.reflect.Type type) {
|
||||||
|
return signature;
|
||||||
|
}
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -635,7 +635,7 @@ public abstract class Sncp {
|
|||||||
List<AsmMethodParam> params = methodBean.getParams();
|
List<AsmMethodParam> params = methodBean.getParams();
|
||||||
for (int i = 0; i < paramTypes.length; i++) {
|
for (int i = 0; i < paramTypes.length; i++) {
|
||||||
AsmMethodParam param = params.get(i);
|
AsmMethodParam param = params.get(i);
|
||||||
mv.visitLocalVariable(param.getName(), param.getDescription(), param.getSignature(), l0, l2, insns.get(i));
|
mv.visitLocalVariable(param.getName(), param.description(paramTypes[i]), param.signature(paramTypes[i]), l0, l2, insns.get(i));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
mv.visitMaxs(20, 20);
|
mv.visitMaxs(20, 20);
|
||||||
@@ -974,10 +974,13 @@ public abstract class Sncp {
|
|||||||
// mv.visitMaxs(1, 1);
|
// mv.visitMaxs(1, 1);
|
||||||
// mv.visitEnd();
|
// mv.visitEnd();
|
||||||
// }
|
// }
|
||||||
|
Map<String, AsmMethodBean> methodBeans = AsmMethodBoost.getMethodBeans(serviceTypeOrImplClass);
|
||||||
for (final SncpRemoteAction entry : info.getActions()) {
|
for (final SncpRemoteAction entry : info.getActions()) {
|
||||||
final java.lang.reflect.Method method = entry.method;
|
final java.lang.reflect.Method method = entry.method;
|
||||||
{
|
{
|
||||||
mv = new MethodDebugVisitor(cw.visitMethod(ACC_PUBLIC, method.getName(), Type.getMethodDescriptor(method), null, null));
|
mv = new MethodDebugVisitor(cw.visitMethod(ACC_PUBLIC, method.getName(), Type.getMethodDescriptor(method), null, null));
|
||||||
|
Label l0 = new Label();
|
||||||
|
mv.visitLabel(l0);
|
||||||
//mv.setDebug(true);
|
//mv.setDebug(true);
|
||||||
{ //给参数加上 Annotation
|
{ //给参数加上 Annotation
|
||||||
final Annotation[][] anns = method.getParameterAnnotations();
|
final Annotation[][] anns = method.getParameterAnnotations();
|
||||||
@@ -992,14 +995,16 @@ public abstract class Sncp {
|
|||||||
|
|
||||||
mv.visitLdcInsn(entry.actionid.toString());
|
mv.visitLdcInsn(entry.actionid.toString());
|
||||||
|
|
||||||
|
AsmMethodBean methodBean = AsmMethodBean.get(methodBeans, method);
|
||||||
|
List<Integer> insns = new ArrayList<>();
|
||||||
|
java.lang.reflect.Type[] paramTypes = entry.paramTypes;
|
||||||
{ //传参数
|
{ //传参数
|
||||||
int paramlen = entry.paramTypes.length;
|
int paramlen = entry.paramTypes.length;
|
||||||
Asms.visitInsn(mv, paramlen);
|
Asms.visitInsn(mv, paramlen);
|
||||||
mv.visitTypeInsn(ANEWARRAY, "java/lang/Object");
|
mv.visitTypeInsn(ANEWARRAY, "java/lang/Object");
|
||||||
java.lang.reflect.Type[] paramtypes = entry.paramTypes;
|
|
||||||
int insn = 0;
|
int insn = 0;
|
||||||
for (int j = 0; j < paramtypes.length; j++) {
|
for (int j = 0; j < paramTypes.length; j++) {
|
||||||
final java.lang.reflect.Type pt = paramtypes[j];
|
final java.lang.reflect.Type pt = paramTypes[j];
|
||||||
mv.visitInsn(DUP);
|
mv.visitInsn(DUP);
|
||||||
insn++;
|
insn++;
|
||||||
Asms.visitInsn(mv, j);
|
Asms.visitInsn(mv, j);
|
||||||
@@ -1020,6 +1025,7 @@ public abstract class Sncp {
|
|||||||
mv.visitVarInsn(ALOAD, insn);
|
mv.visitVarInsn(ALOAD, insn);
|
||||||
}
|
}
|
||||||
mv.visitInsn(AASTORE);
|
mv.visitInsn(AASTORE);
|
||||||
|
insns.add(insn);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1053,6 +1059,16 @@ public abstract class Sncp {
|
|||||||
mv.visitInsn(ARETURN);
|
mv.visitInsn(ARETURN);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (methodBean != null && paramTypes.length > 0) {
|
||||||
|
Label l2 = new Label();
|
||||||
|
mv.visitLabel(l2);
|
||||||
|
//mv.visitLocalVariable("this", thisClassDesc, null, l0, l2, 0);
|
||||||
|
List<AsmMethodParam> params = methodBean.getParams();
|
||||||
|
for (int i = 0; i < paramTypes.length; i++) {
|
||||||
|
AsmMethodParam param = params.get(i);
|
||||||
|
mv.visitLocalVariable(param.getName(), param.description(paramTypes[i]), param.signature(paramTypes[i]), l0, l2, insns.get(i));
|
||||||
|
}
|
||||||
|
}
|
||||||
mv.visitMaxs(20, 20);
|
mv.visitMaxs(20, 20);
|
||||||
mv.visitEnd();
|
mv.visitEnd();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user