gpt4 book ai didi

java - 如何以自动方式获取给定字节码位置的 Java 源代码?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:54:57 25 4
gpt4 key购买 nike

我正在试验一种进行静态分析的工具。该工具适用于字节码而不是源代码。 (不过,我也有源代码)。

该工具从字节码输出一些行号,现在我需要一种简单的方法来映射回源代码。 Netbeans/Eclipse 一直这样做(当我单击包含的库中的方法时,IDE 会将我带到源代码(如果它可用)),所以我知道这是可能的。我只是想不出办法。

例如,考虑以下 hello world 程序:

package mypackage;
import java.io.*;
class MyMainClass {
public static void main(String[] args) {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String name0 = "Alice";
String name1 = "Bob";
try {
name0 = in.readLine();
}
catch(Exception e) {
System.out.println("Caught an exception!");
}
System.out.println("Hello " + name0 + "!");
System.out.println("Hello " + name1 + "!");
}
}

生成的字节码(从javap获得)是:

Compiled from "MyMainClass.java"
class mypackage.MyMainClass extends java.lang.Object{
mypackage.MyMainClass();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."<init>":()V
4: return

public static void main(java.lang.String[]);
Code:
0: new #2; //class java/io/BufferedReader
3: dup
4: new #3; //class java/io/InputStreamReader
7: dup
8: getstatic #4; //Field java/lang/System.in:Ljava/io/InputStream;
11: invokespecial #5; //Method java/io/InputStreamReader."<init>":(Ljava/io/InputStream;)V
14: invokespecial #6; //Method java/io/BufferedReader."<init>":(Ljava/io/Reader;)V
17: astore_1
18: ldc #7; //String Alice
20: astore_2
21: ldc #8; //String Bob
23: astore_3
24: aload_1
25: invokevirtual #9; //Method java/io/BufferedReader.readLine:()Ljava/lang/String;
28: astore_2
29: goto 42
32: astore 4
34: getstatic #11; //Field java/lang/System.out:Ljava/io/PrintStream;
37: ldc #12; //String Caught an exception!
39: invokevirtual #13; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
42: getstatic #11; //Field java/lang/System.out:Ljava/io/PrintStream;
45: new #14; //class java/lang/StringBuilder
48: dup
49: invokespecial #15; //Method java/lang/StringBuilder."<init>":()V
52: ldc #16; //String Hello
54: invokevirtual #17; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
57: aload_2
58: invokevirtual #17; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
61: ldc #18; //String !
63: invokevirtual #17; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
66: invokevirtual #19; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
69: invokevirtual #13; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
72: getstatic #11; //Field java/lang/System.out:Ljava/io/PrintStream;
75: new #14; //class java/lang/StringBuilder
78: dup
79: invokespecial #15; //Method java/lang/StringBuilder."<init>":()V
82: ldc #16; //String Hello
84: invokevirtual #17; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
87: aload_3
88: invokevirtual #17; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
91: ldc #18; //String !
93: invokevirtual #17; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
96: invokevirtual #19; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
99: invokevirtual #13; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
102: return
Exception table:
from to target type
24 29 32 Class java/lang/Exception


}

该工具的输出类似于:

<mypackage.MyMainClass> 39, 69, 99

这些对应于字节码行号。手动我可以弄清楚这些行必须对应于源代码中的以下行:

System.out.println("Caught an exception!"); 
System.out.println("Hello " + name0 + "!");
System.out.println("Hello " + name1 + "!");

但是,我需要自动执行此过程。任何帮助,将不胜感激。

最佳答案

如果您可以访问源文件和相应的行号,任务应该很简单,只需按行加载文件,然后只需选择与该行号对应的行即可。

您的方法的问题在于它依赖于根据 class file format 存储在已编译代码中的可选元数据, 形式为 attributes .有问题的两个,即SourceFileLineNumberTable都是可选,这意味着不能保证它们会出现在您的代码中。验证您正在分析的类文件是否已编译为包含此信息!

注意:这些相同的属性用于通过 Throwable.getStackTrace 提供堆栈跟踪信息。 ,如果您想知道的话。

关于java - 如何以自动方式获取给定字节码位置的 Java 源代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10680898/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com