Merge pull request #8841 from dcalap/master

BAEL-3461 NullAwayExample
This commit is contained in:
Greg 2020-03-11 10:44:38 -04:00 committed by GitHub
commit bcef7cb251
2 changed files with 82 additions and 1 deletions

View File

@ -435,7 +435,23 @@
<artifactId>reflections</artifactId>
<version>${reflections.version}</version>
</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>
<repositories>
@ -552,6 +568,47 @@
</execution>
</executions>
</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>
</build>

View File

@ -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);
}
}