Interface Integration
Provides hooks for integrating JRebel with custom containers, frameworks and classloaders.
To use it acquire an instance from IntegrationFactory
A typical (classloader) integration will look like this:
class MyClassLoader extends URLClassLoader {
public MyClassLoader() {
...
IntegrationFactory.getInstance().registerClassLoader(
this,
new FindResourceClassResourceSource(this)));
}
...
public Class findClass(String classname) {
synchronized(classloader) {
Class result =
classloader.findLoadedClass(classname);
if (result != null)
return result;
result =
IntegrationFactory.getInstance()
.findReloadableClass(this, classname);
if (result != null)
return result;
}
...
}
}
You can use the addIntegrationProcessor(String, ClassBytecodeProcessor) and
addIntegrationProcessor(String[], ClassBytecodeProcessor) instrument specific
classes to enhance their behaviour with JRebel.
- Author:
- Jevgeni Kabanov
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionvoidaddAfterMainCallback(AfterMainCallback callback) Registers a callback that will be invoked after the main method is called.voidaddBootClassProcessor(String[] classNames, ClassBytecodeProcessor processor) Adds a bytecode processor for set of given classes in boot classloader.voidaddBootClassProcessor(String className, ClassBytecodeProcessor processor) Adds a bytecode processor for the given class in boot classloader.voidaddBootClassProcessor(ClassBytecodeProcessor classBytecodeProcessor) Adds a bytecode processor in boot classloader.voidaddCacheCleaner(CacheCleaner cacheCleaner) An integration point to add once per reload caches that should be cleared after reload.voidRegisters a call back invoked when a given class loader is being destroyed.voidaddIntegrationProcessor(ClassLoader cl, String[] classNames, ClassBytecodeProcessor processor) Adds a bytecode processor for the set of given classes in the given classloader.voidaddIntegrationProcessor(ClassLoader cl, String className, ClassBytecodeProcessor processor) Adds a bytecode processor for the given class in the given classloader.voidaddIntegrationProcessor(ClassLoader cl, ClassBytecodeProcessor processor) Adds a bytecode processor for the given classloader.voidaddIntegrationProcessor(ClassLoader cl, ClassBytecodeProcessor processor, boolean managedOnly) AAdds a bytecode processor for the given classloader.voidaddIntegrationProcessor(String[] classNames, ClassBytecodeProcessor processor) Adds a bytecode processor for the set of given classes.voidaddIntegrationProcessor(String className, ClassBytecodeProcessor processor) Adds a bytecode processor for the given class.voidaddIntegrationProcessor(ClassBytecodeProcessor processor, boolean managedOnly) Adds a bytecode processor for all classes.voidbindToClassLoader(ClassLoader cl, Object value) Add reference from given class loader to given object.Class<?>defineReloadableClass(ClassLoader cl, String className, byte[] bytecode, ProtectionDomain protectionDomain) Same as defineReloadableClass(cl, className, bytecode, protectionDomain, true)Class<?>defineReloadableClass(ClassLoader cl, String className, byte[] bytecode, ProtectionDomain protectionDomain, boolean processedByLoader) (Re)defines a class managed by JRebel.Class<?>defineReloadableClass(ClassLoader cl, String className, Resource mutableResource, ProtectionDomain protectionDomain, boolean processedByLoader) (Re)defines a class managed by JRebel.voidDisable running plugins in given class loader.voidDisable class reload check in given class loader.voidDisable class reloading in given class loader.voidDisables the SecurityManager in the current thread.voidRe-enables the disabled SecurityManager in the current thread.findRebelResource(ClassLoader cl, String name) Resolves resources only managed by JRebel.findRebelResources(ClassLoader cl, String name) Resolves resources only managed by JRebel.Class<?>findReloadableClass(ClassLoader cl, String className) Resolves classes managed by JRebel.findResource(ClassLoader cl, String name) Resolves resources managed by JRebel.findResources(ClassLoader cl, String name) Resolves resources managed by JRebel.Returns an instance ofClassBytecodeProcessorCachewhich can be used by class bytecode processors to cache their transformations between runs.Returns the additional class path of the givenClassLoadermanaged by JRebel.URL[]Returns the additional class path of the givenClassLoadermanaged by JRebel.URL[]getRebelURLs(ClassLoader cl, boolean onlyFullMatch) Returns the additional class path of the givenClassLoadermanaged by JRebel.getScanHelper(ClassLoader cl, String name) Returns helper object to perform class path scanning in the givenClassLoader.getTransparentResource(ClassLoader cl, String name) Returns the original resource with given name.getTransparentResources(ClassLoader cl, String name) Returns the original resources with given name.booleanReturnstrueifPlugin.checkDependencies(ClassLoader, ClassResourceSource)is being called for the given class loader.booleanSays if thisClassLoaderis registerd with JRebel.booleanisResourceReplaced(ClassLoader cl, String name) Returnstrueif the given resource is managed by JRebel.voidrecordClassResource(ClassLoader cl, String className, URL url) Record location of found class.voidrecordClassResource(ClassLoader cl, String className, URL url, byte[] bytes) voidredefineClasses(ClassDefinition... classDefinitions) Redefine a set of classes managed by JRebel.booleanredefineReloadableClass(Class<?> klass, byte[] bytecode) Same as redefineReloadableClass(klass, bytecode, true)booleanredefineReloadableClass(Class<?> klass, byte[] bytecode, boolean processedByLoader) Redefines a class managed by JRebel.Class<?>redefineReloadableHiddenClass(Class<?> klass, byte[] bytecode) Redefines a hidden class managed by JRebel.voidRegisters aClassLoaderwith JRebel associating with it the class-to-file resolution strategy provided byClassResourceSource.voidReinitializes theClassLoaderwith JRebel.voidRemoves a call back invoked when a given class loader is being destroyed.voidremoveIntegrationProcessor(ClassBytecodeProcessor processor) Remove bytecode processor.voidRemove bytecode processors that are loaded by the ClassLoadervoidreportError(Throwable e, String version, String jarFile) Report CBP failure, implementation must be async.voidUnregisters theClassLoaderfrom JRebel.voidCache results for class and resource request separately.
-
Method Details
-
registerClassLoader
Registers aClassLoaderwith JRebel associating with it the class-to-file resolution strategy provided byClassResourceSource. It is necessary to do this before any calls tofindReloadableClass(ClassLoader, String), the best place is usually the constructor.- Parameters:
cl- The custom class loader being integrated.cfs- The class-to-file resolution strategy source.- See Also:
-
reinitializeClassLoader
Reinitializes theClassLoaderwith JRebel.This should be called if the class path of the given
ClassLoaderwas updated.- Parameters:
cl- The custom class loader being integrated.
-
unregisterClassLoader
Unregisters theClassLoaderfrom JRebel. It is recommended to do this in case the correspondingClassLoaderis destroyed (e.g. the web application is undeployed).- Parameters:
cl- The custom class loader being integrated.
-
isRegisteredClassLoader
Says if thisClassLoaderis registerd with JRebel.- Parameters:
cl- The custom class loader being integrated.- Returns:
- true if this
ClassLoaderis registerd with JRebel.
-
getRebelURLs
Returns the additional class path of the givenClassLoadermanaged by JRebel.Same as
getRebelURLs(cl, false).- Parameters:
cl- The custom class loader being integrated.- Returns:
- additional class path of the given
ClassLoaderornullif nothing found.
-
getRebelURLs
Returns the additional class path of the givenClassLoadermanaged by JRebel.- Parameters:
cl- The custom class loader being integrated.onlyFullMatch- whether to only return locations with no custom patterns.- Returns:
- additional class path of the given
ClassLoaderornullif nothing found.
-
getRebelSources
Returns the additional class path of the givenClassLoadermanaged by JRebel.- Parameters:
cl- The custom class loader being integrated.- Returns:
- additional class path of the given
ClassLoaderornullif nothing found.
-
getScanHelper
Returns helper object to perform class path scanning in the givenClassLoader. NOTE: this api is experimental and may be removed or changed in future versions.- Parameters:
cl- The class loader used to find resources to scan.name- name of resource to start scanning from.- Returns:
- helper for class path scanning or
nullif not available.
-
findReloadableClass
Resolves classes managed by JRebel. These and only these classes will be reloadable. Returns null if class couldn't be resolved and the non-JRebel class loading should be used as fail-over. ThrowsClassNotFoundExceptionif class does not exist and the non-JRebel class loading should be skipped.- Parameters:
cl- The custom class loader being integrated.className- The public class name.- Returns:
- Resolved class or null.
- Throws:
ClassNotFoundException- if class does not exist.- See Also:
-
defineReloadableClass
Class<?> defineReloadableClass(ClassLoader cl, String className, Resource mutableResource, ProtectionDomain protectionDomain, boolean processedByLoader) (Re)defines a class managed by JRebel. When the class is reloaded, the Resource provided here is used to get the new bytes. This can be used to re-generate the bytes for a proxy class after a reload. Returns null if this class loader is not registered with JRebel.- Parameters:
cl- The custom class loader being integrated.className- The public class name.mutableResource- bytes for the class.protectionDomain- The ProtectionDomain of the class (may benull).processedByLoader- Whether given bytes should be processed by class loader in the same way as a normally loaded classes. Set tofalsewhen class is supposed to be define similarly to reflectively callingClassLoader.defineClassthat bypasses logic inside defining loader.- Returns:
- (Re)defined class or
null. - Since:
- 18.2.5
-
defineReloadableClass
Class<?> defineReloadableClass(ClassLoader cl, String className, byte[] bytecode, ProtectionDomain protectionDomain, boolean processedByLoader) (Re)defines a class managed by JRebel.This method may be called for loading as well as reloading the class.
This method is intended to replace defineClass, e.g. used from the same context as defineClass is currently used. So in normal java ClassLoader the caller of defineClass should already be synchronized and we should not do it inside defineReloadableClass in order to avoid creating extra deadlocks.
The defined class will only be reloaded if this method is called again. Even if the class was first loaded by
Returns null if this class loader is not registered with JRebel.findReloadableClass(ClassLoader, String)the resolved class resource will not be checked for updates.- Parameters:
cl- The custom class loader being integrated.className- The public class name.bytecode- class bytes.protectionDomain- The ProtectionDomain of the class (may benull).processedByLoader- Whether given bytes should be processed by class loader in the same way as a normally loaded classes. Set tofalsewhen class is supposed to be define similarly to reflectively callingClassLoader.defineClassthat bypasses logic inside defining loader.- Returns:
- (Re)defined class or
null. - Since:
- 7.0.1
-
defineReloadableClass
Class<?> defineReloadableClass(ClassLoader cl, String className, byte[] bytecode, ProtectionDomain protectionDomain) Same as defineReloadableClass(cl, className, bytecode, protectionDomain, true)- Parameters:
cl- The custom class loader being integrated.className- The public class name.bytecode- class bytes.protectionDomain- The ProtectionDomain of the class (may benull).- Returns:
trueif the class was managed by JRebel and redefined.- Since:
- 3.0
-
redefineReloadableClass
Redefines a class managed by JRebel.- Parameters:
klass- Class managed by JRebel.bytecode- class bytes.processedByLoader- Whether given bytes should be processed by class loader in the same way as a normally loaded classes. Set tofalsewhen class is supposed to be define similarly to reflectively callingClassLoader.defineClassthat bypasses logic inside defining loader.- Returns:
trueif the class was managed by JRebel and redefined.- Since:
- 7.0.1
-
redefineReloadableClass
Same as redefineReloadableClass(klass, bytecode, true)- Parameters:
klass- Class managed by JRebel.bytecode- class bytes.- Returns:
trueif the class was managed by JRebel and redefined.- Since:
- 3.0
-
redefineReloadableHiddenClass
Redefines a hidden class managed by JRebel.- Parameters:
klass- hidden class managed by JRebel.bytecode- class bytes.- Returns:
- the redefined hidden class.
- Since:
- 2022.1.2
-
redefineClasses
Redefine a set of classes managed by JRebel.- Parameters:
classDefinitions- Array of ClassDefinition objects containing the Class and new class-bytes
-
isResourceReplaced
Returnstrueif the given resource is managed by JRebel.In that case resources can be resolved using the corresponding methods of the
Integration. If any of these methods returns anullit must be returned by theClassLoaderas well.- Parameters:
cl- The custom class loader being integrated.name- The public resource name.- Returns:
trueif the given resource is managed by JRebel.
-
findResource
Resolves resources managed by JRebel.- Parameters:
cl- The custom class loader being integrated.name- The public resource name.- Returns:
- Resolved resource URL or null.
-
findRebelResource
Resolves resources only managed by JRebel.- Parameters:
cl- The custom class loader being integrated.name- The public resource name.- Returns:
- Resolved resource URL or null.
-
findResources
Resolves resources managed by JRebel.- Parameters:
cl- The custom class loader being integrated.name- The public resource name.- Returns:
- Resolved resource URLs or null.
-
findRebelResources
Resolves resources only managed by JRebel.- Parameters:
cl- The custom class loader being integrated.name- The public resource name.- Returns:
- Resolved resource URLs or null.
-
getTransparentResources
Returns the original resources with given name. This is used to get the original result unaltered by JRebel.- Parameters:
cl- The custom class loader being integrated.name- The resource name.- Returns:
- Resolved resource URLs or null.
-
getTransparentResource
Returns the original resource with given name. This is used to get the original result unaltered by JRebel.- Parameters:
cl- The custom class loader being integrated.name- The resource name.- Returns:
- Resolved resource URL or null.
-
recordClassResource
Record location of found class. This method may be used to improve cache hit ration for overhead resource requests in class loaders whose findClass does not call findResource.- Parameters:
cl- The custom class loader being integrated.className- The public class name.url- URL of class resource.- Since:
- 4.6
-
recordClassResource
-
useSeparateCacheForClassesAndResources
Cache results for class and resource request separately. Use when the result of resource lookup as returned by findResource may be different from what is recorded with recordClassResource.- Parameters:
cl- The custom class loader being integrated.
-
addIntegrationProcessor
Adds a bytecode processor for the given class in the given classloader. It will be run before that class is loaded and is mainly use to enable integration with a specific framework or classloader. Allows to additionally restrict the bytecode processing to the given classloader.- Parameters:
cl- the ClassLoader for the classclassName- the target class by nameprocessor- the processor to add
-
addIntegrationProcessor
Adds a bytecode processor for the given class. It will be run before that class is loaded and is mainly use to enable integration with a specific framework or classloader.- Parameters:
className- the target class by nameprocessor- the processor to add
-
addIntegrationProcessor
Adds a bytecode processor for the given classloader. It will be run before that class is loaded and is mainly use to enable integration with a specific framework or classloader. Allows to additionally restrict the bytecode processing to the given classloader.- Parameters:
cl- the ClassLoader for the classesprocessor- the processor to add
-
addIntegrationProcessor
Adds a bytecode processor for all classes. It will be run before that class is loaded and is mainly used for integration with preprocessors like AspectJ.If
managedOnlyistruethe processor will only be run for the classes managed by JRebel. This is useful to enable processing usually done in the class loaderfindClass()method, e.g. the JBoss AOP.- Parameters:
processor- the processor to addmanagedOnly- should we only run it on reloadable classes
-
addIntegrationProcessor
AAdds a bytecode processor for the given classloader. It will be run before that class is loaded and is mainly used for integration with preprocessors like AspectJ.If
managedOnlyistruethe processor will only be run for the classes managed by JRebel. This is useful to enable processing usually done in the class loaderfindClass()method, e.g. the JBoss AOP.- Parameters:
cl- the ClassLoader for the classesprocessor- the processor to addmanagedOnly- should we only run it on reloadable classes
-
addIntegrationProcessor
Adds a bytecode processor for the set of given classes. It will be run before those classes are loaded and is mainly use to enable integration with a specific framework or classloader.- Parameters:
classNames- the class to associate the processor withprocessor- the processor to add- See Also:
-
addIntegrationProcessor
Adds a bytecode processor for the set of given classes in the given classloader. It will be run before those classes are loaded and is mainly use to enable integration with a specific framework or classloader. Allows to additionally restrict the bytecode processing to the given classloader.- Parameters:
cl- the ClassLoader for the classesclassNames- the class to associate the processor withprocessor- the processor to add- See Also:
-
removeIntegrationProcessor
Remove bytecode processor.- Parameters:
processor- the processor to remove
-
removeIntegrationProcessors
Remove bytecode processors that are loaded by the ClassLoader- Parameters:
cl- the ClassLoader to remove all processors from
-
addBootClassProcessor
Adds a bytecode processor for the given class in boot classloader. It will be run before that class is loaded and is mainly use to enable integration with a specific framework or classloader.- Parameters:
className- the class to associate the processor withprocessor- the processor to add
-
addBootClassProcessor
Adds a bytecode processor for set of given classes in boot classloader. It will be run before that class is loaded and is mainly use to enable integration with a specific framework or classloader.- Parameters:
classNames- the classes to associate the processor withprocessor- the processor to add- See Also:
-
addBootClassProcessor
Adds a bytecode processor in boot classloader. It is mainly use to enable integration with a specific framework or classloader. This API is only available when JRebel is configured as java agent.- Parameters:
classBytecodeProcessor- the processor to add
-
reportError
Report CBP failure, implementation must be async.- Parameters:
e- the error to reportversion- versionjarFile- location
-
addClassLoaderDestructionListener
Registers a call back invoked when a given class loader is being destroyed.- Parameters:
cl- The custom class loader being integrated.listener- class loader destruction listener.
-
removeClassLoaderDestructionListener
Removes a call back invoked when a given class loader is being destroyed.- Parameters:
listener- class loader destruction listener.
-
isCheckingPluginDependencies
ReturnstrueifPlugin.checkDependencies(ClassLoader, ClassResourceSource)is being called for the given class loader.During the given process no class path entries should be altered.
- Parameters:
cl- the target ClassLoader- Returns:
trueifPlugin.checkDependencies(ClassLoader, ClassResourceSource)is being called for the given class loader.
-
disablePlugins
Disable running plugins in given class loader.- Parameters:
cl- the target ClassLoader
-
disableReloading
Disable class reloading in given class loader. Resource management will still be enabled.- Parameters:
cl- the target ClassLoader
-
disableReloadDetection
Disable class reload check in given class loader. Resource management will still be enabled.- Parameters:
cl- the target ClassLoader
-
addAfterMainCallback
Registers a callback that will be invoked after the main method is called. Callback will be invoked in the same order as they were added. If a callback is added after the main method has been called it will be invoked immediately.- Parameters:
callback- the callback
-
disableSecurityManager
void disableSecurityManager()Disables the SecurityManager in the current thread. -
enableSecurityManager
void enableSecurityManager()Re-enables the disabled SecurityManager in the current thread. -
getClassBytecodeProcessorCache
ClassBytecodeProcessorCache getClassBytecodeProcessorCache()Returns an instance ofClassBytecodeProcessorCachewhich can be used by class bytecode processors to cache their transformations between runs.- Returns:
- cacheCleaner the cache
-
bindToClassLoader
Add reference from given class loader to given object. This will prevent GC from collecting the input object before class loader is collected.- Parameters:
cl- Class loader that will be used to keep object alive.value- Object that will be made to live as long as class loader.- Since:
- 2019.2.2
-
addCacheCleaner
An integration point to add once per reload caches that should be cleared after reload.- Parameters:
cacheCleaner- a listener to be called for cleanup- Since:
- 2020.1.2
-