在 Java 中,我想找到一种允许程序访问自己的源代码的方法,主要用于调试和元编程目的(例如在运行时打印方法签名,或允许程序读取自己的注释,或允许一个 Java 类来打印某种类型的所有方法,或者允许程序生成它自己的源代码的新版本等)。
有没有办法让 Java 程序访问它自己的源代码的副本,并逐行读取它?
//this is the first line of the program
//this method is not implemented
public class inspectSourceCode(){
public static String getLine(int lineNumber){
//get the line of the program's own source code as a string,
//this is not currently implemented
}
//this method is implemented
public static void main(String[] args){
System.out.println(getLine(0));
//should print "//this is the first line of the program",
//if the method getLine works correctly
}
}