Re: find certain strings in java files not inside comments
- From: "A. Sinan Unur" <1usa@xxxxxxxxxxxxxxxxxxx>
- Date: Mon, 11 Jul 2005 18:10:00 GMT
"Hike Mike" <hikemike@xxxxxxxxx> wrote in
news:1121103647.478148.115280@xxxxxxxxxxxxxxxxxxxxxxxxxxxx:
> I want to determine if there any of the following strings in a java
> file that do not exist as part of a comment:
>
> System.out.print
> System.err.print
> printStatckTrace
ITYM printStackTrace
> I'm totally new at Perl and I wrote the following script that get's
> called from CVS when developers check in code (the script gets called
> with a list of java files starting at $ARGV[1]. The idea is to not
> allow any uncommented strings of the above type into the repository
> unless they are inside comments.
>
> It does not work for instances of a dis-allowed string that is on a
> newline inside of a comment:
>
> /* blah blah blah
> System.out.println("foo");
> */
Well, there is, of course, nothing that is stopping you from doing a lot
fo work here but, why not just run the file throught the C
pre-processor, and match on the strings you are looking for?
> basically the problem is the following match code:
>
>
> unless ($item =~ m/^((\/\/+)|(\/\*+)|(\*+))/) {
I am never ever going to read code like this.
unless($item =~ m{^((//+)|(/\*+)|(\*+))/) {
Anyway, what's up with all the capturing? What do you think this line of
code does?
> use Socket;
> use CGI qw(:standard escape);
How are these two modules even relevant to what you are doing?
> use strict;
use warnings;
rather than perl -w
> my $sArg = "";
> shift @ARGV;
> foreach( @ARGV )
> {
> #print "received ARGV: $_\n";
> if ($_ =~ m/java/) {
> #print "received java file: $_\n";
> $sArg .= $_ . " ";
> }
> }
So, do you consider java.c to be a java file? I do not know why you
would do such a thing but the above can be reduced to (untested):
shift @ARGV;
my $sArg = join(' ', grep { /\.java$/ } @ARGV);
> chop( $sArg );
Why?
> if ($sArg) {
> print "CVS commitinfo is checking .java files for disallowed code.
> If
> your commit fails, check for un-commented 'System.out.
> print', 'System.err.print', or 'printStackTrace()' in: $sArg \n";
> }
>
> my @rgFiles = split(" ", $sArg);
OK, so you joined all the filenames together, now you are blinding
splitting on space. What if one of the elements of @ARGV contained a
path with a space.
> my $SystemOut = "System.out.print";
> my $SystemErr = "System.err.print";
> my $StackTrace = "printStackTrace";
Do you not see how pointless it is to have n variables whose names are
the same as the values they hold?
my @bad = qw(System.out.print System.err.print printStackTrace);
> my @badFiles;
> my @rawData;
> my $counter = 0;
> my @instances;
> my $item;
Declare your variables in the smallest applicable scope.
> foreach( @rgFiles )
> {
> check($_, $SystemOut);
> check($_, $SystemErr);
> checkStackTrace($_, $StackTrace);
> }
If the check finds an uncommented 'System.out.print', do you need to
check the other two?
Sinan
--
A. Sinan Unur <1usa@xxxxxxxxxxxxxxxxxxx>
(reverse each component and remove .invalid for email address)
comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
.
- References:
- find certain strings in java files not inside comments
- From: Hike Mike
- find certain strings in java files not inside comments
- Prev by Date: find certain strings in java files not inside comments
- Next by Date: Pool of database connections
- Previous by thread: find certain strings in java files not inside comments
- Next by thread: Re: find certain strings in java files not inside comments
- Index(es):
Relevant Pages
|