Eclipse type security not valid when returning

Eclipse seems to be doing the wrong analyzes, the test1 method is fine, but the test2 method gives an error:

Null type security: String expression requires raw conversion to match @NonNull

public class TestCase { public Object o; @NonNull public Object test1() { Object local = new Object(); return local; } @NonNull public Object test2() { o = new Object(); return o; } } 
+4
source share
2 answers

I suspect the problem is that you are returning a value that could be changed by another thread. In this case, this method may return a null reference. You can avoid this by using a temporary variable:

 @NonNull public Object test2() { Object tmp = new Object(); o = tmp; return tmp; } 
+4
source

In Eclipse 4.3, you can now use @NonNull for class members, so you can say

  @NonNull public Object o; 

which will stop the warning, but you must be sure that the element is really initialized!

+1
source

Source: https://habr.com/ru/post/1499910/


All Articles