summaryrefslogtreecommitdiff
path: root/scripts
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2008-05-25 01:20:40 +0000
committerPaul Buetow <paul@buetow.org>2008-05-25 01:20:40 +0000
commit56126ce3bd8d84c005f16212d23327e98e6976aa (patch)
treedd056dca92800554964075eaa5c3b6f371da70c1 /scripts
parentbc4135bf1e842c2c4b0f62bbe7b2ce5434ab4e3f (diff)
Diffstat (limited to 'scripts')
-rw-r--r--scripts/sortmethods.pl117
1 files changed, 117 insertions, 0 deletions
diff --git a/scripts/sortmethods.pl b/scripts/sortmethods.pl
new file mode 100644
index 0000000..29800bd
--- /dev/null
+++ b/scripts/sortmethods.pl
@@ -0,0 +1,117 @@
+#!/usr/bin/perl
+
+# Automatically sorts the functions of a java class alphanumerical
+# (C) 2008 by Paul C. Buetow
+
+use strict;
+use warnings;
+
+use File::Find;
+
+my @files;
+
+sub usage () {
+ return "Usage: perl $0 <sourcedir>\n";
+}
+
+sub process (@) {
+ my ($file, @input) = @_;
+
+ my $package;
+ my @imports;
+ my $classOrInterface;
+ my @variables;
+ my @constructors;
+ my @methods;
+
+ my $isMethod = 0;
+ my $isInnerClass = 0;
+ my $methodparant = 0;
+
+ for (@input) {
+ if (/^package/) {
+ $package = $_;
+
+ } elsif (/^import/) {
+ push @imports, $_;
+
+ } elsif (/^[^ ].*class/) {
+ $classOrInterface = $_;
+
+ } elsif (/^[^ ].*interface/) {
+ $classOrInterface = $_;
+
+ } elsif (!$isMethod && /;/ && !/{/) {
+ push @variables, $_;
+
+ } elsif (!$isMethod && !/;/ && /{/) {
+ $methodparant = 0;
+ ++$methodparant while /{/g;
+ --$methodparant while /}/g;
+ my ($name) = /(\w*?\(.*) {/i;
+ next unless defined $name;
+ $isMethod = 1;
+ my %method = (
+ name => $name,
+ prototype => $_,
+ code => [],
+ );
+ push @methods, \%method;
+
+ } elsif ($isMethod) {
+ ++$methodparant while /{/g;
+ --$methodparant while /}/g;
+
+ $isMethod = 0 if $methodparant == 0;
+ push @{$methods[-1]->{code}}, $_;
+ }
+ }
+
+ die "undef package in $file\n" unless defined $package;
+ die "undef classOrInterface in $file\n" unless defined $classOrInterface;
+
+ my @output = ();
+
+ push @output, $package;
+ push @output, "\n";
+
+ if (@imports) {
+ push @output, sort @imports;
+ push @output, "\n";
+ }
+
+ push @output, $classOrInterface;
+
+ if (@variables) {
+ push @output, sort @variables;
+ push @output, "\n";
+ }
+
+ if (@methods) {
+ push @output,
+ map { "@" .$_->{name} . "=>". $_->{prototype}, @{$_->{code}}, "\n" }
+ sort { $a->{name} cmp $b->{name} } @methods;
+ }
+
+ push @output, "}\n";
+
+ return @output;
+}
+
+my $startDir = shift || die usage();
+find(sub { push @files, $File::Find::name if /\.java$/ }, $startDir);
+
+for (@files) {
+ open my $file, $_ or die "$!: $_\n";
+ my @input = <$file>;
+ close $file;
+
+ my @output = process($_, @input);
+ print @output;
+
+ print "=================== END $_\n";
+}
+
+
+=cut
+