Skip to content

Instantly share code, notes, and snippets.

View mehdiyari's full-sized avatar

Mehdi Yari mehdiyari

View GitHub Profile
/**
* This is simple processor that use KSP API to generate
* extension function for annotated classes with [HelloWorldAnnotation]
*/
class HelloWorldProcessor(
private val logger: KSPLogger,
private val codeGenerator: CodeGenerator
) : SymbolProcessor {
override fun process(resolver: Resolver): List<KSAnnotated> {
@mehdiyari
mehdiyari / MetaData.kt
Last active April 22, 2022 16:48
This gist is part of meta-programming with kotlin articles
@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.CLASS)
@SinceKotlin("1.3")
annotation class Metadata(
@get:JvmName("k") val kind: Int = 1,
@get:JvmName("mv") val metadataVersion: IntArray = [],
@get:JvmName("bv") val bytecodeVersion: IntArray = [1, 0, 3],
@get:JvmName("di") val data1: Array<String> = [],
@get:JvmName("d2") val data2: Array<String> = [],
@get:JvmName("xs") val extraString: String = "",
@mehdiyari
mehdiyari / HelloWorldAnnotation.kt
Created April 22, 2022 16:43
This gist is part of meta-programming with kotlin articles
// Declare Annotation in Kotlin
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.SOURCE)
@Repeatable
annotation class HelloWorldAnnotation
@mehdiyari
mehdiyari / HelloWorldProcessor.kt
Created March 31, 2022 16:37
This gist is part of meta-programming with kotlin articles
/**
* This is simple processor that use JSR-269 API and kotlinpoet library to generate
* extension function for annotated classes with [HelloWorldAnnotation]
*/
class HelloWorldProcessor : AbstractProcessor() {
override fun process(annotations: MutableSet<out TypeElement>?, roundEnv: RoundEnvironment?): Boolean {
val typeElement = annotations?.firstOrNull() ?: return false
for (annotatedElement in roundEnv?.getElementsAnnotatedWith(typeElement) ?: setOf()) {
@mehdiyari
mehdiyari / RefelectionNotAllowed.kt
Created March 1, 2022 08:55
This gist is part of meta-programming with kotlin articles
/**
* Simply security manager that check the packages and if equal
* to "java.lang.reflect" or "kotlin.reflect" throws [SecurityException]
*/
class AppSecurityManager : SecurityManager() {
override fun checkPackageAccess(pkg: String?) {
if (pkg == "java.lang.reflect" || pkg == "kotlin.reflect") {
throw SecurityException("Reflection is not allowed in this program")
}
@mehdiyari
mehdiyari / Singleton.kt
Created March 1, 2022 08:19
This gist is part of meta-programming with kotlin articles
/**
* Simple singleton class with double-check pattern
*/
class Singleton private constructor() {
private val data: String = "data property value"
@Volatile
private var singleton: Singleton? = null
@mehdiyari
mehdiyari / RefelectionSideEffect.kt
Created March 1, 2022 08:17
This gist is part of meta-programming with kotlin articles
fun main() {
val singletonKClass = Singleton::class
singletonKClass.constructors.firstOrNull { it.visibility == KVisibility.PRIVATE }?.also { privateConstructor ->
/**
* We give access to private constructor and we can create instance
* from [Singleton] class [privateConstructor.call] method
*/
privateConstructor.isAccessible = true
val singletonNewObject = privateConstructor.call()
dependencies {
implementation "org.jetbrains.kotlin:kotlin-reflect:{latest_version}"
}
import java.math.BigDecimal
import java.nio.ByteBuffer
import java.nio.charset.StandardCharsets
import java.util.*
import java.util.regex.Pattern
/**
* simple Base85/Ascii85 encoder-decoder for kotlin
* @see <a href="Base85">https://en.wikipedia.org/wiki/Ascii85</a>
*/