(java)如何获得一个方法(method)的行数
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"); } }
参考:
http://stackoverflow.com/questions/12834887/how-to-get-the-line-number-of-a-method