summaryrefslogtreecommitdiff
path: root/scripts/sortmethods.pl
blob: 3ee19b1533084d96789f48ad377e1bd4a55cb7ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/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;
use re 'eval';

my @files;

sub usage () {
	return "Usage: perl $0 <sourcedir>\n";
}

sub process ($$) {
	my %class; 
	my $indent;

	$_[1] =~ 
		m<
			(?{ print "Start parsing $_[0]\n" })
			(package .*?;\n) (?{ 
					print "Found package: $1";
					$class{package} = $1;
				})
			(?:
			 	.*?
				(import .*?;\n) (?{ 
						print "Found import: $2";
						push @{$class{imports}}, $2;
					})
			)+
			.*?
			((?:(?:class)|(?:interface)) .*?) {\n+ (?{
					print "Sorting imports\n";
					@{$class{imports}} = sort @{$class{imports}};
					print "Found class prototype: $3"; 
					$class{prototype} = $3;
			})
			(?=
			 	(\s+) (?{ 
					$indent = length $4;
					print "Class indent is " . $indent . "\n";
				})
			)
			(?:
			\n?(??{'\s' x $indent})
			 (.+?{.*? \n(??{'\s' x $indent}) }) (?{
				 	print "Found [[$5]]";
				 })
			)*
		>xs;
	();
}

find(sub { push @files, $File::Find::name if /\.java$/ }, shift || die usage);

for (@files) {
	open my $file, $_ or die "$!: $_\n";
	print process $_, join '', <$file>;
	close $file;
}