javassist之取上级调用方法

private static Method getInvokingMethod(int index) {
    try {
        StackTraceElement stack[] = Thread.currentThread().getStackTrace();
        StackTraceElement parent = stack[index];
        Class cls = Class.forName(parent.getClassName());
        String methodName = parent.getMethodName();
        ClassPool pool = ClassPool.getDefault();
        ClassClassPath classPath = new ClassClassPath(cls);
        pool.insertClassPath(classPath);
        CtClass cc = pool.get(cls.getName());
        Class[] paramsTypes = null;
        for (CtMethod ctMethod : cc.getMethods()) {
            MethodInfo methodInfo = ctMethod.getMethodInfo();
            if (!methodName.equals(methodInfo.getName())) {
                continue;
            }
            //int ctMethodLine = methodInfo.getLineNumber(0);
            /* 取得方法起始行和结束行,然后对比执行的代码是否在此方法区间内 */
            int ctMethodMaxLine = methodInfo.getLineNumber(Integer.MAX_VALUE);
            int ctMethodMinLine = methodInfo.getLineNumber(0);
            int methodLine = parent.getLineNumber();
            /* 探测StackTraceElement中所带的行数是否超出了方法体外*/
            if(!(methodLine <= ctMethodMaxLine && methodLine >= ctMethodMinLine)){
                continue;
            }
            CtClass[] ctClass = ctMethod.getParameterTypes();
            paramsTypes = new Class[ctMethod.getParameterTypes().length];
            for (int i = 0; i < ctClass.length; i++) {
                CtClass paramsType = ctClass[i];
                //CtPrimitiveType

                Class type = null;
                String clzzName = null;

                if(paramsType.isPrimitive()){
                    type = ClassUtils.forName(paramsType.getSimpleName(), Thread.currentThread().getContextClassLoader());
                }else{
                    type = ClassUtils.forName((paramsType.getPackageName()+"."+paramsType.getSimpleName()), Thread.currentThread().getContextClassLoader());
                }
                paramsTypes[i] = type;
            }
            break;
        }
        return ReflectionUtils.findMethod(cls,methodName,paramsTypes);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
/**
 * 取上层调用方法
 * @return
 */
public static Method getParentInvokingMethod() {
    return getInvokingMethod(4);
}

/**
 * 取得调用该方法的上层方法
 * @return
 */
public static Method getParentInvokingThisMethod() {
    return getInvokingMethod(3);
}

导包

import javassist.*;
import javassist.bytecode.CodeAttribute;
import javassist.bytecode.LocalVariableAttribute;
import javassist.bytecode.MethodInfo;
import org.springframework.util.*;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值