<commit_before>package com.marcinmoskala.activitystarter

import activitystarter.ActivityStarterNameConstruction
import activitystarter.Arg
import android.app.Activity
import android.app.Fragment
import java.lang.reflect.Method
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
import kotlin.reflect.full.findAnnotation

fun <T> Activity.argExtra(default: T? = null) = BoundToArgValueDelegateProvider(default)

fun <T> Fragment.argExtra(default: T? = null) = BoundToArgValueDelegateProvider(default)

fun <T> android.support.v4.app.Fragment.argExtra(default: T? = null) = BoundToArgValueDelegateProvider(default)

class BoundToArgValueDelegateProvider<T>(val default: T? = null) {

    operator fun provideDelegate(
            thisRef: Any?,
            prop: KProperty<*>
    ): ReadWriteProperty<Any, T> {
        val annotation = prop.getter.findAnnotation<Arg>()
        when {
            annotation == null -> throw Error(ErrorMessages.noAnnotation)
            annotation.optional && !prop.returnType.isMarkedNullable && default == null -> throw Error(ErrorMessages.optionalValueNeeded)
        }
        return BoundToValueDelegate(default)
    }
}

internal object ErrorMessages {
    const val noAnnotation = "Element getter must be annotated with Arg"
    const val optionalValueNeeded = "Arguments that are optional and have not-nullable type must have defaut value specified"
}

private class BoundToValueDelegate<T>(var default: T?) : ReadWriteProperty<Any, T> {

    var valueSet = false
    var value: T? = null

    override fun getValue(thisRef: Any, property: KProperty<*>): T {
        synchronized(this) {
            if (valueSet) @Suppress("UNCHECKED_CAST") return value as T
            val typedValue = getValueFromStarter(thisRef, thisRef.javaClass, property.name, default)
            setNewValue(typedValue)
            @Suppress("UNCHECKED_CAST") return typedValue as T<commit_msg>Prevent annotation processing error<commit_after>package com.marcinmoskala.activitystarter

import activitystarter.ActivityStarterNameConstruction
import activitystarter.Arg
import android.app.Activity
import android.app.Fragment
import java.lang.reflect.Method
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
import kotlin.reflect.full.findAnnotation

fun <T> Activity.argExtra(default: T? = null) = BoundToArgValueDelegateProvider(default)

fun <T> Fragment.argExtra(default: T? = null) = BoundToArgValueDelegateProvider(default)

fun <T> android.support.v4.app.Fragment.argExtra(default: T? = null) = BoundToArgValueDelegateProvider(default)

class BoundToArgValueDelegateProvider<T>(val default: T? = null) {

    operator fun provideDelegate(
            thisRef: Any?,
            prop: KProperty<*>
    ): ReadWriteProperty<Any, T> {
        val annotation = try {
            prop.getter.findAnnotation<Arg>()
        } catch (e: Error) {
            return BoundToValueDelegate(default)
        }
        when {
            annotation == null -> throw Error(ErrorMessages.noAnnotation)
            annotation.optional && !prop.returnType.isMarkedNullable && default == null -> throw Error(ErrorMessages.optionalValueNeeded)
        }
        return BoundToValueDelegate(default)
    }
}

internal object ErrorMessages {
    const val noAnnotation = "Element getter must be annotated with Arg"
    const val optionalValueNeeded = "Arguments that are optional and have not-nullable type must have defaut value specified"
}

private class BoundToValueDelegate<T>(var default: T?) : ReadWriteProperty<Any, T> {

    var valueSet = false
    var value: T? = null

    override fun getValue(thisRef: Any, property: KProperty<*>): T {
        synchronized(this) {
            if (valueSet) @Suppress("UNCHECKED_CAST") return value as T
            val typedValue = getValueFromStarter(thisRef, thisRef.javaClass, property.name, default)
            setNewValue(typedValue)
            @Suppress("UNCHECKED_CAST") return typedValue as T
{'fraction_non_alphanumeric': 0.08925, 'fraction_numerical': 0.0005, 'mean_word_length': 3.89119804400978, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5566839', 'n_tokens_mistral': 1112, 'n_tokens_neox': 1081, 'n_words': 280}