Skip to content

Commit

Permalink
Fix documentation for NoFunctionReferenceToJavaClass (#34)
Browse files Browse the repository at this point in the history
This is more technically accurate.
  • Loading branch information
Hexcles authored May 7, 2024
1 parent c972cc8 commit ae51aa4
Showing 1 changed file with 9 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,21 @@ import io.gitlab.arturbosch.detekt.api.Severity
import org.jetbrains.kotlin.psi.KtCallableReferenceExpression

/**
* No use of `::javaClass`; use `.javaClass` instead.
* Do not call `::javaClass`.
*
* `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.
* extension function. This bug can hide deep if you then proceed to access a name that exists on both
* [java.base.Class] and [kotlin.reflect.KCallable] : e.g.
* ```
* println(Foo::javaClass.simpleName) // gives you "javaClass"
* println(Foo.javaClass.simpleName) // gives you "Foo"
* class Foo
* val foo = Foo()
* println(Foo::javaClass.name) // gives you "javaClass"
* println(foo.javaClass.name) // gives you "Foo"
* ```
*/
internal class NoFunctionReferenceToJavaClass(config: Config = Config.empty) : Rule(config) {
Expand All @@ -46,6 +49,7 @@ internal class NoFunctionReferenceToJavaClass(config: Config = Config.empty) : R
}

companion object {
const val RULE_DESCRIPTION = "Do not call ::javaClass; did you mean .javaClass?"
const val RULE_DESCRIPTION = "Do not call ::javaClass; " +
"did you mean someObject.javaClass or SomeClass::class.java?"
}
}

0 comments on commit ae51aa4

Please sign in to comment.