为什么我得到不同的编译结果依赖于java导入和静态导入序列顺序? [英] Why do I get different compilation result depending on java imports and static imports sequence order?

查看:172
本文介绍了为什么我得到不同的编译结果依赖于java导入和静态导入序列顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到编译的问题,但不明白为什么会发生。
实际上花费了很多时间来理解原因是什么(这在垃圾项目中是显而易见的),但是在再现了错误之后,我大大简化了所有的代码,显示一个特别是你的示例: / p>

包结构:



 公司
|
---- Main.class
|
---- maker
|
---- Maker.class

Maker.class

  package com.company.maker; 

public interface Maker {
}

.class

  package com.company; 

import static com.company.Main.MakerImpl.Strategy.STRATEGY1;
import static com.company.Main.MakerImpl.Strategy.STRATEGY2;
import com.company.maker.Maker;

public class Main {

public static void main(String [] args){
System.out.println(STRATEGY1.name()+ STRATEGY2.name ());
}

静态类MakerImpl实现Maker {
enum策略{
STRATEGY1,STRATEGY2
}
}
}

我在Main类中遇到编译错误:



错误:(15,39)java:找不到符号
symbol:class Maker
location:class com.company.Main



如果我从

 更改导入顺序import static com.company.Main.MakerImpl.Strategy.STRATEGY1 ; 
import static com.company.Main.MakerImpl.Strategy.STRATEGY2;
- > import com.company.maker.Maker;

   - > import com.company.maker.Maker; 
import static com.company.Main.MakerImpl.Strategy.STRATEGY1;
import static com.company.Main.MakerImpl.Strategy.STRATEGY2;

然后编译成功。



是Java编译器的正常行为吗?如果是这样,我想清楚了解为什么会发生。



PS 使用java版本1.8.0_112和1.7.0_80(MacOS) p>

解决方案

检查:



http://bugs.java.com/bugdatabase/view_bug.do?bug_id=6391197



看起来编译器看到第一个静态导入,然后跳转来处理你的内部类,但失败,因为它没有读取最后的非静态导入。



所以当你改变导入顺序时,这个问题不会发生,因为当编译器读取静态导入和跳转来处理内部类,因为这些导入在内部类中使用,编译器很高兴,因为它已经导入非静态Maker接口。



这意味着导入是急切评估。



我希望这有助于。


I've faced an issue with compilation, but cannot understand why it occurs. Actually much time was spent to understand where the reason is (it was far from obvious in a "crap" project), but after reproducing that error I greatly simplifies all the code to show a little example especially for you:

Package structure:

com.company
|
----Main.class
|
----maker
    |
    ----Maker.class

Maker.class

package com.company.maker;

public interface Maker {
}

Main.class

package com.company;

import static com.company.Main.MakerImpl.Strategy.STRATEGY1;
import static com.company.Main.MakerImpl.Strategy.STRATEGY2;
import com.company.maker.Maker;

public class Main {

    public static void main(String[] args) {
        System.out.println(STRATEGY1.name() + STRATEGY2.name());
    }

    static class MakerImpl implements Maker {
        enum Strategy {
            STRATEGY1, STRATEGY2
        }
    }
}

And I got compilation error in Main class:

Error:(15, 39) java: cannot find symbol symbol: class Maker location: class com.company.Main

And if I change the import sequence from

import static com.company.Main.MakerImpl.Strategy.STRATEGY1;
import static com.company.Main.MakerImpl.Strategy.STRATEGY2;
->import com.company.maker.Maker;

to

->import com.company.maker.Maker;
import static com.company.Main.MakerImpl.Strategy.STRATEGY1;
import static com.company.Main.MakerImpl.Strategy.STRATEGY2;

then it is compiled successfully.

Is it normal behaviour of Java Compiler? If so I want to clearly understand why it happens.

P.S. tested using java version 1.8.0_112 and 1.7.0_80 (MacOS)

解决方案

check this :

http://bugs.java.com/bugdatabase/view_bug.do?bug_id=6391197

It seems that the compiler sees the first static import and then jumps to take care of your inner class, but fails because it did not read the last non static import.

So when you change the import order, this problem does not occur, since when the compiler reads the static import and jumps to take care of the inner class because those imports are used in the inner class, the compiler is happy since it already imported the non static Maker Interface.

Wich means that the imports are evaluated eagerly.

I hope this helps.

这篇关于为什么我得到不同的编译结果依赖于java导入和静态导入序列顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