Skip to content

Instantly share code, notes, and snippets.

Avatar

Adib Faramarzi adibfara

View GitHub Profile
@adibfara
adibfara / ListedProcessor.kt
Created May 5, 2022
Completing the processor
View ListedProcessor.kt
internal class ListedProcessor(
private val environment: SymbolProcessorEnvironment,
) : SymbolProcessor {
private fun Resolver.findAnnotations(
kClass: KClass<*>,
) = getSymbolsWithAnnotation(
kClass.qualifiedName.toString())
.filterIsInstance<KSFunctionDeclaration>().filter {
@adibfara
adibfara / ListedProcessor.kt
Created May 5, 2022
Simple Code Generator
View ListedProcessor.kt
internal class ListedProcessor(
private val environment: SymbolProcessorEnvironment,
) : SymbolProcessor {
private fun Resolver.findAnnotations(
kClass: KClass<*>,
) = getSymbolsWithAnnotation(
kClass.qualifiedName.toString())
.filterIsInstance<KSFunctionDeclaration>().filter {
it.parameters.isEmpty()
@adibfara
adibfara / build.gradle
Created May 5, 2022
Adding the generator to the application's build.gradle
View build.gradle
// Inside your app's module, declare a dependency to your ksp module
dependencies {
implementation project(":ksp") // since you want to use your @Listed annotation
ksp project(":ksp") // to make KSP work
}
View SymbolProcessorProvider.kt
// Create the directories: yourmodule/src/main/resources/META-INF/services/
// Create a file named com.google.devtools.ksp.processing.SymbolProcessorProvider
// Inside the file, add the following line (and nothing more):
com.snaky.ksp.processor.provider.ListedProcessorProvider
// For a reference, you can check out here:
// https://github.com/adibfara/ListGen/blob/main/ksp/src/main/resources/META-INF/services/com.google.devtools.ksp.processing.SymbolProcessorProvider
View ListedProcessor.kt
import com.google.devtools.ksp.processing.Resolver
import com.google.devtools.ksp.processing.SymbolProcessor
import com.google.devtools.ksp.processing.SymbolProcessorEnvironment
import com.google.devtools.ksp.symbol.KSAnnotated
class ListedProcessor(private val environment: SymbolProcessorEnvironment): SymbolProcessor {
override fun process(resolver: Resolver): List<KSAnnotated> {
return emptyList() // we'll return an empty list for now, since we haven't processed anything.
}
}
@adibfara
adibfara / Listed Annotation.kt
Created May 5, 2022
Create Listed Annotation
View Listed Annotation.kt
@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.SOURCE)
annotation class Listed(
val name: String,
)
@adibfara
adibfara / KSP Setup.kt
Last active May 5, 2022
Setup Guide for KSP
View KSP Setup.kt
// to help the IDE recognize the KSP generated files, add the following to your app's build.gradle
android {
kotlin {
sourceSets.debug {
kotlin.srcDirs += 'build/generated/ksp/debug/kotlin'
}
sourceSets.release {
kotlin.srcDirs += 'build/generated/ksp/release/kotlin'
}
}
View kotlin-contracts-returns-case.kt
/**
* @author Adib Faramarzi (adibfara@gmail.com)
*/
fun Any?.isAValidName():Boolean{
return this!=null && this is String && this.length > 3
}
fun getName():String? { ... }
fun testUserName(){
View kotlin-contracts-run-once.kt
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.contract
/**
* @author Adib Faramarzi (adibfara@gmail.com)
*/
@ExperimentalContracts
fun createOnce(runFunction: ()-> Unit) {
contract {
View enable-kotlin-1-3.gradle
// add kotlin gradle plugin (v1.3 and above)
buildscript {
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3"
}
}