如何从 java.lang.Class 对象获取源文件名/行号

发布于 2024-12-05 06:18:48 字数 307 浏览 1 评论 0原文

给定一个 java.lang.Class 对象,是否可以获取源文件名和声明该类的行号?

数据应该在 .class 文件的调试信息中可用。据我所知,JDK 返回此类调试信息的唯一位置是 java.lang.StackTraceElement,但我不确定是否可以强制 Java 创建 java.lang。任意类的 StackTraceElement 实例,因为我们没有执行该类中的方法。

我的确切用例是一个匿名内部类,它具有编译器生成的名称。我想知道类声明的文件名和行号。

我不喜欢使用字节码操作框架,但如果必须的话我可以使用它。

Is it possible, given a java.lang.Class object, to get the source file name and the line number at which the class was declared?

The data should be available in the .class file's debug info. The only place I know of, where the JDK returns such debug info is in java.lang.StackTraceElement but I'm not sure if it's possible to force Java to create a java.lang.StackTraceElement instance for an arbitrary class, because we are not executing a method in the class.

My exact use case is an anonymous inner class which has a compiler generated name. I want to know the file name and the line number for the class declaration.

I prefer not to use a byte-code manipulation framework, but I can fall back to it if I have to.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

提笔书几行 2024-12-12 06:18:48

这个问题的答案取决于您对实现监听器的代码有多少控制权。你是对的,如果不在方法中,就不可能创建堆栈跟踪。

一般技术是在构造函数中创建 Exception(),但不要抛出它。其中包含堆栈跟踪信息,您可以根据需要使用这些信息。这将为您提供构造函数的行号,但不是类的行号。请注意,此方法的性能也不是特别好,因为创建堆栈跟踪的成本很高。

您将需要:

  1. 强制在构造函数中执行一些代码(如果您的监听器是您控制的抽象类,则相对容易)
  2. 以某种方式检测代码(这里的治疗方法似乎比疾病更糟糕)。
  3. 对类的命名方式做出一些假设。
  4. 读取 jar (执行与 javac -p 相同的操作)

对于 1),您只需将 Exception 创建放在抽象类中,并且构造函数由子类调用:

class Top {
    Top() {
        new Exception().printStackTrace(System.out);
    }
}

class Bottom extends Top {
    public static void main(String[] args) {
        new Bottom();
    }
}

这会产生类似以下内容:

java.lang.Exception
    at uk.co.farwell.stackoverflow.Top.<init>(Top.java:4)
    at uk.co.farwell.stackoverflow.Bottom.<init>(Bottom.java: 11)
    at uk.co.farwell.stackoverflow.Bottom.main(Bottom.java: 18)

一般来说,有一些命名规则如下: 如果你有一个名为 Actor 的外部类和一​​个名为 Consumer 的内部类,那么编译后的类将被称为 Actor$Consumer。匿名内部类按照它们在文件中出现的顺序命名,因此 Actor$1 在文件中将出现在 Actor$2 之前。我不认为这实际上是在任何地方指定的,所以这可能只是一个约定,如果您正在使用多个 jvm 等做任何复杂的事情,则不应依赖它。

正如 jmg 指出的那样,您可以在同一文件中定义多个顶级类。如果您有一个公共类 Foo,则必须在 Foo.java 中定义它,但非公共类可以包含在另一个文件中。上面的方法就可以解决这个问题。

说明:

如果反汇编java(javap -c -verbose),您会看到调试信息中有行号,但它们仅适用于方法。使用以下内部类:

static class Consumer implements Runnable {
    public void run() {
        // stuff
    }
}

并且 javap 输出包含:

uk.co.farwell.stackoverflow.Actors$Consumer();
  Code:
   Stack=1, Locals=1, Args_size=1
   0:   aload_0
   1:   invokespecial   #10; //Method java/lang/Object."<init>":()V
   4:   return
  LineNumberTable: 
   line 20: 0

  LocalVariableTable: 
   Start  Length  Slot  Name   Signature
   0      5      0    this       Luk/co/farwell/stackoverflow/Actors$Consumer;

LineNumberTable 包含适用于方法的行号列表。因此,我的 Consumer 构造函数从第 20 行开始。但这是构造函数的第一行,而不是类的第一行。它只是同一行,因为我使用的是默认构造函数。如果我添加一个构造函数,那么行号将会改变。编译器不存储声明该类的行。所以如果不解析java本身就无法找到类的声明位置。您根本没有可用的信息。

但是,如果您使用的是匿名内部类,例如:

Runnable run = new Runnable() {
    public void run() {
    }
};

那么构造函数和类的行号将匹配[*],因此这会给您一个行号。

[*] 除非“new”和“Runnable()”位于不同的行。

The answer to this comes down to how much control you have over the code that is implementing the Listener. You are right that it is not possible to create a stacktrace without being in a method.

The general technique is to create an Exception(), in the constructor, but don't throw it. This contains the stacktrace information, which you can use how you want. This will give you the line number of the constructor, but not of the class. Please note that this method is not particularly performant either, because creating a stacktrace is expensive.

You will need to either:

  1. force some code to be executed in the constructor (relatively easy if your Listener is an abstract class which you control)
  2. Instrument the code somehow (the cure seems worse than the disease here).
  3. Make some assumptions about the way classes are named.
  4. Read the jar (do the same thing as javac -p)

For 1), you'd simply put the Exception creation in the abstract class, and the constructor gets called by the subclass:

class Top {
    Top() {
        new Exception().printStackTrace(System.out);
    }
}

class Bottom extends Top {
    public static void main(String[] args) {
        new Bottom();
    }
}

this produces something like:

java.lang.Exception
    at uk.co.farwell.stackoverflow.Top.<init>(Top.java:4)
    at uk.co.farwell.stackoverflow.Bottom.<init>(Bottom.java: 11)
    at uk.co.farwell.stackoverflow.Bottom.main(Bottom.java: 18)

In general, there are some naming rules which are followed: If you have an outer class called Actor and an inner called Consumer, then the compiled class will be called Actor$Consumer. Anonymous inner classes are named in the order in which they appear in the file, so Actor$1 will appear in the file before Actor$2. I don't think this is actually specified anywhere, so this is probably just a convention, and shouldn't be relied upon if you're doing anything sophisticated with multiple jvms etc.

It is possible, as jmg pointed out, that you can define multiple top level classes in the same file. If you have a public class Foo, this must be defined in Foo.java, but a non-public class can be included in another file. The above method will cope with this.

Explanation:

If you disassemble the java (javap -c -verbose), you'll see that there are line numbers in the debug information, but they only apply to methods. Using the following inner class:

static class Consumer implements Runnable {
    public void run() {
        // stuff
    }
}

and the javap output contains:

uk.co.farwell.stackoverflow.Actors$Consumer();
  Code:
   Stack=1, Locals=1, Args_size=1
   0:   aload_0
   1:   invokespecial   #10; //Method java/lang/Object."<init>":()V
   4:   return
  LineNumberTable: 
   line 20: 0

  LocalVariableTable: 
   Start  Length  Slot  Name   Signature
   0      5      0    this       Luk/co/farwell/stackoverflow/Actors$Consumer;

The LineNumberTable contains the list of line numbers which apply to a method. So my constructor for the Consumer starts at line 20. But this is the first line of the constructor, not the first line of the class. It is only the same line because I'm using the default constructor. If I add a constructor, then the line numbers will change. the compiler does not store the line that the class is declared on. So you can't find where the class is declared without parsing the java itself. You simply don't have the information available.

However, if you're using an anonymous inner class such as:

Runnable run = new Runnable() {
    public void run() {
    }
};

Then the line number of the constructor and class will match[*], so this gives you an line number.

[*] Except if the "new" and "Runnable()" are on different lines.

以为你会在 2024-12-12 06:18:48

你可以找到uut当前的代码行:

Throwable t = new Throwable();
System.out.println(t.getStackTrace()[0].getLineNumber());

但看起来StackTraceElements是由Throwable内部的原生JDK方法创建的。

public synchronized native Throwable fillInStackTrace();

事件如果您使用字节码操作框架,向创建 throwable 的类添加方法,您将不会获得正确的类声明代码行。

You can find uut current line of code:

Throwable t = new Throwable();
System.out.println(t.getStackTrace()[0].getLineNumber());

But it looks like StackTraceElements are created by native JDK method inside Throwable.

public synchronized native Throwable fillInStackTrace();

Event if you use byte-code manipulation framework, to add a method to a class that creates throwable, you won't get proper line of code of class declaration.

阿楠 2024-12-12 06:18:48

您可以通过调用 getStackTrace()。因此,对于当前线程,您必须调用 Thread.currentThread().getStackTrace()。

You can get a stack trace from any thread by calling getStackTrace(). Therfore for the current thread you have to call Thread.currentThread().getStackTrace().

娇妻 2024-12-12 06:18:48

出于您的目的,仅为其堆栈跟踪生成异常是正确的答案。

但如果这不起作用,您还可以使用 Apache BCEL 来分析 Java字节码。如果这听起来像是过度杀戮,那么你可能是对的。

public boolean isScala(Class jvmClass) {
   JavaClass bpelClass = Repository.lookupClass(jvmClass.getName());
   return (bpelClass.getFileName().endsWith(".scala");
}

(警告:我尚未测试此代码。)

另一个选择是构建自定义 doclet 以在编译时收集适当的元数据。我过去曾将其用于需要在运行时了解特定超类的所有子类的应用程序。将它们添加到工厂方法中太麻烦了,这也让我可以直接链接到 javadoc。

现在我正在 Scala 中编写新代码,我正在考虑使用上述技术,并通过搜索构建目录来生成类列表。

For your purposes, generating an exception just for its stack trace is the right answer.

But in cases where that doesn't work, you can also use Apache BCEL to analyze Java byte code. If this sounds like heavy-handed overkill, you're probably right.

public boolean isScala(Class jvmClass) {
   JavaClass bpelClass = Repository.lookupClass(jvmClass.getName());
   return (bpelClass.getFileName().endsWith(".scala");
}

(Warning: I haven't tested this code.)

Another option is to build a custom doclet to gather the appropriate metadata at compile time. I've used this in the past for an application which needed to know at runtime all the subclasses of a particular superclass. Adding them to a factory method was too unwieldy, and this also let me link directly to javadoc.

Now that I'm writing new code in Scala, I'm considering using a technique like the above, and generating a list of classes by searching the build directory.

太傻旳人生 2024-12-12 06:18:48

我就是这样做的:

import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * This class is used to determine where an object is instantiated from by extending it. 
 */
public abstract class StackTracer {

    protected StackTracer() {
        System.out.println( shortenedStackTrace( new Exception(), 6 ) );
    }

    public static String shortenedStackTrace(Exception e, int maxLines) {
        StringWriter writer = new StringWriter();
        e.printStackTrace( new PrintWriter(writer) );
        String[] lines = writer.toString().split("\n");
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < Math.min(lines.length, maxLines); i++) {
            sb.append(lines[i]).append("\n");
        }
        return sb.toString();
    }

}

编辑:回顾这一点,我现在可能会使用 AOP 来做到这一点。事实上,对这个项目进行一些细微的更改,我可能可以做到。 这是一个带有提示的堆栈问题

This is how I did it:

import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * This class is used to determine where an object is instantiated from by extending it. 
 */
public abstract class StackTracer {

    protected StackTracer() {
        System.out.println( shortenedStackTrace( new Exception(), 6 ) );
    }

    public static String shortenedStackTrace(Exception e, int maxLines) {
        StringWriter writer = new StringWriter();
        e.printStackTrace( new PrintWriter(writer) );
        String[] lines = writer.toString().split("\n");
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < Math.min(lines.length, maxLines); i++) {
            sb.append(lines[i]).append("\n");
        }
        return sb.toString();
    }

}

EDIT: Looking back on this, I would probably use AOP to do this now. In fact, some minor changes to this project and I could probably do it. Here is a stack question with a hint.

花期渐远 2024-12-12 06:18:48

给定一个 java.lang.Class 实例是否可以获取源文件名和声明该类的行号?

在大多数情况下,源文件与类名密切相关。该文件中只有一行声明了该类。不需要将这种明确的信息编码在调试信息中。

数据应该在 .class 文件的调试信息中可用

为什么?

Is it possible given a java.lang.Class instance to get the source file name and the line number at which the class has been declared?

The source file is in most cases strongly related to the class name. There is only one line in that file in which the class is declared. There is no need for this kind of unambiguous information to be encoded in the debug information.

The data should be available in the .class file 's debug info

Why?

像你 2024-12-12 06:18:48

这个答案不包括行号,但它满足了我的需求,并且应该适用于您拥有的任何源文件。

这是针对具有多个项目的 IDE 根据类文件查找源文件位置的答案。它利用类加载器和您的 java 项目约定来解决这个问题。

您将需要更新源文件夹 (src) 和输出文件夹 (bin) 以符合您的约定。您必须提供自己的实现,

String searchReplace(old, new, inside)

这是代码

public static String sourceFileFromClass(Class<?> clazz) {
    String answer = null;
    try {
        if (clazz.getEnclosingClass() != null) {
            clazz = clazz.getEnclosingClass();
        }
        String simpleName = clazz.getSimpleName();

        // the .class file should exist from where it was loaded!
        URL url = clazz.getResource(simpleName + ".class");
        String sourceFile = searchReplace(".class", ".java", url.toString());
        sourceFile = searchReplace("file:/", "", sourceFile);
        sourceFile = searchReplace("/bin/", "/src/", sourceFile);
        if( new java.io.File(sourceFile).exists() ) {
            answer = sourceFile;
        }
    }
    catch (Exception ex) {
        // ignore
    }

    return answer;
}

This answer does not cover line numbers, but it met my needs and should work for any source file you own.

This is an answer for finding the location of a source file based on a class file for an IDE that has multiple projects. It leverages the class loader and your java project conventions to solve this issue.

You will need to update your source folder (src) and output folder (bin) to match your convention. You will have to supply your own implementation of

String searchReplace(old, new, inside)

Here is the code

public static String sourceFileFromClass(Class<?> clazz) {
    String answer = null;
    try {
        if (clazz.getEnclosingClass() != null) {
            clazz = clazz.getEnclosingClass();
        }
        String simpleName = clazz.getSimpleName();

        // the .class file should exist from where it was loaded!
        URL url = clazz.getResource(simpleName + ".class");
        String sourceFile = searchReplace(".class", ".java", url.toString());
        sourceFile = searchReplace("file:/", "", sourceFile);
        sourceFile = searchReplace("/bin/", "/src/", sourceFile);
        if( new java.io.File(sourceFile).exists() ) {
            answer = sourceFile;
        }
    }
    catch (Exception ex) {
        // ignore
    }

    return answer;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文