null
analysis is, what it is good for, and why it is necessary to overcome corporate laziness right now and start using this analysis in projects. There was no article containing complete systematized information about the item without reference to the IDE, so I had to write it myself. And although, as a result of complete systematization, it did not work out, I still wanted to share the material with a wider audience. There will be a minimum of text and a lot of images.org.eclipse.jdt.annotation.*
And org.jetbrains.annotations.*
), As well as because they are available at Maven Central. For those who use Maven, just add the following to the <dependencies/>
section: <dependency> <groupId>com.google.code.findbugs</groupId> <artifactId>jsr305</artifactId> <version>3.0.0</version> </dependency>
@Nonnull public String getName() { // ... }
public @Nonnull String getName() { // ... }
@NonNullByDefault
or @ParametersAreNonnullByDefault
only new packages that are created (a huge amount of already written code, including JDK offal, tritely does not meet the requirement of "non-zero parameters" and will lead to compilation errors). Just in case - the annotations on the package as a whole (in the file “package-info.java”) are “hung up” like this: @ParametersAreNonnullByDefault package com.example; import javax.annotation.ParametersAreNonnullByDefault;
field
cannot be null
at the time of the hashCode()
call:@SuppressWarnings("null")
, but this will @SuppressWarnings("null")
all the benefits of null analysis.assert
:@Nonnull
and continue to work with it:@Target
. FindBugs annotations do not meet this requirement. Therefore, if you use Eclipse up to 4.4 inclusive (Luna) and Java 1.8, then null
analysis using FindBugs annotations will not work ( null
analysis, it seems, since the beginning of time (check Constant conditions & exceptions and @ NotNull / @Nullable problems ).@NonNullByDefault
or @ParametersAreNonnullByDefault
) are not supported.@ParametersAreNonnullByDefault
and @ParametersAreNullableByDefault
at the package level are recognized and analyzed, although they do not appear anywhere in the settings (thanks to Borz ). Moreover, the functionality appears to be present in IDEA 13.1.6 (build 135.1306).null
analysis since 7.3, see next. article english .null
should be marked not only as @Nullable
, but also as @CheckForNull
— FindBugs will “calm down” only if it sees @CheckForNull
. public void test() throws Exception { final Calendar calendar = myVeryCleverNonStandardApiCall(); final int year = calendar.get(Calendar.YEAR); assertEquals(1997, year); assertEquals("1.1", System.getProperty("java.specification.version")); }
public void test() throws Exception { final Calendar calendar = myVeryCleverNonStandardApiCall(); final int year = calendar.get(Calendar.YEAR); // assertEquals(1997, year); // assertEquals("1.1", System.getProperty("java.specification.version")); }
public void test() throws Exception { Thread.sleep(1000); } public void _test() throws Exception { final Calendar calendar = null; //myVeryCleverNonStandardApiCall(); final int year = calendar.get(Calendar.YEAR); // assertEquals(1997, year); // assertEquals("1.1", System.getProperty("java.specification.version")); }
Source: https://habr.com/ru/post/204518/
All Articles