Skip to content

Commit

Permalink
use module opener only when supported
Browse files Browse the repository at this point in the history
  • Loading branch information
SylvainJuge committed Dec 19, 2024
1 parent 101710d commit 9c251f0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@
import java.util.Set;
import java.util.logging.Logger;

/**
* Module opener provides ability to open JPMS modules and allows instrumentation classloader to
* access module contents without requiring JVM arguments modification. <br>
* Usage of this class must be guarded with an {@code net.bytebuddy.utility.JavaModule#isSupported}
* check as it's compiled for Java 9+, otherwise an {@link UnsupportedClassVersionError} will be
* thrown for java 8.
*/
public class ModuleOpener {

private static final Logger logger = Logger.getLogger(ModuleOpener.class.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import net.bytebuddy.agent.builder.AgentBuilder;
import net.bytebuddy.utility.JavaModule;

public class IndyModuleRegistry {

Expand Down Expand Up @@ -88,19 +89,22 @@ public static InstrumentationModuleClassLoader getInstrumentationClassLoader(
throw new IllegalStateException("global instrumentation not available");
}

experimentalModule
.jpmsModulesToOpen()
.forEach(
(className, packages) -> {
Class<?> type;
try {
type = Class.forName(className, false, instrumentedClassLoader);
} catch (ClassNotFoundException e) {
throw new IllegalStateException("missing witness class " + className, e);
}

ModuleOpener.open(instrumentation, type, loader, packages);
});
if (JavaModule.isSupported()) {
// module opener only usable for java 9+
experimentalModule
.jpmsModulesToOpen()
.forEach(
(className, packages) -> {
Class<?> type;
try {
type = Class.forName(className, false, instrumentedClassLoader);
} catch (ClassNotFoundException e) {
throw new IllegalStateException("missing witness class " + className, e);
}

ModuleOpener.open(instrumentation, type, loader, packages);
});
}
}
return loader;
}
Expand Down

0 comments on commit 9c251f0

Please sign in to comment.