在 IntelliJ Idea 中查看使用过的 Java 类的来源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10781135/
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-31 02:33:00  来源:igfitidea点击:

View sources of used Java classes in IntelliJ Idea

javaintellij-idea

提问by Mohamad Ali Honarpisheh

In visual studio development environment when you use a library if you have the related pdb you can dig into their sources during debug. I wonder is there any way to have this feature when you are developing under Java with IntelliJ Idea IDE? I want to see what hibernate do.

在 Visual Studio 开发环境中,当您使用库时,如果您有相关的 pdb,您可以在调试期间深入研究它们的源代码。我想知道当您使用 IntelliJ Idea IDE 在 Java 下进行开发时,有什么方法可以拥有此功能?我想看看 hibernate 做了什么。

采纳答案by Tomasz Nurkiewicz

Of course, quoting the documentation:

当然,引用文档

Libraries may optionally include the source code for the library classes as well as corresponding API documentation. [...] However, it adds the ability to use inline documentation extracted from the source code, and also to view the API documentation right from the IDE.

库可以选择包含库类的源代码以及相应的 API 文档。[...] 但是,它增加了使用从源代码中提取的内联文档的功能,以及直接从 IDE 中查看 API 文档的功能。

The documentation doesn't mention you can then debug external libraries, which is obviously also possible.

文档没有提到您可以调试外部库,这显然也是可能的。

In order to add sources choose:

要添加源,请选择:

  • File | Project Structure
  • Click Libraries tab
  • Pick library and click +icon
  • Find a JAR file containing sources.
  • 档案 | 项目结构
  • 单击库选项卡
  • 选择库并单击+图标
  • 查找包含源的 JAR 文件。

If you are using build tool like maven, IntelliJ will automatically download and attach available source to all libraries, no manual work needed.

如果您使用的是maven 之类的构建工具,IntelliJ 将自动下载可用源并将其附加到所有库中,无需手动操作。

回答by manyways

In my case, I had to add the JDK source files to the project as below.

就我而言,我必须将 JDK 源文件添加到项目中,如下所示。

Go to Project Structure-> SDKs-> Sourcepath

转到项目结构-> SDKs-> 源路径

Use the Plus Sign on the right, and provide the path to your $JAVA_HOME/src.zip file.

使用右侧的加号,并提供 $JAVA_HOME/src.zip 文件的路径。

enter image description here

在此处输入图片说明

回答by tk_

Project Structure| Libraries(or Module Dependenciesif libraries are defined there):

Project Structure| Libraries(如果在那里定义了库,则为模块依赖项):

Project Structure

项目结构

Then in the libries tab click on the plus mark:

然后在库选项卡中单击加号:

Libraries

图书馆

Select the source type:

选择来源类型:

Add source

添加源

Then you have to select the source path and add the wanted libraries finally click apply!

然后你必须选择源路径并添加想要的库,最后点击应用!

回答by Mikita Belahlazau

If you use maven, there is a button "Download sourcec" on maven panel.
If you don't, you need to get sources of hibernate first, search on the internet. And then attach them: File -> Project Structure -> Libraries -> Your hibernate lib -> and attach source

如果您使用 maven,maven 面板上有一个“下载 sourcec”按钮。
如果没有,则需要先获取hibernate的来源,在互联网上搜索。然后附上它们:File -> Project Structure -> Libraries -> Your hibernate lib -> and attach source

回答by parsecer

Go to? Implementation(s)

Go to? Implementation(s)

No one suggested the simple route with a context-menu:

没有人建议使用上下文菜单的简单路线:

  1. import java.util.DesiredClass
  2. select this import statement
  3. right click-> Go to-> Implementation(s)
  1. import java.util.DesiredClass
  2. 选择这个导入语句
  3. 右键单击->转到->实施

Done! The source code of the DesiredClassopens in a new tab.

完毕!的源代码DesiredClass在新选项卡中打开。

This also works with selected class name in the body of your code.

这也适用于代码正文中选定的类名。

screenshot of code editor with selected class name for which a context menu item "Go To" >"Implementation(s)" is chosen

选择了上下文菜单项“转到”>“实现”的具有选定类名的代码编辑器的屏幕截图