regex - How to replace java code block to another code block automatically or pragramatically? -
i have many java source files in want change code block code block in every java files have in project.
e.g. project has 300 java source file , every file has code block
while (p.getcurrenttoken() != jsontoken.end_object) { p.nexttoken(); field = p.getcurrentname(); obj.populatefromjsonstring(field, p); }
i want replace code block
while (p.nexttoken() != null) { field = p.getcurrentname(); if(field!=null){ obj.populatefromjsonstring(field, p); } }
in java files. object obj
in code block different different files
i know somehow can done text processing sed , awk. thinking alternate solution way compilers read , parse java source file. there may application based on way solving problem.
please share way or application solve problem. sed/awk based solutions welcome.
it's best use ide this, if wanted use sed, put in file called j.sed
/while (p\.getcurrenttoken() != jsontoken\.end_object) {/{ :loop n /}/b s/field = p\.getcurrentname();/if (field!=null) {/ s/p\.nexttoken();/field = p\.getcurrentname();/ s/...\.populatefromjsonstring(field, p);/\t&\n\t}/ b loop }
then use in command line
find . -type f -name '*.java' -exec sed -i.bak -f j.sed {} \;
this on input:
while (p.getcurrenttoken() != jsontoken.end_object) { p.nexttoken(); field = p.getcurrentname(); obj.populatefromjsonstring(field, p); }
output this:
while (p.getcurrenttoken() != jsontoken.end_object) { field = p.getcurrentname(); if (field!=null) { obj.populatefromjsonstring(field, p); } }
the -i.bak edit in place , store backup filename.java.bak
Comments
Post a Comment