一、Eclipse安装反编译插件

在没有源码和反编译工具时,打开.class文件,显示如下:

1、安装步骤

  • 解压(jd-eclipse-site-1.0.0-RC2)

  • 点击Help > Install New Software...

点击Add...,选择解压后的文件,输入Name:JD-Eclipse Update Site:

  • 勾选Java Decompiler Eclipse Plug-in

  • 点击Next,next…,Finish,重启Eclipse

2、效果

再次打开.class文件,即可看到反编译后的代码了:

二、断点调试行号不同步问题

1、JD-GUI设置

可以通过Help > Preferences来设置在保存源码时是否显示行号和元信息。

  • 源文件
package com;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HelloWorld {

	/**
	 * IF you can keep your head when all about you Are losing theirs and blaming it on you;
	 * 
	 * IF you can trust yourself when all men doubt you, But make allowance for their doubting too;
	 * 
	 * If you can wait and not be tired by waiting, Or, being lied about, don't deal in lies,
	 * 
	 * Or, being hated, don't give way to hating, And yet don't look too good, nor talk too wise;
	 * 
	 * @param input
	 */
	public void test(String input){
		
		Pattern pattern = Pattern.compile("\\d{4}-(\\d{2})-\\d{2}");
		Matcher matcher = pattern.matcher(input);
		
		//If you can talk with crowds and keep your virtue
		//Or walk with kings - nor lose the common touch
		//If neither foes nor loving friends can hurt you
		//If all men count with you, but none too much
		//If you can fill the unforgiving minute With sixty seconds' worth of distance run
		//Yours is the Earth and everything that's in it
		//And - which is more - you'll be a Man my son!
		
		System.out.println("Input: " + input);
		while(matcher.find()){
			System.out.println("Month: " + matcher.group(1));
		}
		
	}
}

将此类导出为:hello-world.jar

  • 显示行号和元信息

反编译的源码:

/*    */ package com;
/*    */ 
/*    */ import java.io.PrintStream;
/*    */ import java.util.regex.Matcher;
/*    */ import java.util.regex.Pattern;
/*    */ 
/*    */ public class HelloWorld
/*    */ {
/*    */   public void test(String input)
/*    */   {
/* 21 */     Pattern pattern = Pattern.compile("\\d{4}-(\\d{2})-\\d{2}");
/* 22 */     Matcher matcher = pattern.matcher(input);
/*    */ 
/* 32 */     System.out.println("Input: " + input);
/* 33 */     while (matcher.find())
/* 34 */       System.out.println("Month: " + matcher.group(1));
/*    */   }
/*    */ }

/* Location:           G:\Temp\hello-world.jar
 * Qualified Name:     com.HelloWorld
 * JD-Core Version:    0.6.2
 */
  • 不显示行号和元信息

反编译的源码:

package com;

import java.io.PrintStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HelloWorld
{
  public void test(String input)
  {
    Pattern pattern = Pattern.compile("\\d{4}-(\\d{2})-\\d{2}");
    Matcher matcher = pattern.matcher(input);

    System.out.println("Input: " + input);
    while (matcher.find())
      System.out.println("Month: " + matcher.group(1));
  }
}

2、行号问题

最近在使用.jar反编译的.java文件远程调试服务器代码时遇到实际执行代码和显示当前调试代码不一致的问题:

为此,写了一段程序来将行数处理成一致(依赖commons-io-2.6.jar):

public static void dispose(String path, boolean lineNumbers){
	File file = new File(path);
	try {
		final String encoding = "UTF-8";
		List<String> lines = FileUtils.readLines(file, encoding);
		final String regex = "^/\\*\\s*(\\d+)\\s*\\*/.+";
		final String lnRegex = "/\\*\\s*(\\d+)?\\s*\\*/";
		int number = 1;
		StringBuilder sb = new StringBuilder();
		for(String line : lines){
			if(line.matches(regex)){
				int num = Integer.parseInt(line.replaceFirst(regex, "$1"));
				while(number < num){
					//补空行
					sb.append("\r\n");
					number++;
				}
			}
			sb.append(lineNumbers ? line : line.replaceFirst(lnRegex, "")).append("\r\n");
			number++;
		}
		//重新写入文件
		FileUtils.write(file, sb, encoding);
	} catch (IOException e) {
		e.printStackTrace();
	}
}

其中参数lineNumbers表示是否保留行号。

测试代码:

public static void main(String[] args) {
	dispose("G:/Temp/HelloWorld.java", true);
}

源文件与处理后的文件对比:

参考资料: