Add Checkstyle and Error Prone to the Gradle build

We make some modifications to the internal Google checkstyle file because Google's linter uses a modified build of Checkstyle that introduces some new classes and allows for more specific checks than the open-source Checkstyle (e.g. only enforcing UPPER_SNAKE_CASE on deeply immutable fields). There exists a public Checkstyle file that purports to be the Google java format file (https://github.com/checkstyle/checkstyle/blob/master/src/main/resources/google_checks.xml) but it doesn't quite match up with what the internal linter says in certain situations (e.g. what operators must/can appear on new lines).

The suppressions are basically "don't run on generated code + don't care about Javadoc in test code"

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=231283700
This commit is contained in:
gbrodman 2019-01-28 13:57:32 -08:00 committed by Michael Muller
parent 5dedc1e889
commit 44e822ed86
3 changed files with 287 additions and 0 deletions

View file

@ -6,6 +6,7 @@ buildscript {
dependencies {
classpath 'com.google.cloud.tools:appengine-gradle-plugin:1.3.3'
classpath "net.ltgt.gradle:gradle-errorprone-plugin:0.6.1"
classpath 'org.sonatype.aether:aether-api:1.13.1'
classpath 'org.sonatype.aether:aether-impl:1.13.1'
}
@ -18,6 +19,8 @@ plugins {
// Ensures that source code is generated at an appropriate location.
id 'net.ltgt.apt' version '0.19' apply false
id 'com.bmuschko.docker-java-application' version '4.0.4' apply false
id 'net.ltgt.errorprone' version '0.6.1'
id 'checkstyle'
}
apply from: 'dependencies.gradle'
@ -104,6 +107,27 @@ subprojects {
apply plugin: 'nebula.dependency-lock'
apply plugin: 'nebula.lint'
apply plugin: 'net.ltgt.apt'
apply plugin: 'net.ltgt.errorprone'
apply plugin: 'checkstyle'
// Checkstyle should run as part of the testing task
tasks.test.dependsOn tasks.checkstyleMain
tasks.test.dependsOn tasks.checkstyleTest
dependencies {
// compatibility with Java 8
errorproneJavac("com.google.errorprone:javac:9+181-r4173-1")
errorprone("com.google.errorprone:error_prone_core:2.3.2")
}
tasks.withType(JavaCompile).configureEach {
options.compilerArgs << "-Werror"
options.errorprone.disableWarningsInGeneratedCode = true
options.errorprone.errorproneArgumentProviders.add([
asArguments: {
return ['-XepExcludedPaths:.*/build/generated/.*']
}] as CommandLineArgumentProvider)
}
version = '1.0'
sourceCompatibility = '1.8'

View file

@ -0,0 +1,251 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module PUBLIC
"-//Puppy Crawl//DTD Check Configuration 1.3//EN"
"http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
<!-- This is a checkstyle configuration file. For descriptions of
what the following rules do, please see the checkstyle configuration
page at http://checkstyle.sourceforge.net/config.html -->
<!-- Checks with numbered comments refer to recommendations made
by Joshua Bloch in his book Effective Java -->
<module name="Checker">
<module name="SuppressionFilter"> <!-- baseline-gradle: README.md -->
<property name="file" value="${config_loc}/suppressions.xml"/>
</module>
<property name="charset" value="UTF-8"/>
<module name="FileTabCharacter">
<!-- Checks that there are no tab characters in the file.-->
</module>
<module name="RegexpSingleline">
<!-- Checks that FIXME is not used in comments. TODO is preferred. -->
<property name="format" value="((//.*)|(\*.*))FIXME" />
<property name="message" value='TODO is preferred to FIXME. e.g. "TODO(johndoe): Refactor when v2 is released."' />
</module>
<!-- All Java AST specific tests live under TreeWalker module. -->
<module name="TreeWalker">
<!--
IMPORT CHECKS
-->
<module name="RedundantImport">
<property name="severity" value="error"/>
</module>
<module name="AvoidStarImport">
<property name="severity" value="error"/>
</module>
<module name="UnusedImports">
<!-- DPL is a notable violator of this rule. -->
<property name="severity" value="error"/>
<!-- Imports used only in Javadoc are tolerated. See http://b/838496 -->
<property name="processJavadoc" value="true"/>
<message key="import.unused" value="Unused import: {0}." />
</module>
<module name="ImportOrder">
<!-- Checks for out of order import statements. -->
<metadata name="altname" value="ImportOrder"/>
<property name="severity" value="warning"/>
<!-- This ensures that static imports go first. -->
<property name="option" value="top"/>
<property name="tokens" value="STATIC_IMPORT, IMPORT"/>
<property name="separatedStaticGroups" value="true"/>
<property name="sortStaticImportsAlphabetically" value="true"/>
<message key="import.ordering" value="Wrong order for {0} import."/>
</module>
<!--
JAVADOC CHECKS
-->
<module name="JavadocTypeCheck">
<!-- Item 28 - Write doc comments for all exposed API elements. -->
<!-- Ensure all classes with visibility greater than or equal to
protected have class level documentation. -->
<property name="scope" value="protected"/>
<property name="severity" value="error"/>
<!-- Style guide doesn't prohibit custom tags. Typos will be caught by other tools. -->
<property name="allowUnknownTags" value="true"/>
<property name="allowMissingParamTags" value="true"/>
<message key="javadoc.missing" value="Missing a Javadoc comment."/>
</module>
<!--
NAMING CHECKS
-->
<!-- Item 38 - Adhere to generally accepted naming conventions -->
<module name="PackageName">
<!-- Validates identifiers for package names against the
supplied expression. -->
<property name="format" value="^([a-z][a-z0-9]*)(\.[a-z][a-z0-9]*)*$"/>
<property name="severity" value="warning"/>
</module>
<module name="TypeNameCheck">
<!-- Validates static, final fields against the supplied expression. -->
<metadata name="altname" value="TypeName"/>
<property name="severity" value="warning"/>
<property name="format" value="^[A-Z][a-zA-Z0-9]*(_CustomFieldSerializer)?$"/>
</module>
<module name="MemberNameCheck">
<!-- Validates non-static members against the supplied expression. -->
<metadata name="altname" value="MemberName"/>
<property name="applyToPublic" value="true"/>
<property name="applyToProtected" value="true"/>
<property name="applyToPackage" value="true"/>
<property name="applyToPrivate" value="true"/>
<!-- allows for googles deprecated foo_ member naming scheme -->
<property name="format" value="^[a-z][a-zA-Z0-9]*_?$"/>
<property name="severity" value="warning"/>
</module>
<module name="MethodNameCheck">
<!-- Validates identifiers for method names. -->
<metadata name="altname" value="MethodName"/>
<property name="format" value="^([a-z][a-zA-Z0-9]*(_[a-zA-Z0-9]+)*|__constructor__|__staticInitializer__)$"/>
<property name="severity" value="warning"/>
</module>
<module name="ParameterName">
<!-- Validates identifiers for method parameters against the
expression "^[a-z][a-zA-Z0-9]*$". -->
<property name="severity" value="warning"/>
</module>
<module name="LocalFinalVariableName">
<!-- Validates identifiers for local final variables against the
expression "^[a-z][a-zA-Z0-9]*$". -->
<property name="severity" value="warning"/>
</module>
<module name="LocalVariableName">
<!-- Validates identifiers for local variables against the
expression "^[a-z][a-zA-Z0-9]*$". -->
<property name="severity" value="warning"/>
</module>
<!--
LENGTH and CODING CHECKS
-->
<module name="LineLength">
<!-- Checks if a line is too long. -->
<property name="max" value="${com.puppycrawl.tools.checkstyle.checks.sizes.LineLength.max}" default="100"/>
<property name="severity" value="error"/>
<!-- Ignore lines that have any series of 80 or more non-whitespace characters.
These lines likely cannot be broken.
-->
<property name="ignorePattern"
value="${com.puppycrawl.tools.checkstyle.checks.sizes.LineLength.ignorePattern}"
default=".*[^ ]{80,}.*"/>
</module>
<module name="OperatorWrap">
<property name="option" value="nl"/>
<property name="tokens" value="QUESTION, EQUAL, NOT_EQUAL, DIV, PLUS, MINUS, STAR, MOD, SR, BSR, GE, GT, SL, LE, LT, BXOR, BOR, LOR, BAND, LAND, TYPE_EXTENSION_AND, LITERAL_INSTANCEOF"/>
</module>
<module name="LeftCurly">
<!-- Checks for placement of the left curly brace ('{'). -->
<property name="severity" value="warning"/>
</module>
<module name="RightCurly">
<property name="option" value="same"/>
<property name="severity" value="warning"/>
</module>
<!-- Checks for braces around if and else blocks -->
<module name="NeedBraces">
<property name="severity" value="warning"/>
<property name="tokens" value="LITERAL_IF, LITERAL_ELSE, LITERAL_FOR, LITERAL_WHILE, LITERAL_DO"/>
</module>
<!-- Checks for empty catch blocks in non-test files. -->
<module name="EmptyCatchBlock">
<property name="exceptionVariableName" value="expected"/>
<property name="severity" value="error"/>
<message key="catchBlock.empty"
value="Empty catch blocks are not allowed. If this is an expected exception, it should be named 'expected'."/>
</module>
<!--
MODIFIERS CHECKS
-->
<module name="ModifierOrder">
<!-- Warn if modifier order is inconsistent with JLS3 8.1.1, 8.3.1, and
8.4.3. The prescribed order is:
public, protected, private, abstract, static, final, transient, volatile,
synchronized, native, strictfp
-->
</module>
<!--
WHITESPACE CHECKS
-->
<module name="WhitespaceAround">
<!-- Checks that various tokens are surrounded by whitespace.
This includes most binary operators and keywords followed
by regular or curly braces.
-->
<property name="tokens" value="ASSIGN, BAND, BAND_ASSIGN, BOR,
BOR_ASSIGN, BSR, BSR_ASSIGN, BXOR, BXOR_ASSIGN, COLON, DIV, DIV_ASSIGN,
EQUAL, GE, GT, LAND, LE, LITERAL_CATCH, LITERAL_DO, LITERAL_ELSE,
LITERAL_FINALLY, LITERAL_FOR, LITERAL_IF, LITERAL_RETURN,
LITERAL_SYNCHRONIZED, LITERAL_TRY, LITERAL_WHILE, LOR, LT, MINUS,
MINUS_ASSIGN, MOD, MOD_ASSIGN, NOT_EQUAL, PLUS, PLUS_ASSIGN, QUESTION,
SL, SL_ASSIGN, SR_ASSIGN, STAR, STAR_ASSIGN"/>
<property name="severity" value="error"/>
<property name="ignoreEnhancedForColon" value="false"/>
</module>
<module name="WhitespaceAfter">
<!-- Checks that commas, semicolons and typecasts are followed by
whitespace.
-->
<property name="tokens" value="COMMA, SEMI, TYPECAST"/>
</module>
<module name="NoWhitespaceAfter">
<!-- Checks that there is no whitespace after various unary operators.
Linebreaks are allowed.
-->
<property name="tokens" value="BNOT, DEC, DOT, INC, LNOT, UNARY_MINUS,
UNARY_PLUS"/>
<property name="allowLineBreaks" value="true"/>
<property name="severity" value="error"/>
</module>
<module name="NoWhitespaceBefore">
<!-- Checks that there is no whitespace before various unary operators.
Linebreaks are allowed.
-->
<property name="tokens" value="SEMI, DOT, POST_DEC, POST_INC"/>
<property name="allowLineBreaks" value="true"/>
<property name="severity" value="error"/>
</module>
<module name="ParenPad">
<!-- Checks that there is no whitespace before close parens or after
open parens. Allows whitespace after open parens if it is followed
by the end of a line or by the start of a comment. Allows whitespace
before close parens if it is preceeded by the start of a line or by
the end of a block comment.
-->
<metadata name="altname" value="ParenPad"/>
<property name="severity" value="warning"/>
</module>
</module>
</module>

View file

@ -0,0 +1,12 @@
<?xml version="1.0"?>
<!DOCTYPE suppressions PUBLIC
"-//Puppy Crawl//DTD Suppressions 1.1//EN"
"http://www.puppycrawl.com/dtds/suppressions_1_1.dtd">
<suppressions>
<!-- Ignore generated files in Checkstyle -->
<suppress files="[/\\].*[/\\]generated.*[/\\]" checks="."/>
<!-- Ignore Javadoc checks in test files -->
<suppress files="[/\\].*[/\\]javatests.*[/\\]" checks="JavadocType"/>
</suppressions>