首页 技术 正文
技术 2022年11月17日
0 收藏 575 点赞 3,664 浏览 1505 个字

使用反编译的代码作为jar包源码进行调试时,经常会遇到的情况是反编译后的源码之在注释里包含行号,但是与代码所在行经常对应不上。这个时候,就有必要对代码进行对齐了。

 public class Reorg {
public static void main(String[] args) throws IOException {
File file = new File("D:\\OpenSource\\lib\\framework\\frameworkTest");
doReorg(file);
} public static void doReorg(File file) throws IOException {
System.out.println("搜索路径:" + file.getPath());
if (file.isDirectory()) {
File[] javaFiles = file.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
if (name.endsWith(".java"))
return true;
return false;
}
}); for (File javaFile : javaFiles) {
ReorgJavaFile.reorgFile(javaFile);
} File[] paths = file.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
if (pathname.isDirectory())
return true;
return false;
}
}); for (File path : paths) {
doReorg(path);
}
}
} }
 public class ReorgJavaFile {
private static final Pattern p = Pattern.compile("/\\*\\s*(\\d+)\\s*\\*/"); public static void reorgFile(File f) throws IOException {
System.out.println("检查文件:" + f.getPath()); int a = 0;
BufferedReader r = new BufferedReader(new FileReader(f));
StringBuilder sb = new StringBuilder(); String line;
while ((line = r.readLine()) != null) { a++;
Matcher m = p.matcher(line);
if (m.find()) {
int num = Integer.valueOf(m.group(1));
if (num < a) {
System.out.print("\t");
System.out.println(f.getPath());
System.out.print("\t");
System.out.println("In line " + a + ", src file line is " + num);
} else if (num > a) {
while (num != a) {
sb.append("\n");
a++;
}
}
} sb.append(line).append("\n"); } r.close(); BufferedWriter w = new BufferedWriter(new FileWriter(f));
w.write(sb.toString()); w.flush();
w.close();
} }

不过此代码只能对反编译代码进行部分对齐,仍有一些地方需要人工进行对齐。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,000
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,512
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,358
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,141
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,771
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,849