How to search for a string in Java source code and identify in which method it is in -
what need map classes , methods of java source code have inside implementations specific string. translating in real example: need build java application scans java source file searching specific string (a database field example). after finding string need have output in class/method find. so, if have following code:
public class exampleclass{ public static void main(string args[]) { string url = "hello1"; } public void function1(){ string hello2 = "aaaaaa"; } }
in case, if search "hello1" should return like: exampleclass.main. , if search hello2 should return me: exampleclass.function1
thanks lot in advance!
from description first sounds use tool grep scan files. solution, scan files looking occurrence of "hello1"
if sufficient needs. output quite customizable. using grep
command line:
$ grep -r "hello1" /home/user/workspace/project/src
given /home/user/workspace/project/src
project source code root. there implementations of grep windows, make sure version using supporting recursion via r
switch, not implementations do. can call grep
java application using system.exec
. read resulting process's output stream results.
however, if need parse source code in order make sure expression looking proper string
literal @ right place may want use eclipse jdt. reckon might overkill trying achieve since need parse entire project rather complex job.
Comments
Post a Comment