Skip to content

Commit b7d9866

Browse files
KON-1 Allow To Have Actual Types As Parameters (#3
* update KoComplexDeclaration.representsType method and tests * update KoFile.hasAnnotation method and tests * update KoDeclaration.hasAnnotation method and tests * fix spotless
1 parent c75ec64 commit b7d9866

17 files changed

+120
-62
lines changed

konsist/src/main/kotlin/com/lemon/konsist/core/declaration/KoComplexDeclaration.kt

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import com.lemon.konsist.core.declaration.provider.KoInterfaceProvider
99
import com.lemon.konsist.core.declaration.provider.KoObjectProvider
1010
import com.lemon.konsist.core.declaration.provider.KoPropertyProvider
1111
import org.jetbrains.kotlin.psi.KtClassOrObject
12-
import kotlin.reflect.KClass
1312

1413
abstract class KoComplexDeclaration(
1514
private val ktClassOrObject: KtClassOrObject,
@@ -21,7 +20,9 @@ abstract class KoComplexDeclaration(
2120
KoPropertyProvider,
2221
KoFunctionProvider {
2322

24-
fun representsType(kClass: KClass<*>) = kClass.qualifiedName == fullyQualifiedName
23+
fun representsType(name: String) = name == fullyQualifiedName.substringAfterLast(".")
24+
25+
inline fun <reified T>representsType() = T::class.qualifiedName == fullyQualifiedName
2526

2627
override fun declarations(
2728
modifiers: List<KoModifier>,

konsist/src/main/kotlin/com/lemon/konsist/core/declaration/KoDeclaration.kt

+6-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import com.lemon.konsist.util.PackageHelper
66
import org.jetbrains.kotlin.lexer.KtTokens
77
import org.jetbrains.kotlin.psi.KtTypeParameterListOwner
88
import org.jetbrains.kotlin.psi.psiUtil.isTopLevelKtOrJavaMember
9-
import kotlin.reflect.KClass
109

1110
abstract class KoDeclaration(private val ktTypeParameterListOwner: KtTypeParameterListOwner) :
1211
KoNamedDeclaration(ktTypeParameterListOwner) {
@@ -65,10 +64,13 @@ abstract class KoDeclaration(private val ktTypeParameterListOwner: KtTypeParamet
6564

6665
fun isTopLevel() = ktTypeParameterListOwner.isTopLevelKtOrJavaMember()
6766

68-
fun hasAnnotation(kClass: KClass<*>): Boolean {
69-
val qualifiedName = kClass.qualifiedName ?: return false
67+
fun hasAnnotation(name: String) = annotations
68+
.any { it.fullyQualifiedName?.substringAfterLast(".") == name }
7069

71-
return annotations.any { it.fullyQualifiedName == qualifiedName }
70+
inline fun <reified T> hasAnnotation(): Boolean {
71+
val qualifiedName = T::class.qualifiedName ?: return false
72+
73+
return annotations.any { it.fullyQualifiedName?.contains(qualifiedName) ?: false }
7274
}
7375

7476
fun hasModifiers(vararg koModifiers: KoModifier) = koModifiers.all {

konsist/src/main/kotlin/com/lemon/konsist/core/declaration/KoFile.kt

+5-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import org.jetbrains.kotlin.psi.KtImportDirective
1616
import org.jetbrains.kotlin.psi.KtImportList
1717
import org.jetbrains.kotlin.psi.KtTypeAlias
1818
import java.io.File
19-
import kotlin.reflect.KClass
2019

2120
class KoFile private constructor(private val ktFile: KtFile) :
2221
KoNamedDeclaration(ktFile),
@@ -77,8 +76,11 @@ class KoFile private constructor(private val ktFile: KtFile) :
7776
.map { KoAnnotation.getInstance(it) }
7877
}
7978

80-
fun hasAnnotation(kClass: KClass<*>): Boolean {
81-
val qualifiedName = kClass.qualifiedName ?: return false
79+
fun hasAnnotation(name: String) = annotations
80+
.any { it.fullyQualifiedName?.substringAfterLast(".") == name }
81+
82+
inline fun <reified T> hasAnnotation(): Boolean {
83+
val qualifiedName = T::class.qualifiedName ?: return false
8284

8385
return annotations.any { it.fullyQualifiedName?.contains(qualifiedName) ?: false }
8486
}

konsist/src/test/kotlin/com/lemon/konsist/core/declaration/kocomplexdeclaration/KoComplexDeclarationForClassTest.kt

+4-2
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,10 @@ class KoComplexDeclarationForClassTest {
245245

246246
// then
247247
sut.run {
248-
representsType(SampleClass::class) shouldBeEqualTo true
249-
representsType(SampleType::class) shouldBeEqualTo false
248+
representsType("SampleClass") shouldBeEqualTo true
249+
representsType("SampleType") shouldBeEqualTo false
250+
representsType<SampleClass>() shouldBeEqualTo true
251+
representsType<SampleType>() shouldBeEqualTo false
250252
}
251253
}
252254

konsist/src/test/kotlin/com/lemon/konsist/core/declaration/kocomplexdeclaration/KoComplexDeclarationForCompanionObjectTest.kt

+4-2
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,10 @@ class KoComplexDeclarationForCompanionObjectTest {
280280

281281
// then
282282
sut.run {
283-
representsType(SampleCompanionObject::class) shouldBeEqualTo true
284-
representsType(SampleType::class) shouldBeEqualTo false
283+
representsType("SampleCompanionObject") shouldBeEqualTo true
284+
representsType("SampleType") shouldBeEqualTo false
285+
representsType<SampleCompanionObject>() shouldBeEqualTo true
286+
representsType<SampleType>() shouldBeEqualTo false
285287
}
286288
}
287289

konsist/src/test/kotlin/com/lemon/konsist/core/declaration/kocomplexdeclaration/KoComplexDeclarationForInterfaceTest.kt

+4-2
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,10 @@ class KoComplexDeclarationForInterfaceTest {
244244

245245
// then
246246
sut.run {
247-
representsType(SampleInterface::class) shouldBeEqualTo true
248-
representsType(SampleType::class) shouldBeEqualTo false
247+
representsType("SampleInterface") shouldBeEqualTo true
248+
representsType("SampleType") shouldBeEqualTo false
249+
representsType<SampleInterface>() shouldBeEqualTo true
250+
representsType<SampleType>() shouldBeEqualTo false
249251
}
250252
}
251253

konsist/src/test/kotlin/com/lemon/konsist/core/declaration/kocomplexdeclaration/KoComplexDeclarationForObjectTest.kt

+4-2
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,10 @@ class KoComplexDeclarationForObjectTest {
241241

242242
// then
243243
sut.run {
244-
representsType(SampleObject::class) shouldBeEqualTo true
245-
representsType(SampleType::class) shouldBeEqualTo false
244+
representsType("SampleObject") shouldBeEqualTo true
245+
representsType("SampleType") shouldBeEqualTo false
246+
representsType<SampleObject>() shouldBeEqualTo true
247+
representsType<SampleType>() shouldBeEqualTo false
246248
}
247249
}
248250

konsist/src/test/kotlin/com/lemon/konsist/core/declaration/kodeclaration/KoDeclarationForClassTest.kt

+12-6
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,10 @@ class KoDeclarationForClassTest {
5757
// then
5858
sut.run {
5959
annotations shouldHaveSize 1
60-
hasAnnotation(SampleAnnotation::class) shouldBeEqualTo true
61-
hasAnnotation(NonExistingAnnotation::class) shouldBeEqualTo false
60+
hasAnnotation("SampleAnnotation") shouldBeEqualTo true
61+
hasAnnotation("NonExistingAnnotation") shouldBeEqualTo false
62+
hasAnnotation<SampleAnnotation>() shouldBeEqualTo true
63+
hasAnnotation<NonExistingAnnotation>() shouldBeEqualTo false
6264
}
6365
}
6466

@@ -72,9 +74,12 @@ class KoDeclarationForClassTest {
7274
// then
7375
sut.run {
7476
annotations shouldHaveSize 2
75-
hasAnnotation(SampleAnnotation1::class) shouldBeEqualTo true
76-
hasAnnotation(SampleAnnotation2::class) shouldBeEqualTo true
77-
hasAnnotation(NonExistingAnnotation::class) shouldBeEqualTo false
77+
hasAnnotation("SampleAnnotation1") shouldBeEqualTo true
78+
hasAnnotation("SampleAnnotation2") shouldBeEqualTo true
79+
hasAnnotation("NonExistingAnnotation") shouldBeEqualTo false
80+
hasAnnotation<SampleAnnotation1>() shouldBeEqualTo true
81+
hasAnnotation<SampleAnnotation2>() shouldBeEqualTo true
82+
hasAnnotation<NonExistingAnnotation>() shouldBeEqualTo false
7883
}
7984
}
8085

@@ -255,5 +260,6 @@ class KoDeclarationForClassTest {
255260
}
256261
}
257262

258-
private fun getSut(fileName: String) = getSnippetKoScope("core/declaration/kodeclaration/snippet/forclass/", fileName)
263+
private fun getSut(fileName: String) =
264+
getSnippetKoScope("core/declaration/kodeclaration/snippet/forclass/", fileName)
259265
}

konsist/src/test/kotlin/com/lemon/konsist/core/declaration/kodeclaration/KoDeclarationForCompanionObjectTest.kt

+10-5
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,10 @@ class KoDeclarationForCompanionObjectTest {
5757
// then
5858
sut.run {
5959
annotations shouldHaveSize 1
60-
hasAnnotation(SampleAnnotation::class) shouldBeEqualTo true
61-
hasAnnotation(NonExistingAnnotation::class) shouldBeEqualTo false
60+
hasAnnotation("SampleAnnotation") shouldBeEqualTo true
61+
hasAnnotation("NonExistingAnnotation") shouldBeEqualTo false
62+
hasAnnotation<SampleAnnotation>() shouldBeEqualTo true
63+
hasAnnotation<NonExistingAnnotation>() shouldBeEqualTo false
6264
}
6365
}
6466

@@ -72,9 +74,12 @@ class KoDeclarationForCompanionObjectTest {
7274
// then
7375
sut.run {
7476
annotations shouldHaveSize 2
75-
hasAnnotation(SampleAnnotation1::class) shouldBeEqualTo true
76-
hasAnnotation(SampleAnnotation2::class) shouldBeEqualTo true
77-
hasAnnotation(NonExistingAnnotation::class) shouldBeEqualTo false
77+
hasAnnotation("SampleAnnotation1") shouldBeEqualTo true
78+
hasAnnotation("SampleAnnotation2") shouldBeEqualTo true
79+
hasAnnotation("NonExistingAnnotation") shouldBeEqualTo false
80+
hasAnnotation<SampleAnnotation1>() shouldBeEqualTo true
81+
hasAnnotation<SampleAnnotation2>() shouldBeEqualTo true
82+
hasAnnotation<NonExistingAnnotation>() shouldBeEqualTo false
7883
}
7984
}
8085

konsist/src/test/kotlin/com/lemon/konsist/core/declaration/kodeclaration/KoDeclarationForFunctionTest.kt

+12-6
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,10 @@ class KoDeclarationForFunctionTest {
5757
// then
5858
sut.run {
5959
annotations shouldHaveSize 1
60-
hasAnnotation(SampleAnnotation::class) shouldBeEqualTo true
61-
hasAnnotation(NonExistingAnnotation::class) shouldBeEqualTo false
60+
hasAnnotation("SampleAnnotation") shouldBeEqualTo true
61+
hasAnnotation("NonExistingAnnotation") shouldBeEqualTo false
62+
hasAnnotation<SampleAnnotation>() shouldBeEqualTo true
63+
hasAnnotation<NonExistingAnnotation>() shouldBeEqualTo false
6264
}
6365
}
6466

@@ -72,9 +74,12 @@ class KoDeclarationForFunctionTest {
7274
// then
7375
sut.run {
7476
annotations shouldHaveSize 2
75-
hasAnnotation(SampleAnnotation1::class) shouldBeEqualTo true
76-
hasAnnotation(SampleAnnotation2::class) shouldBeEqualTo true
77-
hasAnnotation(NonExistingAnnotation::class) shouldBeEqualTo false
77+
hasAnnotation("SampleAnnotation1") shouldBeEqualTo true
78+
hasAnnotation("SampleAnnotation2") shouldBeEqualTo true
79+
hasAnnotation("NonExistingAnnotation") shouldBeEqualTo false
80+
hasAnnotation<SampleAnnotation1>() shouldBeEqualTo true
81+
hasAnnotation<SampleAnnotation2>() shouldBeEqualTo true
82+
hasAnnotation<NonExistingAnnotation>() shouldBeEqualTo false
7883
}
7984
}
8085

@@ -255,5 +260,6 @@ class KoDeclarationForFunctionTest {
255260
}
256261
}
257262

258-
private fun getSut(fileName: String) = getSnippetKoScope("core/declaration/kodeclaration/snippet/forfunction/", fileName)
263+
private fun getSut(fileName: String) =
264+
getSnippetKoScope("core/declaration/kodeclaration/snippet/forfunction/", fileName)
259265
}

konsist/src/test/kotlin/com/lemon/konsist/core/declaration/kodeclaration/KoDeclarationForInterfaceTest.kt

+12-6
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,10 @@ class KoDeclarationForInterfaceTest {
5454

5555
// then
5656
sut.run {
57-
hasAnnotation(SampleAnnotation::class) shouldBeEqualTo true
58-
hasAnnotation(NonExistingAnnotation::class) shouldBeEqualTo false
57+
hasAnnotation("SampleAnnotation") shouldBeEqualTo true
58+
hasAnnotation("NonExistingAnnotation") shouldBeEqualTo false
59+
hasAnnotation<SampleAnnotation>() shouldBeEqualTo true
60+
hasAnnotation<NonExistingAnnotation>() shouldBeEqualTo false
5961
}
6062
}
6163

@@ -68,9 +70,12 @@ class KoDeclarationForInterfaceTest {
6870

6971
// then
7072
sut.run {
71-
hasAnnotation(SampleAnnotation1::class) shouldBeEqualTo true
72-
hasAnnotation(SampleAnnotation2::class) shouldBeEqualTo true
73-
hasAnnotation(NonExistingAnnotation::class) shouldBeEqualTo false
73+
hasAnnotation("SampleAnnotation1") shouldBeEqualTo true
74+
hasAnnotation("SampleAnnotation2") shouldBeEqualTo true
75+
hasAnnotation("NonExistingAnnotation") shouldBeEqualTo false
76+
hasAnnotation<SampleAnnotation1>() shouldBeEqualTo true
77+
hasAnnotation<SampleAnnotation2>() shouldBeEqualTo true
78+
hasAnnotation<NonExistingAnnotation>() shouldBeEqualTo false
7479
}
7580
}
7681

@@ -251,5 +256,6 @@ class KoDeclarationForInterfaceTest {
251256
}
252257
}
253258

254-
private fun getSut(fileName: String) = getSnippetKoScope("core/declaration/kodeclaration/snippet/forinterface/", fileName)
259+
private fun getSut(fileName: String) =
260+
getSnippetKoScope("core/declaration/kodeclaration/snippet/forinterface/", fileName)
255261
}

konsist/src/test/kotlin/com/lemon/konsist/core/declaration/kodeclaration/KoDeclarationForObjectTest.kt

+10-5
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,10 @@ class KoDeclarationForObjectTest {
5757
// then
5858
sut.run {
5959
annotations shouldHaveSize 1
60-
hasAnnotation(SampleAnnotation::class) shouldBeEqualTo true
61-
hasAnnotation(NonExistingAnnotation::class) shouldBeEqualTo false
60+
hasAnnotation("SampleAnnotation") shouldBeEqualTo true
61+
hasAnnotation("NonExistingAnnotation") shouldBeEqualTo false
62+
hasAnnotation<SampleAnnotation>() shouldBeEqualTo true
63+
hasAnnotation<NonExistingAnnotation>() shouldBeEqualTo false
6264
}
6365
}
6466

@@ -72,9 +74,12 @@ class KoDeclarationForObjectTest {
7274
// then
7375
sut.run {
7476
annotations shouldHaveSize 2
75-
hasAnnotation(SampleAnnotation1::class) shouldBeEqualTo true
76-
hasAnnotation(SampleAnnotation2::class) shouldBeEqualTo true
77-
hasAnnotation(NonExistingAnnotation::class) shouldBeEqualTo false
77+
hasAnnotation("SampleAnnotation1") shouldBeEqualTo true
78+
hasAnnotation("SampleAnnotation2") shouldBeEqualTo true
79+
hasAnnotation("NonExistingAnnotation") shouldBeEqualTo false
80+
hasAnnotation<SampleAnnotation1>() shouldBeEqualTo true
81+
hasAnnotation<SampleAnnotation2>() shouldBeEqualTo true
82+
hasAnnotation<NonExistingAnnotation>() shouldBeEqualTo false
7883
}
7984
}
8085

konsist/src/test/kotlin/com/lemon/konsist/core/declaration/kodeclaration/KoDeclarationForPrimaryConstructorTest.kt

+6-3
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,10 @@ class KoDeclarationForPrimaryConstructorTest {
120120
// then
121121
sut?.let {
122122
it.annotations.map { annotation -> annotation.name } shouldBeEqualTo listOf("SampleAnnotation")
123-
it.hasAnnotation(SampleAnnotation::class) shouldBeEqualTo true
124-
it.hasAnnotation(SampleAnnotation1::class) shouldBeEqualTo false
123+
it.hasAnnotation("SampleAnnotation") shouldBeEqualTo true
124+
it.hasAnnotation("SampleAnnotation1") shouldBeEqualTo false
125+
it.hasAnnotation<SampleAnnotation>() shouldBeEqualTo true
126+
it.hasAnnotation<SampleAnnotation1>() shouldBeEqualTo false
125127
}
126128
}
127129

@@ -136,7 +138,8 @@ class KoDeclarationForPrimaryConstructorTest {
136138
// then
137139
sut?.let {
138140
it.annotations.isEmpty() shouldBeEqualTo true
139-
it.hasAnnotation(SampleAnnotation::class) shouldBeEqualTo false
141+
it.hasAnnotation("SampleAnnotation") shouldBeEqualTo false
142+
it.hasAnnotation<SampleAnnotation>() shouldBeEqualTo false
140143
}
141144
}
142145

konsist/src/test/kotlin/com/lemon/konsist/core/declaration/kodeclaration/KoDeclarationForPropertyTest.kt

+12-6
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,10 @@ class KoDeclarationForPropertyTest {
5757
// then
5858
sut.run {
5959
annotations shouldHaveSize 1
60-
hasAnnotation(SampleAnnotation::class) shouldBeEqualTo true
61-
hasAnnotation(NonExistingAnnotation::class) shouldBeEqualTo false
60+
hasAnnotation("SampleAnnotation") shouldBeEqualTo true
61+
hasAnnotation("NonExistingAnnotation") shouldBeEqualTo false
62+
hasAnnotation<SampleAnnotation>() shouldBeEqualTo true
63+
hasAnnotation<NonExistingAnnotation>() shouldBeEqualTo false
6264
}
6365
}
6466

@@ -72,9 +74,12 @@ class KoDeclarationForPropertyTest {
7274
// then
7375
sut.run {
7476
annotations shouldHaveSize 2
75-
hasAnnotation(SampleAnnotation1::class) shouldBeEqualTo true
76-
hasAnnotation(SampleAnnotation2::class) shouldBeEqualTo true
77-
hasAnnotation(NonExistingAnnotation::class) shouldBeEqualTo false
77+
hasAnnotation("SampleAnnotation1") shouldBeEqualTo true
78+
hasAnnotation("SampleAnnotation2") shouldBeEqualTo true
79+
hasAnnotation("NonExistingAnnotation") shouldBeEqualTo false
80+
hasAnnotation<SampleAnnotation1>() shouldBeEqualTo true
81+
hasAnnotation<SampleAnnotation2>() shouldBeEqualTo true
82+
hasAnnotation<NonExistingAnnotation>() shouldBeEqualTo false
7883
}
7984
}
8085

@@ -255,5 +260,6 @@ class KoDeclarationForPropertyTest {
255260
}
256261
}
257262

258-
private fun getSut(fileName: String) = getSnippetKoScope("core/declaration/kodeclaration/snippet/forproperty/", fileName)
263+
private fun getSut(fileName: String) =
264+
getSnippetKoScope("core/declaration/kodeclaration/snippet/forproperty/", fileName)
259265
}

konsist/src/test/kotlin/com/lemon/konsist/core/declaration/kodeclaration/KoDeclarationForSecondaryConstructorTest.kt

+6-3
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,10 @@ class KoDeclarationForSecondaryConstructorTest {
127127
// then
128128
sut.run {
129129
annotations.map { it.name } shouldBeEqualTo listOf("SampleAnnotation")
130-
hasAnnotation(SampleAnnotation::class) shouldBeEqualTo true
131-
hasAnnotation(SampleAnnotation1::class) shouldBeEqualTo false
130+
hasAnnotation("SampleAnnotation") shouldBeEqualTo true
131+
hasAnnotation("SampleAnnotation1") shouldBeEqualTo false
132+
hasAnnotation<SampleAnnotation>() shouldBeEqualTo true
133+
hasAnnotation<SampleAnnotation1>() shouldBeEqualTo false
132134
}
133135
}
134136

@@ -144,7 +146,8 @@ class KoDeclarationForSecondaryConstructorTest {
144146
// then
145147
sut.run {
146148
annotations.isEmpty() shouldBeEqualTo true
147-
hasAnnotation(SampleAnnotation::class) shouldBeEqualTo false
149+
hasAnnotation("SampleAnnotation") shouldBeEqualTo false
150+
hasAnnotation<SampleAnnotation>() shouldBeEqualTo false
148151
}
149152
}
150153

konsist/src/test/kotlin/com/lemon/konsist/core/declaration/kodeclaration/KoDeclarationForTypeAliasTest.kt

+4-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ class KoDeclarationForTypeAliasTest {
3131
// then
3232
sut.run {
3333
annotations shouldHaveSize 1
34-
hasAnnotation(SampleAnnotation::class) shouldBeEqualTo true
35-
hasAnnotation(NonExistingAnnotation::class) shouldBeEqualTo false
34+
hasAnnotation("SampleAnnotation") shouldBeEqualTo true
35+
hasAnnotation("NonExistingAnnotation") shouldBeEqualTo false
36+
hasAnnotation<SampleAnnotation>() shouldBeEqualTo true
37+
hasAnnotation<NonExistingAnnotation>() shouldBeEqualTo false
3638
}
3739
}
3840

0 commit comments

Comments
 (0)