<commit_before>package com.dbflow5.processor.definition.column

import com.dbflow5.annotation.ConflictAction
import com.dbflow5.data.Blob
import com.dbflow5.processor.ProcessorManager
import com.dbflow5.processor.definition.TypeConverterDefinition
import com.dbflow5.processor.utils.ElementUtility
import com.dbflow5.processor.utils.isNullOrEmpty
import com.dbflow5.quote
import com.squareup.javapoet.ClassName
import com.squareup.javapoet.CodeBlock
import com.squareup.javapoet.TypeName
import javax.lang.model.element.TypeElement
import javax.lang.model.type.TypeMirror

/**
 * Description:
 */
class ReferenceDefinition(private val manager: ProcessorManager,
                          foreignKeyFieldName: String,
                          foreignKeyElementName: String, referencedColumn: ColumnDefinition,
                          private val referenceColumnDefinition: ReferenceColumnDefinition,
                          referenceCount: Int, localColumnName: String = "",
                          var onNullConflict: ConflictAction = ConflictAction.NONE,
                          val defaultValue: String?,
                          typeConverterClassName: ClassName? = null,
                          typeConverterTypeMirror: TypeMirror? = null) {

    val columnName: String = when {
        !localColumnName.isNullOrEmpty() -> localColumnName
        !referenceColumnDefinition.type.isPrimaryField || referenceCount > 0 ->
            "${foreignKeyFieldName}_${referencedColumn.columnName}"
        else -> foreignKeyFieldName
    }
    val foreignColumnName = referencedColumn.columnName
    val columnClassName = referencedColumn.elementTypeName

    var hasCustomConverter: Boolean = false

    val notNull: Boolean
        get() = onNullConflict != ConflictAction.NONE

    private val isReferencedFieldPrivate = referencedColumn.columnAccessor is PrivateScopeColumnAccessor
    private val isReferencedFieldPackagePrivate: Boolean

    var hasTypeConverter: Boolean = false

    internal val creationStatement: CodeBlock
        get() = DefinitionUtils.getCreationStatement(columnClassName, wrapperTypeName, columnName).build()

    internal val primaryKeyName: String
        get() = columnName.quote()


    private val columnAccessor: ColumnAccessor
    var wrapperAccessor: ColumnAccessor? = null
    var wrapperTypeName: TypeName? = null
    var subWrapperAccessor: ColumnAccessor? = null

    val partialAccessor: PartialLoadFromCursorAccessCombiner
    val primaryReferenceField: ForeignKeyAccessField
    val contentValuesField: ForeignKeyAccessField
    val sqliteStatementField: ForeignKeyAccessField

    private fun handleSpecifiedTypeConverter(typeConverterClassName: ClassName?, typeMirror: TypeMirror?) {
        if (typeConverterClassName != null && typeMirror != null &&
                typeConverterClassName != com.dbflow5.processor.ClassNames.TYPE_CONVERTER) {
            evaluateTypeConverter(TypeConverterDefinition(typeConverterClassName, typeMirror, manager), true)
        }
    }

    private fun evaluateTypeConverter(typeConverterDefinition: TypeConverterDefinition?,
                                      isCustom: Boolean) {
        // Any annotated members, otherwise we will use the scanner to find other ones
        typeConverterDefinition?.let {

            if (it.modelTypeName != columnClassName) {
                manager.logError("The specified custom TypeConverter's Model Value %1s from %1s must match the type of the column %1s. ",
                        it.modelTypeName, it.className, columnClassName)
            } else {
                hasTypeConverter = true
                hasCustomConverter = isCustom

                val fieldName = if (hasCustomConverter) {
                    referenceColumnDefinition.baseTableDefinition
                            .addColumnForCustomTypeConverter(referenceColumnDefinition, it.className)
                } else {
                    referenceColumnDefinition.baseTableDefinition
                            .addColumnForTypeConverter(referenceColumnDefinition, it.className)
                }

                wrapperAccessor = TypeConverterScopeColumnAccessor(fieldName)
                wrapperTypeName = it.dbTypeName

                // special case of blob
                if (wrapperTypeName == ClassName.get(Blob::class.java)) {
                    subWrapperAccessor = BlobColumnAccessor()
                }
            }
        }
    }

    init {
        val isPackagePrivate = ElementUtility.isPackagePrivate(referencedColumn.element)
        val isPackagePrivateNotInSamePackage = isPackagePrivate &&
                !ElementUtility.isInSamePackage(manager, referencedColumn.element,
                        referenceColumnDefinition.element)

        isReferencedFieldPackagePrivate = referencedColumn.columnAccessor is PackagePrivateScopeColumnAccessor
                || isPackagePrivateNotInSamePackage

        val tableClassName = ClassName.get(referencedColumn.element.enclosingElement as TypeElement).simpleName()
        val getterSetter = object : GetterSetter {
            override val getterName: String = referencedColumn.column?.getterName ?: ""
            override val setterName: String = referencedColumn.column?.setterName ?: ""
        }
        columnAccessor = when {
            isReferencedFieldPrivate -> PrivateScopeColumnAccessor(foreignKeyFieldName, getterSetter, false)
            isReferencedFieldPackagePrivate -> {
                val accessor = PackagePrivateScopeColumnAccessor(foreignKeyFieldName, referencedColumn.packageName,
                        referenceColumnDefinition.baseTableDefinition.databaseDefinition?.classSeparator,
                        tableClassName)
                PackagePrivateScopeColumnAccessor.putElement(accessor.helperClassName, foreignKeyFieldName)

                accessor
            }
            else -> VisibleScopeColumnAccessor(foreignKeyFieldName)
        }

        hasCustomConverter = false

        handleSpecifiedTypeConverter(typeConverterClassName, typeConverterTypeMirror)
        if (!hasCustomConverter) {
            val typeConverterDefinition = columnClassName?.let { manager.getTypeConverterDefinition(it) }
            evaluateTypeConverter(typeConverterDefinition, false)
        }

        val combiner = Combiner(columnAccessor, columnClassName!!, wrapperAccessor,
                wrapperTypeName, subWrapperAccessor, referenceColumnDefinition.elementName)

        partialAccessor = PartialLoadFromCursorAccessCombiner(columnName, foreignColumnName,
                columnClassName, referenceColumnDefinition.baseTableDefinition.orderedCursorLookUp,
                columnAccessor, wrapperAccessor, wrapperTypeName)

        val defaultValue = referenceColumnDefinition.getDefaultValueBlock(this.defaultValue, columnClassName)
        primaryReferenceField = ForeignKeyAccessField(columnName,
                PrimaryReferenceAccessCombiner(combiner), defaultValue)

        contentValuesField = ForeignKeyAccessField(columnName,
                ContentValuesCombiner(combiner), defaultValue)

        sqliteStatementField = ForeignKeyAccessField("",
                SqliteStatementAccessCombiner(combiner), defaultValue)
    }


}
<commit_msg>[Processor] cleanup code. fix foreignkeyelement name not referenced properly from refactor.<commit_after>package com.dbflow5.processor.definition.column

import com.dbflow5.annotation.ConflictAction
import com.dbflow5.data.Blob
import com.dbflow5.processor.ProcessorManager
import com.dbflow5.processor.definition.TypeConverterDefinition
import com.dbflow5.processor.utils.ElementUtility
import com.dbflow5.processor.utils.isNullOrEmpty
import com.dbflow5.quote
import com.squareup.javapoet.ClassName
import com.squareup.javapoet.CodeBlock
import com.squareup.javapoet.TypeName
import javax.lang.model.element.TypeElement
import javax.lang.model.type.TypeMirror

/**
 * Description:
 */
class ReferenceDefinition(private val manager: ProcessorManager,
                          foreignKeyFieldName: String,
                          foreignKeyElementName: String, referencedColumn: ColumnDefinition,
                          private val referenceColumnDefinition: ReferenceColumnDefinition,
                          referenceCount: Int, localColumnName: String = "",
                          var onNullConflict: ConflictAction = ConflictAction.NONE,
                          val defaultValue: String?,
                          typeConverterClassName: ClassName? = null,
                          typeConverterTypeMirror: TypeMirror? = null) {

    val columnName: String = when {
        !localColumnName.isNullOrEmpty() -> localColumnName
        !referenceColumnDefinition.type.isPrimaryField || referenceCount > 0 ->
            "${foreignKeyFieldName}_${referencedColumn.columnName}"
        else -> foreignKeyFieldName
    }
    val foreignColumnName = referencedColumn.columnName
    val columnClassName = referencedColumn.elementTypeName

    var hasCustomConverter: Boolean = false

    val notNull: Boolean
        get() = onNullConflict != ConflictAction.NONE

    private val isReferencedFieldPrivate = referencedColumn.columnAccessor is PrivateScopeColumnAccessor
    private val isReferencedFieldPackagePrivate: Boolean

    var hasTypeConverter: Boolean = false

    internal val creationStatement: CodeBlock
        get() = DefinitionUtils.getCreationStatement(columnClassName, wrapperTypeName, columnName).build()

    internal val primaryKeyName: String
        get() = columnName.quote()


    private val columnAccessor: ColumnAccessor
    var wrapperAccessor: ColumnAccessor? = null
    var wrapperTypeName: TypeName? = null
    var subWrapperAccessor: ColumnAccessor? = null

    val partialAccessor: PartialLoadFromCursorAccessCombiner
    val primaryReferenceField: ForeignKeyAccessField
    val contentValuesField: ForeignKeyAccessField
    val sqliteStatementField: ForeignKeyAccessField

    private fun handleSpecifiedTypeConverter(typeConverterClassName: ClassName?, typeMirror: TypeMirror?) {
        if (typeConverterClassName != null && typeMirror != null &&
                typeConverterClassName != com.dbflow5.processor.ClassNames.TYPE_CONVERTER) {
            evaluateTypeConverter(TypeConverterDefinition(typeConverterClassName, typeMirror, manager), true)
        }
    }

    private fun evaluateTypeConverter(typeConverterDefinition: TypeConverterDefinition?,
                                      isCustom: Boolean) {
        // Any annotated members, otherwise we will use the scanner to find other ones
        typeConverterDefinition?.let { typeConverter ->

            if (typeConverter.modelTypeName != columnClassName) {
                manager.logError("The specified custom TypeConverter's Model Value " +
                        "${typeConverter.modelTypeName} from ${typeConverter.className}" +
                        " must match the type of the column $columnClassName.")
            } else {
                hasTypeConverter = true
                hasCustomConverter = isCustom

                val fieldName = if (hasCustomConverter) {
                    referenceColumnDefinition.baseTableDefinition
                            .addColumnForCustomTypeConverter(referenceColumnDefinition, typeConverter.className)
                } else {
                    referenceColumnDefinition.baseTableDefinition
                            .addColumnForTypeConverter(referenceColumnDefinition, typeConverter.className)
                }

                wrapperAccessor = TypeConverterScopeColumnAccessor(fieldName)
                wrapperTypeName = typeConverter.dbTypeName

                // special case of blob
                if (wrapperTypeName == ClassName.get(Blob::class.java)) {
                    subWrapperAccessor = BlobColumnAccessor()
                }
            }
        }
    }

    init {
        val isPackagePrivate = ElementUtility.isPackagePrivate(referencedColumn.element)
        val isPackagePrivateNotInSamePackage = isPackagePrivate &&
                !ElementUtility.isInSamePackage(manager, referencedColumn.element,
                        referenceColumnDefinition.element)

        isReferencedFieldPackagePrivate = referencedColumn.columnAccessor is PackagePrivateScopeColumnAccessor
                || isPackagePrivateNotInSamePackage

        val tableClassName = ClassName.get(referencedColumn.element.enclosingElement as TypeElement).simpleName()
        val getterSetter = object : GetterSetter {
            override val getterName: String = referencedColumn.column?.getterName ?: ""
            override val setterName: String = referencedColumn.column?.setterName ?: ""
        }
        columnAccessor = when {
            isReferencedFieldPrivate -> PrivateScopeColumnAccessor(foreignKeyElementName, getterSetter, false)
            isReferencedFieldPackagePrivate -> {
                val accessor = PackagePrivateScopeColumnAccessor(foreignKeyElementName, referencedColumn.packageName,
                        referenceColumnDefinition.baseTableDefinition.databaseDefinition?.classSeparator,
                        tableClassName)
                PackagePrivateScopeColumnAccessor.putElement(accessor.helperClassName, foreignKeyElementName)

                accessor
            }
            else -> VisibleScopeColumnAccessor(foreignKeyElementName)
        }

        hasCustomConverter = false

        handleSpecifiedTypeConverter(typeConverterClassName, typeConverterTypeMirror)
        if (!hasCustomConverter) {
            val typeConverterDefinition = columnClassName?.let { manager.getTypeConverterDefinition(it) }
            evaluateTypeConverter(typeConverterDefinition, false)
        }

        val combiner = Combiner(columnAccessor, columnClassName!!, wrapperAccessor,
                wrapperTypeName, subWrapperAccessor, referenceColumnDefinition.elementName)

        partialAccessor = PartialLoadFromCursorAccessCombiner(columnName, foreignColumnName,
                columnClassName, referenceColumnDefinition.baseTableDefinition.orderedCursorLookUp,
                columnAccessor, wrapperAccessor, wrapperTypeName)

        val defaultValue = referenceColumnDefinition.getDefaultValueBlock(this.defaultValue, columnClassName)
        primaryReferenceField = ForeignKeyAccessField(columnName,
                PrimaryReferenceAccessCombiner(combiner), defaultValue)

        contentValuesField = ForeignKeyAccessField(columnName,
                ContentValuesCombiner(combiner), defaultValue)

        sqliteStatementField = ForeignKeyAccessField("",
                SqliteStatementAccessCombiner(combiner), defaultValue)
    }


}

{'fraction_non_alphanumeric': 0.06092094539527303, 'fraction_numerical': 0.0015620755229557185, 'mean_word_length': 2.8506799163179917, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2502027', 'n_tokens_mistral': 3360, 'n_tokens_neox': 3193, 'n_words': 664}