Dominik
10/30/2024, 10:11 AMFooClass and an enum BarEnum.
class FooClass {
val enum: BarEnum = BarEnum.BAR
}
enum class BarEnum {
BAR
}
Now what I need to figure out is:
Does FooClass contain a property of any Enum type?
Happy for any ideas/input.Blaž Vantur
10/30/2024, 10:30 AM@Test
fun `has class enum in it`() {
Konsist
.scopeFromProject()
.classes().filter { it.name == "FooClass" }.assertTrue {
it.properties().any {
(it.type?.declaration as? KoClassDeclaration)?.hasEnumModifier == true
}
}
}
But maybe there is a more efficient way of doing this. 😅Dominik
10/30/2024, 10:49 AMDominik
10/30/2024, 10:50 AMKoClassDeclaration but a KoExternalDeclarationCoreBlaž Vantur
10/30/2024, 11:01 AM@Test
fun `has class enum in it`() {
Konsist
.scopeFromProject()
.classes().filter { it.name == "FooClass" }.assertTrue {
it.properties().any { property ->
property.type?.containingDeclaration.toString() == "enum"
}
}
}Dominik
10/30/2024, 12:01 PMtest it does not work anymorePoisonedYouth
10/30/2024, 1:00 PMKonsist
.scopeFromProject()
.classes()
.withName("FooClass")
.assertTrue {
it.properties().any {
it.hasTypeOf(BarEnum::class)
}
}Dominik
10/30/2024, 2:01 PMDominik
10/30/2024, 2:01 PMit.hasTypeOf(Enum::class) because internally every enum is of the internal type Enum but it did not workPoisonedYouth
10/30/2024, 2:27 PMisEnum is currently missing for the parameter type. You can just try a workaround like below:
Class.forName(...).isEnum
I did not check if it is possible to get the information from the KoPropertyDeclaration to get the class.Dominik
10/30/2024, 8:02 PMClass.forNamePoisonedYouth
10/30/2024, 8:09 PMDominik
10/30/2024, 8:16 PMPoisonedYouth
10/30/2024, 8:26 PMigor.wojda
11/04/2024, 3:38 PMNatalia Peterwas
11/25/2024, 1:48 PMKonsist
.scopeFromProduction()
.classes()
.properties()
.types
.assertTrue {
it
.sourceDeclaration
?.asClassDeclaration()
?.hasEnumModifier == true
}