javassist获得方法参数名称

举例,方法get(String name, long id),目的想获得name,id这两个名称:

初衷:做日志拦截的时候,比如拦截了方法get(String name, long id),想在日志表中记录成姓名:admin,主键:123,

就需要获取方法的参数名称,以便将参数名称翻译成对应的中文名,比如name对应姓名,id对应主键

package cn.sniper.reflect.utils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import javassist.ClassClassPath;
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.audaque.webkit.web.action.IndexAction;
public class ReflectUtil {
 
 private static final Map<String, List<String>> PARAM_MAP = new HashMap<String, List<String>>();
 
 private static final ReadWriteLock RWL = new ReentrantReadWriteLock();
 
 private static final ClassPool CLASS_POOL = ClassPool.getDefault();  
 
 static {
  //解决javassist.NotFoundException报错
  CLASS_POOL.insertClassPath(new ClassClassPath(IndexAction.class));
 }
 /**
  * 拿到方法的参数名称
  * 比如get(long id, String name)
  * 获得id, name
  */
 public static List<String> getParamNameList(Class clazz, String methodName) {
  RWL.readLock().lock();
  List<String> paramNameList = PARAM_MAP.get(clazz.getName()+"-"+methodName);
  try {
   if(paramNameList == null || paramNameList.size() <= 0) {
    try {
     RWL.readLock().unlock();
     RWL.writeLock().lock();
     if(paramNameList == null || paramNameList.size() <= 0) {
      try {  
       paramNameList = new ArrayList<String>();
          CtClass cc = CLASS_POOL.get(clazz.getName());  
          CtMethod cm = cc.getDeclaredMethod(methodName);  
       
          // 使用javaassist的反射方法获取方法的参数名  
          MethodInfo methodInfo = cm.getMethodInfo();  
          CodeAttribute codeAttribute = methodInfo.getCodeAttribute();  
          LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag);  
          if (attr != null) {  
           int pos = Modifier.isStatic(cm.getModifiers()) ? 0 : 1;  
           for (int i = 0; i < cm.getParameterTypes().length; i++) {
               paramNameList.add(attr.variableName(i + pos));
           }
          }  
      } catch (NotFoundException e) {  
          e.printStackTrace();  
      }  
      
      PARAM_MAP.put(clazz.getName()+"-"+methodName, paramNameList);
     }
    } finally {
     RWL.writeLock().unlock();
     RWL.readLock().lock();
    }
   }
   
  } finally {
   RWL.readLock().unlock();
  }
  
  return paramNameList;
 }
 
}

转载于:https://my.oschina.net/sniperLi/blog/492401

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值