This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.content.Intent; | |
import android.database.Cursor; | |
import android.os.Bundle; | |
import android.support.v4.app.ListFragment; | |
import android.support.v4.app.LoaderManager; | |
import android.support.v4.content.Loader; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (C) 2014 [email protected] | |
* | |
* Licensed 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val IMPLEMENTATION = Implementation( | |
KotlinShouldNotThrowExceptionsDetector::class.java, | |
Scope.JAVA_FILE_SCOPE | |
) | |
val ISSUE: Issue = Issue.create( | |
id = "KotlinShouldNotThrowExceptionsDetector", | |
briefDescription = "Kotlin code should not throw Exceptions", | |
explanation = """ | |
Kotlin does not support checked Exceptions, so we should not rely on Exceptions to propagate errors in our application. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override fun getApplicableUastTypes(): List<Class<out UElement>>? { | |
return listOf(UMethod::class.java, UCallExpression::class.java) | |
} | |
override fun createUastHandler(context: JavaContext): UElementHandler? { | |
return KotlinThrowsHandler(context) | |
} | |
private inner class KotlinThrowsHandler(private val context: JavaContext) : UElementHandler() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private inner class ThrowCallVisitor(private val context: JavaContext) : AbstractUastVisitor() { | |
override fun visitCallExpression(node: UCallExpression): Boolean { | |
if(node.uastParent is KotlinUThrowExpression) { | |
val type = node.returnType ?: throw IllegalStateException("type of KotlinUThrowExpression cannot be null") | |
if(!isUncheckedException(type)) { | |
reportUsage(context, context.getLocation(element = node)) | |
} | |
} | |
return false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TooManyParametersDetectorTest : LintDetectorTest() { | |
override fun getDetector(): Detector = TODO() | |
override fun getIssues(): MutableList<Issue> = TODO() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TooManyParametersDetector : Detector(), SourceCodeScanner { | |
private fun reportUsage(context: JavaContext, location: Location) { | |
context.report( | |
issue = ISSUE, | |
location = location, | |
message = "Method should not declare more than 5 parameters" | |
) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override fun getDetector(): Detector = TooManyParametersDetector() | |
override fun getIssues(): MutableList<Issue> = mutableListOf(TooManyParametersDetector.ISSUE) | |
@Test | |
fun `java method with 0 parameters`() { | |
val javaFile = java( | |
""" | |
package com.brokoli.lint; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
fun `kotlin method with 6 parameters`() { | |
val kotlinFile = kotlin( | |
""" | |
package com.brokoli.lint; | |
class MyClass { | |
fun methodWith6Parameters(first: Boolean, second: String, third: Int, fourth: Long, fifth: Char, sixth: Boolean) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override fun getApplicableUastTypes(): List<Class<out UElement>>? { | |
return listOf(UMethod::class.java) | |
} | |
override fun createUastHandler(context: JavaContext): UElementHandler? { | |
return MethodHandler(context) | |
} | |
inner class MethodHandler(private val context: JavaContext) : UElementHandler() { | |
OlderNewer