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

com.likethesalad.stem.utils.TemplatesProviderLoader.kt Maven / Gradle / Ivy

package com.likethesalad.stem.utils

import com.likethesalad.android.templates.common.utils.CommonConstants
import com.likethesalad.android.templates.provider.api.TemplatesProvider
import io.github.classgraph.ClassGraph
import io.github.classgraph.ScanResult
import java.io.File
import java.net.URLClassLoader

object TemplatesProviderLoader {

    @Suppress("UNCHECKED_CAST")
    fun load(jars: List): List {
        if (jars.isEmpty()) {
            return emptyList()
        }

        val scanResult = scanJars(jars.toSet())
        return scanResult.use { result ->
            result.getClassesImplementing(TemplatesProvider::class.java).loadClasses().map {
                it.newInstance()
            }
        } as List
    }

    private fun scanJars(jarFiles: Set): ScanResult {
        return ClassGraph()
            .acceptPackages(CommonConstants.PROVIDER_PACKAGE_NAME)
            .overrideClassLoaders(createLocalClassloader(jarFiles))
            .scan()
    }

    private fun createLocalClassloader(jarFiles: Set): ClassLoader {
        val urls = jarFiles.map { it.toURI().toURL() }
        return URLClassLoader(urls.toTypedArray(), javaClass.classLoader)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy