如何获取方法的行号? [英] How to get the line number of a method?

查看:595
本文介绍了如何获取方法的行号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用反射或其他魔法获取方法的行号?

如果方法在当前的Stacktrace中,则可能。使用 Thread.currentThread()。getStackTrace(),可以获得 StackTraceElement 。但是,如果我只获得,我该怎么办? java.lang.reflect.Method 对象?


我发现这个,对于classes-> 如何从中获取源文件名/行号java.lang.Class对象,但它对方法没有用。

Is it possible to get the line number of an method using reflection or other magic?
It is possible if the method is inside the current Stacktrace. Using Thread.currentThread().getStackTrace(), one can get the line number of an StackTraceElement. But what can I do if I only got the java.lang.reflect.Method Object?

I found this, for classes-> How to get source file-name/line-number from a java.lang.Class object but it's not useful for methods.

推荐答案

我想做同样的事情,经过一些研究确定了javassist。您将需要添加javassist(我使用版本3.15.0-GA)。

I wanted to do the same thing and after some research settled on javassist. You will need to add javassist (I used version 3.15.0-GA).

给定以下类确定x方法的位置。方法名称x是硬编码的,但如果你和我在同一条船上,反射并不难,所以我相信你可以得到一个方法名称列表,然后以下内容将让你获得行号方法:

Given the following class determine the location of the "x" method. The method name "x" is hard coded however if you are in the same boat as me, the reflection isn't hard so I'm confident you can get a list of method names, then the following will let you get the line numbers of the methods:

public class Widget {
    void x(){System.out.println("I'm x\n");}
    //comment added to create space
    void y(){System.out.println("I'm y\n");} 
}







import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
import javassist.NotFoundException;

public class App {
    public static void main(String[] args) throws NotFoundException {
        System.out.println("Get method line number with javassist\n");
        ClassPool pool = ClassPool.getDefault();
        CtClass cc = pool.get("com.quaternion.demo.Widget");
        CtMethod methodX = cc.getDeclaredMethod("x");
        int xlineNumber = methodX.getMethodInfo().getLineNumber(0);
        System.out.println("method x is on line " + xlineNumber + "\n");
    }
}

输出方法x在第12行在我的情况下是准确的我删除了一些评论......

Output: method x is on line 12 which in my case is accurate I cut out some comments...

注意:正如Pete83在评论中所提到的,这个方法实际上返回了方法中的第一行代码,而不是声明方法的行。这通常不会成为问题,因为大多数人可能想要建立相对位置(声明它们的顺序)并将此信息用于您自己的约定。只要您觉得需要在注释中包含序数值,这可以通过代码本身内的位置轻松确定,就会出现这种情况。



快速参考javassist的Maven坐标:

Note: As mentioned by Pete83 in the comments, this method actually returns the first line of code in the method and not the line which declares the method. This usually won't be an issue as most will probably want to establish relative position (the order in which they were declared) and use this information for your own conventions. This would come up any time you felt the need to include an ordinal value within an annotation which could be readily determined by the position within the code itself.


For quick reference Maven coordinates for javassist:

<dependency>
   <groupId>org.javassist</groupId> <!-- if a version prior to 3.13.0-GA is needed use "javassist" and not "org.javassist" -->
   <artifactId>javassist</artifactId>
   <version>3.15.0-GA</version>
</dependency>

这篇关于如何获取方法的行号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