简体   繁体   English

IntelliJ IDEA - Java 注释

[英]IntelliJ IDEA - Java annotations

I've been using Android Studio (that is based on IntelliJ IDEA), and fine with using annotations such as @NonNull.我一直在使用 Android Studio(基于 IntelliJ IDEA),并且可以很好地使用 @NonNull 等注释。

In addition to Android Studio, I installed IntelliJ IDEA.除了 Android Studio,我还安装了 IntelliJ IDEA。 I found that annotations such as @NonNull are not working.我发现 @NonNull 之类的注释不起作用。 When I build the project, I got the error message:当我构建项目时,我收到错误消息:

java: cannot find symbol
  symbol:   class NonNull
  location: class Main

Could you show me how to enable the standard annotations in IntellJ IDEA?你能告诉我如何在 IntellJ IDEA 中启用标准注释吗?

[Edit 1]: [编辑1]:

In IntelliJ IDEA: Settings -> Build, Execution, Deployment -> Compiler -> Configure annotations -> org.jetbrains.annotations.NotNull already checked.在 IntelliJ IDEA 中:设置 -> 构建、执行、部署 -> 编译器 -> 配置注释 -> org.jetbrains.annotations.NotNull 已选中。

[Edit 2]: [编辑2]:

In Eclipse: I have no problems using the @NonNull annotation.在 Eclipse 中:我使用 @NonNull 注释没有问题。 I ran into the same problem of unresolved annotation symbol in Eclipse at the beginning, however, the quick fix tips popped up on Eclipse (when I pointed the cursor to the unresolved symbol) helped me fix the problem.一开始我在 Eclipse 中遇到了未解决的注释符号的相同问题,但是,Eclipse 上弹出的快速修复提示(当我将光标指向未解决的符号时)帮助我解决了这个问题。

By clicking the quick fixes:通过单击快速修复:

  • Eclipse added "org.eclipse.jdt.annotation_2_2.600.v2020408-1511.jar" under Referenced Libraries. Eclipse 在 Referenced Libraries 下添加了“org.eclipse.jdt.annotation_2_2.600.v2020408-​​1511.jar”。
  • Also, Eclipse added "requires org.eclipse.jdt.annotation;"此外,Eclipse 添加了“需要 org.eclipse.jdt.annotation;” in module-info.java.在模块-info.java 中。

At the time of writing, I haven't been able to get @NonNull working in IntelliJ.在撰写本文时,我无法让 @NonNull 在 IntelliJ 中工作。

I solved the problem.我解决了这个问题。 My issue was about using regular Java annotations in IntelliJ IDEA.我的问题是关于在 IntelliJ IDEA 中使用常规 Java 注释。 Here is the solution.这是解决方案。

  • Project Structure -> Libraries -> the 'plus' button -> From Maven -> org.jetbrains:annotations:16.0.2 -> OK项目结构 -> 库 -> '加号' 按钮 -> 从 Maven -> org.jetbrains:annotations:16.0.2 -> OK
  • In the Java code, use @NotNull (instead of @NonNull)在Java代码中,使用@NotNull(而不是@NonNull)
  • In the Java code, import org.jetbrains.annotations.NotNull;在Java代码中,import org.jetbrains.annotations.NotNull;

Besides, I was asked which JDK version that I used.此外,有人问我使用的是哪个 JDK 版本。 I've been experimenting some latest Java features by using JDK 14.0.2.我一直在使用 JDK 14.0.2 试验一些最新的 Java 特性。

Here is my code example:这是我的代码示例:

Function<String, Boolean> checkFavoriteDrink = (@NotNull var drink) -> {
    return switch (drink) {
        case "mocha", "cappuccino", "iced tea" -> true;
        default -> false;
    };
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM