电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

最好的Java 反編譯工具的使用對(duì)比分析

瀏覽:11日期:2022-08-12 10:30:38
前言

Java 反編譯,一聽(tīng)可能覺(jué)得高深莫測(cè),其實(shí)反編譯并不是什么特別高級(jí)的操作,Java 對(duì)于 Class 字節(jié)碼文件的生成有著嚴(yán)格的要求,如果你非常熟悉 Java 虛擬機(jī)規(guī)范,了解 Class 字節(jié)碼文件中一些字節(jié)的作用,那么理解反編譯的原理并不是什么問(wèn)題。甚至像下面這樣的 Class 文件你都能看懂一二。

最好的Java 反編譯工具的使用對(duì)比分析

一般在逆向研究和代碼分析中,反編譯用到的比較多。不過(guò)在日常開(kāi)發(fā)中,有時(shí)候只是簡(jiǎn)單的看一下所用依賴(lài)類(lèi)的反編譯,也是十分重要的。

恰好最近工作中也需要用到 Java 反編譯,所以這篇文章介紹目前常見(jiàn)的的幾種 Java 反編譯工具的使用,在文章的最后也會(huì)通過(guò)編譯速度、語(yǔ)法支持以及代碼可讀性三個(gè)維度,對(duì)它們進(jìn)行測(cè)試,分析幾款工具的優(yōu)缺點(diǎn)。

Procyon

Github 鏈接:https://github.com/mstrobel/procyonProcyon 不僅僅是反編譯工具,它其實(shí)是專(zhuān)注于 Java 代碼的生成和分析的一整套的 Java 元編程工具。主要包括下面幾個(gè)部分:

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

可以看到反編譯只是 Procyon 的其中一個(gè)模塊,Procyon 原來(lái)托管于 bitbucket,后來(lái)遷移到了 GitHub,根據(jù) GitHub 的提交記錄來(lái)看,也有將近兩年沒(méi)有更新了。不過(guò)也有依賴(lài) Procyon 的其他的開(kāi)源反編譯工具如** decompiler-procyon**,更新頻率還是很高的,下面也會(huì)選擇這個(gè)工具進(jìn)行反編譯測(cè)試。

使用 Procyon

<!-- https://mvnrepository.com/artifact/org.jboss.windup.decompiler/decompiler-procyon --><dependency> <groupId>org.jboss.windup.decompiler</groupId> <artifactId>decompiler-procyon</artifactId> <version>5.1.4.Final</version></dependency>

寫(xiě)一個(gè)簡(jiǎn)單的反編譯測(cè)試。

package com.wdbyte.decompiler;import java.io.IOException;import java.nio.file.Path;import java.nio.file.Paths;import java.util.Iterator;import java.util.List;import org.jboss.windup.decompiler.api.DecompilationFailure;import org.jboss.windup.decompiler.api.DecompilationListener;import org.jboss.windup.decompiler.api.DecompilationResult;import org.jboss.windup.decompiler.api.Decompiler;import org.jboss.windup.decompiler.procyon.ProcyonDecompiler;/** * Procyon 反編譯測(cè)試 * * @author https://github.com/niumoo * @date 2021/05/15 */public class ProcyonTest { public static void main(String[] args) throws IOException {Long time = procyon('decompiler.jar', 'procyon_output_jar');System.out.println(String.format('decompiler time: %dms', time)); } public static Long procyon(String source,String targetPath) throws IOException {long start = System.currentTimeMillis();Path outDir = Paths.get(targetPath);Path archive = Paths.get(source);Decompiler dec = new ProcyonDecompiler();DecompilationResult res = dec.decompileArchive(archive, outDir, new DecompilationListener() { public void decompilationProcessComplete() {System.out.println('decompilationProcessComplete'); } public void decompilationFailed(List<String> inputPath, String message) {System.out.println('decompilationFailed'); } public void fileDecompiled(List<String> inputPath, String outputPath) { } public boolean isCancelled() {return false; }});if (!res.getFailures().isEmpty()) { StringBuilder sb = new StringBuilder(); sb.append('Failed decompilation of ' + res.getFailures().size() + ' classes: '); Iterator failureIterator = res.getFailures().iterator(); while (failureIterator.hasNext()) {DecompilationFailure dex = (DecompilationFailure)failureIterator.next();sb.append(System.lineSeparator() + ' ').append(dex.getMessage()); } System.out.println(sb.toString());}System.out.println('Compilation results: ' + res.getDecompiledFiles().size() + ' succeeded, ' + res.getFailures().size() + ' failed.');dec.close();Long end = System.currentTimeMillis();return end - start; }}

Procyon 在反編譯時(shí)會(huì)實(shí)時(shí)輸出反編譯文件數(shù)量的進(jìn)度情況,最后還會(huì)統(tǒng)計(jì)反編譯成功和失敗的 Class 文件數(shù)量。

....五月 15, 2021 10:58:28 下午 org.jboss.windup.decompiler.procyon.ProcyonDecompiler$3 call信息: Decompiling 650 / 783五月 15, 2021 10:58:30 下午 org.jboss.windup.decompiler.procyon.ProcyonDecompiler$3 call信息: Decompiling 700 / 783五月 15, 2021 10:58:37 下午 org.jboss.windup.decompiler.procyon.ProcyonDecompiler$3 call信息: Decompiling 750 / 783decompilationProcessCompleteCompilation results: 783 succeeded, 0 failed.decompiler time: 40599ms

