-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create new rule: NoFunctionReferenceToJavaClass (#32)
Drive-by: check in some auto generated changes in `.idea` (probably due to a newer version of IntelliJ)
- Loading branch information
Showing
5 changed files
with
89 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
51 changes: 51 additions & 0 deletions
51
src/main/kotlin/com/faire/detekt/rules/NoFunctionReferenceToJavaClass.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.faire.detekt.rules | ||
|
||
import io.gitlab.arturbosch.detekt.api.CodeSmell | ||
import io.gitlab.arturbosch.detekt.api.Config | ||
import io.gitlab.arturbosch.detekt.api.Debt | ||
import io.gitlab.arturbosch.detekt.api.Entity | ||
import io.gitlab.arturbosch.detekt.api.Issue | ||
import io.gitlab.arturbosch.detekt.api.Rule | ||
import io.gitlab.arturbosch.detekt.api.Severity | ||
import org.jetbrains.kotlin.psi.KtCallableReferenceExpression | ||
|
||
/** | ||
* No use of `::javaClass`; use `.javaClass` instead. | ||
* | ||
* `Foo::bar` gives you a reference to function/property `bar` on class `Foo`: | ||
* https://kotlinlang.org/docs/reflection.html#function-references | ||
* [javaClass] is an extension function on [Any]: | ||
* https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.jvm/java-class.html | ||
* | ||
* 99% of the time, you want to get the JVM class of some class/object instead of the callable reference to this | ||
* extension function. This bug can hide deep if you then proceed to call any method on [Class]: e.g. | ||
* ``` | ||
* println(Foo::javaClass.simpleName) // gives you "javaClass" | ||
* println(Foo.javaClass.simpleName) // gives you "Foo" | ||
* ``` | ||
*/ | ||
internal class NoFunctionReferenceToJavaClass(config: Config = Config.empty) : Rule(config) { | ||
override val issue = Issue( | ||
id = javaClass.simpleName, | ||
severity = Severity.CodeSmell, | ||
description = RULE_DESCRIPTION, | ||
debt = Debt.FIVE_MINS, | ||
) | ||
|
||
override fun visitCallableReferenceExpression(expression: KtCallableReferenceExpression) { | ||
super.visitCallableReferenceExpression(expression) | ||
if (expression.callableReference.getReferencedName() == "javaClass") { | ||
report( | ||
CodeSmell( | ||
issue = issue, | ||
entity = Entity.from(expression), | ||
message = RULE_DESCRIPTION, | ||
), | ||
) | ||
} | ||
} | ||
|
||
companion object { | ||
const val RULE_DESCRIPTION = "Do not call ::javaClass; did you mean .javaClass?" | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/test/kotlin/com/faire/detekt/rules/NoFunctionReferenceToJavaClassTest.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.faire.detekt.rules | ||
|
||
import io.gitlab.arturbosch.detekt.test.lint | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.Test | ||
|
||
internal class NoFunctionReferenceToJavaClassTest { | ||
private val rule: NoFunctionReferenceToJavaClass = NoFunctionReferenceToJavaClass() | ||
|
||
@Test | ||
fun `should report double-colon javaClass`() { | ||
val findings = rule.lint( | ||
""" | ||
class Foo | ||
val buggy = Foo::javaClass.name | ||
""".trimIndent(), | ||
) | ||
|
||
assertThat(findings).hasSize(1) | ||
assertThat(findings.first().issue.id).isEqualTo("NoFunctionReferenceToJavaClass") | ||
} | ||
|
||
@Test | ||
fun `should not report dot javaClass`() { | ||
val findings = rule.lint( | ||
""" | ||
class Foo | ||
val correct = Foo.javaClass.name | ||
""".trimIndent(), | ||
) | ||
|
||
assertThat(findings).isEmpty() | ||
} | ||
} |