Skip to content

Commit

Permalink
Configure Google Error Prone static code analysis tool
Browse files Browse the repository at this point in the history
- for more info see https://errorprone.info/

- Configure Lombok to add @javax.annotation.Generated("lombok")
  on all fields, methods, and types that are generated.
  This is required for static code analysis tools like Error Prone

- add a separate maven profile for running errorprone analysis
  it is not active by default

- example usage:
  JDK8
  "mvn -Perrorprone,errorprone-jdk8,core-modules compile"
  JDK11+
  "mvn -Perrorprone,errorprone-jdk11,core-modules compile"
  - usability is better when used together with IntelliJ
    since one can click on the error message to navigate
    to the code location

- also add configuration for Error Prone SLF4J plugin
  https://github.com/KengoTODA/errorprone-slf4j
  which helps detect misusage of SLF4J API
  • Loading branch information
lhotari committed Dec 9, 2020
1 parent 6987d91 commit 17713cf
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 1 deletion.
24 changes: 24 additions & 0 deletions lombok.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#

config.stopBubbling = true
# add @javax.annotation.Generated("lombok") on all fields, methods, and types that are generated
# this is required for static code analysis tools like Error Prone
lombok.addJavaxGeneratedAnnotation = true

112 changes: 112 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,10 @@ flexible messaging model and an intuitive client API.</description>
<jacoco-maven-plugin.version>0.8.3</jacoco-maven-plugin.version>
<spotbugs-maven-plugin.version>4.1.3</spotbugs-maven-plugin.version>
<spotbugs.version>4.2.0</spotbugs.version>
<errorprone.version>2.4.0</errorprone.version>
<errorprone.javac.version>9+181-r4173-1</errorprone.javac.version>
<errorprone-slf4j.version>0.1.4</errorprone-slf4j.version>


<!-- Used to configure rename.netty.native. Libs -->
<rename.netty.native.libs>rename-netty-native-libs.sh</rename.netty.native.libs>
Expand Down Expand Up @@ -1051,6 +1055,13 @@ flexible messaging model and an intuitive client API.</description>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<!-- required on JDK9+ when there is "lombok.addJavaxGeneratedAnnotation = true" in lombok.config -->
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>${javax.annotation-api.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<!-- We use MockedBookKeeper in many unit tests -->
Expand Down Expand Up @@ -1105,6 +1116,13 @@ flexible messaging model and an intuitive client API.</description>
<optimize>true</optimize>
<!-- workaround https://issues.apache.org/jira/browse/MCOMPILER-205 -->
<useIncrementalCompilation>false</useIncrementalCompilation>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
Expand Down Expand Up @@ -1726,6 +1744,100 @@ flexible messaging model and an intuitive client API.</description>
</modules>
</profile>

<!-- configure Google Error Prone static code analyser, http://errorprone.info -->
<!-- consists of 3 profiles: errorprone, errorprone-jdk8 and errorprone-jdk11 -->
<!-- usage: activate errorprone and one of errorprone-jdk8 or errorprone-jdk11 depending on the JVM version -->
<!-- For example: "mvn -Perrorprone,errorprone-jdk11,main compile" -->
<!-- revisiting warnings and errors is possible in IntelliJ after activating
errorprone, errorprone-jdk11 and main profiles and choosing Build -> Rebuild Project -->
<profile>
<id>errorprone</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<fork>true</fork>
<meminitial>128m</meminitial>
<maxmem>1024m</maxmem>
<optimize>false</optimize>
<compilerArgs combine.children="append">
<arg>-XDcompilePolicy=simple</arg>
<arg>-Xlint:-options</arg>
<!-- configure Error Prone . Disable some checks that crash the compiler or are annoying -->
<!-- the following argument must be kept on one line when building with JDK8 -->
<arg>-Xplugin:ErrorProne -XepExcludedPaths:.*/target/generated-sources/.* -XepDisableWarningsInGeneratedCode -Xep:UnusedVariable:OFF -Xep:FallThrough:OFF -Xep:OverrideThrowableToString:OFF -Xep:UnusedMethod:OFF -Xep:StringSplitter:OFF -Xep:CanonicalDuration:OFF ${errorprone.arguments.jdk11}</arg>
</compilerArgs>
<annotationProcessorPaths combine.children="append">
<path>
<groupId>com.google.errorprone</groupId>
<artifactId>error_prone_core</artifactId>
<version>${errorprone.version}</version>
</path>
<path>
<groupId>org.mockito</groupId>
<artifactId>mockito-errorprone</artifactId>
<version>${mockito.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<!-- running errorprone on JDK 8 requires special javac configuration -->
<profile>
<id>errorprone-jdk8</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArgs combine.children="append">
<arg>
-J-Xbootclasspath/p:${settings.localRepository}/com/google/errorprone/javac/${errorprone.javac.version}/javac-${errorprone.javac.version}.jar</arg>
</compilerArgs>
<annotationProcessorPaths combine.children="append">
<path>
<groupId>com.google.errorprone</groupId>
<artifactId>javac</artifactId>
<version>${errorprone.javac.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>errorprone-jdk11</id>
<properties>
<!-- pass the additional properties to -Xplugin:ErrorProne argument defined in errorprone profile -->
<!-- change configuration of slf4j checks to be more permissive -->
<errorprone.arguments.jdk11>-Xep:Slf4jSignOnlyFormat:WARN -Xep:Slf4jLoggerShouldBeNonStatic:OFF</errorprone.arguments.jdk11>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths combine.children="append">
<!-- add https://github.com/KengoTODA/errorprone-slf4j Error Prone plugin -->
<!-- detects slf4j misusage. Doesn't run on Java 8, so this is why it's in the errorprone-jdk11 profile -->
<path>
<groupId>jp.skypencil.errorprone.slf4j</groupId>
<artifactId>errorprone-slf4j</artifactId>
<version>${errorprone-slf4j.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>

<repositories>
Expand Down
1 change: 0 additions & 1 deletion pulsar-io/jdbc/lombok.config
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,3 @@
## This file is to fix the conflict with jackson error like this:
## com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of ...
lombok.anyConstructor.addConstructorProperties=true
config.stopBubbling = true

0 comments on commit 17713cf

Please sign in to comment.