简体   繁体   English

使用Preconditions.checkNotNull()时,如何避免“潜在的空指针访问”?

[英]How do I avoid “potential null pointer access” when using Preconditions.checkNotNull()?

Eclipse gives me the warning "Potential null pointer access: The variable ann may be null at this location": Eclipse给我警告“潜在空指针访问:变量ann在此位置可能为null”:

SomeAnnotation ann = type.getAnnotation( SomeAnnotation.class );
Preconditions.checkNotNull( ann, "Missing annotation on %s", type );

for( String value : ann.value() ) { // <-- warning happens here
}

I'm using Eclipse 3.7 and Guava . 我正在使用Eclipse 3.7和Guava Is there a way to get rid of this warning? 有没有办法摆脱这个警告?

I could use SuppressWarnings("null") but I would have to attach it to the method which I feel would be a bad idea. 我可以使用SuppressWarnings("null")但我必须将它附加到我认为不太好的方法。

Eclipse e4 has much better support for null checks and resource tracking in the compiler. Eclipse e4对编译器中的空检查和资源跟踪有更好的支持。

Another solution is writing your own version of checkNotNull like so: 另一种解决方案是编写自己的checkNotNull版本,如下所示:

@Nonnull
public static <T> T checkNotNull(@Nullable T reference) {
  if (reference == null) {
    throw new NullPointerException();
  }
  return reference;   
}

Now you can use this approach: 现在你可以使用这种方法:

SomeAnnotation ann = Preconditions.checkNotNull( type.getAnnotation( SomeAnnotation.class ) );

(I've omitted the version of checkNotNull() which take error messages; they work in the same way). (我省略了checkNotNull()错误消息的checkNotNull()版本;它们以相同的方式工作)。

I'm wondering why Guava doesn't do that since they already use these annotation elsewhere. 我想知道为什么Guava没有这样做,因为他们已经在其他地方使用了这些注释。

You could stop using Eclipse. 你可以停止使用Eclipse。

Hey, it's a solution. 嘿,这是一个解决方案。 It may not be the one you want, but it solves the problem and isn't really a bad solution. 它可能不是你想要的那个,但它解决了问题,并不是一个真正糟糕的解决方案。

More seriously, you could: 更严重的是,您可以:

  • adjust compiler warning settings, 调整编译器警告设置,
  • use SuppressWarnings at method or class level, 在方法或类级别使用SuppressWarnings
  • use a more modern compiler (apparently later versions do not trigger this for simple cases), 使用更现代的编译器(显然更新的版本不会为简单的情况触发),
  • rewrite your code to work and assign the return value of checkNotNull to ann . 重写您的代码,并将checkNotNull的返回值赋给ann

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

相关问题 同时使用@Nonnull和Preconditions.checkNotNull(…) - Using both @Nonnull and Preconditions.checkNotNull(…) java.util.Objects.requireNonNull vs Preconditions.checkNotNull - java.util.Objects.requireNonNull vs Preconditions.checkNotNull 避免“可能的空指针访问” - avoiding “Potential null pointer access” 当检查在方法内部时,如何避免空指针访问警告? - How can I avoid null pointer access warning when the check is inside a method? 潜在的空指针访问:在此位置,变量流可能为空 - Potential null pointer access: The variable stream may be null at this location Java blobstore 方法 completeMultipartUpload 抛出 null checkNotNull 指针异常 - Java blobstore method completeMultipartUpload throw null pointer exception for checkNotNull 为什么编译器会指出“可能的空指针访问”警告? - Why the compiler points this “Potential null pointer access” warning? Eclipse 中潜在的空指针访问 - 看似微不足道的例子 - Potential null pointer access in Eclipse - seemingly trivial example 在检查潜在的空指针时如何配置Eclipse(Java)以识别自定义断言 - How to configure Eclipse (Java) to recognize custom assertion when checking against potential null pointer 如何避免空指针错误 - How to avoid null pointer error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM