假设我们有:
@WebserviceUrl("/withObj")
public void caller(Object obj){
called();
}
@WebserviceUrl("/withoutObj")
public void caller(){
called();
}
Run Code Online (Sandbox Code Playgroud)
如你所见,来电者有两个签名.为了获得堆栈跟踪,我们可以使用:
StackTraceElement[] stacktrace = Thread.currentThread().getStackTrace();
Run Code Online (Sandbox Code Playgroud)
但它只返回方法的名称.我怎样才能找到真正的真实来电者?
更新:问题
的主要目的是阅读在方法的注释中声明的webservice url.错误检测调用方法,导致调用错误的Web服务.
有趣的。我想到的一种可能的方法是像人类一样:读取堆栈跟踪中的行号,然后转到班级。看起来这是可行的:How to get the line number of a method? 。这并不直接适用,因为CtClass.getDeclaredMethod只为您提供该方法的一个签名。然而,你可以这样做:
String className;
String methodName;
int lineNumber;
// parse the stacktrace to get the name of the class, the name of the method and its line number
ClassPool pool = ClassPool.getDefault();
CtClass cc = pool.get(className);
CtMethod methodWhereExceptionOccurred =
Stream.of(cc.getDeclaredMethods())
.filter(method -> method.getName().equals(methodName))
.filter(method -> method.getMethodInfo().getLineNumber(0) == lineNumber)
.findFirst()
.get();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
423 次 |
| 最近记录: |