4
likes look again, the motivation is unlimited. Hello world search "1611699fcefbdf 1611699fcefbe0 program Alang ".
This article Github.com/niumoo/JavaNotes and unread code blog has been included, there are many knowledge points and series of articles.

Preface

Java decompilation may feel unpredictable. In fact, decompilation is not a particularly advanced operation. Java has strict requirements for the generation of Class bytecode files. If you are very familiar with the Java virtual machine specification, understand Class The role of some bytes in the bytecode file, then understanding the principle of decompilation is not a problem.
You can even understand a Class file like the following.

Generally, in reverse research and code analysis, decompilation is used more. However, in daily development, sometimes it is very important to simply look at the decompilation of the dependent classes used.

Java decompilation is also needed in recent work, so this article introduces the use of several common Java decompilation tools. At the end of the article, compilation speed , syntax support and code readability three dimensions, they test , several analysis tools advantages and disadvantages .

<!-- more -->

Procyon

Github link: https://github.com/mstrobel/procyon
Procyon not just a decompilation tool, it is actually a complete set of Java metaprogramming tools focused on the generation and analysis of Java code.
It mainly includes the following parts:

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

Decompile can see just Procyon of one of the modules, Procyon originally hosted bitbucket, and later moved to GitHub, according to the GitHub commit record point of view, there has not been updated for almost two years. However, there are other open source decompilation tools Procyon decompiler-procyon , and the update frequency is still very high. This tool will also be selected for decompilation testing.

Use 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>

Write a simple decompilation test.

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 反编译测试
 *
 *  @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 will output the progress of the number of decompiled files in real time during decompilation, and finally count the number of class files that have been successfully decompiled and failed.

....
五月 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 / 783
decompilationProcessComplete
Compilation results: 783 succeeded, 0 failed.
decompiler time: 40599ms

Procyon GUI

For Procyon decompilation, there is also an open source GUI interface based on this implementation on GitHub. Those who are interested can download and try it.
Github address: https://github.com/deathmarine/Luyten

CFR

GitHub address: https://github.com/leibnitz27/cfr
CFR official website: http://www.benf.org/other/cfr/ (FQ may be required)
Maven warehouse: https://mvnrepository.com/artifact/org.benf/cfr

CFR Java 9, Java 12, Java 14 and other latest versions of Java code. And the code of CFR itself is written by Java 6 , so CFR can basically be used in any version of Java program. It is worth mentioning that using CFR can even decompile JVM class files written in other languages back to Java files.

CFR command line use

When using CFR decompilation, you can download the released JAR package and perform command line decompilation, or you can use the method introduced by Maven and use it in the code. Let's talk about the way the command line runs.

directly at GitHub Tags. You can run it directly to view the help.

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

If you just decompile a certain class.

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

Decompile a certain JAR.

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

The decompilation result will be written into the specified folder according to the package path of the class.

Used in CFR code

Adding dependencies is not mentioned here.

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

In fact, I haven't seen specific unit test examples on the official website or GitHub. But it doesn't matter. Since it can be run on the command line, you can directly view the decompiled Main method entry in IDEA to see how the command line is executed, and you can write your own unit test.

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 jar
        List<String> files = new ArrayList<>();
        files.add(source);
        // target dir
        HashMap<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 address: https://github.com/java-decompiler/jd-core
JD-core official website: https://java-decompiler.github.io/
JD-core is an independent Java library that can be used for Java decompilation and supports bytecode decompilation from Java 1 to Java 12, including Lambda expressions, method references, default methods, etc. The well-known JD-GUI and Eclipse seamless integration decompilation engine is JD-core.
JD-core provides some core functions of decompilation, and also provides a separate Class decompilation method, but if you want to directly decompile the entire JAR package in your own code, you still need some modification. Anonymous functions, Lambda, etc., although they can be decompiled directly, they also require additional consideration.

Use 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>

In order to decompile the entire JAR package, I made some simple modifications to the code used to facilitate the comparison test of the last part. However, in this example, internal classes are not considered. Lambda etc. will compile multiple Class files, so it cannot be directly Used in production.

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();
    // 存放字节码
    private HashMap<String,byte[]> classByteMap = new HashMap<>();

    /**
     * 注意:没有考虑一个 Java 类编译出多个 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() {
        @Override
        public byte[] load(String internalName) {
            return classByteMap.get(internalName);
        }
        @Override
        public 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 address: https://github.com/java-decompiler/jd-gui
JD-core also provides an official GUI interface, if you need it, you can download it and try it directly.

Jadx

GitHub address: https://github.com/skylot/jadx
Jadx is a decompilation tool that can decompile JAR, APK, DEX, AAR, AAB, ZIP files, and is also equipped with Jadx-gui for interface operation.
Jadx uses Grade for dependency management, and can clone the warehouse and run it by itself.

git clone https://github.com/skylot/jadx.git
cd jadx
./gradlew dist
# 查看帮助
 ./build/jadx/bin/jadx --help
 
jadx - dex to java decompiler, version: dev

usage: 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 help
Example:
  jadx -d out classes.dex

According to the HELP information, if you want to decompiler decompiler.jar to the out folder.

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

Fernflower

GitHub address: https://github.com/fesh0r/fernflower
Like Jadx, Fernflower uses Grade for dependency management, and can clone the warehouse and run it by itself.

➜  fernflower-master ./gradlew build

BUILD SUCCESSFUL in 32s
4 actionable tasks: 4 executed

➜  fernflower-master java -jar build/libs/fernflower.jar
Usage: java -jar fernflower.jar [-<option>=<value>]* [<source>]+ <destination>
Example: java -jar fernflower.jar -dgs=true c:\my\source\ c:\my.jar d:\decompiled\

➜  fernflower-master mkdir out
➜  fernflower-master java -jar build/libs/fernflower.jar ~/Desktop/decompiler.jar ./out
INFO:  Decompiling class com/strobel/assembler/metadata/ArrayTypeLoader
INFO:  ... done
INFO:  Decompiling class com/strobel/assembler/metadata/ParameterDefinition
INFO:  ... done
INFO:  Decompiling class com/strobel/assembler/metadata/MethodHandle
...

➜  fernflower-master ll out
total 1288
-rw-r--r--  1 darcy  staff   595K  5 16 17:47 decompiler.jar
➜  fernflower-master

When Fernflower decompiles the JAR package, the default decompilation result is also a JAR package. Jad

Decompilation speed

So far, five Java decompilation tools have been introduced, so which one should we use in daily development? Or which one should we choose during code analysis? I think the two situations are different, and the focus of use is also different. If it is for daily use, reading the code, I think it should be more demanding on readability, if it is a lot of code analysis work, then the speed of decompilation and syntax support may be higher.
In order to have a simple reference data, I used the JMH microbenchmark test tool to perform simple tests on these five decompilation tools. The following are some test results.

Test environment

Environment variabledescribe
processor2.6 GHz six-core Intel Core i7
RAM16 GB 2667 MHz DDR4
Java versionJDK 14.0.2
test methodJMH benchmark test.
JAR to be decompiled 1procyon-compilertools-0.5.33.jar (1.5 MB)
To be decompiled JAR 2python2java4common-1.0.0-20180706.084921-1.jar (42 MB)

Decompile JAR 1: procyon-compilertools-0.5.33.jar (1.5 MB)

BenchmarkModeCntScoreUnits
cfravgt106548.642 ± 363.502ms/op
fernfloweravgt1012699.147 ± 1081.539ms/op
jdcoreavgt105728.621 ± 310.645ms/op
procyonavgt1026776.125 ± 2651.081ms/op
jadxavgt107059.354 ± 323.351ms/op

Decompile JAR 2: python2java4common-1.0.0-20180706.084921-1.jar (42 MB)

The JAR 2 package is relatively large. It is a combination of many code repositories. At the same time, there are many codes generated from Python to Java. In theory, the complexity of the code will be higher.

BenchmarkCntScore
Cfr1413838.826ms
fernflower1246819.168ms
jdcore1Error
procyon1487647.181ms
jadx1505600.231ms

Syntax support and readability

If you need to look at the decompiled code yourself, then the more readable code is more dominant. Below I have written some codes, mainly Java 8 and below code syntax and some nested flow control, take a look at the reverse What is the effect after compilation.

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!");
        }
    }
}

The decompilation results of all tools were originally posted here, but due to the length of the article and reading experience, they were not released. However, I have complete code for the release of my personal blog. The layout of my personal website is relatively free. You can use the Tab option. The way the card is displayed. If you need to view, you can visit https://www.wdbyte.com to view.

Procyon

Seeing the results of Procyon's decompilation, I was quite surprised. In the case of normal decompilation, the decompiled code is basically original . The only thing that changes the syntax of the source code after decompilation is that the initialization operation of a set is slightly different.

// 源码
 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);
}

Other parts of the code, such as boxing and unboxing, Switch syntax, Lambda expressions, streaming operations and flow control, are almost identical, and there is no obstacle to reading.

The boxing and unboxing operations are completely the same after decompilation, and there is no redundant type conversion code.

// 源码
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);
}

The Switch part is also the same, and the process control part has not changed.

// 源码 switch
public 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 expressions are exactly the same as streaming operations.

// 源码
// 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();
}
// 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();
}

Process control. After decompilation, it is found that meaningless code parts are missing, which is not accessible for reading.

// 源码
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();
        }
    }
}

In view of the length of the code, the comparison of the following decompilation results will only list the differences, and the similarities will be skipped directly.

CFR

The decompilation result of CFR has an extra type conversion part. Personally, it is not as authentic as Procyon, but it is also very good. The only dissatisfaction in the test case is the processing while continue

// CFR 反编译结果
// 装箱拆箱
private void e(Short s) {
   this.b(s.shortValue()); // 装箱拆箱多出了类型转换部分。
   this.c(s.shortValue()); // 装箱拆箱多出了类型转换部分。
   this.e(s);
   this.f(s);
}
// 流程控制
void test1(String path) {
    try {
        int n = 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();
        }
    }
}
// Lambda 和 Stream 操作完全一致,不提。
// switch 处,反编译后功能一致,但是流程控制有所更改。
public static void testSwitch3(int x) {
    block6: while (true) { // 源码中只有 while(true),反编译后多了 block6
        if (x < 5) {
            switch ("test") {
                case "okay": {
                    continue block6; // 多了 block6
                }
            }
            continue;
        }
        System.out.println("wow x2!");
    }
}

JD-Core

JD-Core is the same as CFR. For packing and unpacking operations, it is no longer consistent after decompilation. There is more type conversion part, and the data type is automatically optimized. Personally, if you read it yourself after decompilation, the conversion and optimization of the data type throughout the article will still have a big impact.

// JD-Core 反编译
private void d(Double d) {
  c(d.doubleValue()); // 新增了数据类型转换
  d(d);
}

private void e(Short s) {
  b(s.shortValue()); // 新增了数据类型转换
  c(s.shortValue()); // 新增了数据类型转换
  e(s);
  f(s.shortValue()); // 新增了数据类型转换
}

private void f(short s) {
  b(s);
  c(s);
  e(Short.valueOf(s)); // 新增了数据类型转换
  f(s);
}
// 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.valueOf(x.intValue() + 2)).mapToInt(x -> x.intValue());
  s.toArray();
}

Jadx

First Jadx when decompile test code, reported the error , the result of decompilation also has prompted not decompile Lambda and Stream operation , decompile results variable name disorganized , process control almost killed, If you want to read with the naked eye after decompilation, Jadx is definitely not a good choice.

// Jadx 反编译
private void e(Short s) {
    b(s.shortValue());// 新增了数据类型转换
    c((double) s.shortValue());// 新增了数据类型转换
    e(s);
    f(s.shortValue());// 新增了数据类型转换
}

private void f(short s) {
    b(s);
    c((double) s);// 新增了数据类型转换
    e(Short.valueOf(s));// 新增了数据类型转换
    f(s);
}
public int testLambda() { // testLambda 反编译失败
    /*
        r2 = this;
        r0 = 3
        r1 = move-result
        java.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-result
        java.util.stream.Stream r1 = r1.filter(r2)
        r2 = move-result
        java.util.stream.Stream r1 = r1.map(r2)
        r2 = move-result
        java.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 操作无法正常阅读,和源码出入较大。
    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's decompilation results are generally good, but there are also shortcomings. Its designation of variable names and the decompilation results of Switch strings are not ideal.

//反编译后变量命名不利于阅读,有很多 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 反编译结果使用了hashCode
public 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!");
      }
   }
}

Summarize

Comparing the five decompilation tools, combining the decompilation speed and the code readability test, it seems that the CFR tool wins, and the Procyon follows closely behind. CFR does not fall short in terms of speed, and the readability of the decompiled code is the best. It is mainly reflected in the decompiled variable named , boxing and unboxing , type conversion , process control , Lambda expression, Stream streaming operation and Switch syntax support are very good. According to the official CFR introduction, Java 14 grammar has been supported, and as of the time of writing this test article, the latest CFR code submission time was 11 hours ago, and the update speed is very fast.

Part of the code in the article has been uploaded to GitHub: github.com/niumoo/lab-notes/tree/master/java-decompiler

Final words

<end>

Hello world:) I am Aran, a first-line technical tool person, and write articles seriously.

likes . All of them are talents. Not only are they handsome and good-looking, they are also good-sounding.

The article is continuously updated. You can follow the public Program " or visit " Unread Code Blog ".

Reply [Information] There are various series of knowledge points and must-read books I prepared.

This article Github.com/niumoo/JavaNotes has been included, there are many knowledge points and series of articles, welcome to Star.

等你好久


程序猿阿朗
376 声望1.8k 粉丝

Hello world :)