Skip to content

Instantly share code, notes, and snippets.

View guilhermekrz's full-sized avatar

Guilherme Krzisch guilhermekrz

View GitHub Profile
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;
@guilhermekrz
guilhermekrz / CursorRecyclerViewAdapter
Last active January 6, 2018 14:44
Ormlite+RecyclerView
/*
* 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
@guilhermekrz
guilhermekrz / KotlinShouldNotThrowExceptionsDetector-Issue.kt
Last active June 2, 2020 00:24
KotlinShouldNotThrowExceptionsDetector Issue
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.
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() {
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
class TooManyParametersDetectorTest : LintDetectorTest() {
override fun getDetector(): Detector = TODO()
override fun getIssues(): MutableList<Issue> = TODO()
}
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"
)
}
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;
@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) {
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() {