简体   繁体   English

IntelliJ 中的自动静态导入

[英]Auto static import in IntelliJ

In below piece of code I have used Class name to import static methods in IntelliJ IDEA 2022 Community Edition在下面的代码中,我使用类名在 IntelliJ IDEA 2022 社区版中导入静态方法

BDDMockito.given(employeeRepository.findByEmail(employee.getEmail()))
    .willReturn(Optional.empty());
BDDMockito.given(employeeRepository.save(employee)).willReturn(employee);

Is there any way or keyboard shortcut that can remove the class name and use static import like this:是否有任何方法或键盘快捷键可以删除类名并使用静态导入,如下所示:

import static org.mockito.BDDMockito.given;

given(employeeRepository.findByEmail(employee.getEmail()))
    .willReturn(Optional.empty());
given(employeeRepository.save(employee)).willReturn(employee);

I googled lot and read IntelliJ articles but could not find any solution.我搜索了很多并阅读了 IntelliJ 文章,但找不到任何解决方案。 Any IntelliJ expert can help me here?任何 IntelliJ 专家都可以在这里帮助我吗?

您可以将插入符号放在方法名称上,按“Alt+Enter”并选择“为...添加静态导入”

You can configure your IDE to automatically import static functions/vars from specific packages with * .您可以将 IDE 配置为使用*自动从特定包中导入静态函数/变量。 This is the way to do it:这是这样做的方法:

  1. Settings -> Editor -> Code Style -> Java -> Imports (tab)
  2. Add your package name inside在里面添加你的包名在此处输入图像描述

It will result in a * import though and you may not want this effect.虽然它会导致*导入,但您可能不想要这种效果。 But I do not know of any other way.但我不知道任何其他方式。

The good thing about this method is that the functions will be available for auto completion and that makes your life easier :)这种方法的好处是这些功能将可用于自动完成,这让您的生活更轻松:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM