Correction:
Perl script.
#!/usr/bin/perl
$verbose = 0;
$classdir = "";
$getclassdir = 0;
fileloop:
foreach $i (@ARGV) {
if ($i eq "-v"){
$verbose = 1;
next;
}
if ($i eq "-d"){
$getclassdir = 1;
next;
}
if ($getclassdir){
$classdir = $i;
if ($verbose){
print "Look for classes in: $classdir\n";
}
$getclassdir = 0;
next;
}
if ($verbose){
print STDOUT "$i\n";
}
my $imports = "";
my $classfile;
if ($i =~ /^(.*)\.java$/) {
$classfile = $1 . ".class";
if ($classdir ne ""){
print "oldclass: $classfile\n" if ($verbose);
$classfile =~ s/^(.*\/)?([^\/]*\.class)/$classdir\/$2/;
print "newclass: $classfile\n" if ($verbose);
}
} else {
print STDERR "$i is not a java file... skipping\n";
next;
}
print "classfile: $classfile\n" if ($verbose);
if (! -f $classfile){
print STDERR "$i must be compiled first... skipping\n";
next;
}
open(JAD, "jad -p -pi999 $classfile 2>&1 |") || die "Can't run jad:
$!\n";
my $fucked = 0;
while (<JAD>){
if (/^import /){
$imports .= $_;
}
if (/JavaClassFileParseException/){
$fucked = 1;
print STDERR $_;
last;
}
}
close JAD || die "Error running jad: see jad error above: $!\n";
if ($fucked){
print STDERR "jad error... skipping\n";
next;
}
unlink("$i.prejiffy");
rename($i, "$i.prejiffy") || die "Can't rename original file $i:
$!\n";
open(FILEOUT, ">$i") || die "Can't open $i for writing: $!\n";
open(FILEIN, "<$i.prejiffy")
|| die "Can't open $i.prejiffy for reading: $!\n";
while (<FILEIN>){
if (/^import /){
print FILEOUT $imports;
$imports = "";
} else {
print FILEOUT;
}
}
close FILEOUT;
close FILEIN;
}