gpt4 book ai didi

java - Java 中的注解会导致编译时传递依赖吗?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:20:58 30 4
gpt4 key购买 nike

一个简单的示例,其中 Ent.java 使用注释并使用必要的 jar 依赖项进行编译,然后编译 Includer.java 进而导入 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 时导致以下警告:

.\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 用于此测试。

最佳答案

这似乎与 this bug: JDK-6550655 有关:

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 crashes when compiling against an annotated class .

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

After the fix, the compiler will accept the program but issuea warning. The rationale is that the missing annotation can causeproblems when running the program.

同样来自评论:

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

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

关于java - Java 中的注解会导致编译时传递依赖吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29260270/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com