Package org.jacoco.agent.rt.internal
Class AgentModule
- java.lang.Object
-
- org.jacoco.agent.rt.internal.AgentModule
-
public class AgentModule extends java.lang.Object
An isolated class loader and distinct module to encapsulate JaCoCo runtime classes. This isolated environment allows to specifically open JDK packages to the agent runtime without changing package accessibility for the application under test.The implementation uses the property that the unnamed module is distinct from all run-time modules (including unnamed modules) bound to other class loaders.
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.ClassLoader
classLoader
private java.util.Set<java.lang.String>
scope
-
Constructor Summary
Constructors Constructor Description AgentModule()
Creates a new isolated module.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private void
addToScopeWithInnerClasses(java.lang.Class<?> c)
private static java.lang.Class<?>
getModuleClass()
static boolean
isSupported()
Checks whether Java modules are supported by the current Java runtime.<T> java.lang.Class<T>
loadClassInModule(java.lang.Class<T> original)
Loads a copy of the given class in the isolated classloader.void
openPackage(java.lang.instrument.Instrumentation instrumentation, java.lang.Class<?> classInPackage)
Opens the package of the provided class to the module created in thisAgentModule()
instance.
-
-
-
Method Detail
-
isSupported
public static boolean isSupported()
Checks whether Java modules are supported by the current Java runtime.- Returns:
true
is modules are supported
-
openPackage
public void openPackage(java.lang.instrument.Instrumentation instrumentation, java.lang.Class<?> classInPackage) throws java.lang.Exception
Opens the package of the provided class to the module created in thisAgentModule()
instance.- Parameters:
instrumentation
- service provided to the agent by the Java runtimeclassInPackage
- example class of the package to open- Throws:
java.lang.Exception
- if package cannot be opened
-
loadClassInModule
public <T> java.lang.Class<T> loadClassInModule(java.lang.Class<T> original) throws java.lang.Exception
Loads a copy of the given class in the isolated classloader. Also any inner classes are loader from the isolated classloader.- Type Parameters:
T
- type of the class to load- Parameters:
original
- original class definition which is used as source- Returns:
- class object from the isolated class loader
- Throws:
java.lang.Exception
- if the class cannot be loaded
-
addToScopeWithInnerClasses
private void addToScopeWithInnerClasses(java.lang.Class<?> c)
-
getModuleClass
private static java.lang.Class<?> getModuleClass() throws java.lang.ClassNotFoundException
- Throws:
java.lang.ClassNotFoundException
-
-