All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jetbrains.kotlin.kapt3.base.incremental.IncrementalAptCache.kt Maven / Gradle / Ivy

/*
 * Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
 * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
 */

package org.jetbrains.kotlin.kapt3.base.incremental

import java.io.File
import java.io.Serializable

class IncrementalAptCache : Serializable {

    private val aggregatingGenerated: MutableSet = mutableSetOf()
    private val isolatingMapping: MutableMap = mutableMapOf()
    // Annotations claimed by aggregating annotation processors
    private val aggregatingClaimedAnnotations: MutableSet = mutableSetOf()

    var isIncremental = true
        private set

    fun updateCache(processors: List): Boolean {
        val aggregating = mutableListOf()
        val isolating = mutableListOf()
        val nonIncremental = mutableListOf()
        processors.forEach {
            when (it.getRuntimeType()) {
                RuntimeProcType.AGGREGATING -> aggregating.add(it)
                RuntimeProcType.ISOLATING -> isolating.add(it)
                RuntimeProcType.NON_INCREMENTAL -> nonIncremental.add(it)
            }
        }

        if (nonIncremental.isNotEmpty()) {
            invalidateCache()
            return false
        }

        aggregatingGenerated.clear()
        aggregatingGenerated.addAll(aggregating.flatMap { it.getGeneratedToSources().keys })

        aggregatingClaimedAnnotations.clear()
        aggregatingClaimedAnnotations.addAll(aggregating.flatMap { it.supportedAnnotationTypes })

        for (isolatingProcessor in isolating) {
            isolatingProcessor.getGeneratedToSources().forEach {
                isolatingMapping[it.key] = it.value!!
            }
        }
        return true
    }

    fun getAggregatingClaimedAnnotations(): Set = aggregatingClaimedAnnotations

    /** Returns generated Java sources originating from aggregating APs. */
    fun invalidateAggregating(): List {
        val dirtyAggregating = aggregatingGenerated.filter { it.extension == "java" }
        aggregatingGenerated.forEach { it.delete() }
        aggregatingGenerated.clear()

        return dirtyAggregating
    }

    /** Returns generated Java sources originating from the specified sources, and generated  by isloating APs. */
    fun invalidateIsolatingGenerated(fromSources: Set): List {
        val allInvalidated = mutableListOf()
        var changedSources = fromSources.toSet()

        // We need to do it in a loop because mapping could be: [AGenerated.java -> A.java, AGeneratedGenerated.java -> AGenerated.java]
        while (changedSources.isNotEmpty()) {
            val generated = isolatingMapping.filter { changedSources.contains(it.value) }.keys
            generated.forEach {
                if (it.extension == "java") allInvalidated.add(it)

                it.delete()
                isolatingMapping.remove(it)
            }
            changedSources = generated
        }
        return allInvalidated
    }

    private fun invalidateCache() {
        isIncremental = false
        aggregatingGenerated.clear()
        isolatingMapping.clear()
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy