Skip to content

Commit

Permalink
feat(android): replace clang-format with gradle checkstyle
Browse files Browse the repository at this point in the history
- "checkstyle" tool enforces Java coding standards and checks for common Java coding mistakes. Provides more readable error messages.
- Applied checkstyle as a pre-build step to Java compile.
  • Loading branch information
jquick-axway committed Mar 6, 2020
1 parent 6e066e0 commit 3cbc754
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 4 deletions.
13 changes: 13 additions & 0 deletions android/app/build.gradle
Expand Up @@ -7,6 +7,7 @@

apply plugin: 'com.android.application'

// Set up Android app project.
android {
compileSdkVersion 29
defaultConfig {
Expand Down Expand Up @@ -35,6 +36,18 @@ android {
}
}

// Checks our Java code against our style guidelines and for common coding mistakes using "checkstyle.xml".
// Will trigger a build failure if any violations have been detected.
task checkJavaStyle(type: Checkstyle) {
source android.sourceSets.main.java.srcDirs
include '**/*.java'
classpath = files()
}
tasks.withType(JavaCompile) {
dependsOn checkJavaStyle
}

// Define library dependencies used by this app.
dependencies {
implementation(project(':titanium')) {
// Uncomment the below to exclude Google Play Services from app.
Expand Down
11 changes: 11 additions & 0 deletions android/build.gradle
Expand Up @@ -23,6 +23,17 @@ allprojects {
google()
jcenter()
}

// Load plugin used to enforce our Java coding style guidelines.
project.apply plugin: 'checkstyle'
checkstyle {
toolVersion = '8.30'
configFile file("${rootDir}/checkstyle.xml");
ignoreFailures false
showViolations true
}

// Include Titanium's gradle constants in all gradle sub-projects.
project.apply from: "${rootDir}/templates/build/ti.constants.gradle"
}

Expand Down
117 changes: 117 additions & 0 deletions android/checkstyle.xml
@@ -0,0 +1,117 @@
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
"-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
"https://checkstyle.org/dtds/configuration_1_3.dtd">

<!-- Used by gradle "Checkstyle" task to enforce Java coding guidelines and check for common mistakes. -->
<module name="Checker">
<!-- Define number of spaces a single tab character should be. -->
<property name="tabWidth" value="4"/>

<!-- Do not allow lines of code to exceed 120 characters, except for comments. -->
<module name="LineLength">
<property name="max" value="120"/>
<property name="ignorePattern" value="\/\*|^\s*\*|\/\/"/>
</module>

<!-- Do not allow multiple empty lines in a row. -->
<module name="RegexpMultiline">
<property name="format" value="^\s*\n\s*\n"/>
<property name="message" value="You cannot have multiple empty lines."/>
</module>

<module name="TreeWalker">
<!-- Check import statements. -->
<!-- See: https://checkstyle.sourceforge.io/config_imports.html -->
<module name="AvoidStarImport"/>
<module name="IllegalImport"/>
<module name="UnusedImports"/>

<!-- Custom regex checks. -->
<!-- See: https://checkstyle.sourceforge.io/config_regexp.html -->
<module name="RegexpSinglelineJava">
<property name="format" value="^\t* {4}"/>
<property name="message" value="Must use tabs for indentation."/>
<property name="ignoreComments" value="true"/>
</module>
<module name="RegexpSinglelineJava">
<property name="format" value="\S+?[ \t]+?$"/>
<property name="message" value="Line cannot have whitespace at the end."/>
<property name="ignoreComments" value="true"/>
</module>

<!-- Checks for whitespace. -->
<!-- See: https://checkstyle.sourceforge.io/config_whitespace.html -->
<module name="EmptyLineSeparator">
<property name="allowMultipleEmptyLines" value="false"/>
<property name="allowMultipleEmptyLinesInsideClassMembers" value="false"/>
<property name="allowNoEmptyLineBetweenFields" value="true"/>
<property name="tokens" value="CLASS_DEF"/>
</module>
<module name="GenericWhitespace"/>
<module name="MethodParamPad"/>
<module name="NoWhitespaceAfter">
<property name="tokens" value="AT,INC,DEC,UNARY_MINUS,UNARY_PLUS,BNOT,LNOT,DOT,INDEX_OP"/>
<property name="allowLineBreaks" value="true"/>
</module>
<module name="NoWhitespaceBefore"/>
<module name="OperatorWrap"/>
<module name="ParenPad"/>
<module name="TypecastParenPad"/>
<module name="WhitespaceAfter"/>
<module name="WhitespaceAround">
<property name="allowEmptyCatches" value="true"/>
<property name="allowEmptyConstructors" value="true"/>
<property name="allowEmptyLambdas" value="true"/>
<property name="allowEmptyLoops" value="true"/>
<property name="allowEmptyMethods" value="true"/>
<property name="allowEmptyTypes" value="true"/>
</module>

<!-- Checks private/protected/public access modifiers. -->
<!-- See: https://checkstyle.sourceforge.io/config_modifier.html -->
<module name="RedundantModifier">
<property name="tokens" value="INTERFACE_DEF,METHOD_DEF"/>
</module>

<!-- Checks placement of curly braces. -->
<!-- See: https://checkstyle.sourceforge.io/config_blocks.html -->
<module name="LeftCurly">
<property name="option" value="nl"/>
<property name="tokens" value="CLASS_DEF,CTOR_DEF,LITERAL_SYNCHRONIZED,METHOD_DEF,STATIC_INIT"/>
</module>
<module name="LeftCurly">
<property name="option" value="eol"/>
<property name="tokens" value="
ANNOTATION_DEF,ENUM_CONSTANT_DEF,ENUM_DEF,INTERFACE_DEF,LAMBDA,LITERAL_CASE,LITERAL_CATCH,
LITERAL_DEFAULT,LITERAL_DO,LITERAL_ELSE,LITERAL_FINALLY,LITERAL_FOR,LITERAL_IF,LITERAL_SWITCH,
LITERAL_TRY,LITERAL_WHILE"/>
</module>
<module name="NeedBraces">
<property name="tokens" value="LITERAL_DO"/>
</module>

<!-- Check for miscellaneous issues. -->
<!-- See: https://checkstyle.sourceforge.io/config_misc.html -->
<module name="ArrayTypeStyle"/>
<module name="Indentation">
<property name="basicOffset" value="4"/>
<property name="braceAdjustment" value="0"/>
<property name="caseIndent" value="4"/>
<property name="throwsIndent" value="4"/>
<property name="lineWrappingIndentation" value="4"/>
<property name="arrayInitIndent" value="4"/>
</module>
<module name="OuterTypeFilename"/>
<module name="UpperEll"/>

<!-- Check for common coding mistakes. -->
<!-- See: https://checkstyle.sourceforge.io/config_coding.html -->
<module name="CovariantEquals"/>
<module name="EqualsHashCode"/>
<module name="IllegalInstantiation"/>
<module name="PackageDeclaration"/>
<module name="StringLiteralEquality"/>
<module name="UnnecessarySemicolonAfterTypeMemberDeclaration"/>
</module>
</module>
12 changes: 12 additions & 0 deletions android/kroll-apt/build.gradle
Expand Up @@ -10,7 +10,19 @@ apply plugin: 'java'
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8

// Checks our Java code against our style guidelines and for common coding mistakes using "checkstyle.xml".
// Will trigger a build failure if any violations have been detected.
task checkJavaStyle(type: Checkstyle) {
source 'src/main/java'
include '**/*.java'
classpath = files()
}

// Hook into Java compile task.
tasks.withType(JavaCompile) {
// Check Java code for mistakes before compiling.
dependsOn checkJavaStyle

// Suppress compiler warning "bootstrap class path not set in conjunction with source" which happens when
// building with JDK newer than the Java version we're targeting. (Ex: Build with JDK 8, but target Java 7.)
// Note: Build tool wants a reference to runtime JAR of the same version we're targeting. Not going to happen.
Expand Down
5 changes: 1 addition & 4 deletions package.json
Expand Up @@ -42,7 +42,7 @@
"ios-sanity-check": "./build/scons check-ios-toplevel",
"link": "npm run deploy -- --symlink",
"lint": "npm-run-all --parallel lint:**",
"lint:android": "clang-format-lint $npm_package_config_format_android",
"lint:android": "echo Disabled in favor of gradle checkstyle tool.",
"lint:docs": "tdoc-validate ./apidoc",
"lint:ios": "clang-format-lint $npm_package_config_format_ios",
"lint:js": "eslint .",
Expand Down Expand Up @@ -70,9 +70,6 @@
"iphone/TitaniumKit/TitaniumKit/Sources/API/TopTiModule.m": [
"npm run ios-sanity-check --"
],
"android/!(templates)/**/*.java": [
"npx clang-format -style=file -i"
],
"!(**/locales/**/*).js": "eslint"
},
"commitlint": {
Expand Down

0 comments on commit 3cbc754

Please sign in to comment.