|
| 1 | +package org.example.detekt |
| 2 | + |
| 3 | +import io.gitlab.arturbosch.detekt.api.* |
| 4 | +import org.jetbrains.kotlin.psi.KtClass |
| 5 | +import org.jetbrains.kotlin.psi.KtElement |
| 6 | +import org.jetbrains.kotlin.psi.KtNamedFunction |
| 7 | +import org.jetbrains.kotlin.psi.KtVisitorVoid |
| 8 | +import org.jetbrains.kotlin.resolve.BindingContext |
| 9 | +import org.jetbrains.kotlin.resolve.isValueClassType |
| 10 | +import org.jetbrains.kotlin.types.KotlinType |
| 11 | + |
| 12 | +/** Fixes occurrences of inline data class when enabled to allow for jvm language interop. */ |
| 13 | +class PublicDataClassConstructorWithValueParameters(config: Config) : Rule(config) { |
| 14 | + |
| 15 | + override val issue: Issue = |
| 16 | + Issue( |
| 17 | + javaClass.simpleName, |
| 18 | + Severity.Defect, |
| 19 | + "This rule reports public data classes that are incompatible with other jvm languages.", |
| 20 | + Debt.FIVE_MINS |
| 21 | + ) |
| 22 | + |
| 23 | + /** * Reports inline value classes not marked @JvmInline as a lint warning. */ |
| 24 | + override fun visitClass(ktClass: KtClass) { |
| 25 | + try { |
| 26 | + if ( |
| 27 | + ktClass.isData() && |
| 28 | + ktClass.hasPrimaryConstructor() && |
| 29 | + ktClass.primaryConstructor?.valueParameters?.any { |
| 30 | + val ktpe = bindingContext.get(BindingContext.TYPE, it.typeReference) |
| 31 | + |
| 32 | + (ktpe as KotlinType).unwrap().isValueClassType() |
| 33 | + } == true |
| 34 | + ) { |
| 35 | + var hasJvmStaticConstructor = false |
| 36 | + val hasCompanionObjects = ktClass.companionObjects.isNotEmpty() |
| 37 | + if (!hasCompanionObjects) { |
| 38 | + hasJvmStaticConstructor = false |
| 39 | + } else { |
| 40 | + ktClass.companionObjects.forEach { ktObjectDeclaration -> |
| 41 | + if (!hasJvmStaticConstructor) { |
| 42 | + ktObjectDeclaration.acceptChildren( |
| 43 | + object : KtVisitorVoid() { |
| 44 | + override fun visitKtElement(element: KtElement) { |
| 45 | + try { |
| 46 | + element.acceptChildren(this) |
| 47 | + } catch (_: UnsupportedOperationException) {} |
| 48 | + } |
| 49 | + |
| 50 | + override fun visitNamedFunction(function: KtNamedFunction) { |
| 51 | + if (!hasJvmStaticConstructor) { |
| 52 | + val klassParametersAsString = |
| 53 | + ktClass.primaryConstructor?.valueParameterList?.parameters?.fold("") { |
| 54 | + acc, |
| 55 | + parameter -> |
| 56 | + if (acc == "") { |
| 57 | + acc + parameter.name + ":" + parameter.typeReference?.text |
| 58 | + } else { |
| 59 | + acc + ", " + parameter.name + ":" + parameter.typeReference?.text |
| 60 | + } |
| 61 | + } |
| 62 | + val functionParametersAsStrings = |
| 63 | + function.valueParameterList?.parameters?.fold("") { acc, parameter -> |
| 64 | + if (acc == "") { |
| 65 | + acc + parameter.name + ":" + parameter.typeReference?.text |
| 66 | + } else { |
| 67 | + acc + ", " + parameter.name + ":" + parameter.typeReference?.text |
| 68 | + } |
| 69 | + } |
| 70 | + val functionHasJvmStatic = |
| 71 | + function.annotationEntries.any { it.shortName?.asString() == "JvmStatic" } |
| 72 | + val functionHasAllButLastDefaultConstructorParameter = |
| 73 | + klassParametersAsString == functionParametersAsStrings |
| 74 | + hasJvmStaticConstructor = |
| 75 | + functionHasJvmStatic && functionHasAllButLastDefaultConstructorParameter |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + ) |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + if (!hasJvmStaticConstructor) { |
| 84 | + report( |
| 85 | + CorrectableCodeSmell( |
| 86 | + issue, |
| 87 | + Entity.from(ktClass), |
| 88 | + "Kotlin data classes must have a JVM static factory method to be compatible with other jvm languages.", |
| 89 | + emptyList(), |
| 90 | + listOf(Entity.from(ktClass)), |
| 91 | + true |
| 92 | + ) |
| 93 | + ) |
| 94 | + } |
| 95 | + } |
| 96 | + } catch (_: Exception) {} |
| 97 | + } |
| 98 | +} |
0 commit comments