java代码如何获取一个方法的行号呢?

java-教程王 Java经验 发布时间:2022-04-19 21:36:28 阅读数:19580 1
下文笔者讲述使用Java代码获取“一个函数在代码中的行号”的方法分享,如下所示
实现思路:
    使用ClassPool获取一个包对象
	然后获取其中的方法
例: 获取一个函数的行号
package com.java265.other;

import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
import javassist.NotFoundException;
public class Test16 {
	/**
	 * java265.com 示例程序
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {

		ClassPool pool = ClassPool.getDefault();
		CtClass cc = pool.get("com.java265.other.Test16");
		CtMethod methodX = cc.getDeclaredMethod("hello");
		int xlineNumber = methodX.getMethodInfo().getLineNumber(0);
		System.out.println("方法hello在行: " + xlineNumber + "\n");

	}

}

class TestClass16 {
	void hello() {
		System.out.println("hello method()!");
	}
}


版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaJingYan/202204/16503762042952.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者