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

查看:29
本文介绍了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天全站免登陆