Class ScalaPluginClassLoader

java.lang.Object
java.lang.ClassLoader
java.security.SecureClassLoader
java.net.URLClassLoader
xyz.janboerman.scalaloader.plugin.ScalaPluginClassLoader
All Implemented Interfaces:
Closeable, AutoCloseable, IScalaPluginClassLoader

public class ScalaPluginClassLoader extends URLClassLoader implements IScalaPluginClassLoader
ClassLoader that loads ScalaPlugins. The ScalaPluginLoader will create instances per scala plugin.
  • Constructor Details

  • Method Details

    • getPlugin

      public ScalaPlugin getPlugin()
      Get the ScalaPlugin loaded by this class loader.
      Specified by:
      getPlugin in interface IScalaPluginClassLoader
      Returns:
      the plugin
    • getScalaVersion

      public String getScalaVersion()
      Get the version of Scala used for the plugin loaded by this class loader.
      Returns:
      the scala version
    • getScalaRelease

      public ScalaRelease getScalaRelease()
      Get the compatibility-release version of Scala used by the plugin.
      Returns:
      the compatibility release
    • getPluginLoader

      public ScalaPluginLoader getPluginLoader()
      Get the plugin loader that uses this class loader.
      Specified by:
      getPluginLoader in interface IScalaPluginClassLoader
      Returns:
      the scala plugin loader
    • getServer

      public Server getServer()
      Get the server the plugin runs on.
      Specified by:
      getServer in interface IScalaPluginClassLoader
      Returns:
      the server
    • getExtraPluginYaml

      public Map<String,Object> getExtraPluginYaml()
      Get the extra plugin settings that was not defined in the constructor, but defined in the plugin.yml.
      Returns:
      the extra plugin settings
    • getPluginJarFile

      public File getPluginJarFile()
      Get the file for the plugin.
      Specified by:
      getPluginJarFile in interface IScalaPluginClassLoader
      Returns:
      the file
    • getApiVersion

      public ApiVersion getApiVersion()
      Get the version of bukkit's api the plugin uses.
      Specified by:
      getApiVersion in interface IScalaPluginClassLoader
      Returns:
      bukkit's api version
    • getMainClassName

      public String getMainClassName()
      Get the name of the main class of the plugin.
      Specified by:
      getMainClassName in interface IScalaPluginClassLoader
      Returns:
      the fully qualified name
    • loadClass

      public Class<?> loadClass(String name) throws ClassNotFoundException

      Tries to load a class with the given name using the following search priorities:

      1. Search for the class in the Scala standard library
      2. Search for the class in the ScalaPlugin's own jar file
      3. Search for the class in one of the plugin's library dependencies
      4. Search for the class in other ScalaPlugins
      5. Search for the class in JavaPlugins and Bukkit/Server implementation classes
      Overrides:
      loadClass in class ClassLoader
      Parameters:
      name - the name of the class
      Returns:
      a class with the given name
      Throws:
      ClassNotFoundException - if a class could not be found by this classloader
      See Also:
    • findClass

      public Class<?> findClass(String name, boolean searchInScalaPluginLoader) throws ClassNotFoundException
      Finds and loads a class used by the ScalaPlugin loaded by this ClassLoader.
      Parameters:
      name - the name of the class to be found
      searchInScalaPluginLoader - whether or not to search in the 'global' classes cache of the ScalaPluginLoader.
      Returns:
      a class with the given name, if found
      Throws:
      ClassNotFoundException - if no class with the given name could be found
      API Note:
      this method never returns null, it either returns a class, or throws an exception or error
    • addClass

      public Class<?> addClass(Class<?> toAdd)
      Adds a class to this ClassLoader so that this ScalaPluginClassLoader can find the class and the class can be used by the ScalaPlugin.
      Parameters:
      toAdd - the class
      Returns:
      the same class, or an already existing class if one with the same name was found already
    • getOrDefineClass

      public ClassDefineResult getOrDefineClass(String className, ClassGenerator classGenerator, boolean persist)
      Generates a class for this class loader, or gets a cached version if a class with the same name was already loaded.
      Specified by:
      getOrDefineClass in interface IScalaPluginClassLoader
      Parameters:
      className - the name of the class
      classGenerator - the generator for the class
      persist - whether to automatically re-generate this class again the next time the plugin loads
      Returns:
      the result of a class definition
    • getResource

      public URL getResource(String resourcePath)
      Finds a resource that is in the ScalaPlugin's jar file.
      Overrides:
      getResource in class ClassLoader
      Parameters:
      resourcePath - the name of the resource
      Returns:
      the URL of the resource, or null if a resource with the given name did not exist
    • getResources

      public Enumeration<URL> getResources(String resourcePath) throws IOException
      Finds resources that are in the ScalaPlugin's jar file.
      Overrides:
      getResources in class ClassLoader
      Parameters:
      resourcePath - the name of the resource
      Returns:
      An Enumeration of URLs. If the loader is closed, the Enumeration contains no elements.
      Throws:
      IOException - if an I/O exception occurs
    • getClasses

      protected final Map<String,Class<?>> getClasses()
      Gets a view of the plugin's classes.
      Returns:
      an immutable view of the classes
    • injectIntoJavaPluginLoaderScope

      @Deprecated protected final void injectIntoJavaPluginLoaderScope(String className, Class<?> clazz)
      Deprecated.
      JavaPlugins that try to find classes using the JavaPluginLoader expect to only find JavaPlugins.
      Similar to ScalaPluginLoader.addClassGlobally(ScalaRelease, String, Class), the JavaPluginLoader also has such a method: setClass. This is used by JavaPlugins to make their classes accessible to other JavaPlugins. Since we want a ScalaPlugin's classes to be accessible to JavaPlugins, this method can be called to share a class with ALL JavaPlugins and ScalaPlugins. This is dangerous business because it can pollute the JavaPluginLoader with classes for multiple (binary incompatible) versions of Scala.
      Be sure to call removeFromJavaPluginLoaderScope(String) again when the class is no longer needed to prevent memory leaks.
      It is better (but still not ideal) to use ScalaPluginLoader.openUpToJavaPlugin(ScalaPlugin, JavaPlugin) instead.
      Parameters:
      className - the name of the class
      clazz - the class
      See Also:
    • removeFromJavaPluginLoaderScope

      @Deprecated protected final void removeFromJavaPluginLoaderScope(String className)
      Deprecated.
      JavaPlugins that try to find classes using the JavaPluginLoader expect to only find JavaPlugins
      Removes a class from the JavaPluginLoader's global classes cache.
      Parameters:
      className - the name of the class
      See Also:
    • addURL

      @Deprecated public final void addURL(URL url)
      Deprecated.
      use libraries instead. The only reason this method still exist is that that method does not support user-defined repositories yet.

      Adds a url to this classloader. This will allow more classes to be found that the plugin can then depend on.

      Only use this if you know what you are doing!!!

      Overrides:
      addURL in class URLClassLoader
      Parameters:
      url - the location of the dependency
      API Note:
      Be sure to call this in the constructor or initializer of your plugin, and don't use the dependency before that point or else you will get a NoClassDefFoundError
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class URLClassLoader
      Throws:
      IOException