前言

Java 反编译,一听可能觉得高深莫测,其实反编译并不是什么特别高级的操作,Java 对于 Class 字节码文件的生成有着严格的要求,如果你非常熟悉 Java 虚拟机规范,了解 Class 字节码文件中一些字节的作用,那么理解反编译的原理并不是什么问题。

甚至像下面这样的 Class 文件你都能看懂一二。

一般在逆向研究和代码分析中,反编译用到的比较多。不过在日常开发中,有时候只是简单的看一下所用依赖类的反编译,也是十分重要的。

恰好最近工作中也需要用到 Java 反编译,所以这篇文章介绍目前常见的的几种 Java 反编译工具的使用,在文章的最后也会通过编译速度语法支持以及代码可读性三个维度,对它们进行测试,分析几款工具的优缺点

Procyon

Github 链接:https://github.com/mstrobel/procyon

Procyon 不仅仅是反编译工具,它其实是专注于 Java 代码的生成和分析的一整套的 Java 元编程工具。

主要包括下面几个部分:

  • Core Framework
  • Reflection Framework
  • Expressions Framework
  • Compiler Toolset (Experimental)
  • Java Decompiler (Experimental)

可以看到反编译只是 Procyon 的其中一个模块,Procyon 原来托管于 bitbucket,后来迁移到了 GitHub,根据 GitHub 的提交记录来看,也有将近两年没有更新了。不过也有依赖 Procyon 的其他的开源反编译工具如** decompiler-procyon**,更新频率还是很高的,下面也会选择这个工具进行反编译测试。

使用 Procyon

  1. <!-- https://mvnrepository.com/artifact/org.jboss.windup.decompiler/decompiler-procyon -->
  2. <dependency>
  3. <groupId>org.jboss.windup.decompiler</groupId>
  4. <artifactId>decompiler-procyon</artifactId>
  5. <version>5.1.4.Final</version>
  6. </dependency>

写一个简单的反编译测试。

  1. package com.wdbyte.decompiler;
  2. import java.io.IOException;
  3. import java.nio.file.Path;
  4. import java.nio.file.Paths;
  5. import java.util.Iterator;
  6. import java.util.List;
  7. import org.jboss.windup.decompiler.api.DecompilationFailure;
  8. import org.jboss.windup.decompiler.api.DecompilationListener;
  9. import org.jboss.windup.decompiler.api.DecompilationResult;
  10. import org.jboss.windup.decompiler.api.Decompiler;
  11. import org.jboss.windup.decompiler.procyon.ProcyonDecompiler;
  12. /**
  13. * Procyon 反编译测试
  14. *
  15. * @author https://github.com/niumoo
  16. * @date 2021/05/15
  17. */
  18. public class ProcyonTest {
  19. public static void main(String[] args) throws IOException {
  20. Long time = procyon("decompiler.jar", "procyon_output_jar");
  21. System.out.println(String.format("decompiler time: %dms", time));
  22. }
  23. public static Long procyon(String source,String targetPath) throws IOException {
  24. long start = System.currentTimeMillis();
  25. Path outDir = Paths.get(targetPath);
  26. Path archive = Paths.get(source);
  27. Decompiler dec = new ProcyonDecompiler();
  28. DecompilationResult res = dec.decompileArchive(archive, outDir, new DecompilationListener() {
  29. public void decompilationProcessComplete() {
  30. System.out.println("decompilationProcessComplete");
  31. }
  32. public void decompilationFailed(List<String> inputPath, String message) {
  33. System.out.println("decompilationFailed");
  34. }
  35. public void fileDecompiled(List<String> inputPath, String outputPath) {
  36. }
  37. public boolean isCancelled() {
  38. return false;
  39. }
  40. });
  41. if (!res.getFailures().isEmpty()) {
  42. StringBuilder sb = new StringBuilder();
  43. sb.append("Failed decompilation of " + res.getFailures().size() + " classes: ");
  44. Iterator failureIterator = res.getFailures().iterator();
  45. while (failureIterator.hasNext()) {
  46. DecompilationFailure dex = (DecompilationFailure)failureIterator.next();
  47. sb.append(System.lineSeparator() + " ").append(dex.getMessage());
  48. }
  49. System.out.println(sb.toString());
  50. }
  51. System.out.println("Compilation results: " + res.getDecompiledFiles().size() + " succeeded, " + res.getFailures().size() + " failed.");
  52. dec.close();
  53. Long end = System.currentTimeMillis();
  54. return end - start;
  55. }
  56. }

Procyon 在反编译时会实时输出反编译文件数量的进度情况,最后还会统计反编译成功和失败的 Class 文件数量。

  1. ....
  2. 五月 15, 2021 10:58:28 下午 org.jboss.windup.decompiler.procyon.ProcyonDecompiler$3 call
  3. 信息: Decompiling 650 / 783
  4. 五月 15, 2021 10:58:30 下午 org.jboss.windup.decompiler.procyon.ProcyonDecompiler$3 call
  5. 信息: Decompiling 700 / 783
  6. 五月 15, 2021 10:58:37 下午 org.jboss.windup.decompiler.procyon.ProcyonDecompiler$3 call
  7. 信息: Decompiling 750 / 783
  8. decompilationProcessComplete
  9. Compilation results: 783 succeeded, 0 failed.
  10. decompiler time: 40599ms

Procyon GUI

对于 Procyon 反编译来说,在 GitHub 上也有基于此实现的开源 GUI 界面,感兴趣的可以下载尝试。

Github 地址:https://github.com/deathmarine/Luyten

CFR

GitHub 地址:https://github.com/leibnitz27/cfr

CFR 官方网站:http://www.benf.org/other/cfr/(可能需要FQ)

Maven 仓库:https://mvnrepository.com/artifact/org.benf/cfr

CFR(Class File Reader) 可以支持 Java 9、Java 12、Java 14 以及其他的最新版 Java 代码的反编译工作。而且 CFR 本身的代码是由 Java 6 编写,所以基本可以使用 CFR 在任何版本的 Java 程序中。值得一提的是,使用 CFR 甚至可以将使用其他语言编写的的 JVM 类文件反编译回 Java 文件。

CFR 命令行使用

使用 CFR 反编译时,你可以下载已经发布的 JAR 包,进行命令行反编译,也可以使用 Maven 引入的方式,在代码中使用。下面先说命令行运行的方式。

直接在GitHub Tags 下载已发布的最新版 JAR. 可以直接运行查看帮助。

  1. # 查看帮助
  2. java -jar cfr-0.151.jar --help

如果只是反编译某个 class.

  1. # 反编译 class 文件,结果输出到控制台
  2. java -jar cfr-0.151.jar WindupClasspathTypeLoader.class
  3. # 反编译 class 文件,结果输出到 out 文件夹
  4. java -jar cfr-0.151.jar WindupClasspathTypeLoader.class --outputpath ./out

反编译某个 JAR.

  1. # 反编译 jar 文件,结果输出到 output_jar 文件夹
  2. Desktop java -jar cfr-0.151.jar decompiler.jar --outputdir ./output_jar
  3. Processing decompiler.jar (use silent to silence)
  4. Processing com.strobel.assembler.metadata.ArrayTypeLoader
  5. Processing com.strobel.assembler.metadata.ParameterDefinition
  6. Processing com.strobel.assembler.metadata.MethodHandle
  7. Processing com.strobel.assembler.metadata.signatures.FloatSignature
  8. .....

反编译结果会按照 class 的包路径写入到指定文件夹中。

CFR 代码中使用

添加依赖这里不提。

  1. <!-- https://mvnrepository.com/artifact/org.benf/cfr -->
  2. <dependency>
  3. <groupId>org.benf</groupId>
  4. <artifactId>cfr</artifactId>
  5. <version>0.151</version>
  6. </dependency>

实际上我在官方网站和 GitHub 上都没有看到具体的单元测试示例。不过没有关系,既然能在命令行运行,那么直接在 IDEA 中查看反编译后的 Main 方法入口,看下命令行是怎么执行的,就可以写出自己的单元测试了。

  1. package com.wdbyte.decompiler;
  2. import java.io.IOException;
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import org.benf.cfr.reader.api.CfrDriver;
  7. import org.benf.cfr.reader.util.getopt.OptionsImpl;
  8. /**
  9. * CFR Test
  10. *
  11. * @author https://github.com/niumoo
  12. * @date 2021/05/15
  13. */
  14. public class CFRTest {
  15. public static void main(String[] args) throws IOException {
  16. Long time = cfr("decompiler.jar", "./cfr_output_jar");
  17. System.out.println(String.format("decompiler time: %dms", time));
  18. // decompiler time: 11655ms
  19. }
  20. public static Long cfr(String source, String targetPath) throws IOException {
  21. Long start = System.currentTimeMillis();
  22. // source jar
  23. List<String> files = new ArrayList<>();
  24. files.add(source);
  25. // target dir
  26. HashMap<String, String> outputMap = new HashMap<>();
  27. outputMap.put("outputdir", targetPath);
  28. OptionsImpl options = new OptionsImpl(outputMap);
  29. CfrDriver cfrDriver = new CfrDriver.Builder().withBuiltOptions(options).build();
  30. cfrDriver.analyse(files);
  31. Long end = System.currentTimeMillis();
  32. return (end - start);
  33. }
  34. }

JD-Core

GiHub 地址:https://github.com/java-decompiler/jd-core

JD-core 官方网址:https://java-decompiler.github.io/

JD-core 是一个的独立的 Java 库,可以用于 Java 的反编译,支持从 Java 1 至 Java 12 的字节码反编译,包括 Lambda 表达式、方式引用、默认方法等。知名的 JD-GUI 和 Eclipse 无缝集成反编译引擎就是 JD-core。

JD-core 提供了一些反编译的核心功能,也提供了单独的 Class 反编译方法,但是如果你想在自己的代码中去直接反编译整个 JAR 包,还是需要一些改造的,如果是代码中有匿名函数,Lambda 等,虽然可以直接反编译,不过也需要额外考虑。

使用 JD-core

  1. <!-- https://mvnrepository.com/artifact/org.jd/jd-core -->
  2. <dependency>
  3. <groupId>org.jd</groupId>
  4. <artifactId>jd-core</artifactId>
  5. <version>1.1.3</version>
  6. </dependency>

为了可以反编译整个 JAR 包,使用的代码我做了一些简单改造,以便于最后一部分的对比测试,但是这个示例中没有考虑内部类,Lambda 等会编译出多个 Class 文件的情况,所以不能直接使用在生产中。

  1. package com.wdbyte.decompiler;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.nio.file.Files;
  6. import java.nio.file.Path;
  7. import java.nio.file.Paths;
  8. import java.util.Enumeration;
  9. import java.util.HashMap;
  10. import java.util.jar.JarFile;
  11. import java.util.zip.ZipEntry;
  12. import java.util.zip.ZipFile;
  13. import org.apache.commons.io.IOUtils;
  14. import org.apache.commons.lang3.StringUtils;
  15. import org.jd.core.v1.ClassFileToJavaSourceDecompiler;
  16. import org.jd.core.v1.api.loader.Loader;
  17. import org.jd.core.v1.api.printer.Printer;
  18. /**
  19. * @author https://github.com/niumoo
  20. * @date 2021/05/15
  21. */
  22. public class JDCoreTest {
  23. public static void main(String[] args) throws Exception {
  24. JDCoreDecompiler jdCoreDecompiler = new JDCoreDecompiler();
  25. Long time = jdCoreDecompiler.decompiler("decompiler.jar","jd_output_jar");
  26. System.out.println(String.format("decompiler time: %dms", time));
  27. }
  28. }
  29. class JDCoreDecompiler{
  30. private ClassFileToJavaSourceDecompiler decompiler = new ClassFileToJavaSourceDecompiler();
  31. // 存放字节码
  32. private HashMap<String,byte[]> classByteMap = new HashMap<>();
  33. /**
  34. * 注意:没有考虑一个 Java 类编译出多个 Class 文件的情况。
  35. *
  36. * @param source
  37. * @param target
  38. * @return
  39. * @throws Exception
  40. */
  41. public Long decompiler(String source,String target) throws Exception {
  42. long start = System.currentTimeMillis();
  43. // 解压
  44. archive(source);
  45. for (String className : classByteMap.keySet()) {
  46. String path = StringUtils.substringBeforeLast(className, "/");
  47. String name = StringUtils.substringAfterLast(className, "/");
  48. if (StringUtils.contains(name, "$")) {
  49. name = StringUtils.substringAfterLast(name, "$");
  50. }
  51. name = StringUtils.replace(name, ".class", ".java");
  52. decompiler.decompile(loader, printer, className);
  53. String context = printer.toString();
  54. Path targetPath = Paths.get(target + "/" + path + "/" + name);
  55. if (!Files.exists(Paths.get(target + "/" + path))) {
  56. Files.createDirectories(Paths.get(target + "/" + path));
  57. }
  58. Files.deleteIfExists(targetPath);
  59. Files.createFile(targetPath);
  60. Files.write(targetPath, context.getBytes());
  61. }
  62. return System.currentTimeMillis() - start;
  63. }
  64. private void archive(String path) throws IOException {
  65. try (ZipFile archive = new JarFile(new File(path))) {
  66. Enumeration<? extends ZipEntry> entries = archive.entries();
  67. while (entries.hasMoreElements()) {
  68. ZipEntry entry = entries.nextElement();
  69. if (!entry.isDirectory()) {
  70. String name = entry.getName();
  71. if (name.endsWith(".class")) {
  72. byte[] bytes = null;
  73. try (InputStream stream = archive.getInputStream(entry)) {
  74. bytes = IOUtils.toByteArray(stream);
  75. }
  76. classByteMap.put(name, bytes);
  77. }
  78. }
  79. }
  80. }
  81. }
  82. private Loader loader = new Loader() {
  83. @Override
  84. public byte[] load(String internalName) {
  85. return classByteMap.get(internalName);
  86. }
  87. @Override
  88. public boolean canLoad(String internalName) {
  89. return classByteMap.containsKey(internalName);
  90. }
  91. };
  92. private Printer printer = new Printer() {
  93. protected static final String TAB = " ";
  94. protected static final String NEWLINE = "\n";
  95. protected int indentationCount = 0;
  96. protected StringBuilder sb = new StringBuilder();
  97. @Override public String toString() {
  98. String toString = sb.toString();
  99. sb = new StringBuilder();
  100. return toString;
  101. }
  102. @Override public void start(int maxLineNumber, int majorVersion, int minorVersion) {}
  103. @Override public void end() {}
  104. @Override public void printText(String text) { sb.append(text); }
  105. @Override public void printNumericConstant(String constant) { sb.append(constant); }
  106. @Override public void printStringConstant(String constant, String ownerInternalName) { sb.append(constant); }
  107. @Override public void printKeyword(String keyword) { sb.append(keyword); }
  108. @Override public void printDeclaration(int type, String internalTypeName, String name, String descriptor) { sb.append(name); }
  109. @Override public void printReference(int type, String internalTypeName, String name, String descriptor, String ownerInternalName) { sb.append(name); }
  110. @Override public void indent() { this.indentationCount++; }
  111. @Override public void unindent() { this.indentationCount--; }
  112. @Override public void startLine(int lineNumber) { for (int i=0; i<indentationCount; i++) sb.append(TAB); }
  113. @Override public void endLine() { sb.append(NEWLINE); }
  114. @Override public void extraLine(int count) { while (count-- > 0) sb.append(NEWLINE); }
  115. @Override public void startMarker(int type) {}
  116. @Override public void endMarker(int type) {}
  117. };
  118. }

JD-GUI

GitHub 地址:https://github.com/java-decompiler/jd-gui

JD-core 也提供了官方的 GUI 界面,需要的也可以直接下载尝试。

Jadx

GitHub 地址:https://github.com/skylot/jadx

Jadx 是一款可以反编译 JAR、APK、DEX、AAR、AAB、ZIP 文件的反编译工具,并且也配有 Jadx-gui 用于界面操作。

