我有(在许多其他没有这个问题的 Util 类中)一个 FileUtil 类,它没有通过私有构造函数的测试。
类定义是:
public final class FileUtil {
/**
* Hidden constructor to prevent instantiation.
*/
private FileUtil() {
}
// ...
// many static methods
}
如上所述,我有许多其他类经过测试是否具有私有构造函数。测试方法是:
@Test
public void testPrivateConstructor() throws Exception {
Constructor<FileUtil> constructor = FileUtil.class.getDeclaredConstructor();
Assert.assertTrue("Constructor is not private", Modifier.isPrivate(constructor.getModifiers()));
constructor.setAccessible(true);
constructor.newInstance();
}
但是对于此类 - 并且仅对于此类 - isPrivate() 方法返回 false 并且如果在调试器中检查,则构造函数确实显示为公共。但是,如果尝试以编程方式调用构造函数,Eclipse 会告诉我该方法不可见。
我正在使用 Eclipse 和 Maven,当调用 Maven 构建时,问题显示在 Eclipse 和命令行中。所以这确实是一个 Java 问题,但是当我在整个库中毫无问题地使用这种模式时,我不明白为什么它只在这个类上失败。
有什么想法吗?