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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7483421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 20:10:53  来源:igfitidea点击:

How to get source file-name/line-number from a java.lang.Class object

javareflectiondebug-symbols

提问by Saintali

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

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

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

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

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.

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

采纳答案by Matthew Farwell

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.

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

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)
  1. 强制在构造函数中执行一些代码(如果您的 Listener 是您控制的抽象类,则相对容易)
  2. 以某种方式检测代码(这里的治疗似乎比疾病更糟糕)。
  3. 对类的命名方式做一些假设。
  4. 读取 jar(与 javac -p 做同样的事情)

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

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

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.

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

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.

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

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:

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

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

and the javap output contains:

并且 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;

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.

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

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.

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

回答by Piotr Gwiazda

You can find uut current line of code:

您可以找到 uut 当前代码行:

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

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

但它看起来像是StackTraceElements由本地 JDK 方法创建的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.

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

回答by David Leppik

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 BCELto analyze Java byte code. If this sounds like heavy-handed overkill, you're probably right.

但如果这不起作用,您也可以使用Apache BCEL来分析 Java 字节码。如果这听起来像是粗暴的矫枉过正,那么您可能是对的。

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.

另一种选择是构建自定义 doclet 以在编译时收集适当的元数据。我过去曾将它用于需要在运行时知道特定超类的所有子类的应用程序。将它们添加到工厂方法太笨拙了,这也让我可以直接链接到 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.

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

回答by djangofan

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 projectand I could probably do it. Here is a stack questionwith a hint.

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

回答by Robert

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

您可以通过调用getStackTrace()从任何线程获取堆栈跟踪。因此,对于当前线程,您必须调用Thread.currentThread().getStackTrace().

回答by user207421

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?

是否可以通过 java.lang.Class 实例获取源文件名和声明类的行号?

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

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

Why?

为什么?

回答by englebart

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.

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

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

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

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;
}