读书人

javassist取得参数名

发布时间: 2012-11-10 10:48:51 作者: rapoo

javassist获得参数名

import javassist.ClassPool;import javassist.CtClass;import javassist.CtMethod;import javassist.Modifier;import javassist.NotFoundException;import javassist.bytecode.CodeAttribute;import javassist.bytecode.LocalVariableAttribute;import javassist.bytecode.MethodInfo;import com.chinaGPS.driverBook.service.impl.UserManagerService;public class VLInterface {public String [] getParamterName(Class className,String method){String[] paramNames = null;try {ClassPool pool = ClassPool.getDefault();CtClass ctClass = pool.get(className.getName());CtMethod cm = ctClass.getDeclaredMethod(method);CtClass[] parameterTypes = cm.getParameterTypes();MethodInfo methodInfo = cm.getMethodInfo();CodeAttribute codeAttribute = methodInfo.getCodeAttribute();LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag);paramNames = new String[cm.getParameterTypes().length];int pos = Modifier.isStatic(cm.getModifiers()) ? 0 : 1;for (int j = 0; j < paramNames.length; j++)paramNames[j] = attr.variableName(j + pos);} catch (NotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}return paramNames;}public static void main(String[] args) throws NotFoundException, SecurityException, NoSuchMethodException {new VLInterface().getParamterName(UserManagerService.class, "modifyPwd");}}

?反射无法获取参数值,只能采用javassist。。。。。

读书人网 >编程

热点推荐