Java中的注释会导致编译时传递依赖吗? [英] Do annotations in Java result in compile-time transitive dependencies?

查看:188
本文介绍了Java中的注释会导致编译时传递依赖吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个简单的示例,其中Ent.java使用批注并使用必要的jar依赖项进行编译,然后编译Includer.java,然后导入Ent.java.

A simple example where Ent.java uses annotations and is compiled with the necessary jar dependency after which Includer.java is compiled which in turn imports Ent.java.

Ent.java:

import javax.persistence.Entity;
import javax.persistence.Table;

@Entity
@Table(name="securities")
public class Ent {}

Includer.java:

public class Includer {

    public void f() {
        Ent s = new Ent();
    }
}

使用...进行编译

javac -cp C:/apache-tomcat-7.0.59/lib/javax.persistence_2.1.0.v201304241213.jar Ent.java
javac Includer.java

...在编译Includer.java时导致以下警告:

...results in the following warning when compiling Includer.java:

.\Ent.class: warning: Cannot find annotation method 'name()' in type 'Table': class file for javax.persistence.Table not found

如果我们添加更多注释,当然也会发生同样的情况,但是只有带有参数的注释似乎会导致这种行为.在编译Includer.java时,将jar依赖项从第一次编译添加到类路径中即可解决此问题,但并没有遵循我通常认为的依赖项处理方式.由于我对注解还很陌生,是我们在编译Includer.java时需要将Ent.java的依赖项添加到类路径中的预期行为(可以说添加了依赖项的依赖关系...)还是这可能是某些预期的行为?某种错误或某种其他特殊情况...?此测试使用了编译器版本javac 1.8.0_31.

The same happens of course if we add more annotations but only annotations that take parameters seem to be causing this behaviour. Adding the jar dependency from the first compilation to the classpath when compiling Includer.java solves the problem but doesn't follow how I usually think dependencies are handled. Since I'm quite new to annotations, is it the expected behaviour that we need to add the dependencies of Ent.java to the classpath when compiling Includer.java (adding dependencies of dependencies so to speak...) or is this likely some sort of bug or some other kind of special case...? Compiler version javac 1.8.0_31 was used for this test.

推荐答案

这似乎与有关这个错误:JDK-6550655 :

单独编译依赖于另一个类的类时出现编译器错误,该类已经被编译,并且依赖于来自persistence-api.jar的类

Compiler error when compiling a class alone that depends on another class, which has already been compiled and has a dependency on a class from persistence-api.jar

另请参见相关的错误 JDK-6365854:针对带注释的类进行编译时,javac崩溃.

要回答您的问题,不需要将依赖项类(Ent.java)的依赖项放置到依赖类(Includer.java)的类路径中.但是,似乎javac也会读取依赖项类中的注释.根据所引用的错误,这曾经导致编译失败(com.sun.tools.javac.code.Symbol$CompletionFailure).如评论中所述,此方法已修改为仅发出警告:

To answer your question, it is not required to place dependencies of the dependency class (Ent.java) to the classpath of the depending class (Includer.java). However, it seems javac also reads the annotations in the dependency class. According to the referenced bug, this used to cause a compilation failure (a com.sun.tools.javac.code.Symbol$CompletionFailure). As mentioned in the comments, this is modified to only throw a warning:

修复后,编译器将接受程序但会发出 一个警告.理由是缺少的注释可能会导致 运行程序时出现问题.

After the fix, the compiler will accept the program but issue a warning. The rationale is that the missing annotation can cause problems when running the program.

也来自评论:

编译器不应崩溃,此问题将首先得到解决.

The compiler should not crash and this problem will be addressed first.

尽管希望在不进行编译的情况下继续进行编译 注释,我将不得不研究它是否允许.例如, 编译器无法确定缺少的注释是否具有 元注释@Inherited. 这可能会导致注释处理器出现问题.

While it is desireable to allow the compilation to continue without the annotation, I will have to investigate if it is allowable. For example, the compiler cannot determine if the missing annotation has the meta-annotation @Inherited. This can cause problems for annotation processors.

这篇关于Java中的注释会导致编译时传递依赖吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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