Re: find certain strings in java files not inside comments



"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
.



Relevant Pages

  • Re: Java compatibility issues (WAS: MF having issues?)
    ... I believe, that the JLS (Java Language Specification) REQUIRES that this optimization be done. ... package testPackage; ... true true true true false true ... * Literal strings within different classes in the same package represent references to the same String object. ...
    (comp.lang.cobol)
  • Re: Performance of hash_set vs. Java
    ... The main benefit Java has in hashing is that Strings cache their ... >that my hash function may be slow from the call to c_str. ... The above code may be where your main bottleneck is. ...
    (comp.lang.cpp)
  • Re: Ressourcensparend programmieren
    ... Mein Java Client arbeitet mit einem C++ Server zusammen und es kommen ... nur 1 byte Character vor (UTF-8). ... Seite als Strings behandelt. ... Tabelle mit engen Zellen kam ich nicht über 10 000 getValueAbfragen ...
    (de.comp.lang.java)
  • Re: How do you use another class file... Seriously
    ... >>application as thats the entry point to your program. ... The mainmethod must be static, not return a value, and take an array ... of strings. ... cannot do this in Java 1.1. ...
    (comp.lang.java.programmer)
  • Re: Hendrix on telly
    ... Maybe it's a generation thing but any live clips of Hendrix I think ... sound that created rock as we know it blah blah blah, ... This approach was OK when everyone was using OEM 13-56 strings because the innate tension of the strings and guitar usually kept the instrument in tune, but once people started switching to Fender R'n'R strings, all tuning bets were off and for many, would stay off until a better understanding of the geometry of the instrument started spreading in the mid-70s. ...
    (uk.music.guitar)