shell to find out DBCP ( try - catch ) in java

i need a shell to find out DBCP in Java

The complete spec is here.

shell file can be as follows

if ( input fileS is in the format of wrong1.txt ) {

Print the fileS name.

}

wrong1.txt

DataBaseConnection dbconn = new DataBaseConnection(connectionString);
try{
something...
somefuntion(dbconn,...)
dbconn..
..
..
}catch{
print excecption;
}finally{
dbconn.destroy();
}

           ---->> here connection with DB is outside of Try block -->> is wrong.

correct1.txt

DataBaseConnection dbconn = null;
try{
dbconn = new DataBaseConnection(connectionString);
something...
somefuntion(dbconn,...)
dbconn..
..
..
}catch{
print excecption;
}finally{
dbconn.destroy();
}

NB:
The line --> DataBaseConnection dbconn = new DataBaseConnection(connectionString);
may change (i mean it will not be exactly as it is in all the input files)

.

does this work ? (not tested)

find yourPath -name "*.java" |xargs awk '/try{/{t=1} /}catch/{t=0} (!t && $0~/new DataBaseConnection\(/){print FILENAME}'

Thanks Mr.sk1418..

Great.. wonderful.. in one line you have did that.

But i am getting output Partially only.

Problem is:
i am getting Problematic file's list , But list of files NOT containing the word "new DataBaseConnection" also getting..

.

is that mean that you want all java files that don't contain "new DataBaseConnection(...)" to be listed out as well?

does the grep command help?

grep -lv "new DatabaseConnection" *.java

oh.. i am sorry.. i wrote wrongly.

what i mean is :

the code

find yourPath -name "*.java" |xargs awk '/try{/{t=1} /}catch/{t=0} (!t && $0~/new DataBaseConnection\(/){print FILENAME}'

is working. But listing in correct1.txt file format. (as in my first post)

i changed the code as

find yourPath -name "*.java" |xargs awk '/try{/{t=1} /}catch/{t=0} (t && $0~/new DataBaseConnection\(/){print FILENAME}'  
--> with only t NOT !t

Then i am getting in wrong1.txt file format (as in my first post)
But here the problem is :
the java files Does not containing any database connection is also listing...

i know i did wrong as i deleted the ! charector from t.

but..

could you please change the code to get all the *.java files in Wrong1.txt format Alone.

.

here i made some dummy java file, and my initial command (with "!t") gives the right result, the java files, which have creating connection statement outside the try/catch block.

yes sk1418,

with dummy java files its working fine. and I am sorry I cannot provide you the original details.

Thank you for your time.

basically, the command is NOT gonna print any file, that doesn't containing "
new DataBaseConnection" string. Can that be that the string in your java file as comment? so you think it should be the "correct" case, but listed out by my command?

yes of course.

i am very very happy.

But the thing is it's listing some "un"problematic files also :slight_smile:

Let it be. No problem. I will find all those manually.

Any way my work reduced a lot.

Thanks again.

.

---------- Post updated at 06:30 PM ---------- Previous update was at 05:42 PM ----------

sk1418,

i did it. extracted those files again.

yourCommand > fileOne.txt -->> >1800 files

cp `cat fileOne.txt` | grep -v "new Databaseconnection" > newFile.txt ---> 158files.

again i need to filter..

or you could try grep first, then pass to my awk. (although I thought the printed out file by my awk must contain "new Databaseconnection..." in java codes or java comments). e.g.

grep -il "new databaseconnection(" | xargs awk ....

can it reduce your effort a little bit?

yes.. thanks.

You rocks..