Home > Archives > asmtools的简单使用

asmtools的简单使用

Publish:

asmtools的下载地址

1
2
3
4
5
6
7
public class Foo {
        public static void main(String[] args) {
                boolean flag = true;
                if (flag) System.out.println("Hello, Java!");
                if (flag == true) System.out.println("Hello, JVM!");
        }
}
1
2
3
4
5
6
7
8
9
10
baoguo@MacBook-Pro demo3 % javac Foo.java
baoguo@MacBook-Pro demo3 % ls
Foo.class	Foo.java
baoguo@MacBook-Pro demo3 % java Foo
Hello, Java!
Hello, JVM!
baoguo@MacBook-Pro demo3 % java -jar /Users/baoguo/Downloads/asmtools-7.0/lib/asmtools.jar jdis Foo.class > Foo.jasm
baoguo@MacBook-Pro demo3 % ls
Foo.class	Foo.jasm	Foo.java
baoguo@MacBook-Pro demo3 % vim Foo.jasm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
super public class Foo
        version 52:0
{


public Method "<init>":"()V"
        stack 1 locals 1
{
                aload_0;
                invokespecial   Method java/lang/Object."<init>":"()V";
                return;
}

public static Method main:"([Ljava/lang/String;)V"
        stack 2 locals 2
{
                iconst_1;
                istore_1;
                iload_1;
                ifeq    L14;
                getstatic       Field java/lang/System.out:"Ljava/io/PrintStream;";
                ldc     String "Hello, Java!";
                invokevirtual   Method java/io/PrintStream.println:"(Ljava/lang/String;)V";
        L14:    stack_frame_type append;
                locals_map int;
                iload_1;
                iconst_1;
                if_icmpne       L27;
                getstatic       Field java/lang/System.out:"Ljava/io/PrintStream;";
                ldc     String "Hello, JVM!";
                invokevirtual   Method java/io/PrintStream.println:"(Ljava/lang/String;)V";
        L27:    stack_frame_type same;
                return;
}

} // end Class Foo
~

cfr下载地址

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
baoguo@MacBook-Pro demo3 % java -jar cfr-0.152.jar Foo.class
/*
 * Decompiled with CFR 0.152.
 */
public class Foo {
    public static void main(String[] stringArray) {
        boolean bl = true;
        if (bl) {
            System.out.println("Hello, Java!");
        }
        if (bl) {
            System.out.println("Hello, JVM!");
        }
    }
}

使用vim将Method Main中的第一个iconst_1改为iconst_2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
super public class Foo
        version 52:0
{


public Method "<init>":"()V"
        stack 1 locals 1
{
                aload_0;
                invokespecial   Method java/lang/Object."<init>":"()V";
                return;
}

public static Method main:"([Ljava/lang/String;)V"
        stack 2 locals 2
{
                iconst_2;
                istore_1;
                iload_1;
                ifeq    L14;
                getstatic       Field java/lang/System.out:"Ljava/io/PrintStream;";
                ldc     String "Hello, Java!";
                invokevirtual   Method java/io/PrintStream.println:"(Ljava/lang/String;)V";
        L14:    stack_frame_type append;
                locals_map int;
                iload_1;
                iconst_1;
                if_icmpne       L27;
                getstatic       Field java/lang/System.out:"Ljava/io/PrintStream;";
                ldc     String "Hello, JVM!";
                invokevirtual   Method java/io/PrintStream.println:"(Ljava/lang/String;)V";
        L27:    stack_frame_type same;
                return;
}

} // end Class Foo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
baoguo@MacBook-Pro demo3 % mv Foo.class Foo.class.bak
baoguo@MacBook-Pro demo3 % ls
Foo.class.bak	Foo.jasm	Foo.java	cfr-0.152.jar
baoguo@MacBook-Pro demo3 % java -jar /Users/baoguo/Downloads/asmtools-7.0/lib/asmtools.jar jasm Foo.jasm
baoguo@MacBook-Pro demo3 % ls
Foo.class	Foo.class.bak	Foo.jasm	Foo.java	cfr-0.152.jar
baoguo@MacBook-Pro demo3 % java Foo
Hello, Java!
baoguo@MacBook-Pro demo3 % java -jar cfr-0.152.jar Foo.class
/*
 * Decompiled with CFR 0.152.
 */
public class Foo {
    public static void main(String[] stringArray) {
        int n = 2;
        if (n != 0) {
            System.out.println("Hello, Java!");
        }
        if (n == 1) {
            System.out.println("Hello, JVM!");
        }
    }
}

参考

声明: 本文采用 BY-NC-SA 授权。转载请注明转自: Ding Bao Guo