我们知道,在反编译出来的java源代码中会看到很多由编译器自动生成的默认构造函数的代码,我们需要删掉这部分代码。
还是用Perl脚本来实现这个目标。
直接上Perl源代码:
使用方法:
将下面源代码复制到一个文本文件中,后缀改为.pl,即perl脚本。
如:removeThis.pl
需要处理的源代码目录路径作为参数运行。
即在命令行: perl removeThis.pl [PATH_TO_SOURCE]
#!/usr/bin/perl #功能: # 删除掉.java源代码中的编译器自动生成的 this$0等代码 如: # final AdvancedMenu this$0; # # # { # this$0 = AdvancedMenu.this; # super(); # } #第一个参数为:需要处理的源代码目录路径 use strict; use File::Find; my $init_folder = $ARGV[0]; if (not -d $init_folder) { print "目录:$init_folder不存在.\n"; print "$0 <source_folder>\n"; exit; } find(\&CallBackMe, $init_folder ); print "Well Done.\n"; sub CallBackMe { my $fullpath = $File::Find::name; if(-d $fullpath) { print "[Dir] $fullpath\n"; } else { #print "[File] $fullpath\n"; if($fullpath =~ /\.java$/i) { print "remove this\$0 in $fullpath\n"; RemoveThisCode($fullpath); } } } my $skip_line1="^\/\/\sDecompiled\sby\sJad"; my $skip_line2="^\/\/\sJad\shome\spage"; my $skip_line3="^\/\/\sDecompiler\soptions"; my $skip_line4="^\/\/\sReferenced\sclasses\sof\spackage"; my $skip_line5="^\/\/\s+GoProResponseHandler"; sub RemoveThisCode { my $filename = shift @_; open FILE, "< $filename" or die "Can’t open ‘$filename’: $!"; my @outlines; my $line; while (<FILE>) { $line = $_; if ($line =~ /^\s+final\s\w+\sthis\$0;/) { my @templines; #print $line; #start to skip lines my $publicFound=0; my $count = 0; while (<FILE>) { $line = $_; #print $line; if($line =~ /\$/) { print "this\$0 skipped\n"; } else { push @templines,$line; } if($line =~ /^\s+public/) { $publicFound=1; print "default public constructor found\n"; } if($line =~ /^\s+{\n$/) { $count++; #print "count:" . $count . "\n"; } if($line =~ /^\s+}\n$/) { $count--; #print "count:" . $count . "\n"; if($count==0) { last; #exit loop } } } if($publicFound==1) { push @outlines,@templines; } } else { if($line =~ /^\/\/\sDecompiled\sby\sJad/) { #print "skip:$line\n"; } elsif ($line =~ /^\/\/\sJad\shome\spage/) { #print "skip:$line\n"; } elsif ($line =~ /^\/\/\sDecompiler\soptions/) { #print "skip:$line\n"; } else { push @outlines,$line; } } } close FILE; open my $out , "> $filename" or die "couldn't write $filename"; #foreach my $l (@lines) #{ # print $out $l; #} print $out @outlines; close $out; }