This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Application : Application() { | |
override fun onCreate() { | |
super.onCreate() | |
if (BuildConfig.DEBUG) { | |
applyStrictMode() | |
} | |
} | |
private fun applyStrictMode() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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> { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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 = "", |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Declare Annotation in Kotlin | |
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION) | |
@Retention(AnnotationRetention.SOURCE) | |
@Repeatable | |
annotation class HelloWorldAnnotation |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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()) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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") | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Simple singleton class with double-check pattern | |
*/ | |
class Singleton private constructor() { | |
private val data: String = "data property value" | |
@Volatile | |
private var singleton: Singleton? = null |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dependencies { | |
implementation "org.jetbrains.kotlin:kotlin-reflect:{latest_version}" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | |
*/ |