Jadx 使用 Grade 进行依赖管理,可以自行克隆仓库打包运行。

  1. git clone https://github.com/skylot/jadx.git
  2. cd jadx
  3. ./gradlew dist
  4. # 查看帮助
  5. ./build/jadx/bin/jadx --help
  6. jadx - dex to java decompiler, version: dev
  7. usage: jadx [options] <input files> (.apk, .dex, .jar, .class, .smali, .zip, .aar, .arsc, .aab)
  8. options:
  9. -d, --output-dir - output directory
  10. -ds, --output-dir-src - output directory for sources
  11. -dr, --output-dir-res - output directory for resources
  12. -r, --no-res - do not decode resources
  13. -s, --no-src - do not decompile source code
  14. --single-class - decompile a single class
  15. --output-format - can be 'java' or 'json', default: java
  16. -e, --export-gradle - save as android gradle project
  17. -j, --threads-count - processing threads count, default: 6
  18. --show-bad-code - show inconsistent code (incorrectly decompiled)
  19. --no-imports - disable use of imports, always write entire package name
  20. --no-debug-info - disable debug info
  21. --add-debug-lines - add comments with debug line numbers if available
  22. --no-inline-anonymous - disable anonymous classes inline
  23. --no-replace-consts - don't replace constant value with matching constant field
  24. --escape-unicode - escape non latin characters in strings (with \u)
  25. --respect-bytecode-access-modifiers - don't change original access modifiers
  26. --deobf - activate deobfuscation
  27. --deobf-min - min length of name, renamed if shorter, default: 3
  28. --deobf-max - max length of name, renamed if longer, default: 64
  29. --deobf-cfg-file - deobfuscation map file, default: same dir and name as input file with '.jobf' extension
  30. --deobf-rewrite-cfg - force to save deobfuscation map
  31. --deobf-use-sourcename - use source file name as class name alias
  32. --deobf-parse-kotlin-metadata - parse kotlin metadata to class and package names
  33. --rename-flags - what to rename, comma-separated, 'case' for system case sensitivity, 'valid' for java identifiers, 'printable' characters, 'none' or 'all' (default)
  34. --fs-case-sensitive - treat filesystem as case sensitive, false by default
  35. --cfg - save methods control flow graph to dot file
  36. --raw-cfg - save methods control flow graph (use raw instructions)
  37. -f, --fallback - make simple dump (using goto instead of 'if', 'for', etc)
  38. -v, --verbose - verbose output (set --log-level to DEBUG)
  39. -q, --quiet - turn off output (set --log-level to QUIET)
  40. --log-level - set log level, values: QUIET, PROGRESS, ERROR, WARN, INFO, DEBUG, default: PROGRESS
  41. --version - print jadx version
  42. -h, --help - print this help
  43. Example:
  44. jadx -d out classes.dex

根据 HELP 信息,如果想要反编译 decompiler.jar 到 out 文件夹。

  1. ./build/jadx/bin/jadx -d ./out ~/Desktop/decompiler.jar
  2. INFO - loading ...
  3. INFO - processing ...
  4. INFO - doneress: 1143 of 1217 (93%)

Fernflower

GitHub 地址:https://github.com/fesh0r/fernflower

Fernflower 和 Jadx 一样使用 Grade 进行依赖管理,可以自行克隆仓库打包运行。

  1. fernflower-master ./gradlew build
  2. BUILD SUCCESSFUL in 32s
  3. 4 actionable tasks: 4 executed
  4. fernflower-master java -jar build/libs/fernflower.jar
  5. Usage: java -jar fernflower.jar [-<option>=<value>]* [<source>]+ <destination>
  6. Example: java -jar fernflower.jar -dgs=true c:\my\source\ c:\my.jar d:\decompiled\
  7. fernflower-master mkdir out
  8. fernflower-master java -jar build/libs/fernflower.jar ~/Desktop/decompiler.jar ./out
  9. INFO: Decompiling class com/strobel/assembler/metadata/ArrayTypeLoader
  10. INFO: ... done
  11. INFO: Decompiling class com/strobel/assembler/metadata/ParameterDefinition
  12. INFO: ... done
  13. INFO: Decompiling class com/strobel/assembler/metadata/MethodHandle
  14. ...
  15. fernflower-master ll out
  16. total 1288
  17. -rw-r--r-- 1 darcy staff 595K 5 16 17:47 decompiler.jar
  18. fernflower-master

Fernflower 在反编译 JAR 包时,默认反编译的结果也是一个 JAR 包。Jad

反编译速度

到这里已经介绍了五款 Java 反编译工具了,那么在日常开发中我们应该使用哪一个呢?又或者在代码分析时我们又该选择哪一个呢?我想这两种情况的不同,使用时的关注点也是不同的。如果是日常使用,读读代码,我想应该是对可读性要求更高些,如果是大量的代码分析工作,那么可能反编译的速度和语法的支持上要求更高些。

为了能有一个简单的参考数据,我使用 JMH 微基准测试工具分别对这五款反编译工具进行了简单的测试,下面是一些测试结果。

测试环境

环境变量 描述
处理器 2.6 GHz 六核Intel Core i7
内存 16 GB 2667 MHz DDR4
Java 版本 JDK 14.0.2
测试方式 JMH 基准测试。
待反编译 JAR 1 procyon-compilertools-0.5.33.jar (1.5 MB)
待反编译 JAR 2 python2java4common-1.0.0-20180706.084921-1.jar (42 MB)

反编译 JAR 1:procyon-compilertools-0.5.33.jar (1.5 MB)

Benchmark Mode Cnt Score Units
cfr avgt 10 6548.642 ± 363.502 ms/op
fernflower avgt 10 12699.147 ± 1081.539 ms/op
jdcore avgt 10 5728.621 ± 310.645 ms/op
procyon avgt 10 26776.125 ± 2651.081 ms/op
jadx avgt 10 7059.354 ± 323.351 ms/op

反编译 JAR 2: python2java4common-1.0.0-20180706.084921-1.jar (42 MB)

JAR 2 这个包是比较大的,是拿很多代码仓库合并到一起的,同时还有很多 Python 转 Java 生成的代码,理论上代码的复杂度会更高。

Benchmark Cnt Score
Cfr 1 413838.826ms
fernflower 1 246819.168ms
jdcore 1 Error
procyon 1 487647.181ms
jadx 1 505600.231ms

语法支持和可读性

