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 block
			e.printStackTrace();
		}
		return paramNames;
	}

	public static void main(String[] args) throws NotFoundException, SecurityException, NoSuchMethodException {
		new VLInterface().getParamterName(UserManagerService.class, "modifyPwd");
	}
}

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

你可能感兴趣的:(javassist获得参数名)