- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
这是 Eclipse 中的错误还是为什么我不能使用 @NonNull
注释具有完全限定类型名称 (FQN) 的参数?
import org.eclipse.jdt.annotation.NonNull;
public class Foo {
// No problem
public void bar(@NonNull String x) {
}
// Error: Type annotations are not allowed on type names used to access
// static members
public void baz(@NonNull java.lang.String x) {
}
}
最佳答案
这不是 Eclipse 中的错误。相反,它与 Java 编译器处理注释类型的方式有关。下面给出的解释说明了这个问题。
首先,@NonNull
注解可以应用于类型的任何使用(它被注解为@Target(ElementType.TYPE_USE)
)。但是,根据 Java 语言规范,它适用的类型是注释最接近的类型。这并不总是预期的:
在第一种情况下,@NonNull
注释适用于 String
类型的使用,这是一个有效类型。因此,注释是有效的。
然而,在第二种情况下,注释适用于 java
,这是一个包名。因此,它不是有效的类型,因此注释不能在此处应用。这将导致编译时错误。
Java 语言规范(第 9.7.4 段)指出:
For example, assume an annotation type TA which is meta-annotated with just
@Target(ElementType.TYPE_USE)
. The terms@TA java.lang.Object
andjava.@TA lang.Object
are illegal because the simple name to which@TA
is closest is classified as a package name. On the other hand,java.lang.@TA
Object is legal.
由此我们可以得出结论,@NonNull java.lang.String x
将不起作用,但是java.lang.@NonNull String x
会。
关于java - 为什么不允许在 FQN 上使用 @NonNull 注解?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29097160/
我正在使用 IntelliJ 13.1.5 和 Java 8 SDK,我已经设置了 IntelliJ 内置的空性检查器以使用 checkers.nullness.quals.NonNull 作为我选择
Lombok's @NonNull VS javax.annotation.Nonnull 哪个更适合用于方法参数以及何时使用? The answer here模糊地比较了所有注释,但无法推断出哪一个
这两个注解有什么区别? org.jetbrains.annotations.NonNull android.support.annotation.NonNull kotlin 编译器 IntelliJ
这是我的数据代码: import androidx.room.ColumnInfo; import androidx.room.Entity; import androidx.room.Primary
Java 允许将参数和返回标记为 @Nonnull。 对我来说,这感觉是一种不好的做法,我不想在 99% 的用例中告诉它它不应该为 null 并用无用的噪音污染代码。 相反,我更愿意告诉它 - 给定包
我要安装Pushe plugin对于 flutter ,但我遇到了这个错误: 我什至将这一行添加到 native 导入中: 导入 android.support.annotation.NonNull;
我想我会更深入地研究旧的 Java 功能,例如注释。我遇到了 Java 8 类型注释,它使我们能够扩展 Java 类型系统。IntelliJ 注释库或 Checkerframework 中最常见的注释
我的 Controller 中有以下方法: @PostMapping("/register") public String registerNewUser(@Valid User user,
我是 @Nonnull 注释的新手。 我尝试使用它并编写了以下测试: public class TestNonull{ public static void doSmth(@Nonnull I
我不明白 NonNull 注释应该如何提供帮助。所以,假设我有这个: void noNullArg(@NonNull Object o) { // stuff } 如果我这样做,我会收到一条关
我的类的一个 String 成员变量是 Optional。用 Lombok 注释 @NonNull 标记它是否有意义? 如果成员变量是Map而不是String,我想问同样的问题。答案会改变吗? 最佳答
我正在使用 org.eclipse.jdt.annotation.NonNull 为静态空值分析添加额外信息。我不知道如何正确注释数组: 我怎么能说数组引用是非空的? 我怎么能说数组由非空元素组成?
虽然我认为应该有一个关于是否继承注释的通用规则,但我特别感兴趣的是让 FindBugs 识别我的规则,所以这个问题是 FindBugs 特定的。 据我所知,JavaDoc 注释取自接口(interfa
我的问题是 this one 的后续问题。 . 在过去的 FindBugs 版本中,可以使用 @DefaultAnnotation(Nonnull.class) 或 @DefaultAnnotatio
@nonnull inflater 错误,谁能解决这个错误?我是 android 的新手,我不知道如何解决这个错误的任何解决方案?是主题错误还是其他原因,我的代码有什么大问题吗??你能解决这个错误吗?
这个问题在这里已经有了答案: Non-nullable string type, how to use with Asp.Net Core options (1 个回答) 2年前关闭。 我有一个在 E
我知道使用方法nonnull和 nullable是 nonnull: the value won’t be nil; bridges to a regular reference. nullable:
我在这段代码上遇到了这个错误。 You must annotate primary keys with @NonNull. "uidString" is nullable.当我用@NonNull 和@
这个问题在这里已经有了答案: Meaning of Android Studio error: Not annotated parameter overrides @NonNull paramete
我想在我的应用程序中使用 FindBugs 来检测空指针故障,我关心的是代码 public class Test { public static void main(String[] args
我是一名优秀的程序员,十分优秀!