Procyon GUI

對(duì)于 Procyon 反編譯來(lái)說(shuō),在 GitHub 上也有基于此實(shí)現(xiàn)的開(kāi)源 GUI 界面,感興趣的可以下載嘗試。Github 地址:https://github.com/deathmarine/Luyten

CFR

GitHub 地址:https://github.com/leibnitz27/cfrCFR 官方網(wǎng)站:http://www.benf.org/other/cfr/(可能需要FQ)Maven 倉(cāng)庫(kù): https://mvnrepository.com/artifact/org.benf/cfr

CFR(Class File Reader) 可以支持 Java 9、Java 12、Java 14 以及其他的最新版 Java 代碼的反編譯工作。而且 CFR 本身的代碼是由 Java 6 編寫(xiě),所以基本可以使用 CFR 在任何版本的 Java 程序中。值得一提的是,使用 CFR 甚至可以將使用其他語(yǔ)言編寫(xiě)的的 JVM 類(lèi)文件反編譯回 Java 文件。

CFR 命令行使用

使用 CFR 反編譯時(shí),你可以下載已經(jīng)發(fā)布的 JAR 包,進(jìn)行命令行反編譯,也可以使用 Maven 引入的方式,在代碼中使用。下面先說(shuō)命令行運(yùn)行的方式。

直接在 GitHub Tags 下載已發(fā)布的最新版 JAR. 可以直接運(yùn)行查看幫助。

# 查看幫助java -jar cfr-0.151.jar --help

如果只是反編譯某個(gè) class.

# 反編譯 class 文件,結(jié)果輸出到控制臺(tái)java -jar cfr-0.151.jar WindupClasspathTypeLoader.class# 反編譯 class 文件,結(jié)果輸出到 out 文件夾java -jar cfr-0.151.jar WindupClasspathTypeLoader.class --outputpath ./out

反編譯某個(gè) JAR.

# 反編譯 jar 文件,結(jié)果輸出到 output_jar 文件夾➜ Desktop java -jar cfr-0.151.jar decompiler.jar --outputdir ./output_jarProcessing decompiler.jar (use silent to silence)Processing com.strobel.assembler.metadata.ArrayTypeLoaderProcessing com.strobel.assembler.metadata.ParameterDefinitionProcessing com.strobel.assembler.metadata.MethodHandleProcessing com.strobel.assembler.metadata.signatures.FloatSignature.....

反編譯結(jié)果會(huì)按照 class 的包路徑寫(xiě)入到指定文件夾中。

最好的Java 反編譯工具的使用對(duì)比分析

CFR 代碼中使用

添加依賴(lài)這里不提。

<!-- https://mvnrepository.com/artifact/org.benf/cfr --><dependency> <groupId>org.benf</groupId> <artifactId>cfr</artifactId> <version>0.151</version></dependency>

實(shí)際上我在官方網(wǎng)站和 GitHub 上都沒(méi)有看到具體的單元測(cè)試示例。不過(guò)沒(méi)有關(guān)系,既然能在命令行運(yùn)行,那么直接在 IDEA 中查看反編譯后的 Main 方法入口,看下命令行是怎么執(zhí)行的,就可以寫(xiě)出自己的單元測(cè)試了。

