Package io.github.connellite.jcl
Class JarClassLoader
- java.lang.Object
-
- java.lang.ClassLoader
-
- io.github.connellite.jcl.AbstractClassLoader
-
- io.github.connellite.jcl.JarClassLoader
-
public class JarClassLoader extends AbstractClassLoader
Reads the class bytes from jar files and other resources using ClasspathResources- Author:
- Kamran Zafar
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class io.github.connellite.jcl.AbstractClassLoader
AbstractClassLoader.OsgiBootLoader
-
-
Field Summary
Fields Modifier and Type Field Description protected Map<String,Class<?>>classesClass cacheprotected ClasspathResourcesclasspathResourcesprotected Set<String>definedPackages-
Fields inherited from class io.github.connellite.jcl.AbstractClassLoader
loaders
-
-
Constructor Summary
Constructors Constructor Description JarClassLoader()JarClassLoader(ClassLoader parent)JarClassLoader(Object[] sources)Loads classes from different sources.JarClassLoader(List<?> sources)Loads classes from different sources.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(File file)Adds a local classpath resource.voidadd(InputStream jarStream)Adds classes from a JAR input stream.voidadd(Object source)Adds a local or remote classpath source.voidadd(String resourceName)Adds a local or remote classpath resource.voidadd(URL url)Adds a local or remote classpath resource.voidaddAll(Object[] sources)Adds all jar/class sources.voidaddAll(List<?> sources)Adds all jar/class sources.protected Class<?>defineLoadedClass(String className, byte[] classBytes, int offset, int length, boolean resolve)Defines a class from bytes after applying package-info annotations.protected voidensurePackageDefined(String className)Loadspackage-info.classwhen present so package-level annotations are visible viaPackage.getAnnotation(Class).protected StringformatClassName(String className)Converts a binary class name to the resource path used byClasspathResources.chargetClassNameReplacementChar()Map<String,Class<?>>getLoadedClasses()Returns all JCL-loaded classes as an immutable MapMap<String,byte[]>getLoadedResources()Returns all loaded classes and resourcesProxyClassLoadergetLocalLoader()voidinitialize()Some initialisationsprotected byte[]loadClassBytes(String className)Reads the class bytes from different local and remote resources usingClasspathResources.voidsetClassNameReplacementChar(char classNameReplacementChar)voidunloadClass(String className)Attempts to unload a class; only locally loaded JCL classes are removed from the cache.-
Methods inherited from class io.github.connellite.jcl.AbstractClassLoader
addDefaultLoader, addLoader, getCurrentLoader, getOsgiBootLoader, getParentLoader, getResource, getResourceAsStream, getResources, getSystemLoader, getThreadLoader, loadClass, loadClass
-
Methods inherited from class java.lang.ClassLoader
clearAssertionStatus, defineClass, defineClass, defineClass, defineClass, definePackage, findClass, findClass, findLibrary, findLoadedClass, findResource, findResource, findResources, findSystemClass, getClassLoadingLock, getDefinedPackage, getDefinedPackages, getName, getPackage, getPackages, getParent, getPlatformClassLoader, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, getUnnamedModule, isRegisteredAsParallelCapable, registerAsParallelCapable, resolveClass, resources, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners
-
-
-
-
Constructor Detail
-
JarClassLoader
public JarClassLoader()
-
JarClassLoader
public JarClassLoader(ClassLoader parent)
-
JarClassLoader
public JarClassLoader(Object[] sources)
Loads classes from different sources.- Parameters:
sources- classpath entries to add
-
JarClassLoader
public JarClassLoader(List<?> sources)
Loads classes from different sources.- Parameters:
sources- classpath entries to add
-
-
Method Detail
-
initialize
public void initialize()
Some initialisations
-
addAll
public void addAll(Object[] sources)
Adds all jar/class sources.- Parameters:
sources- classpath entries to add
-
addAll
public void addAll(List<?> sources)
Adds all jar/class sources.- Parameters:
sources- classpath entries to add
-
add
public void add(Object source)
Adds a local or remote classpath source.- Parameters:
source- jar path, URL, file, or input stream
-
add
public void add(String resourceName)
Adds a local or remote classpath resource.- Parameters:
resourceName- path or URL of the resource to load
-
add
public void add(File file)
Adds a local classpath resource.- Parameters:
file- jar file, class file, or directory to load
-
add
public void add(InputStream jarStream)
Adds classes from a JAR input stream.- Parameters:
jarStream- JAR content stream
-
add
public void add(URL url)
Adds a local or remote classpath resource.- Parameters:
url- resource URL
-
loadClassBytes
protected byte[] loadClassBytes(String className)
Reads the class bytes from different local and remote resources usingClasspathResources.- Parameters:
className- binary name of the class to load- Returns:
- class bytes, or
nullif not found
-
unloadClass
public void unloadClass(String className)
Attempts to unload a class; only locally loaded JCL classes are removed from the cache.- Parameters:
className- binary name of the class to unload
-
formatClassName
protected String formatClassName(String className)
Converts a binary class name to the resource path used byClasspathResources.- Parameters:
className- binary name of the class- Returns:
- resource path ending in
.class
-
ensurePackageDefined
protected void ensurePackageDefined(String className)
Loadspackage-info.classwhen present so package-level annotations are visible viaPackage.getAnnotation(Class).
-
defineLoadedClass
protected final Class<?> defineLoadedClass(String className, byte[] classBytes, int offset, int length, boolean resolve)
Defines a class from bytes after applying package-info annotations.- Parameters:
className- binary name of the classclassBytes- class file bytesoffset- start offset inclassByteslength- length of class dataresolve- whether to link the class- Returns:
- the defined class, or
nullif definition failed
-
getClassNameReplacementChar
public char getClassNameReplacementChar()
-
setClassNameReplacementChar
public void setClassNameReplacementChar(char classNameReplacementChar)
-
getLoadedResources
public Map<String,byte[]> getLoadedResources()
Returns all loaded classes and resources- Returns:
- Map
-
getLocalLoader
public ProxyClassLoader getLocalLoader()
- Returns:
- Local JCL ProxyClassLoader
-
-