导入顺序对于成功编译似乎很重要

导入顺序对于成功编译似乎很重要

本文介绍了导入顺序对于成功编译似乎很重要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Eclipse中的某些Java类编写单元测试。我总是让Eclipse自动处理我的导入,并根据任何默认方案对其进行排序。

I'm writing a unit test for some Java class in Eclipse. I always let Eclipse handle my imports automatically, and it orders them according to whatever default scheme.

现在,我遇到以下情况。单元测试可以在Eclipse中构建并正常运行。但是,当我在命令行(使用一些Ant目标)上构建测试时,javac抱怨找不到我导入的类之一。由于我正在使用缓存进行测试,因此测试文件的相关位(比如说Test.java)是:

Now, I have the following situation. The unit test builds and runs just fine in Eclipse. However, when I build the tests on the command line (using some Ant targets), javac complains that it can't find one of the classes I've imported. Since I'm testing something with caching, the relevant bits of the test file (Test.java let's say) are:

import com.abc.my.stuff.MyCacheLoader.Operation;
import com.google.common.cache.CacheLoader;

public class Test {
 ... test code, such as ...

     List<MyKey> recordedKeys = dummyLoader.getKeysFor(Operation.LoadAll);

}

// Dummy cache loader which just records the keys
class MyCacheLoader extends CacheLoader<MyKey, MyValue> {
   enum Operation { Load, LoadAll }

   @Override
   public MyValue load(MyKey key) throws Exception { .... whatever .... }

   @Override
   public MyValue loadAll(Iterable<MyKey> key) throws Exception { .... whatever .... }

   public List<MyKey> getKeysFor(Operation op) { ... impl ... }
}

本地类的内部枚举的导入出现在Guava CacheLoader类的导入之前,因为它们是按字母顺序排序的。当我尝试在命令行上使用Ant进行编译时,javac找不到 CacheLoader。当我切换导入顺序时,构建成功(而Eclipse不在乎)。

Notice that the import for my local class's inner enum appears before the import for the Guava CacheLoader class, because they are ordered alphabetically. When I try to compile this using Ant on the command line, javac can't find 'CacheLoader'. When I switch the order of the imports, the build succeeds (whereas Eclipse doesn't care either way).

我对这里发生的事情感到迷惑。 / p>

I'm mystified as to what's going on here.

推荐答案

显然,Eclipse中的编译器没有以下Sun bug:

Apparently, the compiler in Eclipse doesn't have the following Sun bug:

我通过以下方式找到:

这解释了为什么翻转导入顺序对Eclipse没有影响,但确实如此Sun编译器。问题的一部分已在Java 8中修复,但我的特殊表现形式是针对Java 9的修复:

This explains why flipping the order of imports makes no difference to Eclipse, but it does to the Sun compiler. Part of the problem is fixed in Java 8, but my particular manifestation is targeted for a fix in Java 9: http://bugs.java.com/bugdatabase/view_bug.do?bug_id=7101822

这篇关于导入顺序对于成功编译似乎很重要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 18:26