package com.wdbyte.decompiler;import java.io.IOException;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import org.benf.cfr.reader.api.CfrDriver;import org.benf.cfr.reader.util.getopt.OptionsImpl;/** * CFR Test * * @author https://github.com/niumoo * @date 2021/05/15 */public class CFRTest { public static void main(String[] args) throws IOException {Long time = cfr('decompiler.jar', './cfr_output_jar');System.out.println(String.format('decompiler time: %dms', time));// decompiler time: 11655ms } public static Long cfr(String source, String targetPath) throws IOException {Long start = System.currentTimeMillis();// source jarList<String> files = new ArrayList<>();files.add(source);// target dirHashMap<String, String> outputMap = new HashMap<>();outputMap.put('outputdir', targetPath);OptionsImpl options = new OptionsImpl(outputMap);CfrDriver cfrDriver = new CfrDriver.Builder().withBuiltOptions(options).build();cfrDriver.analyse(files);Long end = System.currentTimeMillis();return (end - start); }}JD-Core

GiHub 地址:https://github.com/java-decompiler/jd-coreJD-core 官方網(wǎng)址:https://java-decompiler.github.io/JD-core 是一個(gè)的獨(dú)立的 Java 庫(kù),可以用于 Java 的反編譯,支持從 Java 1 至 Java 12 的字節(jié)碼反編譯,包括 Lambda 表達(dá)式、方式引用、默認(rèn)方法等。知名的 JD-GUI 和 Eclipse 無(wú)縫集成反編譯引擎就是 JD-core。JD-core 提供了一些反編譯的核心功能,也提供了單獨(dú)的 Class 反編譯方法,但是如果你想在自己的代碼中去直接反編譯整個(gè) JAR 包,還是需要一些改造的,如果是代碼中有匿名函數(shù),Lambda 等,雖然可以直接反編譯,不過(guò)也需要額外考慮。

使用 JD-core

<!-- https://mvnrepository.com/artifact/org.jd/jd-core --><dependency> <groupId>org.jd</groupId> <artifactId>jd-core</artifactId> <version>1.1.3</version></dependency>

為了可以反編譯整個(gè) JAR 包,使用的代碼我做了一些簡(jiǎn)單改造,以便于最后一部分的對(duì)比測(cè)試,但是這個(gè)示例中沒(méi)有考慮內(nèi)部類(lèi),Lambda 等會(huì)編譯出多個(gè) Class 文件的情況,所以不能直接使用在生產(chǎn)中。

package com.wdbyte.decompiler;import java.io.File;import java.io.IOException;import java.io.InputStream;import java.nio.file.Files;import java.nio.file.Path;import java.nio.file.Paths;import java.util.Enumeration;import java.util.HashMap;import java.util.jar.JarFile;import java.util.zip.ZipEntry;import java.util.zip.ZipFile;import org.apache.commons.io.IOUtils;import org.apache.commons.lang3.StringUtils;import org.jd.core.v1.ClassFileToJavaSourceDecompiler;import org.jd.core.v1.api.loader.Loader;import org.jd.core.v1.api.printer.Printer;/** * @author https://github.com/niumoo * @date 2021/05/15 */public class JDCoreTest { public static void main(String[] args) throws Exception {JDCoreDecompiler jdCoreDecompiler = new JDCoreDecompiler();Long time = jdCoreDecompiler.decompiler('decompiler.jar','jd_output_jar');System.out.println(String.format('decompiler time: %dms', time)); }}class JDCoreDecompiler{ private ClassFileToJavaSourceDecompiler decompiler = new ClassFileToJavaSourceDecompiler(); // 存放字節(jié)碼 private HashMap<String,byte[]> classByteMap = new HashMap<>(); /** * 注意:沒(méi)有考慮一個(gè) Java 類(lèi)編譯出多個(gè) Class 文件的情況。 * * @param source * @param target * @return * @throws Exception */ public Long decompiler(String source,String target) throws Exception {long start = System.currentTimeMillis();// 解壓archive(source);for (String className : classByteMap.keySet()) { String path = StringUtils.substringBeforeLast(className, '/'); String name = StringUtils.substringAfterLast(className, '/'); if (StringUtils.contains(name, '$')) {name = StringUtils.substringAfterLast(name, '$'); } name = StringUtils.replace(name, '.class', '.java'); decompiler.decompile(loader, printer, className); String context = printer.toString(); Path targetPath = Paths.get(target + '/' + path + '/' + name); if (!Files.exists(Paths.get(target + '/' + path))) {Files.createDirectories(Paths.get(target + '/' + path)); } Files.deleteIfExists(targetPath); Files.createFile(targetPath); Files.write(targetPath, context.getBytes());}return System.currentTimeMillis() - start; } private void archive(String path) throws IOException {try (ZipFile archive = new JarFile(new File(path))) { Enumeration<? extends ZipEntry> entries = archive.entries(); while (entries.hasMoreElements()) {ZipEntry entry = entries.nextElement();if (!entry.isDirectory()) { String name = entry.getName(); if (name.endsWith('.class')) {byte[] bytes = null;try (InputStream stream = archive.getInputStream(entry)) { bytes = IOUtils.toByteArray(stream);}classByteMap.put(name, bytes); }} }} } private Loader loader = new Loader() {@Overridepublic byte[] load(String internalName) { return classByteMap.get(internalName);}@Overridepublic boolean canLoad(String internalName) { return classByteMap.containsKey(internalName);} }; private Printer printer = new Printer() {protected static final String TAB = ' ';protected static final String NEWLINE = 'n';protected int indentationCount = 0;protected StringBuilder sb = new StringBuilder();@Override public String toString() { String toString = sb.toString(); sb = new StringBuilder(); return toString;}@Override public void start(int maxLineNumber, int majorVersion, int minorVersion) {}@Override public void end() {}@Override public void printText(String text) { sb.append(text); }@Override public void printNumericConstant(String constant) { sb.append(constant); }@Override public void printStringConstant(String constant, String ownerInternalName) { sb.append(constant); }@Override public void printKeyword(String keyword) { sb.append(keyword); }@Override public void printDeclaration(int type, String internalTypeName, String name, String descriptor) { sb.append(name); }@Override public void printReference(int type, String internalTypeName, String name, String descriptor, String ownerInternalName) { sb.append(name); }@Override public void indent() { this.indentationCount++; }@Override public void unindent() { this.indentationCount--; }@Override public void startLine(int lineNumber) { for (int i=0; i<indentationCount; i++) sb.append(TAB); }@Override public void endLine() { sb.append(NEWLINE); }@Override public void extraLine(int count) { while (count-- > 0) sb.append(NEWLINE); }@Override public void startMarker(int type) {}@Override public void endMarker(int type) {} };}JD-GUI

GitHub 地址:https://github.com/java-decompiler/jd-guiJD-core 也提供了官方的 GUI 界面,需要的也可以直接下載嘗試。

最好的Java 反編譯工具的使用對(duì)比分析

Jadx

GitHub 地址:https://github.com/skylot/jadxJadx 是一款可以反編譯 JAR、APK、DEX、AAR、AAB、ZIP 文件的反編譯工具,并且也配有 Jadx-gui 用于界面操作。Jadx 使用 Grade 進(jìn)行依賴(lài)管理,可以自行克隆倉(cāng)庫(kù)打包運(yùn)行。

git clone https://github.com/skylot/jadx.gitcd jadx./gradlew dist# 查看幫助 ./build/jadx/bin/jadx --help jadx - dex to java decompiler, version: devusage: jadx [options] <input files> (.apk, .dex, .jar, .class, .smali, .zip, .aar, .arsc, .aab)options: -d, --output-dir - output directory -ds, --output-dir-src - output directory for sources -dr, --output-dir-res - output directory for resources -r, --no-res- do not decode resources -s, --no-src- do not decompile source code --single-class - decompile a single class --output-format - can be ’java’ or ’json’, default: java -e, --export-gradle - save as android gradle project -j, --threads-count - processing threads count, default: 6 --show-bad-code - show inconsistent code (incorrectly decompiled) --no-imports- disable use of imports, always write entire package name --no-debug-info - disable debug info --add-debug-lines - add comments with debug line numbers if available --no-inline-anonymous - disable anonymous classes inline --no-replace-consts - don’t replace constant value with matching constant field --escape-unicode - escape non latin characters in strings (with u) --respect-bytecode-access-modifiers - don’t change original access modifiers --deobf - activate deobfuscation --deobf-min - min length of name, renamed if shorter, default: 3 --deobf-max - max length of name, renamed if longer, default: 64 --deobf-cfg-file - deobfuscation map file, default: same dir and name as input file with ’.jobf’ extension --deobf-rewrite-cfg - force to save deobfuscation map --deobf-use-sourcename - use source file name as class name alias --deobf-parse-kotlin-metadata - parse kotlin metadata to class and package names --rename-flags - what to rename, comma-separated, ’case’ for system case sensitivity, ’valid’ for java identifiers, ’printable’ characters, ’none’ or ’all’ (default) --fs-case-sensitive - treat filesystem as case sensitive, false by default --cfg - save methods control flow graph to dot file --raw-cfg - save methods control flow graph (use raw instructions) -f, --fallback - make simple dump (using goto instead of ’if’, ’for’, etc) -v, --verbose - verbose output (set --log-level to DEBUG) -q, --quiet - turn off output (set --log-level to QUIET) --log-level - set log level, values: QUIET, PROGRESS, ERROR, WARN, INFO, DEBUG, default: PROGRESS --version - print jadx version -h, --help - print this helpExample: jadx -d out classes.dex

根據(jù) HELP 信息,如果想要反編譯 decompiler.jar 到 out 文件夾。

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

Fernflower

GitHub 地址:https://github.com/fesh0r/fernflowerFernflower 和 Jadx 一樣使用 Grade 進(jìn)行依賴(lài)管理,可以自行克隆倉(cāng)庫(kù)打包運(yùn)行。

➜ fernflower-master ./gradlew buildBUILD SUCCESSFUL in 32s4 actionable tasks: 4 executed➜ fernflower-master java -jar build/libs/fernflower.jarUsage: java -jar fernflower.jar [-<option>=<value>]* [<source>]+ <destination>Example: java -jar fernflower.jar -dgs=true c:mysource c:my.jar d:decompiled➜ fernflower-master mkdir out➜ fernflower-master java -jar build/libs/fernflower.jar ~/Desktop/decompiler.jar ./outINFO: Decompiling class com/strobel/assembler/metadata/ArrayTypeLoaderINFO: ... doneINFO: Decompiling class com/strobel/assembler/metadata/ParameterDefinitionINFO: ... doneINFO: Decompiling class com/strobel/assembler/metadata/MethodHandle...➜ fernflower-master ll outtotal 1288-rw-r--r-- 1 darcy staff 595K 5 16 17:47 decompiler.jar➜ fernflower-master

Fernflower 在反編譯 JAR 包時(shí),默認(rèn)反編譯的結(jié)果也是一個(gè) JAR 包。Jad

反編譯速度

到這里已經(jīng)介紹了五款 Java 反編譯工具了,那么在日常開(kāi)發(fā)中我們應(yīng)該使用哪一個(gè)呢?又或者在代碼分析時(shí)我們又該選擇哪一個(gè)呢?我想這兩種情況的不同,使用時(shí)的關(guān)注點(diǎn)也是不同的。如果是日常使用,讀讀代碼,我想應(yīng)該是對(duì)可讀性要求更高些,如果是大量的代碼分析工作,那么可能反編譯的速度和語(yǔ)法的支持上要求更高些。為了能有一個(gè)簡(jiǎn)單的參考數(shù)據(jù),我使用 JMH 微基準(zhǔn)測(cè)試工具分別對(duì)這五款反編譯工具進(jìn)行了簡(jiǎn)單的測(cè)試,下面是一些測(cè)試結(jié)果。

測(cè)試環(huán)境

環(huán)境變量 描述 處理器 2.6 GHz 六核Intel Core i7 內(nèi)存 16 GB 2667 MHz DDR4 Java 版本 JDK 14.0.2 測(cè)試方式 JMH 基準(zhǔn)測(cè)試。 待反編譯 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 這個(gè)包是比較大的,是拿很多代碼倉(cāng)庫(kù)合并到一起的,同時(shí)還有很多 Python 轉(zhuǎn) Java 生成的代碼,理論上代碼的復(fù)雜度會(huì)更高。

Benchmark Cnt Score Cfr 1 413838.826ms fernflower 1 246819.168ms jdcore 1 Error procyon 1 487647.181ms jadx 1 505600.231ms 語(yǔ)法支持和可讀性

如果反編譯后的代碼需要自己看的話(huà),那么可讀性更好的代碼更占優(yōu)勢(shì),下面我寫(xiě)了一些代碼,主要是 Java 8 及以下的代碼語(yǔ)法和一些嵌套的流程控制,看看反編譯后的效果如何。

package com.wdbyte.decompiler;import java.util.ArrayList;import java.util.List;import java.util.stream.IntStream;import org.benf.cfr.reader.util.functors.UnaryFunction;/** * @author https://www.wdbyte.com * @date 2021/05/16 */public class HardCode <A, B> { public HardCode(A a, B b) { } public static void test(int... args) { } public static void main(String... args) {test(1, 2, 3, 4, 5, 6); } int byteAnd0() {int b = 1;int x = 0;do { b = (byte)((b ^ x));} while (b++ < 10);return b; } private void a(Integer i) {a(i);b(i);c(i); } private void b(int i) {a(i);b(i);c(i); } private void c(double d) {c(d);d(d); } private void d(Double d) {c(d);d(d); } private void e(Short s) {b(s);c(s);e(s);f(s); } private void f(short s) {b(s);c(s);e(s);f(s); } void test1(String path) {try { int x = 3;} catch (NullPointerException t) { System.out.println('File Not found'); if (path == null) { return; } throw t;} finally { System.out.println('Fred'); if (path == null) { throw new IllegalStateException(); }} } private final List<Integer> stuff = new ArrayList<>();{stuff.add(1);stuff.add(2); } public static int plus(boolean t, int a, int b) {int c = t ? a : b;return c; } // Lambda Integer lambdaInvoker(int arg, UnaryFunction<Integer, Integer> fn) {return fn.invoke(arg); } // Lambda public int testLambda() {return lambdaInvoker(3, x -> x + 1);//return 1; } // Lambda public Integer testLambda(List<Integer> stuff, int y, boolean b) {return stuff.stream().filter(b ? x -> x > y : x -> x < 3).findFirst().orElse(null); } // stream public static <Y extends Integer> void testStream(List<Y> list) {IntStream s = list.stream() .filter(x -> {System.out.println(x);return x.intValue() / 2 == 0;}) .map(x -> (Integer)x+2) .mapToInt(x -> x);s.toArray(); } // switch public void testSwitch1(){int i = 0;switch(((Long)(i + 1L)) + '') { case '1':System.out.println('one');} } // switch public void testSwitch2(String string){switch (string) { case 'apples':System.out.println('apples');break; case 'pears':System.out.println('pears');break;} } // switch public static void testSwitch3(int x) {while (true) { if (x < 5) {switch ('test') { case 'okay':continue; default:continue;} } System.out.println('wow x2!');} }}

此處本來(lái)貼出了所有工具的反編譯結(jié)果,但是礙于文章長(zhǎng)度和閱讀體驗(yàn),沒(méi)有放出來(lái),不過(guò)我在個(gè)人博客的發(fā)布上是有完整代碼的,個(gè)人網(wǎng)站排版比較自由,可以使用 Tab 選項(xiàng)卡的方式展示。如果需要查看可以訪問(wèn) https://www.wdbyte.com 進(jìn)行查看。

Procyon

看到 Procyon 的反編譯結(jié)果,還是比較吃驚的,在正常反編譯的情況下,反編譯后的代碼基本上都是原汁原味。唯一一處反編譯后和源碼語(yǔ)法上有變化的地方,是一個(gè)集合的初始化操作略有不同。

// 源碼 public HardCode(A a, B b) { } private final List<Integer> stuff = new ArrayList<>();{ stuff.add(1); stuff.add(2); }// Procyon 反編譯private final List<Integer> stuff; public HardCode(final A a, final B b) { (this.stuff = new ArrayList<Integer>()).add(1); this.stuff.add(2);}

而其他部分代碼, 比如裝箱拆箱,Switch 語(yǔ)法,Lambda 表達(dá)式,流式操作以及流程控制等,幾乎完全一致,閱讀沒(méi)有障礙。

裝箱拆箱操作反編譯后完全一致,沒(méi)有多余的類(lèi)型轉(zhuǎn)換代碼。

// 源碼private void a(Integer i) { a(i); b(i); c(i);}private void b(int i) { a(i); b(i); c(i);}private void c(double d) { c(d); d(d);}private void d(Double d) { c(d); d(d);}private void e(Short s) { b(s); c(s); e(s); f(s);}private void f(short s) { b(s); c(s); e(s); f(s);}// Procyon 反編譯private void a(final Integer i) { this.a(i); this.b(i); this.c(i);}private void b(final int i) { this.a(i); this.b(i); this.c(i);}private void c(final double d) { this.c(d); this.d(d);}private void d(final Double d) { this.c(d); this.d(d);}private void e(final Short s) { this.b(s); this.c(s); this.e(s); this.f(s);}private void f(final short s) { this.b(s); this.c(s); this.e(s); this.f(s);}

Switch 部分也是一致,流程控制部分也沒(méi)有變化。

// 源碼 switchpublic void testSwitch1(){ int i = 0; switch(((Long)(i + 1L)) + '') {case '1': System.out.println('one'); }}public void testSwitch2(String string){ switch (string) {case 'apples': System.out.println('apples'); break;case 'pears': System.out.println('pears'); break; }}public static void testSwitch3(int x) { while (true) {if (x < 5) { switch ('test') {case 'okay': continue;default: continue; }}System.out.println('wow x2!'); }}// Procyon 反編譯public void testSwitch1() { final int i = 0; final String string = (Object)(i + 1L) + ''; switch (string) {case '1': { System.out.println('one'); break;} }}public void testSwitch2(final String string) { switch (string) {case 'apples': { System.out.println('apples'); break;}case 'pears': { System.out.println('pears'); break;} }} public static void testSwitch3(final int x) { while (true) {if (x < 5) { final String s = 'test'; switch (s) {case 'okay': { continue;}default: { continue;} }}else { System.out.println('wow x2!');} }}

Lambda 表達(dá)式和流式操作完全一致。

// 源碼// Lambdapublic Integer testLambda(List<Integer> stuff, int y, boolean b) { return stuff.stream().filter(b ? x -> x > y : x -> x < 3).findFirst().orElse(null);}// streampublic static <Y extends Integer> void testStream(List<Y> list) { IntStream s = list.stream().filter(x -> { System.out.println(x); return x.intValue() / 2 == 0; }).map(x -> (Integer)x+2).mapToInt(x -> x); s.toArray();}// Procyon 反編譯public Integer testLambda(final List<Integer> stuff, final int y, final boolean b) { return stuff.stream().filter(b ? (x -> x > y) : (x -> x < 3)).findFirst().orElse(null);}public static <Y extends Integer> void testStream(final List<Y> list) { final IntStream s = list.stream().filter(x -> {System.out.println(x);return x / 2 == 0; }).map(x -> x + 2).mapToInt(x -> x); s.toArray();}

流程控制,反編譯后發(fā)現(xiàn)丟失了無(wú)意義的代碼部分,閱讀來(lái)說(shuō)并無(wú)障礙。

// 源碼void test1(String path) { try {int x = 3; } catch (NullPointerException t) {System.out.println('File Not found');if (path == null) { return; }throw t; } finally {System.out.println('Fred');if (path == null) { throw new IllegalStateException(); } }}// Procyon 反編譯void test1(final String path) { try {} catch (NullPointerException t) {System.out.println('File Not found');if (path == null) { return;}throw t; } finally {System.out.println('Fred');if (path == null) { throw new IllegalStateException();} }}

鑒于代碼篇幅,下面幾種的反編譯結(jié)果的對(duì)比只會(huì)列出不同之處,相同之處會(huì)直接跳過(guò)。

CFR

CFR 的反編譯結(jié)果多出了類(lèi)型轉(zhuǎn)換部分,個(gè)人來(lái)看沒(méi)有 Procyon 那么原汁原味,不過(guò)也算是十分優(yōu)秀,測(cè)試案例中唯一不滿(mǎn)意的地方是對(duì) while continue 的處理。

// CFR 反編譯結(jié)果// 裝箱拆箱private void e(Short s) { this.b(s.shortValue()); // 裝箱拆箱多出了類(lèi)型轉(zhuǎn)換部分。 this.c(s.shortValue()); // 裝箱拆箱多出了類(lèi)型轉(zhuǎn)換部分。 this.e(s); this.f(s);}// 流程控制void test1(String path) { try {int n = 3;// 流程控制反編譯結(jié)果十分滿(mǎn)意,原汁原味,甚至此處的無(wú)意思代碼都保留了。 } catch (NullPointerException t) {System.out.println('File Not found');if (path == null) { return;}throw t; } finally {System.out.println('Fred');if (path == null) { throw new IllegalStateException();} }}// Lambda 和 Stream 操作完全一致,不提。// switch 處,反編譯后功能一致,但是流程控制有所更改。public static void testSwitch3(int x) { block6: while (true) { // 源碼中只有 while(true),反編譯后多了 block6if (x < 5) { switch ('test') {case 'okay': { continue block6; // 多了 block6} } continue;}System.out.println('wow x2!'); }}JD-Core

JD-Core 和 CFR 一樣,對(duì)于裝箱拆箱操作,反編譯后不再一致,多了類(lèi)型轉(zhuǎn)換部分,而且自動(dòng)優(yōu)化了數(shù)據(jù)類(lèi)型。個(gè)人感覺(jué),如果是反編譯后自己閱讀,通篇的數(shù)據(jù)類(lèi)型的轉(zhuǎn)換優(yōu)化影響還是挺大的。

// JD-Core 反編譯private void d(Double d) { c(d.doubleValue()); // 新增了數(shù)據(jù)類(lèi)型轉(zhuǎn)換 d(d);}private void e(Short s) { b(s.shortValue()); // 新增了數(shù)據(jù)類(lèi)型轉(zhuǎn)換 c(s.shortValue()); // 新增了數(shù)據(jù)類(lèi)型轉(zhuǎn)換 e(s); f(s.shortValue()); // 新增了數(shù)據(jù)類(lèi)型轉(zhuǎn)換}private void f(short s) { b(s); c(s); e(Short.valueOf(s)); // 新增了數(shù)據(jù)類(lèi)型轉(zhuǎn)換 f(s);}// Stream 操作中,也自動(dòng)優(yōu)化了數(shù)據(jù)類(lèi)型轉(zhuǎn)換,閱讀起來(lái)比較累。public static <Y extends Integer> void testStream(List<Y> list) { IntStream s = list.stream().filter(x -> {System.out.println(x);return (x.intValue() / 2 == 0); }).map(x -> Integer.valueOf(x.intValue() + 2)).mapToInt(x -> x.intValue()); s.toArray();}Jadx

首先 Jadx 在反編譯測(cè)試代碼時(shí),報(bào)出了錯(cuò)誤,反編譯的結(jié)果里也有提示不能反編 Lambda 和 Stream 操作,反編譯結(jié)果中變量名稱(chēng)雜亂無(wú)章,流程控制幾乎陣亡,如果你想反編譯后生物肉眼閱讀,Jadx 肯定不是一個(gè)好選擇。

// Jadx 反編譯private void e(Short s) { b(s.shortValue());// 新增了數(shù)據(jù)類(lèi)型轉(zhuǎn)換 c((double) s.shortValue());// 新增了數(shù)據(jù)類(lèi)型轉(zhuǎn)換 e(s); f(s.shortValue());// 新增了數(shù)據(jù)類(lèi)型轉(zhuǎn)換}private void f(short s) { b(s); c((double) s);// 新增了數(shù)據(jù)類(lèi)型轉(zhuǎn)換 e(Short.valueOf(s));// 新增了數(shù)據(jù)類(lèi)型轉(zhuǎn)換 f(s);}public int testLambda() { // testLambda 反編譯失敗 /*r2 = this;r0 = 3r1 = move-resultjava.lang.Integer r0 = r2.lambdaInvoker(r0, r1)int r0 = r0.intValue()return r0 */ throw new UnsupportedOperationException('Method not decompiled: com.wdbyte.decompiler.HardCode.testLambda():int');}// Stream 反編譯失敗public static <Y extends java.lang.Integer> void testStream(java.util.List<Y> r3) { /*java.util.stream.Stream r1 = r3.stream()r2 = move-resultjava.util.stream.Stream r1 = r1.filter(r2)r2 = move-resultjava.util.stream.Stream r1 = r1.map(r2)r2 = move-resultjava.util.stream.IntStream r0 = r1.mapToInt(r2)r0.toArray()return */ throw new UnsupportedOperationException('Method not decompiled: com.wdbyte.decompiler.HardCode.testStream(java.util.List):void');}public void testSwitch2(String string) { // switch 操作無(wú)法正常閱讀,和源碼出入較大。 char c = 65535; switch (string.hashCode()) {case -1411061671: if (string.equals('apples')) {c = 0;break; } break;case 106540109: if (string.equals('pears')) {c = 1;break; } break; } switch (c) {case 0: System.out.println('apples'); return;case 1: System.out.println('pears'); return;default: return; }}Fernflower

Fernflower 的反編譯結(jié)果總體上還是不錯(cuò)的,不過(guò)也有不足,它對(duì)變量名稱(chēng)的指定,以及 Switch 字符串時(shí)的反編譯結(jié)果不夠理想。

//反編譯后變量命名不利于閱讀,有很多 var 變量int byteAnd0() { int b = 1; byte x = 0; byte var10000; do { int b = (byte)(b ^ x); var10000 = b; b = b + 1; } while(var10000 < 10); return b;}// switch 反編譯結(jié)果使用了hashCodepublic static void testSwitch3(int x) { while(true) { if (x < 5) { String var1 = 'test'; byte var2 = -1; switch(var1.hashCode()) { case 3412756: if (var1.equals('okay')) { var2 = 0; } default: switch(var2) { case 0: } } } else { System.out.println('wow x2!'); } }}總結(jié)

五種反編譯工具比較下來(lái),結(jié)合反編譯速度和代碼可讀性測(cè)試,看起來(lái) CFR 工具勝出,Procyon 緊隨其后。CFR 在速度上不落下風(fēng),在反編譯的代碼可讀性上,是最好的,主要體現(xiàn)在反編譯后的變量命名、裝箱拆箱、類(lèi)型轉(zhuǎn)換流程控制上,以及對(duì) Lambda 表達(dá)式、Stream 流式操作和 Switch語(yǔ)法支持上,都非常優(yōu)秀。根據(jù) CFR 官方介紹,已經(jīng)支持到 Java 14 語(yǔ)法,而且截止寫(xiě)這篇測(cè)試文章時(shí),CFR 最新提交代碼時(shí)間實(shí)在 11 小時(shí)之前,更新速度很快。

文章中部分代碼已經(jīng)上傳 GitHub :github.com/niumoo/lab-notes/tree/master/java-decompiler

以上就是最好的Java 反編譯工具的使用對(duì)比分析的詳細(xì)內(nèi)容,更多關(guān)于Java 反編譯工具的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 热处理温控箱,热处理控制箱厂家-吴江市兴达电热设备厂 | 重庆磨床过滤机,重庆纸带过滤机,机床伸缩钣金,重庆机床钣金护罩-重庆达鸿兴精密机械制造有限公司 | 插针变压器-家用电器变压器-工业空调变压器-CD型电抗器-余姚市中驰电器有限公司 | 伺服电机维修、驱动器维修「安川|三菱|松下」伺服维修公司-深圳华创益 | 烽火安全网_加密软件、神盾软件官网 | LZ-373测厚仪-华瑞VOC气体检测仪-个人有毒气体检测仪-厂家-深圳市深博瑞仪器仪表有限公司 | 不锈钢酒柜|恒温酒柜|酒柜定制|酒窖定制-上海啸瑞实业有限公司 | 昊宇水工|河北昊宇水工机械工程有限公司 | 3D全息投影_地面互动投影_360度立体投影_水幕灯光秀 | 广东银虎 蜂窝块状沸石分子筛-吸附脱硫分子筛-萍乡市捷龙环保科技有限公司 | 上海平衡机-单面卧式动平衡机-万向节动平衡机-圈带动平衡机厂家-上海申岢动平衡机制造有限公司 | 水平筛厂家-三轴椭圆水平振动筛-泥沙震动筛设备_山东奥凯诺矿机 包装设计公司,产品包装设计|包装制作,包装盒定制厂家-汇包装【官方网站】 | 长沙广告公司|长沙广告制作设计|长沙led灯箱招牌制作找望城湖南锦蓝广告装饰工程有限公司 | 高温链条油|高温润滑脂|轴承润滑脂|机器人保养用油|干膜润滑剂-东莞卓越化学 | PCB接线端子_栅板式端子_线路板连接器_端子排生产厂家-置恒电气 喷码机,激光喷码打码机,鸡蛋打码机,手持打码机,自动喷码机,一物一码防伪溯源-恒欣瑞达有限公司 假肢-假肢价格-假肢厂家-河南假肢-郑州市力康假肢矫形器有限公司 | 网站建设-网站制作-网站设计-网站开发定制公司-网站SEO优化推广-咏熠软件 | 超声骨密度仪-动脉硬化检测仪器-人体成分分析仪厂家/品牌/价格_南京科力悦 | 合肥网带炉_安徽箱式炉_钟罩炉-合肥品炙装备科技有限公司 | 江苏农村商业银行招聘网_2024江苏农商行考试指南_江苏农商行校园招聘 | 北京网站建设首页,做网站选【优站网】,专注北京网站建设,北京网站推广,天津网站建设,天津网站推广,小程序,手机APP的开发。 | 上海新光明泵业制造有限公司-电动隔膜泵,气动隔膜泵,卧式|立式离心泵厂家 | 生物颗粒燃烧机-生物质燃烧机-热风炉-生物颗粒蒸汽发生器-丽水市久凯能源设备有限公司 | 螺旋绞龙叶片,螺旋输送机厂家,山东螺旋输送机-淄博长江机械制造有限公司 | 【同风运车官网】一站式汽车托运服务平台,验车满意再付款 | 能耗监测系统-节能监测系统-能源管理系统-三水智能化 | 工作心得_读书心得_学习心得_找心得体会范文就上学道文库 | 冲击式破碎机-冲击式制砂机-移动碎石机厂家_青州市富康机械有限公司 | 飞行者联盟-飞机模拟机_无人机_低空经济_航空技术交流平台 | 硅胶管挤出机厂家_硅胶挤出机生产线_硅胶条挤出机_臣泽智能装备 贵州科比特-防雷公司厂家提供贵州防雷工程,防雷检测,防雷接地,防雷设备价格,防雷产品报价服务-贵州防雷检测公司 | 玉米加工设备,玉米深加工机械,玉米糁加工设备.玉米脱皮制糁机 华豫万通粮机 | 桑茶-七彩贝壳桑叶茶 长寿茶| 自动钻孔机-全自动数控钻孔机生产厂家-多米(广东)智能装备有限公司 | 耐磨陶瓷,耐磨陶瓷管道_厂家-淄博拓创陶瓷科技 | 叉车电池-叉车电瓶-叉车蓄电池-铅酸蓄电池-电动叉车蓄电池生产厂家 | 丹佛斯变频器-Danfoss战略代理经销商-上海津信变频器有限公司 | 免联考国际MBA_在职MBA报考条件/科目/排名-MBA信息网 | 生物除臭剂-除味剂-植物-污水除臭剂厂家-携葵环保有限公司 | 北京森语科技有限公司-模型制作专家-展览展示-沙盘模型设计制作-多媒体模型软硬件开发-三维地理信息交互沙盘 | PTFE接头|聚四氟乙烯螺丝|阀门|薄膜|消解罐|聚四氟乙烯球-嘉兴市方圆氟塑制品有限公司 | 钢格板_钢格栅_格栅板_钢格栅板 - 安平县鑫拓钢格栅板厂家 | 河南砖机首页-全自动液压免烧砖机,小型砌块水泥砖机厂家[十年老厂] | 天然气分析仪-液化气二甲醚分析仪|传昊仪器 |