有没有办法自动转换这个静态方法调用(Arrays.asList):
import java.util.Arrays;
import java.util.List;
public class StaticImport {
public static void main(String[] args) {
List<String> list = Arrays.asList("hello", "world");
System.out.println(list);
}
}
Run Code Online (Sandbox Code Playgroud)
使用以下命令进行此调用static import:
import static java.util.Arrays.asList;
import java.util.List;
public class StaticImport {
public static void main(String[] args) {
List<String> list = asList("hello", "world");
System.out.println(list);
}
}
Run Code Online (Sandbox Code Playgroud)
我知道我可以使用此配置代码完成Window » Preferences » Java » Editor » Content Assist » Favorites,如本答案中所述.
我的问题是关于转换现有的静态方法调用.理想情况下,我不想配置"最喜欢的导入".
eclipse和Java的长期用户.我在Eclipse中发现的一个问题是,似乎没有简单的方法来导入静态成员和方法.
即,fail()来自的jUnit 方法org.junit.Assert
我每天创建几个类,并手动添加
import static org.junit.Assert.fail;
Run Code Online (Sandbox Code Playgroud)
到import语句.这很烦人.我绝对喜欢使用Ctrl+ Shift+ O来组织我的导入,但它仍然找不到static成员和方法.
此外,导入不会出现在eclipse中.

有趣的是,我以前看到它的工作原理,但我不记得变量.
有没有人知道我需要做什么来确保始终识别这个静态导入并且可以使用Ctrl+ Shift+ 找到O?
谢谢@qqilihq.
接受的答案不适用于我在eclipse中首选的Organize Imports键盘快捷键,但适用于"悬停"建议.
我有一些日食问题.
如果我anyMap()在源代码中使用类似的东西,然后按,则CTRL + SHIFT + O导入列表中不会进行任何更新.
如果我import static org.mockito.Matchers.*在我的导入中写出类似的东西,anyMap()那么就知道了.
如果按,然后CTRL + SHIFT + O因为我必须导入一些其他类,import static org.mockito.Matchers.*将被替换为import static org.mockito.Matchers.anyMap
如果我想使用那么anyList(),我必须import static org.mockito.Matchers.*再次手工编写导入.
我知道这个主题可以修改Eclipse组织导入(ctrl + shift + o)命令对静态导入的处理吗?并且解决方案工作正常,但是还有可能因此eclipse自动知道我想要使用的静态类吗?
我经常需要一个可以表示为简单枚举类型的类的变量的情况,例如:
private enum PageOrder {DOWN_THEN_OVER, OVER_THEN_DOWN};
Run Code Online (Sandbox Code Playgroud)
如果在包含变量的类中声明枚举类型,那么我必须使用不合适的限定名称MyClass.PageOrder.但是如果我创建一个新类,我只有一个简单的枚举声明类,这对我来说似乎有些过分.
出于这个原因,我经常使用整数而不是枚举类型.
有关此主题的任何意见/建议吗?
有没有办法在Eclipse中自定义默认导入?
例如,如果我默认打开一个新的JUnit测试类,我会得到这些导入:
import static org.junit.Assert.*;
import org.junit.Test;
Run Code Online (Sandbox Code Playgroud)
我想得到什么:
import static org.junit.Assert.*;
import org.junit.Test;
import static org.hamcrest.Matchers.*;
Run Code Online (Sandbox Code Playgroud) Eclipse可以使用"on-save"操作添加明确的类,但它不会解析静态函数.我并不总是使用自动完成功能,并且返回触发它是很麻烦的.
例如,我经常写代码
printDebug("my value", my_obj);
Run Code Online (Sandbox Code Playgroud)
我想让它自动添加
import static util.DebugOut.printDebug;
Run Code Online (Sandbox Code Playgroud)
注意:重申一下,我不是在寻找(a)需要的任何东西ctrl+space,(b)自动导入课程
我的代码包含以下方式对枚举的引用.
Flowers { ROSE, SUNFLOWER }
import com.mycompany.Flowers;
class A {
public void foo(...) {
Flowers flower = Flowers.ROSE;
}
}
Run Code Online (Sandbox Code Playgroud)
我想上面的代码使用对Flower的静态引用,然后代码就像
import static com.mycompany.Flowers.ROSE;
Flowers flower = ROSE;
Run Code Online (Sandbox Code Playgroud)
如何重新使用我的代码(使用Eclipse)来使用枚举的静态引用而不是正常的引用机制.有没有办法告诉Eclipse修改静态引用的所有常规枚举引用?
这篇博客描述了 Spring Boot 1.4 中的一些测试改进。不幸的是,似乎缺少一些重要信息。什么静态导入需要使用的方法get(),status()并content()从下面的例子吗?
@RunWith(SpringRunner.class)
@WebMvcTest(UserVehicleController.class)
public class UserVehicleControllerTests {
@Autowired
private MockMvc mvc;
@MockBean
private UserVehicleService userVehicleService;
@Test
public void getVehicleShouldReturnMakeAndModel() {
given(this.userVehicleService.getVehicleDetails("sboot"))
.willReturn(new VehicleDetails("Honda", "Civic"));
this.mvc.perform(get("/sboot/vehicle")
.accept(MediaType.TEXT_PLAIN))
.andExpect(status().isOk())
.andExpect(content().string("Honda Civic"));
}
}
Run Code Online (Sandbox Code Playgroud) eclipse ×6
java ×4
eclipse-jdt ×1
enums ×1
import ×1
junit ×1
spring ×1
spring-boot ×1
spring-mvc ×1
testing ×1
unit-testing ×1