java 反编译 行号对齐 decompiler如何去掉行号

有没有感觉你用java反编译工具jd-gui.exe后,得到源码是行号对不上的,如下面,这样的源码在debug起来很不方便,根本对不上,没有调试的价值
/*     */ import org.dom4j.io.SAXReader;
/*     */ 
/*     */ public class ForHttpXMLAction extends HostAccessAction
/*     */ {
/*  75 */   private String sendHeadFormatName = null;
/*     */ 
/*  80 */   private String sendFormatName = "httpSendFormat";
/*     */ 
/*  85 */   private String receiveFormatName = "httpReceiveFormat";
/*     */ 
/*  90 */   private String receiveHeadFormatName = null;
/*     */ 
/*  95 */   private int timeOut = 0;
/*     */ 
/* 100 */   private String serviceName = null;
/*     */ 
/* 105 */   private String urlEncoding = "ISO8859-1";
/*     */ 
/* 110 */   private String encoding = "GBK";
/*     */ 
/* 115 */   private String inputFeild = null;
/*     */ 
/* 120 */   private String outputFeild = null;
/*     */ 
/* 125 */   private String chanlTradNo = null;
/*     */ 

但是幸好的是,上面的行号的注释,比如这样“/* 115* /”

为了方便用eclipse的调试debug,我写了一个程序,来格式式行号,使上面的行号按照注释的对齐,这样调试起来特别方便


你只需要把jd-gui.exe反编译生成的源码目录传到main方法即可,下面代码就会生成一个与源代码平级同名+bak的文件,话不多说,直接看代码


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.core.helpers.FileUtils;

public class FormatSrc {
	public static void main(String[] args) throws Exception {
		
		
		doFo("H:/work/src/7sdfasdf");

	}

	private static String common = null;

	public static void doFo(String path) {
		List javaFile = getJavaFile(path);
		for (File file : javaFile) {
			doCreateFile(file, path);
		}
	}

	public static void doCreateFile(File file, String distDir) {
		String absolutePath = file.getAbsolutePath();
		Map map = doFill(format(absolutePath));
		List keyList = new ArrayList(map.keySet());
		Collections.sort(keyList);
		// (file.getPath());
		// file.deleteOnExit();
		String p1 = absolutePath.substring(0, distDir.substring(0, distDir.lastIndexOf("/")).length());
		String p2 = absolutePath.substring(distDir.length() , absolutePath.length());
		if (common == null) {
			common = distDir.subSequence(distDir.lastIndexOf("/"), distDir.length()) + "-bak";
		}
		String path = p1 + "/" + common + "/" + p2;

		File distFile = new File(path);
		
		try {
			org.apache.commons.io.FileUtils.forceDeleteOnExit(distFile.getParentFile());
			if (!distFile.exists()) {
				distFile.getParentFile().mkdirs();
			}
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} finally {

		}
		Writer out = null;
		BufferedWriter bw = null;
		try {
			out = new FileWriter(distFile);
			bw = new BufferedWriter(out);
			for (Integer key : keyList) {
				bw.write(key + map.get(key));
				bw.newLine();
				bw.flush();
			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (bw != null) {
				try {
					bw.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} finally {
					try {
						out.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}

	}

	public static Integer getLastKey(Set keySet) {
		if (keySet == null || keySet.size() == 0) {
			return 0;
		}
		List list = new ArrayList(keySet);
		Collections.sort(list);
		Collections.reverse(list);
		return list.get(0);

	}

	public static List getJavaFile(String dir) {
		List javaFiles = new ArrayList();
		File file = new File(dir);
		if (file.exists()) {
			File[] list = file.listFiles();
			if (list != null && list.length > 0) {
				for (File f : list) {
					if (f.isFile() && f.getName().lastIndexOf(".java") != -1) {
						javaFiles.add(f);
					} else {
						List javaFile = getJavaFile(f.getAbsolutePath());
						if (javaFile != null) {
							javaFiles.addAll(javaFile);
						}
					}

				}
			}

		}
		return javaFiles;
	}

	public static Map doFill(Map map) {
		Map _map = new Hashtable();
		Set keySet = map.keySet();
		int up;
		int distance;
		for (Integer i : keySet) {
			if (i > 1) {
				up = getLastKey(_map.keySet());
				if (i - up > 1) {
					for (int j = up + 1; j <= i - 1; j++) {
						_map.put(j, "#");
					}
				} else {
					_map.put(i, map.get(i));
				}

			} else {
				_map.put(i, map.get(i));

			}
		}
		return _map;

	}

	public static Map format(String path) {
		Map map = new Hashtable();
		File file = new File(path);
		Reader in = null;
		BufferedReader br = null;
		String oneLine = null;
		String lineNum = null;
		String _lineNum = null;
		String lineCode = null;
		boolean b = false;
		boolean a = false;
		try {
			in = new FileReader(file);
			br = new BufferedReader(in);

			// /* 19 */ private static Map cacheMap = new HashMap();
			int i = 1;
			
			while ((oneLine = br.readLine()) != null && oneLine != "") {
				if (StringUtils.isNotBlank(oneLine)) {
					if (oneLine.lastIndexOf("*/") == -1) {
						map.put(i, oneLine);
						i++;
						b = true;
						if (oneLine.indexOf("Location:") != -1) {
							break;
						}
					} else {
						
						if (oneLine.indexOf("Location:") != -1) {
							break;
						}

						_lineNum = oneLine.split("\\*/")[0];

						lineNum = _lineNum.substring(2, _lineNum.length() - 1);
						lineCode = oneLine.substring(_lineNum.length() + 2, oneLine.length());
						lineCode = lineCode == "" ? "#" : lineCode;
						if (StringUtils.isBlank(lineNum)) {
							Integer lastKey = getLastKey(map.keySet());
							lastKey = lastKey == null ? 0 : lastKey;
							map.put(lastKey + 1, lineCode);
						} else {
							map.put(Integer.valueOf(StringUtils.trim(lineNum)), lineCode);
						}
					}

					// (oneLine + "   " + lineNum + "   " + lineCode);

				} else {
					// ("---->" + oneLine + "<--------" +
					// lineNum);
					if (oneLine.indexOf("Location:") != -1) {
						b = true;
						a = true;
					}
					if (b && a) {
						break;
					} else {
						map.put(i, oneLine);
						i++;
					}
				}
			}
			if (oneLine == "" && a) {
				map.put(i, oneLine);
				i++;
			}
			// i = 1;
		} catch (Exception e) {
			System.out.println("----->" + oneLine + "<--------");
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} finally {
					if (in != null) {
						try {
							in.close();
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
			}
		}
		return map;
	}

}





你可能感兴趣的:(java)