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

name.remal.gradle_plugins.dsl.artifact.HasEntries.kt Maven / Gradle / Ivy

There is a newer version: 1.9.2
Show newest version
package name.remal.gradle_plugins.dsl.artifact

import name.remal.classNameToResourceName
import name.remal.queueOf
import java.io.InputStream
import java.util.jar.JarFile.MANIFEST_NAME
import java.util.jar.Manifest

interface HasEntries {

    val entryNames: Set

    operator fun contains(entryName: String): Boolean = entryName in entryNames
    fun openStream(entryName: String): InputStream
    fun readBytes(entryName: String): ByteArray = openStream(entryName).use { it.readBytes() }

    val classEntryNames: Set
    val classNames: Set

    fun containsClass(className: String): Boolean = className in classNames
    fun containsClass(clazz: Class<*>): Boolean = containsClass(clazz.name)
    fun openStreamForClass(className: String): InputStream = openStream(classNameToResourceName(className))
    fun openStreamForClass(clazz: Class<*>): InputStream = openStreamForClass(clazz.name)
    fun readBytecode(className: String): ByteArray = openStreamForClass(className).use { it.readBytes() }
    fun readBytecode(clazz: Class<*>): ByteArray = openStreamForClass(clazz).use { it.readBytes() }

    val manifestMainAttributes: Map

    fun readManifest(): Manifest? {
        try {
            openStream(MANIFEST_NAME).use {
                return Manifest(it)
            }
        } catch (e: ArtifactFileNotFoundException) {
            return null
        } catch (e: ArtifactEntryNotFoundException) {
            return null
        }
    }

    class Entry(val name: String, inputStreamProvider: () -> InputStream) {
        val inputStream: InputStream by lazy(inputStreamProvider)
    }

    fun forEachEntry(pattern: String? = null, action: (entry: Entry) -> Unit)

    val annotationClassNames: Set

    val annotationsMapping: Map>

    fun getMetaAnnotationClassNames(rootMetaAnnotationClassName: String): Set = sortedSetOf().apply {
        if (rootMetaAnnotationClassName in annotationClassNames) {
            add(rootMetaAnnotationClassName)
        }

        val annotationsMapping = annotationsMapping
        val annotationClassNamesQueue = queueOf(rootMetaAnnotationClassName)
        while (true) {
            val annotationClassName = annotationClassNamesQueue.poll() ?: break
            annotationsMapping[annotationClassName]?.forEach {
                if (add(it)) {
                    annotationClassNamesQueue.add(it)
                }
            }
        }
    }

    fun getMetaAnnotationClassNames(rootMetaAnnotationClass: Class) = getMetaAnnotationClassNames(rootMetaAnnotationClass.name)

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy