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
}
}