Skip to content

Commit

Permalink
Implement Checkstyle (#68)
Browse files Browse the repository at this point in the history
- Add rules
- Update documentation links, remove unused property
- Change Travis jdk target to jdk8 only
  • Loading branch information
NikoYuwono authored and glung committed Nov 14, 2016
1 parent c814c61 commit 1fb861c
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 12 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Expand Up @@ -13,7 +13,6 @@ android:
- android-sdk-license-.*

jdk:
- oraclejdk7
- oraclejdk8

branches:
Expand Down
17 changes: 16 additions & 1 deletion build.gradle
Expand Up @@ -12,7 +12,7 @@ buildscript {

task wrapper(type: Wrapper) {
description 'Creates the gradle wrapper.'
gradleVersion '2.5'
gradleVersion '2.14.1'
}

allprojects {
Expand Down Expand Up @@ -51,9 +51,24 @@ ext.deps = [

// Static analysis
subprojects { project ->
apply plugin: 'checkstyle'
apply plugin: 'pmd'
apply plugin: 'findbugs'

checkstyle {
toolVersion = "7.2"
configFile rootProject.file('checkstyle.xml')
}

task checkstyle(type: Checkstyle) {
source 'src/main/java'
ignoreFailures false
showViolations true
include '**/*.java'

classpath = files()
}

task pmd(type: Pmd) {
description 'Finds common programming flaws throw static analysis of code.'
ignoreFailures false
Expand Down
125 changes: 125 additions & 0 deletions checkstyle.xml
@@ -0,0 +1,125 @@
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
"-//Puppy Crawl//DTD Check Configuration 1.2//EN"
"http://www.puppycrawl.com/dtds/configuration_1_2.dtd">

<module name="Checker">
<module name="FileLength"/>
<module name="FileTabCharacter"/>

<!-- Trailing spaces -->
<module name="RegexpSingleline">
<property name="format" value="\s+$"/>
<property name="message" value="Line has trailing spaces."/>
</module>

<!-- Space after 'for' and 'if' -->
<module name="RegexpSingleline">
<property name="format" value="^\s*(for|if)\b[^ ]"/>
<property name="message" value="Space needed before opening parenthesis."/>
</module>

<!-- For each spacing -->
<module name="RegexpSingleline">
<property name="format" value="^\s*for \(.*?([^ ]:|:[^ ])"/>
<property name="message" value="Space needed around ':' character."/>
</module>

<module name="TreeWalker">

<!-- Checks for Annotation Location -->
<!-- See http://checkstyle.sourceforge.net/config_annotation.html -->
<module name="AnnotationLocation">
<property name="allowSamelineMultipleAnnotations" value="false"/>
<property name="allowSamelineSingleParameterlessAnnotation" value="false"/>
<property name="allowSamelineParameterizedAnnotation" value="false"/>
</module>

<!-- Checks for Naming Conventions. -->
<!-- See http://checkstyle.sourceforge.net/config_naming.html -->
<module name="ConstantName"/>
<module name="LocalFinalVariableName"/>
<module name="LocalVariableName"/>
<module name="MemberName"/>
<module name="MethodName">
<property name="format" value="^[a-z][a-zA-Z0-9_]*$"/>
</module>
<module name="PackageName"/>
<module name="ParameterName"/>
<module name="StaticVariableName"/>
<module name="TypeName">
<property name="format" value="^[A-Z][a-zA-Z0-9_]*$"/>
</module>


<!-- Checks for imports -->
<!-- See http://checkstyle.sourceforge.net/config_imports.html -->
<module name="AvoidStarImport"/>
<module name="IllegalImport"/>
<module name="RedundantImport"/>
<module name="UnusedImports">
<property name="processJavadoc" value="true"/>
</module>


<!-- Checks for Size Violations. -->
<!-- See http://checkstyle.sourceforge.net/config_sizes.html -->
<module name="LineLength">
<property name="max" value="200"/>
</module>
<module name="ParameterNumber"/>


<!-- Checks for whitespace -->
<!-- See http://checkstyle.sourceforge.net/config_whitespace.html -->
<module name="GenericWhitespace"/>
<module name="EmptyForIteratorPad"/>
<module name="MethodParamPad"/>
<module name="NoWhitespaceAfter"/>
<module name="NoWhitespaceBefore"/>
<module name="OperatorWrap"/>
<module name="ParenPad"/>
<module name="TypecastParenPad"/>
<module name="WhitespaceAfter"/>
<module name="WhitespaceAround"/>


<!-- Modifier Checks -->
<!-- See http://checkstyle.sourceforge.net/config_modifier.html -->
<module name="RedundantModifier"/>


<!-- Checks for blocks. -->
<!-- See http://checkstyle.sourceforge.net/config_blocks.html -->
<module name="AvoidNestedBlocks"/>
<module name="LeftCurly"/>
<module name="NeedBraces">
<property name="tokens" value="LITERAL_DO, LITERAL_ELSE, LITERAL_FOR, LITERAL_WHILE"/>
</module>
<module name="RightCurly"/>


<!-- Checks for common coding problems -->
<!-- See http://checkstyle.sourceforge.net/config_coding.html -->
<module name="CovariantEquals"/>
<module name="EmptyStatement"/>
<module name="EqualsAvoidNull"/>
<module name="EqualsHashCode"/>
<module name="IllegalInstantiation"/>
<module name="InnerAssignment"/>
<module name="MissingSwitchDefault"/>
<module name="SimplifyBooleanExpression"/>
<module name="SimplifyBooleanReturn"/>

<!-- Checks for class design -->
<!-- See http://checkstyle.sourceforge.net/config_design.html -->
<module name="InterfaceIsType"/>


<!-- Miscellaneous other checks. -->
<!-- See http://checkstyle.sourceforge.net/config_misc.html -->
<module name="ArrayTypeStyle"/>
<module name="TodoComment"/>
<module name="UpperEll"/>
</module>
</module>
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
@@ -1,6 +1,6 @@
#Tue Mar 15 11:19:20 CET 2016
#Sun Nov 13 16:29:51 JST 2016
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.5-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
6 changes: 6 additions & 0 deletions lightcycle-api/build.gradle
@@ -1,5 +1,11 @@
apply plugin: 'checkstyle'
apply from: rootProject.file('gradle/gradle-mvn-push.gradle')

checkstyle {
configFile rootProject.file('checkstyle.xml')
showViolations true
}

dependencies {
provided deps.android
provided deps.support_v4
Expand Down
6 changes: 6 additions & 0 deletions lightcycle-lib/build.gradle
@@ -1,4 +1,5 @@
apply plugin: 'com.android.library'
apply plugin: 'checkstyle'
apply from: rootProject.file('gradle/gradle-mvn-push.gradle')

android {
Expand Down Expand Up @@ -34,6 +35,11 @@ android {
}
}

checkstyle {
configFile rootProject.file('checkstyle.xml')
showViolations true
}

dependencies {
compile project(':lightcycle-api')
provided deps.support_v4
Expand Down
6 changes: 6 additions & 0 deletions lightcycle-processor/build.gradle
@@ -1,5 +1,11 @@
apply plugin: 'checkstyle'
apply from: rootProject.file('gradle/gradle-mvn-push.gradle')

checkstyle {
configFile rootProject.file('checkstyle.xml')
showViolations true
}

dependencies {
compile project(':lightcycle-api')
compile deps.javapoet
Expand Down
Expand Up @@ -183,8 +183,8 @@ private LightCycleBinder findParent(Set<String> erasedTargetNames, TypeMirror ty
private void verifyFieldsAccessible(Set<? extends Element> elements) {
for (Element element : elements) {
if (element.getModifiers().contains(Modifier.PRIVATE)) {
throw new IllegalStateException("Annotated fields cannot be private: " +
element.getEnclosingElement() + "#" + element + "(" + element.asType() + ")");
throw new IllegalStateException("Annotated fields cannot be private: "
+ element.getEnclosingElement() + "#" + element + "(" + element.asType() + ")");
}
}
}
Expand Down
Expand Up @@ -20,8 +20,8 @@ public class InvalidCaseTest {
@Test
public void shouldThrowExceptionWhenLightCycleFieldIsPrivate() {
expectedException.expect(RuntimeException.class);
expectedException.expectMessage("Annotated fields cannot be private: " +
"com.test.PrivateFieldsTestFragment#lightCycle1(com.test.LightCycle1)");
expectedException.expectMessage("Annotated fields cannot be private: "
+ "com.test.PrivateFieldsTestFragment#lightCycle1(com.test.LightCycle1)");

JavaFileObject privateFieldsTestFragment = forSourceString("com/test/PrivateFieldsTestFragment", Joiner.on("\n").join(
"package com.test;",
Expand Down Expand Up @@ -131,8 +131,8 @@ public void missingGenericTestActivity() {
.that(missingGenericTestActivity)
.processedWith(new LightCycleProcessor())
.failsToCompile()
.withErrorContaining("Expected 1 type argument but found 0. TypeArguments:[]. " +
"Did you forget to add generic types?");
.withErrorContaining("Expected 1 type argument but found 0. TypeArguments:[]. "
+ "Did you forget to add generic types?");
}

@Test
Expand Down Expand Up @@ -164,7 +164,7 @@ public void dispatcherNotFound() {
.that(dispatcherNotFoundTestActivity)
.processedWith(new LightCycleProcessor())
.failsToCompile()
.withErrorContaining("Could not find dispatcher type. " +
"Did you forget to add the generic type?");
.withErrorContaining("Could not find dispatcher type. "
+ "Did you forget to add the generic type?");
}
}

0 comments on commit 1fb861c

Please sign in to comment.