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

walkmc.StabilizedLoader.kt Maven / Gradle / Ivy

package walkmc

import walkmc.extensions.*

/**
 * This object instance is used to obtain a stabilized class loader.
 * This is util when getting a class from another jar file, avoiding the [ClassNotFoundException].
 */
object StabilizedLoader {
	private val classLoader: ClassLoader by lazy {
		try {
			pluginManager.plugins[0].classLoader
		} catch (ex: Exception) {
			/* This is not the stabilized class loader */
			/* this is used when the server not contains any plugin */
			/* and this avoid a index out of bounds exception */
			ClassLoader.getSystemClassLoader()
		}
	}
	
	/**
	 * Gets the stabilized class loader.
	 */
	@JvmStatic
	fun get(): ClassLoader = classLoader
	
	/**
	 * Gets a class from this loader. Almost this returns any class without exception.
	 */
	@JvmStatic
	fun getClass(name: String, init: Boolean = true): Class<*> = Class.forName(name, init, classLoader)
}

/**
 * Gets a stabilized class from the [StabilizedLoader].
 */
fun classOf(name: String, init: Boolean = true) = StabilizedLoader.getClass(name, init)

/**
 * Gets a stabilized class from the [StabilizedLoader].
 */
@JvmName("classOfType")
inline fun  classOf(name: String, init: Boolean = true) =
	StabilizedLoader.getClass(name, init).cast>()




© 2015 - 2025 Weber Informatics LLC | Privacy Policy