如果反编译后的代码需要自己看的话,那么可读性更好的代码更占优势,下面我写了一些代码,主要是 Java 8 及以下的代码语法和一些嵌套的流程控制,看看反编译后的效果如何。

  1. package com.wdbyte.decompiler;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.stream.IntStream;
  5. import org.benf.cfr.reader.util.functors.UnaryFunction;
  6. /**
  7. * @author https://www.wdbyte.com
  8. * @date 2021/05/16
  9. */
  10. public class HardCode <A, B> {
  11. public HardCode(A a, B b) { }
  12. public static void test(int... args) { }
  13. public static void main(String... args) {
  14. test(1, 2, 3, 4, 5, 6);
  15. }
  16. int byteAnd0() {
  17. int b = 1;
  18. int x = 0;
  19. do {
  20. b = (byte)((b ^ x));
  21. } while (b++ < 10);
  22. return b;
  23. }
  24. private void a(Integer i) {
  25. a(i);
  26. b(i);
  27. c(i);
  28. }
  29. private void b(int i) {
  30. a(i);
  31. b(i);
  32. c(i);
  33. }
  34. private void c(double d) {
  35. c(d);
  36. d(d);
  37. }
  38. private void d(Double d) {
  39. c(d);
  40. d(d);
  41. }
  42. private void e(Short s) {
  43. b(s);
  44. c(s);
  45. e(s);
  46. f(s);
  47. }
  48. private void f(short s) {
  49. b(s);
  50. c(s);
  51. e(s);
  52. f(s);
  53. }
  54. void test1(String path) {
  55. try {
  56. int x = 3;
  57. } catch (NullPointerException t) {
  58. System.out.println("File Not found");
  59. if (path == null) { return; }
  60. throw t;
  61. } finally {
  62. System.out.println("Fred");
  63. if (path == null) { throw new IllegalStateException(); }
  64. }
  65. }
  66. private final List<Integer> stuff = new ArrayList<>();{
  67. stuff.add(1);
  68. stuff.add(2);
  69. }
  70. public static int plus(boolean t, int a, int b) {
  71. int c = t ? a : b;
  72. return c;
  73. }
  74. // Lambda
  75. Integer lambdaInvoker(int arg, UnaryFunction<Integer, Integer> fn) {
  76. return fn.invoke(arg);
  77. }
  78. // Lambda
  79. public int testLambda() {
  80. return lambdaInvoker(3, x -> x + 1);
  81. // return 1;
  82. }
  83. // Lambda
  84. public Integer testLambda(List<Integer> stuff, int y, boolean b) {
  85. return stuff.stream().filter(b ? x -> x > y : x -> x < 3).findFirst().orElse(null);
  86. }
  87. // stream
  88. public static <Y extends Integer> void testStream(List<Y> list) {
  89. IntStream s = list.stream()
  90. .filter(x -> {
  91. System.out.println(x);
  92. return x.intValue() / 2 == 0;
  93. })
  94. .map(x -> (Integer)x+2)
  95. .mapToInt(x -> x);
  96. s.toArray();
  97. }
  98. // switch
  99. public void testSwitch1(){
  100. int i = 0;
  101. switch(((Long)(i + 1L)) + "") {
  102. case "1":
  103. System.out.println("one");
  104. }
  105. }
  106. // switch
  107. public void testSwitch2(String string){
  108. switch (string) {
  109. case "apples":
  110. System.out.println("apples");
  111. break;
  112. case "pears":
  113. System.out.println("pears");
  114. break;
  115. }
  116. }
  117. // switch
  118. public static void testSwitch3(int x) {
  119. while (true) {
  120. if (x < 5) {
  121. switch ("test") {
  122. case "okay":
  123. continue;
  124. default:
  125. continue;
  126. }
  127. }
  128. System.out.println("wow x2!");
  129. }
  130. }
  131. }

此处本来贴出了所有工具的反编译结果,但是碍于文章长度和阅读体验,没有放出来,不过我在个人博客的发布上是有完整代码的,个人网站排版比较自由,可以使用 Tab 选项卡的方式展示。如果需要查看可以访问 https://www.wdbyte.com 进行查看。

Procyon

看到 Procyon 的反编译结果,还是比较吃惊的,在正常反编译的情况下,反编译后的代码基本上都是原汁原味。唯一一处反编译后和源码语法上有变化的地方,是一个集合的初始化操作略有不同。

  1. // 源码
  2. public HardCode(A a, B b) { }
  3. private final List<Integer> stuff = new ArrayList<>();{
  4. stuff.add(1);
  5. stuff.add(2);
  6. }
  7. // Procyon 反编译
  8. private final List<Integer> stuff;
  9. public HardCode(final A a, final B b) {
  10. (this.stuff = new ArrayList<Integer>()).add(1);
  11. this.stuff.add(2);
  12. }

而其他部分代码, 比如装箱拆箱,Switch 语法,Lambda 表达式,流式操作以及流程控制等,几乎完全一致,阅读没有障碍。

装箱拆箱操作反编译后完全一致,没有多余的类型转换代码。

  1. // 源码
  2. private void a(Integer i) {
  3. a(i);
  4. b(i);
  5. c(i);
  6. }
  7. private void b(int i) {
  8. a(i);
  9. b(i);
  10. c(i);
  11. }
  12. private void c(double d) {
  13. c(d);
  14. d(d);
  15. }
  16. private void d(Double d) {
  17. c(d);
  18. d(d);
  19. }
  20. private void e(Short s) {
  21. b(s);
  22. c(s);
  23. e(s);
  24. f(s);
  25. }
  26. private void f(short s) {
  27. b(s);
  28. c(s);
  29. e(s);
  30. f(s);
  31. }
  32. // Procyon 反编译
  33. private void a(final Integer i) {
  34. this.a(i);
  35. this.b(i);
  36. this.c(i);
  37. }
  38. private void b(final int i) {
  39. this.a(i);
  40. this.b(i);
  41. this.c(i);
  42. }
  43. private void c(final double d) {
  44. this.c(d);
  45. this.d(d);
  46. }
  47. private void d(final Double d) {
  48. this.c(d);
  49. this.d(d);
  50. }
  51. private void e(final Short s) {
  52. this.b(s);
  53. this.c(s);
  54. this.e(s);
  55. this.f(s);
  56. }
  57. private void f(final short s) {
  58. this.b(s);
  59. this.c(s);
  60. this.e(s);
  61. this.f(s);
  62. }

Switch 部分也是一致,流程控制部分也没有变化。

  1. // 源码 switch
  2. public void testSwitch1(){
  3. int i = 0;
  4. switch(((Long)(i + 1L)) + "") {
  5. case "1":
  6. System.out.println("one");
  7. }
  8. }
  9. public void testSwitch2(String string){
  10. switch (string) {
  11. case "apples":
  12. System.out.println("apples");
  13. break;
  14. case "pears":
  15. System.out.println("pears");
  16. break;
  17. }
  18. }
  19. public static void testSwitch3(int x) {
  20. while (true) {
  21. if (x < 5) {
  22. switch ("test") {
  23. case "okay":
  24. continue;
  25. default:
  26. continue;
  27. }
  28. }
  29. System.out.println("wow x2!");
  30. }
  31. }
  32. // Procyon 反编译
  33. public void testSwitch1() {
  34. final int i = 0;
  35. final String string = (Object)(i + 1L) + "";
  36. switch (string) {
  37. case "1": {
  38. System.out.println("one");
  39. break;
  40. }
  41. }
  42. }
  43. public void testSwitch2(final String string) {
  44. switch (string) {
  45. case "apples": {
  46. System.out.println("apples");
  47. break;
  48. }
  49. case "pears": {
  50. System.out.println("pears");
  51. break;
  52. }
  53. }
  54. }
  55. public static void testSwitch3(final int x) {
  56. while (true) {
  57. if (x < 5) {
  58. final String s = "test";
  59. switch (s) {
  60. case "okay": {
  61. continue;
  62. }
  63. default: {
  64. continue;
  65. }
  66. }
  67. }
  68. else {
  69. System.out.println("wow x2!");
  70. }
  71. }
  72. }

Lambda 表达式和流式操作完全一致。

  1. // 源码
  2. // Lambda
  3. public Integer testLambda(List<Integer> stuff, int y, boolean b) {
  4. return stuff.stream().filter(b ? x -> x > y : x -> x < 3).findFirst().orElse(null);
  5. }
  6. // stream
  7. public static <Y extends Integer> void testStream(List<Y> list) {
  8. IntStream s = list.stream()
  9. .filter(x -> {
  10. System.out.println(x);
  11. return x.intValue() / 2 == 0;
  12. })
  13. .map(x -> (Integer)x+2)
  14. .mapToInt(x -> x);
  15. s.toArray();
  16. }
  17. // Procyon 反编译
  18. public Integer testLambda(final List<Integer> stuff, final int y, final boolean b) {
  19. return stuff.stream().filter(b ? (x -> x > y) : (x -> x < 3)).findFirst().orElse(null);
  20. }
  21. public static <Y extends Integer> void testStream(final List<Y> list) {
  22. final IntStream s = list.stream().filter(x -> {
  23. System.out.println(x);
  24. return x / 2 == 0;
  25. }).map(x -> x + 2).mapToInt(x -> x);
  26. s.toArray();
  27. }

流程控制,反编译后发现丢失了无意义的代码部分,阅读来说并无障碍。

  1. // 源码
  2. void test1(String path) {
  3. try {
  4. int x = 3;
  5. } catch (NullPointerException t) {
  6. System.out.println("File Not found");
  7. if (path == null) { return; }
  8. throw t;
  9. } finally {
  10. System.out.println("Fred");
  11. if (path == null) { throw new IllegalStateException(); }
  12. }
  13. }
  14. // Procyon 反编译
  15. void test1(final String path) {
  16. try {}
  17. catch (NullPointerException t) {
  18. System.out.println("File Not found");
  19. if (path == null) {
  20. return;
  21. }
  22. throw t;
  23. }
  24. finally {
  25. System.out.println("Fred");
  26. if (path == null) {
  27. throw new IllegalStateException();
  28. }
  29. }
  30. }

鉴于代码篇幅,下面几种的反编译结果的对比只会列出不同之处,相同之处会直接跳过。

CFR

CFR 的反编译结果多出了类型转换部分,个人来看没有 Procyon 那么原汁原味,不过也算是十分优秀,测试案例中唯一不满意的地方是对 while continue 的处理。

  1. // CFR 反编译结果
  2. // 装箱拆箱
  3. private void e(Short s) {
  4. this.b(s.shortValue()); // 装箱拆箱多出了类型转换部分。
  5. this.c(s.shortValue()); // 装箱拆箱多出了类型转换部分。
  6. this.e(s);
  7. this.f(s);
  8. }
  9. // 流程控制
  10. void test1(String path) {
  11. try {
  12. int n = 3;// 流程控制反编译结果十分满意,原汁原味,甚至此处的无意思代码都保留了。
  13. }
  14. catch (NullPointerException t) {
  15. System.out.println("File Not found");
  16. if (path == null) {
  17. return;
  18. }
  19. throw t;
  20. }
  21. finally {
  22. System.out.println("Fred");
  23. if (path == null) {
  24. throw new IllegalStateException();
  25. }
  26. }
  27. }
  28. // Lambda 和 Stream 操作完全一致,不提。
  29. // switch 处,反编译后功能一致,但是流程控制有所更改。
  30. public static void testSwitch3(int x) {
  31. block6: while (true) { // 源码中只有 while(true),反编译后多了 block6
  32. if (x < 5) {
  33. switch ("test") {
  34. case "okay": {
  35. continue block6; // 多了 block6
  36. }
  37. }
  38. continue;
  39. }
  40. System.out.println("wow x2!");
  41. }
  42. }

JD-Core

JD-Core 和 CFR 一样,对于装箱拆箱操作,反编译后不再一致,多了类型转换部分,而且自动优化了数据类型。个人感觉,如果是反编译后自己阅读,通篇的数据类型的转换优化影响还是挺大的。

  1. // JD-Core 反编译
  2. private void d(Double d) {
  3. c(d.doubleValue()); // 新增了数据类型转换
  4. d(d);
  5. }
  6. private void e(Short s) {
  7. b(s.shortValue()); // 新增了数据类型转换
  8. c(s.shortValue()); // 新增了数据类型转换
  9. e(s);
  10. f(s.shortValue()); // 新增了数据类型转换
  11. }
  12. private void f(short s) {
  13. b(s);
  14. c(s);
  15. e(Short.valueOf(s)); // 新增了数据类型转换
  16. f(s);
  17. }
  18. // Stream 操作中,也自动优化了数据类型转换,阅读起来比较累。
  19. public static <Y extends Integer> void testStream(List<Y> list) {
  20. IntStream s = list.stream().filter(x -> {
  21. System.out.println(x);
  22. return (x.intValue() / 2 == 0);
  23. }).map(x -> Integer.valueOf(x.intValue() + 2)).mapToInt(x -> x.intValue());
  24. s.toArray();
  25. }

Jadx

首先 Jadx 在反编译测试代码时,报出了错误,反编译的结果里也有提示不能反编 Lambda 和 Stream 操作,反编译结果中变量名称杂乱无章流程控制几乎阵亡,如果你想反编译后生物肉眼阅读,Jadx 肯定不是一个好选择。

  1. // Jadx 反编译
  2. private void e(Short s) {
  3. b(s.shortValue());// 新增了数据类型转换
  4. c((double) s.shortValue());// 新增了数据类型转换
  5. e(s);
  6. f(s.shortValue());// 新增了数据类型转换
  7. }
  8. private void f(short s) {
  9. b(s);
  10. c((double) s);// 新增了数据类型转换
  11. e(Short.valueOf(s));// 新增了数据类型转换
  12. f(s);
  13. }
  14. public int testLambda() { // testLambda 反编译失败
  15. /*
  16. r2 = this;
  17. r0 = 3
  18. r1 = move-result
  19. java.lang.Integer r0 = r2.lambdaInvoker(r0, r1)
  20. int r0 = r0.intValue()
  21. return r0
  22. */
  23. throw new UnsupportedOperationException("Method not decompiled: com.wdbyte.decompiler.HardCode.testLambda():int");
  24. }
  25. // Stream 反编译失败
  26. public static <Y extends java.lang.Integer> void testStream(java.util.List<Y> r3) {
  27. /*
  28. java.util.stream.Stream r1 = r3.stream()
  29. r2 = move-result
  30. java.util.stream.Stream r1 = r1.filter(r2)
  31. r2 = move-result
  32. java.util.stream.Stream r1 = r1.map(r2)
  33. r2 = move-result
  34. java.util.stream.IntStream r0 = r1.mapToInt(r2)
  35. r0.toArray()
  36. return
  37. */
  38. throw new UnsupportedOperationException("Method not decompiled: com.wdbyte.decompiler.HardCode.testStream(java.util.List):void");
  39. }
  40. public void testSwitch2(String string) { // switch 操作无法正常阅读,和源码出入较大。
  41. char c = 65535;
  42. switch (string.hashCode()) {
  43. case -1411061671:
  44. if (string.equals("apples")) {
  45. c = 0;
  46. break;
  47. }
  48. break;
  49. case 106540109:
  50. if (string.equals("pears")) {
  51. c = 1;
  52. break;
  53. }
  54. break;
  55. }
  56. switch (c) {
  57. case 0:
  58. System.out.println("apples");
  59. return;
  60. case 1:
  61. System.out.println("pears");
  62. return;
  63. default:
  64. return;
  65. }
  66. }

Fernflower

Fernflower 的反编译结果总体上还是不错的,不过也有不足,它对变量名称的指定,以及 Switch 字符串时的反编译结果不够理想。

  1. //反编译后变量命名不利于阅读,有很多 var 变量
  2. int byteAnd0() {
  3. int b = 1;
  4. byte x = 0;
  5. byte var10000;
  6. do {
  7. int b = (byte)(b ^ x);
  8. var10000 = b;
  9. b = b + 1;
  10. } while(var10000 < 10);
  11. return b;
  12. }
  13. // switch 反编译结果使用了hashCode
  14. public static void testSwitch3(int x) {
  15. while(true) {
  16. if (x < 5) {
  17. String var1 = "test";
  18. byte var2 = -1;
  19. switch(var1.hashCode()) {
  20. case 3412756:
  21. if (var1.equals("okay")) {
  22. var2 = 0;
  23. }
  24. default:
  25. switch(var2) {
  26. case 0:
  27. }
  28. }
  29. } else {
  30. System.out.println("wow x2!");
  31. }
  32. }
  33. }

总结

五种反编译工具比较下来,结合反编译速度和代码可读性测试,看起来 CFR 工具胜出,Procyon 紧随其后。CFR 在速度上不落下风,在反编译的代码可读性上,是最好的,主要体现在反编译后的变量命名装箱拆箱类型转换流程控制上,以及对 Lambda 表达式、Stream 流式操作和 Switch语法支持上,都非常优秀。根据 CFR 官方介绍,已经支持到 Java 14 语法,而且截止写这篇测试文章时,CFR 最新提交代码时间实在 11 小时之前,更新速度很快。

文章中部分代码已经上传 GitHub :github.com/niumoo/lab-notes/tree/master/java-decompiler

最后的话

文章有帮助可以点个「」或「分享」,都是支持,我都喜欢!

文章每周持续更新,要实时关注我更新的文章以及分享的干货,可以关注「 未读代码 」公众号或者我的博客,也可以加我微信:wn8398。

Java 反编译工具哪家强?对比分析瞧一瞧的更多相关文章

  1. 7 款开源 Java 反编译工具

    今天我们要来分享一些关于Java的反编译工具,反编译听起来是一个非常高上大的技术词汇,通俗的说,反编译是一个对目标可执行程序进行逆向分析,从而得到原始代码的过程.尤其是像.NET.Java这样的运行在 ...

  2. 7款开源Java反编译工具

    今天我们要来分享一些关于Java的反编译工具,反编译听起来是一个非常高上大的技术词汇,通俗的说,反编译是一个对目标可执行程序进行逆向分析,从而得到原始代码的过程.尤其是像.NET.Java这样的运行在 ...

  3. Java反编译工具JD-GUI以及Eclipse的反编译插件

    什么是反编译 高级语言源程序经过编译变成可执行文件,反编译就是逆过程.但是通常不能把可执行文件变成高级语言源代码,只能转换成汇编程序. 反编译是一个复杂的过程,所以越是高级语言,就越难于反编译,但目前 ...

  4. Java基础学习总结(27)——7 款开源 Java 反编译工具

    今天我们要来分享一些关于Java的反编译工具,反编译听起来是一个非常高上大的技术词汇,通俗的说,反编译是一个对目标可执行程序进行逆向分析,从而得到原始代码的过程.尤其是像.NET.Java这样的运行在 ...

  5. java反编译工具JD-GUI

    这款java反编译工具是由C++写的,是一款免费的非商业用途的软件,(Xjad也不错,但是不支持jar反编译) 一.支持众多.class反编译工具 二.支持反编译jar

  6. java反编译工具

    由于JAVA语言安全性高.代码优化.跨平台等特性,从1995年5月由SUN公司发布后,迅速取代了很多传统高级语言,占据了企业级网络应用开发等诸多领域的霸主地位. 不过,JAVA最突出的跨平台优势使得它 ...

  7. java反编译工具(XJad)

    java反编译工具(XJad) 2.2 绿色版 http://www.cr173.com/soft/35032.html Demo.class     --->    Demo.java

  8. 推荐一款非常好用的java反编译工具(转)

    源: 推荐一款非常好用的java反编译工具

  9. Java 反编译工具下载

    反编译,通俗来讲,就是将.java 文件经过编译生成的 .class 文件还原.注意这里的还原不等于 .java 文件.因为Java编译器在编译.java 文件的时候,会对代码进行一些处理. 那么接下 ...

随机推荐

  1. 七种join的书写规范

    在mysql中的两表进行连接时,总共有7种连接情况,具体可见下图 由图的从左到右的顺序 图1.左连接(left join):返回左表中的所有记录和右表中的连接字符字段相等的记录,若右表没有匹配值则补N ...

  2. 【Django笔记0】-Django项目创建,settings设置,运行

    Django项目创建,settings设置,运行 1,项目创建 ​ 通过pip下载Django以后,在cmd中cd到想要创建项目的路径,之后输入: django-admin startproject ...

  3. Web 前端 - 优雅地 Callback 转 Promise :aw

    前言 当今 ES7 标准大行其道,使用 async + await 将异步逻辑同步书写已经普及,但是却有许多旧库或旧代码尚未完全 Promise 化,急需一个小工具去挖去这代码中藓疾. 设计和实现 由 ...

  4. go每日一库 [home-dir] 获取用户主目录

    关于我 我的博客|文章首发 顾名思义,go-homedir用来获取用户的主目录.实际上,通过使用标准库os/user我们也可以得到内容,使用以下方式 标准库使用 package main import ...

  5. CVE-2021-21402 Jellyfin任意文件读取

    CVE-2021-21402 Jellyfin任意文件读取 漏洞简介 jellyfin 是一个自由的软件媒体系统,用于控制和管理媒体和流媒体.它是 emby 和 plex 的替代品,它通过多个应用程序 ...

  6. 使用Docker Toolbox 创建Docker虚拟机的方法-注意正确使用本地文件 file:参数的路径名

    使用Docker Toolbox 创建v1.12.6版的Docker虚拟机的方法, 一定要注意正确使用本地文件 file:// 参数的路径名, 之前尝试创建过多次,一直都没有成功过, 无法使用 fil ...

  7. 02-MySQL主要配置文件

    一.二进制日志log-bin 作用:主从复制 二.错误日志 log-err 默认关闭,记录严重的警告和错误信息,每次启动和关闭的详细信息 三.慢查询日志log 默认关闭,记录查询的sql语句,如果开启 ...

  8. 动态的创建Class对象方法及调用方式性能分析

    有了Class对象,能做什么? 创建类的对象:调用Class对象的newInstance()方法 类必须有一个无参数的构造器. 类的构造器的访问权限需要足够. 思考?没有无参的构造器就不能创建对象吗? ...

  9. 下拉框动态显示options遇到的问题

    百度后发现,目前资源比较多的就是layui和bootstrap这两种框架了,我是用的bootstrap-select,不知道为啥使用layui的formselect,引入css和js文件后,在sele ...

  10. Leedcode算法专题训练(二分查找)

    二分查找实现 非常详细的解释,简单但是细节很重要 https://www.cnblogs.com/kyoner/p/11080078.html 正常实现 Input : [1,2,3,4,5] key ...