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

net.bjoernpetersen.musicbot.api.plugin.NamedPlugin.kt Maven / Gradle / Ivy

There is a newer version: 0.25.0
Show newest version
package net.bjoernpetersen.musicbot.api.plugin

import net.bjoernpetersen.musicbot.api.plugin.management.PluginFinder
import net.bjoernpetersen.musicbot.spi.plugin.Plugin
import net.bjoernpetersen.musicbot.spi.plugin.UserFacing
import kotlin.reflect.KClass


/**
 * Static, serializable representation of a plugin.
 *
 * @param id the qualified name of the plugin's [ID base][IdBase]
 * @param name the plugin's [subject][UserFacing.subject]
 */
data class NamedPlugin(
    val id: String,
    val name: String) {

    @Deprecated("Will be removed in a future version")
    constructor(idClass: KClass, name: String) : this(idClass.java.name, name)

    @Deprecated("Use PluginLookup", ReplaceWith("PluginLookup.lookup(this)"))
    @Throws(IllegalStateException::class)
    fun findPlugin(classLoader: ClassLoader, pluginFinder: PluginFinder): T {
        val base = try {
            @Suppress("UNCHECKED_CAST")
            classLoader.loadClass(id).kotlin as KClass
        } catch (e: ClassNotFoundException) {
            throw IllegalStateException("Could not find plugin class for ID: $id")
        } catch (e: ClassCastException) {
            throw IllegalStateException("Could not cast ID base to KClass")
        }

        val plugin = pluginFinder[base]
            ?: throw IllegalStateException(
                "Could not find provider for class ${base.qualifiedName}")

        return try {
            @Suppress("UNCHECKED_CAST")
            plugin as T
        } catch (e: ClassCastException) {
            throw IllegalStateException()
        }
    }

    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (other !is NamedPlugin<*>) return false

        if (id != other.id) return false

        return true
    }

    override fun hashCode(): Int {
        return id.hashCode()
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy