Compile and decompile

Programming languages are divided into high-level and low-level languages. Low-level languages such as machine language and assembly language. Such languages write commands directly from computer instructions without compilation. These languages can be seen and understood by machines, but programmers struggle to read them. C, Java and Python are high-level languages that programmers can read and understand. Machines can’t read it.

To sum it up, a high-level language is a language that programmers know, while a low-level language is a language that machines know. The process of converting a high-level language into a low-level language is compilation, and decompilation is converting a low-level language into a high-level language. With decompression, we can see bytecodes generated by Java compilers, such as the implementation principles of Synchronized (monitor), enums, syntactic sugar, and generics, all of which require decompression tools.

javap

Javap is a decompiler command that comes with the JDK. It can decompiler code, but not Java files.

Use the format

javap <options> <classes>
Copy the code

Common: Javap-c class name

-help --help -? Output this usage message -version version information -V-verbose Output additional information -L Output line number and local variable table -public Displays only public classes and members -protected Displays protected/public classes and members -package Displays package/protected/public classes and members (default) -p -private Displays all classes and members -c disassembles code -S outputs internal type signatures -sysInfo Displays system information (path, size, date, MD5 hash) -constants Displays final constants -classpath <path> Specifies the location where the user class file is to be found -cp <path> Specifies the location where the user class file is to be found -bootclasspath <path> Overwrites the location of the bootclass fileCopy the code

Synchronized code:

public class SynchronizedTest { private int count = 0; public void addOne() { synchronized (SynchronizedTest.class) { count++; }}}Copy the code

Execute compile and decompile commands

javac SynchronizedTest .java
javap -c SynchronizedTest.class
Copy the code

Using Notepad directly to open the synchronizedtest. class file is a bunch of garbled files, whereas using sublime is a string of numbers

cafe babe 0000 0034 0017 0a00 0400 1209
0003 0013 0700 1407 0015 0100 0563 6f75
6e74 0100 0149 0100 063c 696e 6974 3e01
0003 2829 5601 0004 436f 6465 0100 0f4c
696e 654e 756d 6265 7254 6162 6c65 0100
0661 6464 4f6e 6501 000d 5374 6163 6b4d
6170 5461 626c 6507 0014 0700 1507 0016
Copy the code

Decompiled code:

public class com.SynchronizedTest {
  public com.SynchronizedTest();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: aload_0
       5: iconst_0
       6: putfield      #2                  // Field count:I
       9: return

  public void addOne();
    Code:
       0: ldc           #3                  // class com/yyw/oil/web/admin/controller/purchase/SynchronizedTest
       2: dup
       3: astore_1
       4: monitorenter
       5: aload_0
       6: dup
       7: getfield      #2                  // Field count:I
      10: iconst_1
      11: iadd
      12: putfield      #2                  // Field count:I
      15: aload_1
      16: monitorexit
      17: goto          25
      20: astore_2
      21: aload_1
      22: monitorexit
      23: aload_2
      24: athrow
      25: return
    Exception table:
       from    to  target type
           5    17    20   any
          20    23    20   any
}

Copy the code

Instead of decompiling bytecode into A Java file, Javap generates another readable bytecode. You can see that the synchronized modified code contains monitorenter and Monitorexit. The synchronized underlayer relies on two instructions for synchronization, which can be somewhat arcane here.

CFR

To download the jar from the official website, run the following command:

Java - jar CFR 0.151. Jar SynchronizedTest. ClassCopy the code

You get a decompiled Java file:

public class SynchronizedTest { private int count = 0; /* * WARNING - Removed try catching itself - possible behaviour change. */ public void addOne() { Class<SynchronizedTest> clazz = SynchronizedTest.class; synchronized (SynchronizedTest.class) { ++this.count; // ** MonitorExit[var1_1] (shouldn't be in output) return; }}}Copy the code

CFR also takes some parameters:

parameter annotation
–decodeenumswitch (boolean) Remove syntactic sugar from Switch support for enums
–decodelambdas (boolean) Remove syntactic sugar from lambda expressions
–decodestringswitch (boolean) Remove syntactic sugar from Switch String support
You can run the following command to view other parameters:
` ` `
Java jar CFR – 0.151. The jar — help
` ` `
####idea
Use IDEA to generate the class file, use IDEA to open the class file. Idea is the editor used by most Java programmers. It is convenient and fast to open files using IDEA.

reference

Java code compilation and decompiling those things using Javap