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
ClassLoader that loads
ScalaPlugins.
The ScalaPluginLoader will create instances per scala plugin.-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedScalaPluginClassLoader(ScalaPluginLoader pluginLoader, URL[] urls, ScalaLibraryClassLoader parent, Server server, Map<String, Object> extraPluginYaml, File pluginJarFile, ApiVersion apiVersion, String mainClassName, TransformerRegistry transformerRegistry, Collection<File> dependencies) Construct a ClassLoader that loads classes forScalaPlugins. -
Method Summary
Modifier and TypeMethodDescriptionClass<?> Adds a class to this ClassLoader so that this ScalaPluginClassLoader can find the class and the class can be used by the ScalaPlugin.final voidDeprecated.use libraries instead.voidclose()Class<?> Finds and loads a class used by the ScalaPlugin loaded by this ClassLoader.Get the version of bukkit's api the plugin uses.Gets a view of the plugin's classes.Get the extra plugin settings that was not defined in the constructor, but defined in the plugin.yml.Get the name of the main class of the plugin.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.Get the ScalaPlugin loaded by this class loader.Get the file for the plugin.Get the plugin loader that uses this class loader.getResource(String resourcePath) Finds a resource that is in the ScalaPlugin's jar file.getResources(String resourcePath) Finds resources that are in the ScalaPlugin's jar file.Get the compatibility-release version of Scala used by the plugin.Get the version of Scala used for the plugin loaded by this class loader.Get the server the plugin runs on.protected final voidinjectIntoJavaPluginLoaderScope(String className, Class<?> clazz) Deprecated.JavaPlugins that try to find classes using the JavaPluginLoader expect to only find JavaPlugins.Class<?> Tries to load a class with the given name using the following search priorities:protected final voidremoveFromJavaPluginLoaderScope(String className) Deprecated.JavaPlugins that try to find classes using the JavaPluginLoader expect to only find JavaPluginsMethods inherited from class java.net.URLClassLoader
definePackage, findClass, findResource, findResources, getPermissions, getResourceAsStream, getURLs, newInstance, newInstanceMethods inherited from class java.security.SecureClassLoader
defineClass, defineClassMethods inherited from class java.lang.ClassLoader
clearAssertionStatus, defineClass, defineClass, defineClass, defineClass, definePackage, findClass, findLibrary, findLoadedClass, findResource, findSystemClass, getClassLoadingLock, getDefinedPackage, getDefinedPackages, getName, getPackage, getPackages, getParent, getPlatformClassLoader, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, getUnnamedModule, isRegisteredAsParallelCapable, loadClass, registerAsParallelCapable, resolveClass, resources, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners
-
Constructor Details
-
ScalaPluginClassLoader
protected ScalaPluginClassLoader(ScalaPluginLoader pluginLoader, URL[] urls, ScalaLibraryClassLoader parent, Server server, Map<String, Object> extraPluginYaml, File pluginJarFile, ApiVersion apiVersion, String mainClassName, TransformerRegistry transformerRegistry, Collection<File> dependencies) throws IOException, ScalaPluginLoaderExceptionConstruct a ClassLoader that loads classes forScalaPlugins.- Parameters:
pluginLoader- the ScalaPluginClassLoaderurls- the urls on which the classes are locatedparent- the parent classloader which must be a ScalaLibraryClassLoaderserver- the Server in which the plugin will runextraPluginYaml- extra plugin settings not defined through the ScalaPlugin's constructor, but in the plugin.yml filepluginJarFile- the plugin's jar fileapiVersion- bukkit's api version that's used by the plugin- Throws:
IOException- if the plugin's file could not be read as aJarFileScalaPluginLoaderException- if the plugin instance could not be constructed
-
-
Method Details
-
getPlugin
Get the ScalaPlugin loaded by this class loader.- Specified by:
getPluginin interfaceIScalaPluginClassLoader- Returns:
- the plugin
-
getScalaVersion
Get the version of Scala used for the plugin loaded by this class loader.- Returns:
- the scala version
-
getScalaRelease
Get the compatibility-release version of Scala used by the plugin.- Returns:
- the compatibility release
-
getPluginLoader
Get the plugin loader that uses this class loader.- Specified by:
getPluginLoaderin interfaceIScalaPluginClassLoader- Returns:
- the scala plugin loader
-
getServer
Get the server the plugin runs on.- Specified by:
getServerin interfaceIScalaPluginClassLoader- Returns:
- the server
-
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
Get the file for the plugin.- Specified by:
getPluginJarFilein interfaceIScalaPluginClassLoader- Returns:
- the file
-
getApiVersion
Get the version of bukkit's api the plugin uses.- Specified by:
getApiVersionin interfaceIScalaPluginClassLoader- Returns:
- bukkit's api version
-
getMainClassName
Get the name of the main class of the plugin.- Specified by:
getMainClassNamein interfaceIScalaPluginClassLoader- Returns:
- the fully qualified name
-
loadClass
Tries to load a class with the given name using the following search priorities:
- Search for the class in the Scala standard library
- Search for the class in the ScalaPlugin's own jar file
- Search for the class in one of the plugin's library dependencies
- Search for the class in other ScalaPlugins
- Search for the class in JavaPlugins and Bukkit/Server implementation classes
- Overrides:
loadClassin classClassLoader- 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 foundsearchInScalaPluginLoader- whether or not to search in the 'global' classes cache of theScalaPluginLoader.- 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
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:
getOrDefineClassin interfaceIScalaPluginClassLoader- Parameters:
className- the name of the classclassGenerator- the generator for the classpersist- whether to automatically re-generate this class again the next time the plugin loads- Returns:
- the result of a class definition
-
getResource
Finds a resource that is in the ScalaPlugin's jar file.- Overrides:
getResourcein classClassLoader- Parameters:
resourcePath- the name of the resource- Returns:
- the
URLof the resource, ornullif a resource with the given name did not exist
-
getResources
Finds resources that are in the ScalaPlugin's jar file.- Overrides:
getResourcesin classClassLoader- Parameters:
resourcePath- the name of the resource- Returns:
- An
EnumerationofURLs. If the loader is closed, the Enumeration contains no elements. - Throws:
IOException- if an I/O exception occurs
-
getClasses
Gets a view of the plugin's classes.- Returns:
- an immutable view of the classes
-
injectIntoJavaPluginLoaderScope
Deprecated.JavaPlugins that try to find classes using the JavaPluginLoader expect to only find JavaPlugins.Similar toScalaPluginLoader.addClassGlobally(ScalaRelease, String, Class), theJavaPluginLoaderalso has such a method: setClass. This is used byJavaPlugins to make their classes accessible to other JavaPlugins. Since we want aScalaPlugin'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 callremoveFromJavaPluginLoaderScope(String)again when the class is no longer needed to prevent memory leaks.
It is better (but still not ideal) to useScalaPluginLoader.openUpToJavaPlugin(ScalaPlugin, JavaPlugin)instead.- Parameters:
className- the name of the classclazz- the class- See Also:
-
removeFromJavaPluginLoaderScope
Deprecated.JavaPlugins that try to find classes using the JavaPluginLoader expect to only find JavaPluginsRemoves a class from theJavaPluginLoader's global classes cache.- Parameters:
className- the name of the class- See Also:
-
addURL
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:
addURLin classURLClassLoader- 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
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classURLClassLoader- Throws:
IOException
-