Skip to content

Commit

Permalink
Small code refactoring (#44)
Browse files Browse the repository at this point in the history
* Small code refactoring: ise compileTime() function, add descriptions in the SmartReflekt DSL
  • Loading branch information
nbirillo authored Jun 16, 2021
1 parent 8375541 commit eb66830
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ fun main() {
val smartClasses = SmartReflekt.classes<BInterface>().filter { it.isData() }.resolve()
println(smartClasses)

val smartObjects = SmartReflekt.objects<BInterface>().filter { it.isCompanion() }.resolve()
println(smartObjects)

val smartFunctions = SmartReflekt.functions<() -> Unit>().filter { it.isTopLevel && it.name == "foo" }.resolve()
println(smartFunctions)
smartFunctions.forEach { it() }
Expand Down
13 changes: 7 additions & 6 deletions reflekt-dsl/src/main/kotlin/io/reflekt/ReflektImpl.kt
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
package io.reflekt

import io.reflekt.models.compileTime
import kotlin.reflect.KClass

@Suppress("unused")
@Suppress("UNUSED_PARAMETER")
object ReflektImpl {
class Objects {
fun <T> withSubTypes(fqNames: Set<String>) = WithSubTypes<T>(fqNames)
fun <T> withAnnotations(annotationFqNames: Set<String>, subtypeFqNames: Set<String>) =
WithAnnotations<T>(annotationFqNames, subtypeFqNames)

class WithSubTypes<T>(val fqNames: Set<String>) {
fun toList(): List<T> = error("This method should be replaced during compilation")
fun toList(): List<T> = compileTime()
fun toSet(): Set<T> = toList().toSet()
}

class WithAnnotations<T>(val annotationFqNames: Set<String>, subtypeFqNames: Set<String>) {
fun toList(): List<T> = error("This method should be replaced during compilation")
fun toList(): List<T> = compileTime()
fun toSet(): Set<T> = toList().toSet()
}
}
Expand All @@ -26,20 +27,20 @@ object ReflektImpl {
WithAnnotations<T>(annotationFqNames, subtypeFqNames)

class WithSubTypes<T: Any>(val fqNames: Set<String>) {
fun toList(): List<KClass<T>> = error("This method should be replaced during compilation")
fun toList(): List<KClass<T>> = compileTime()
fun toSet(): Set<KClass<T>> = toList().toSet()
}

class WithAnnotations<T: Any>(val annotationFqNames: Set<String>, subtypeFqNames: Set<String>) {
fun toList(): List<KClass<T>> = error("This method should be replaced during compilation")
fun toList(): List<KClass<T>> = compileTime()
fun toSet(): Set<KClass<T>> = toList().toSet()
}
}

class Functions {
// T - returned class
class WithAnnotations<T: Function<*>>(val annotationFqNames: Set<String>) {
fun toList(): List<T> = error("This method should be replaced during compilation")
fun toList(): List<T> = compileTime()
fun toSet(): Set<T> = toList().toSet()
}

Expand Down
43 changes: 43 additions & 0 deletions reflekt-dsl/src/main/kotlin/io/reflekt/SmartReflekt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,69 @@ import org.jetbrains.kotlin.psi.KtNamedFunction
import org.jetbrains.kotlin.psi.KtObjectDeclaration
import kotlin.reflect.KClass

/*
* The main SmartReflekt DSL for `multi-module` projects
* */
@Suppress("UNUSED_PARAMETER")
object SmartReflekt {
/*
* Find all classes in the project's modules and external libraries (that was marked as libraries to introspect)
* and filter them by user's condition.
* */
class ClassCompileTimeExpression<T : Any> {
/*
* Filter classes by user's condition. All classes will be cast to [T] type.
*/
fun filter(filter: (KtClass) -> Boolean): ClassCompileTimeExpression<T> = compileTime()
/*
* Resolve user's condition - find all classes that satisfy the condition from the filter function.
*/
fun resolve(): List<KClass<T>> = compileTime()
}

/*
* The main function for searching classes. The chain of calls has to end with resolve() function.
*
* For example:
* SmartReflekt.classes<BInterface>().filter { it.isData() }.resolve()
* */
fun <T : Any> classes(): ClassCompileTimeExpression<T> = compileTime()

class ObjectCompileTimeExpression<T : Any> {
/*
* Filter objects by user's condition. All objects will be cast to [T] type.
*/
fun filter(filter: (KtObjectDeclaration) -> Boolean): ObjectCompileTimeExpression<T> = compileTime()
/*
* Resolve user's condition - find all objects that satisfy the condition from the filter function.
*/
fun resolve(): List<T> = compileTime()
}

/*
* The main function for searching objects. The chain of calls has to end with resolve() function.
*
* For example:
* SmartReflekt.objects<BInterface>().filter { it.isCompanion() }.resolve()
* */
fun <T : Any> objects(): ObjectCompileTimeExpression<T> = compileTime()

class FunctionCompileTimeExpression<T : Function<*>> {
/*
* Filter functions by user's condition. All functions will have the same signature.
*/
fun filter(filter: (KtNamedFunction) -> Boolean): FunctionCompileTimeExpression<T> = compileTime()
/*
* Resolve user's condition - find all functions that satisfy the condition from the filter function.
*/
fun resolve(): List<T> = compileTime()
}

/*
* The main function for searching functions. The chain of calls has to end with resolve() function.
*
* For example:
* SmartReflekt.functions<() -> Unit>().filter { it.isTopLevel && it.name == "foo" }.resolve()
* */
fun <T : Function<*>> functions(): FunctionCompileTimeExpression<T> = compileTime()
}

0 comments on commit eb66830

Please sign in to comment.