Re: Analize Java source file with perl?



Hou Bou wrote:
hello, all
I want to get the Class name of .java file with perl ,
How can I do it ? I think it is difficult to result the java comment .
For Example
a java file named AAA.java
--------------------
/* author : John Smith */
// comment
public class ClassA {
/* */
//comment
public static void main(String[] args) {
}
...
}
class ClassB {
...
}
---------------------
I want to get like this
perl ClassChecker.pl AAA.java
ClassA
ClassB

Use the Regexp::Common module to strip comments from the file, and then
parse it looking for class declarations. The below won't work for all
situations (at the very least, a string that contains what looks like a
class declaration will cause false positives) but will probably get
most. It is, however, completely untested.

#!/usr/bin/perl
use strict;
use warnings;
#download these two from CPAN
use Regexp::Common qw/comment/;
use File::Slurp;

my $file = shift or die "Usage: $0 filename.java\n";
my $contents = read_file($file);
$contents =~ s/$RE{comment}{Java}//g;
while ($contents =~ /\bclass\s+(\w+)\s*{/) {
print "$1\n";
}
__END__

.



Relevant Pages

  • Re: Analize Java source file with perl?
    ... I want to get the Class name of .java file with perl, ... public class ClassA { ... class ClassB { ...
    (perl.beginners)
  • Analize Java source file with perl?
    ... I want to get the Class name of .java file with perl, ... public class ClassA { ... class ClassB { ...
    (perl.beginners)
  • Re: Analize Java source file with perl?
    ... I want to get the Class name of .java file with perl, ... >ClassA ... >ClassB ...
    (perl.beginners)
  • Re: Commenting Java
    ... Unfortunately I'm unfamiliar with Perl. ... Do u think its a good idea to start learning Perl just to add some ... I'd like to add a copyright on top of every java file ...
    (comp.lang.java.help)