BAEL-3461 NullAwayExample
This commit is contained in:
parent
a2488e4943
commit
2aff1b5c77
|
@ -435,7 +435,23 @@
|
||||||
<artifactId>reflections</artifactId>
|
<artifactId>reflections</artifactId>
|
||||||
<version>${reflections.version}</version>
|
<version>${reflections.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.uber.nullaway</groupId>
|
||||||
|
<artifactId>nullaway</artifactId>
|
||||||
|
<version>0.3.0</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.codehaus.plexus</groupId>
|
||||||
|
<artifactId>plexus-compiler-javac-errorprone</artifactId>
|
||||||
|
<version>2.8</version>
|
||||||
|
</dependency>
|
||||||
|
<!-- override plexus-compiler-javac-errorprone's dependency on
|
||||||
|
Error Prone with the latest version -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.google.errorprone</groupId>
|
||||||
|
<artifactId>error_prone_core</artifactId>
|
||||||
|
<version>2.1.3</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<repositories>
|
<repositories>
|
||||||
|
@ -552,6 +568,47 @@
|
||||||
</execution>
|
</execution>
|
||||||
</executions>
|
</executions>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>3.5</version>
|
||||||
|
<configuration>
|
||||||
|
<compilerId>javac-with-errorprone</compilerId>
|
||||||
|
<forceJavacCompilerUse>true</forceJavacCompilerUse>
|
||||||
|
<source>1.8</source>
|
||||||
|
<target>1.8</target>
|
||||||
|
<showWarnings>true</showWarnings>
|
||||||
|
<annotationProcessorPaths>
|
||||||
|
<path>
|
||||||
|
<groupId>com.uber.nullaway</groupId>
|
||||||
|
<artifactId>nullaway</artifactId>
|
||||||
|
<version>0.3.0</version>
|
||||||
|
</path>
|
||||||
|
</annotationProcessorPaths>
|
||||||
|
<compilerArgs>
|
||||||
|
<!-- NullAway will warn by default, uncomment the next line to make the build fail -->
|
||||||
|
<!-- <arg>-Xep:NullAway:ERROR</arg> -->
|
||||||
|
<arg>-XepExcludedPaths:(.*)/test/.*|(.*)/streamex/.*</arg>
|
||||||
|
<arg>-XepOpt:NullAway:AnnotatedPackages=com.baeldung.nullaway</arg>
|
||||||
|
</compilerArgs>
|
||||||
|
</configuration>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.codehaus.plexus</groupId>
|
||||||
|
<artifactId>plexus-compiler-javac-errorprone</artifactId>
|
||||||
|
<version>2.8</version>
|
||||||
|
</dependency>
|
||||||
|
<!-- override plexus-compiler-javac-errorprone's dependency on
|
||||||
|
Error Prone with the latest version -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.google.errorprone</groupId>
|
||||||
|
<artifactId>error_prone_core</artifactId>
|
||||||
|
<version>2.1.3</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</plugin>
|
||||||
|
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,24 @@
|
||||||
|
package com.baeldung.nullaway;
|
||||||
|
|
||||||
|
import com.baeldung.distinct.Person;
|
||||||
|
|
||||||
|
public class NullAwayExample {
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 1- NullAway will warn about yearsToRetirement method
|
||||||
|
* 2- Uncomment @Nullable in printAge and NullAway will warn about this method
|
||||||
|
* 3- Add a standard null check to be NullAway compliant
|
||||||
|
* 4- Build will be SUCCESS
|
||||||
|
*/
|
||||||
|
|
||||||
|
static Integer getAge(/*@Nullable*/ Person person) {
|
||||||
|
return person.getAge();
|
||||||
|
}
|
||||||
|
|
||||||
|
Integer yearsToRetirement() {
|
||||||
|
Person p = null;
|
||||||
|
// ... p never gets set correctly...
|
||||||
|
return 65 - getAge(p);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue