Share
Static Code Analysis with Kotlin
Detekt is a static code analysis tool for Kotlin. It can analyze your Kotlin projects for various types of code smells. The tool contains rules for style, performance and potential bugs. The tool is fully configurable so that you may turn off and set thresholds for these rules. It is also extensible so that you could create your own custom rules from your style guide. This tool goes a long way to enforcing good practices in your code base.
Under the hood, the library performs checks against these rules by analyzing the abstract syntax tree provided by the Kotlin Compiler. In this talk, we will learn about how to setup Detekt and how to create custom rules. We will also take a deep dive into how the library enforces these rules by using the abstract syntax tree.
Mohit S
Transcript
137 slides
-
Static Code Analysis with Kotlin • Why Static Code Analysis?
• Setup & Configure Detekt • Program Structure Interface (PSI) • How Detekt uses the PSI to find code smells • Write your own Detekt rules -
• 100,000+ lines of Java • Started 5 years ago
• Legacy code base • Migrating to Kotlin Problem -
Legacy code problems • Maintainability • Performance • Code smells
• Defects • Warnings • Security • Inconsistent styles -
Maintainability class MainActivity : AppCompatActivity() { fun loadPost() { …
} fun loadComments() { … } fun showComments() { … } fun likePost() { … } fun comment() { … } … } -
Potential Bugs class FeedActivity extends AppCompatActivity { @Nullable FeedAdapter feedAdapter;
public void showFeed() { feedAdapter.getItemCount(); feedAdapter.registerAdapterDataObserver(…); feedAdapter.submitList(…); } } -
Potential Bugs class FeedActivity : AppCompatActivity() { var feedAdapter: FeedAdapter?
= null fun showFeed() { feedAdapter!!!.itemCount feedAdapter!!!.registerAdapterDataObserver(…) feedAdapter!!!.submitList(…) } -
Potential Bugs class FeedActivity : AppCompatActivity() { var feedAdapter: FeedAdapter?
= null fun showFeed() { feedAdapter!?.itemCount feedAdapter!?.registerAdapterDataObserver(…) feedAdapter!?.submitList(…) } -
Kotlin Idioms public class Post { private Integer id; private
String title; private String message; … } -
Kotlin Idioms public class Post { public Integer getId() {
return id; } public String getTitle() { return title; } public String getMessage() { return message; } … } -
Kotlin Idioms public class Post { @Override public boolean equals(Object
o) { … } @Override public int hashCode() { … } } -
Kotlin Idioms class Post { var id: Int? = null
var title: String? = null var message: String? = null override fun equals(o: Any?): Boolean { … } override fun hashCode(): Int { … } } -
Kotlin Idioms data class Post( val id: Int? = null,
val title: String? = null, val message: String? = null ) -
Static Code Analysis Analysis KtFile KtClass KtNamedFunction KtNamedFunction Code Smells
Performance Bugs Style Guide -
Static Code Analysis with Kotlin • Why Static Code Analysis?
• Setup & Configure Detekt • Program Structure Interface (PSI) • How Detekt uses the PSI to find code smells • Write your own Detekt rules -
Setup Detekt 1. Add Detekt Gradle Plugin 2. Generate Detekt
Config YML file -
Add Detekt Plugin plugins { id "io.gitlab.arturbosch.detekt" version "1.0.0.RC8" }
detekt { version = "1.0.0.RC8" profile(“main") { input = "$projectDir" config = "$project.rootDir/detekt-config.yml" filters = ".!*/resources/.*,.!*/build/.*" } } -
YML Configs complexity: active: true MethodOverloading: active: true threshold: 3
potential-bugs: active: true UnsafeCallOnNullableType: active: true Rule Rule Set -
YML Configs output-reports: active: true exclude: # - 'HtmlOutputReport' #
- 'PlainOutputReport' # - 'XmlOutputReport' -
Report 182 kotlin files were analyzed. Ruleset: complexity MethodOverloading -
[createPost] at ApiClient.kt:3:1 Ruleset: comments Ruleset: empty-blocks Ruleset: exceptions Ruleset: naming Ruleset: performance Ruleset: potential-bugs Ruleset: style -
Report Complexity Report: - 16662 lines of code (loc) -
9633 source lines of code (sloc) - 6380 logical lines of code (lloc) - 4605 comment lines of code (cloc) - 1884 McCabe complexity (mcc) - 2 number of total code smells - 47 % comment source ratio - 295 mcc per 1000 lloc - 0 code smells per 1000 lloc -
YML Configs console-reports: active: true exclude: # - 'ProjectStatisticsReport' #
- 'ComplexityReport' # - 'NotificationReport' # - 'FindingsReport' # - 'BuildFailureReport' -
YML Configs failFast: false build: maxIssues: 10 weights: # complexity:
2 # LongParameterList: 1 # style: 1 # comments: 1 -
class FeedActivity : AppCompatActivity() { var feedAdapter: FeedAdapter? = null
!// Non-Compliant fun showFeed() { feedAdapter!!!.itemCount feedAdapter!!!.registerAdapterDataObserver(…) feedAdapter!!!.submitList(…) } Nullable Types -
class FeedActivity : AppCompatActivity() { var feedAdapter: FeedAdapter? = null
!// Compliant fun showFeed() { feedAdapter!?.itemCount feedAdapter!?.registerAdapterDataObserver(…) feedAdapter!?.submitList(…) } Nullable Types -
Rule: Unsafe Call on Nullable Type Description: • Guard your
code to prevent NullPointerExceptions Severity: • Defect -
Detekt Check Report 5 kotlin files were analyzed. Ruleset: potential-bugs
UnsafeCallOnNullableType - [showFeed] at FeedActivity.kt:8:17 Ruleset: comments Ruleset: complexity Ruleset: empty-block Ruleset: exceptions Ruleset: naming Ruleset: performance Ruleset: style -
Use Optional Parameters !// Non-Compliant fun createPost(title: String) {} fun
createPost(title: String, message: String) {} fun createPost(title: String, message: String, images: List<Images>) { } -
Use Optional Parameters !// Compliant fun createPost(title: String = “Untitled",
message: String? = null, images: List<Image>? = null) { } -
Rule: Method Overloading Description: • Enforce usage of optional parameters
in Kotlin Severity: • Maintainability -
Configure Rule in YML complexity: MethodOverloading: active: true threshold: 3
Max # of params allowed -
Use Data Classes public class Post { @Override public boolean
equals(Object o) { … } @Override public int hashCode() { … } } -
Use Data Classes !// Non-Compliant class Post { var id:
Int? = null var title: String? = null var message: String? = null override fun equals(o: Any?): Boolean { … } override fun hashCode(): Int { … } } -
Use Data Classes !// Compliant data class Post( val id:
Int? = null, val title: String? = null, val message: String? = null ) -
Rule: Use Data Class Description: • Enforce the usage of
data classes for POJOs. Severity: • Style -
Configure Rule in YML UseDataClass: active: true excludeAnnotatedClasses: “LegacyClass" Exclude
classes with this annotation -
Rule Sets • Comments • Complexity • Empty-blocks • Exceptions
• Formatting • Naming • Performance • Potential-Bugs • Style -
Git Pre-commit Hook ##!/usr/bin/env bash echo "Running detekt check##..." OUTPUT="/tmp/detekt-$(date
+%s)" ./gradlew detektCheck > $OUTPUT EXIT_CODE=$? if [ $EXIT_CODE -ne 0 ]; then cat $OUTPUT rm $OUTPUT echo "***********************************************" echo " Detekt failed " echo " Please fix the above issues before committing " echo "***********************************************" exit $EXIT_CODE fi rm $OUTPUT -
Static Code Analysis with Kotlin • Why Static Code Analysis?
• Setup & Configure Detekt • Program Structure Interface (PSI) • Finding Code Smells with PSI • Write your own Detekt rules. -
Program Structure Interface Program Structure Interface is a layer in
the Intellij Platform for parsing files into a semantic model. Source PsiElement PsiElement PsiElement PsiElement Java, Kotlin, Python, etc.. -
• Code completion • Syntax highlighting • Refactoring • Building
indexes Features Powered by PSI -
Intellij Platform public interface PsiBuilder { Project getProject(); @NotNull Marker
mark(); @NotNull ASTNode getTreeBuilt(); } PsiElement PsiElement PsiElement PsiElement intellij-community/platform/core-api/src/com/intellij/lang/PsiBuilder.java -
Kotlin Embeddable Compiler public class KotlinParsing { void parseFile() {
PsiBuilder.Marker fileMarker = mark(); parsePreamble(); while (!eof()) { parseTopLevelDeclaration(); } checkUnclosedBlockComment(); fileMarker.done(KT_FILE); } } KtFile KtClass KtClassBody KtNamedFunction KtNamedFunction kotlin/compiler/psi/src/org/jetbrains/kotlin/parsing/KotlinParsing.java -
How Detekt Works Analysis KtFile KtClass KtNamedFunction KtNamedFunction Code Smells
Performance Bugs Style Guide -
class ApiClient { fun createPost( title: String, message: String)
{ } } Parse into Tokens CLASS_KEYWORD LBRACE FUN_KEYWORD WHITE_SPACE RBRACE IDENTIFIER -
Parsing Source to Tokens … “typealias" { return KtTokens.TYPE_ALIAS_KEYWORD ;}
"object" { return KtTokens.OBJECT_KEYWORD ;} "while" { return KtTokens.WHILE_KEYWORD ;} "break" { return KtTokens.BREAK_KEYWORD ;} "class" { return KtTokens.CLASS_KEYWORD ;} "throw" { return KtTokens.THROW_KEYWORD ;} "false" { return KtTokens.FALSE_KEYWORD ;} … kotlin/compiler/psi/src/org/jetbrains/kotlin/lexer/Kotlin.flex -
Abstract Syntax Tree ElementType.FILE ElementType.CLASS ElementType.FUN ElementType.FUN class ApiClient {
fun createPost( title: String, message: String) { } } -
PSI Tree KtFile KtClass KtNamedFunction KtNamedFunction class ApiClient { fun
createPost( title: String, message: String) { } } -
Creating/Traverse PSI Tree 1. Configure the compiler. 2. Get PSI
Tree for a Kotlin file. 3. Use visitor pattern to traverse PSI Tree -
Configure Compiler !// Configure Compiler val configuration = CompilerConfiguration() configuration.put(
CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, PrintingMessageCollector(System.err, MessageRenderer.PLAIN_FULL_PATHS, false)) !// Create Project val psiProject: Project = KotlinCoreEnvironment.createForProduction( Disposer.newDisposable(), configuration, EnvironmentConfigFiles.JVM_CONFIG_FILES ).project -
Create PSI Tree val psiFactory = PsiFileFactory(psiProject) !// Get Kotlin
File Content val path = Paths.get(“/path/to/kotlin/file.kt”) val content = path.toFile().readText() -
Create PSI Tree val psiFactory = PsiFileFactory(psiProject) !// Get Kotlin
File Content val path = Paths.get(“/path/to/kotlin/file.kt”) val content = path.toFile().readText() !// Create PSI Tree val ktFile: PsiFile = psiFactory.createFileFromText( path.fileName.toString(), KotlinLanguage.INSTANCE, content, …) -
Create PSI Tree !// Create PSI Tree val ktFile: PsiFile
= psiFactory.createFileFromText( path.fileName.toString(), KotlinLanguage.INSTANCE, content, …) KtFile KtClass KtClassBody KtNamedFunction KtNamedFunction -
How do I traverse it? class ApiClient { fun createPost()
{ … } fun deletePost() { … } } KtFile KtClass KtClassBody KtNamedFunction KtNamedFunction -
Visitor Provided by Compiler public class KtTreeVisitorVoid extends KtVisitor<Void, Void>
{ void visitKtElement(@NotNull KtElement element) { } void visitClass(@NotNull KtClass klass) { } void visitNamedFunction(@NotNull KtNamedFunction function) { } … } kotlin/compiler/psi/src/org/jetbrains/kotlin/KtVisitorVoid.java -
Create Visitor class MyVisitor: KtTreeVisitorVoid() { override fun visitKtFile(file: KtFile)
{ } override fun visitClass(klass: KtClass) { } override fun visitNamedFunction(function: KtNamedFunction) { } } -
Create Visitor !// Create PSI Tree val ktFile: PsiFile =
psiFactory.createFileFromText( path.fileName.toString(), KotlinLanguage.INSTANCE, content, …) !// Specify visitor to traverse tree val visitor = MyVisitor() psiFile.accept(visitor) Start Traversing Tree -
Order of Traversal of PSI Tree class MyVisitor: KtTreeVisitorVoid() {
override fun visitKtFile(file: KtFile) { println("visitKtFile") } override fun visitClass(klass: KtClass) { println("visitClass") } override fun visitNamedFunction(function: KtNamedFunction) { println(“visitNamedFunction") } } -
Output: visitNamedFunction visitNamedFunction visitClassBody visitClass visitKtFile Order of Traversal of
PSI Tree KtFile KtClass KtClassBody KtNamedFunction KtNamedFunction -
Output: visitNamedFunction visitNamedFunction visitClassBody visitClass visitKtFile Order of Traversal of
PSI Tree KtFile KtClass KtClassBody KtNamedFunction KtNamedFunction -
Output: visitNamedFunction visitNamedFunction visitClassBody visitClass visitKtFile Order of Traversal of
PSI Tree KtFile KtClass KtClassBody KtNamedFunction KtNamedFunction -
Output: visitNamedFunction visitNamedFunction visitClassBody visitClass visitKtFile Order of Traversal of
PSI Tree KtFile KtClass KtClassBody KtNamedFunction KtNamedFunction -
Static Code Analysis with Kotlin • Why Static Code Analysis?
• Setup & Configure Detekt • Program Structure Interface (PSI) • How Detekt uses the PSI to find code smells • Write your own Detekt rules. -
Finding Code Smells KtFile KtClass KtClassBody KtNamedFunction KtNamedFunction Code Smells
Style Performance Bugs Analysis -
Rule: Keep Data Classes Simple Description: • Data classes should
ONLY hold data. • Data classes should NOT contain extra functions. • Overridden methods in data classes are OK. -
Rule: Keep Data Classes Simple !// Non-Compliant data class Post(val
id: Int, val message: String) { fun create() { … } fun delete() { … } } -
Rule: Keep Data Classes Simple !// Non-Compliant data class Post(val
id: Int, val message: String) { fun create() { … } fun delete() { … } } Should only store data -
Rule: Keep Data Classes Simple !// Compliant data class Post(val
id: Int, val message: String) fun Post.create(apiClient: ApiClient) { … } fun Post.delete(apiClient: ApiClient) { … } -
Rule: Keep Data Classes Simple !// Compliant data class Post(val
id: Int, val message: String) { override fun hashCode(): Int { … } override fun equals(other: Any?): Boolean { … } override fun toString(): String { … } } -
Strategy for Implementation • Determine class is data class •
Does it any methods? • Are the methods are overridden? KtFile KtClass KtClassBody KtNamedFunction KtNamedFunction -
Implementation Check type of KtClass • Enum • Object •
Data class • Sealed class KtFile KtClass KtClassBody KtNamedFunction KtNamedFunction -
KtClass API public open class KtClass : KtClassOrObject { public
final fun isData(): Boolean { … } public final fun isEnum(): Boolean { … } public final fun isSealed(): Boolean { … } … } kotlin/compiler/psi/src/org/jetbrains/kotlin/parsing/KtClass.kt -
Implementation Get all declaration in the class • Properties •
Companion Objects • Functions • Inner classes • etc… KtFile KtClass KtClassBody KtNamedFunction KtNamedFunction -
KtClassBody API public final class KtClassBody { public open fun
getDeclarations(): MutableList<KtDeclaration> { … } } kotlin/compiler/psi/src/org/jetbrains/kotlin/parsing/KtClassBody.kt -
Override Modifier Token override fun hashCode(): Int { … }
KtTokens.OVERRIDE_MODIFER -
KtNamedFunction API public class KtNamedFunction extends KtModifierListOwner { boolean hasModifier(@NotNull
KtModifierKeywordToken token); … } kotlin/compiler/psi/src/org/jetbrains/kotlin/parsing/KtNamedFunction.kt -
class DataClassContainsFunctions: KtTreeVisitorVoid() { override fun visitClass(klass: KtClass) { if
(klass.isData()) { } } } Implementation -
class DataClassContainsFunctions: KtTreeVisitorVoid() { override fun visitClass(klass: KtClass) { if
(klass.isData()) { klass.getBody()!?.declarations } } } Implementation -
class DataClassContainsFunctions: KtTreeVisitorVoid() { override fun visitClass(klass: KtClass) { if
(klass.isData()) { klass.getBody()!?.declarations !?.filterIsInstance<KtNamedFunction>() } } } Implementation -
class DataClassContainsFunctions: KtTreeVisitorVoid() { override fun visitClass(klass: KtClass) {
if (klass.isData()) { klass.getBody()!?.declarations !?.filterIsInstance<KtNamedFunction>() !?.filterNot { it.hasModifier(KtTokens.OVERRIDE_KEYWORD) } } } } Implementation -
class DataClassContainsFunctions: KtTreeVisitorVoid() { override fun visitClass(klass: KtClass) {
if (klass.isData()) { klass.getBody()!?.declarations !?.filterIsInstance<KtNamedFunction>() !?.filterNot { it.hasModifier(KtTokens.OVERRIDE_KEYWORD) } !?.forEach { reportCodeSmell(it.name) } } } } Implementation -
Rule Set: Complexity Description: • Enforce single responsibility principle in
conditions, functions and classes. Rules: • Too Many Functions -
Rule: Too Many Functions Description: • Set a limit to
# of functions you could have in a file, class, interfaces, etc… Threshold (default 11): • Files • Classes • Interfaces • Objects • Enums • Private -
Threshold: 11 !// Non-Compliant fun isPostPublished(…) { } fun
isCommentingEnabled(…) { } fun sharePost(…) { } 30+ methods Rule: Too Many Functions -
Threshold: 11 !// Non-Compliant fun isPostPublished(…) { } @Deprecated
fun isCommentingEnabled(…) { } fun sharePost(…) { } 30+ methods Rule: Too Many Functions Do not count deprecated methods -
Threshold: 11 !// Non-Compliant fun isPostPublished(…) { } @Deprecated
fun isCommentingEnabled(…) { } fun sharePost(…) { } Strategy for Implementing Rule KtFile KtNamedFunction KtNamedFunction KtNamedFunction -
Strategy for Implementing Rule KtFile KtNamedFunction KtNamedFunction KtNamedFunction • Count
each method in the tree. • Compare # of methods to threshold. • Exclude from counting methods that are deprecated. -
• Check function is top level Implementation
KtFile KtNamedFunction KtNamedFunction KtNamedFunction -
KtNamedFunction API public class KtNamedFunction extends KtFunction, KtAnnotated { public
boolean isTopLevel() { KotlinFunctionStub stub = (KotlinFunctionStub)this.getStub(); return stub !!= null ? stub.isTopLevel() : this.getParent() instanceof KtFile; } } kotlin/compiler/psi/src/org/jetbrains/kotlin/psi/KtNamedFunction.java -
• Get all the annotations of a function • Check
if function has @Deprecated annotation Determine function is deprecated KtFile KtNamedFunction KtNamedFunction KtNamedFunction -
@Deprecated fun isCommentingEnabled(…) { } KtNamedFunction KtAnnotationEntry
Determine Function is Deprecated -
Get all annotations public class KtNamedFunction extends KtAnnotated { @NotNull
List<KtAnnotationEntry> getAnnotationEntries(); } kotlin/compiler/psi/src/org/jetbrains/kotlin/psi/KtAnnotated.java KtNamedFunction KtAnnotationEntry -
Determine Function is Deprecated fun KtNamedFunction.isDeprecated() = annotationEntries.any {
it.typeReference!?.text !== "Deprecated" } KtNamedFunction KtAnnotationEntry -
Implementation class TooManyFunctions: KtTreeVisitorVoid() { var thresholdInFiles: Int = 11
var numOfTopLevelFunctions: Int = 0 } Limit for # functions Counter -
class TooManyFunctions: KtTreeVisitorVoid() { var numOfTopLevelFunctions: Int = 0 override
fun visitNamedFunction(function: KtNamedFunction) { } } Implementation KtNamedFunction KtNamedFunction KtNamedFunction -
Implementation class TooManyFunctions: KtTreeVisitorVoid() { var numOfTopLevelFunctions: Int = 0
override fun visitNamedFunction(function: KtNamedFunction) { if (function.isTopLevel !&& !function.isDeprecated()) { } } } KtNamedFunction KtNamedFunction KtNamedFunction -
Implementation class TooManyFunctions: KtTreeVisitorVoid() { var numOfTopLevelFunctions: Int = 0
override fun visitNamedFunction(function: KtNamedFunction) { if (function.isTopLevel !&& !function.isDeprecated()) { numOfTopLevelFunctions!++ } } } KtNamedFunction KtNamedFunction KtNamedFunction -
Rule: Too Many Functions class TooManyFunctions: KtTreeVisitorVoid() { var thresholdInFiles:
Int = 11 override fun visitKtFile(file: KtFile) { if (numOfTopLevelFunctions !>= thresholdInFiles) { reportCodeSmell() } } } KtFile KtNamedFunction KtNamedFunction KtNamedFunction -
Static Code Analysis with Kotlin • Why Static Code Analysis?
• Setup & Configure Detekt • Program Structure Interface (PSI) • How Detekt uses the PSI to find code smells • Write your own Detekt rules -
Writing Custom Rules 1. Create a custom rules modules 2.
Write your custom rule 3. Write a provider for your custom rules 4. Specify fully qualify name of custom rule in a META-INF file. -
Create Custom Rules Module dependencies { compileOnly “io.gitlab.arturbosch.detekt:detekt-api:1.0.0.RC7–4" testCompile "junit:junit:4.12"
testCompile "org.assertj:assertj-core:3.10.0" testCompile “io.gitlab.arturbosch.detekt:detekt-test:1.0.0.RC7–4" } -
Custom Rule Rule: Importing Internal API Description: Prevent internal APIs
in certain modules -
Detekt’s Visitor abstract class Rule(val config: Config) : KtTreeVisitorVoid {
abstract val issue: Issue … } detekt-api/src/main/kotlin/io/gitlab/arturbosch/detekt/api/Rule.kt -
Create Rule class ImportingInternalAPI(config: Config) : Rule(config) { override val
issue = Issue(“Importing Internal API", Severity.Maintainability, “Do not import internal API", Debt.TWENTY_MINS) } -
import org.jetbrains.kotlin.psi.KtImportDirective import com.learn.detektplayground.internal.InternalPostClass import org.jetbrains.kotlin.com.intellij.psi.PsiFileFactory class SomeClass { …
} Create Rule KtFile KtImportDirective KtImportDirective KtImportDirective -
KtImportDirective API public class KtImportDirective { @Nullable public ImportPath getImportPath()
{ FqName importFqn = this.getImportedFqName(); if (importFqn !== null) { return null; } else { Name alias = null; String aliasName = this.getAliasName(); if (aliasName !!= null) { alias = Name.identifier(aliasName); } return new ImportPath(importFqn, this.isAllUnder(), alias); } kotlin/compiler/psi/src/org/jetbrains/kotlin/parsing/KtImportDirective.java -
Custom Rule class ImportingInternalAPI(config: Config) : Rule(config) { override fun
visitImportDirective(importDirective: KtImportDirective) { val import = importDirective.importPath!?.pathStr if (import!?.contains(“api.internal”) !== true) { report(CodeSmell(issue, Entity.from(importDirective), “You are using an internal API.”)) } } } -
Create Rule Set Provider class CustomRuleSetProvider : RuleSetProvider { override
val ruleSetId: String = “custom-rules" override fun instance(config: Config) = RuleSet( ruleSetId, listOf(ImportingInternalAPI(config)) ) } -
Benefits of Static Code Analysis • Building tools around Kotlin
using PSI • Understanding Kotlin Compiler • Building Intellij Plugins