-
Notifications
You must be signed in to change notification settings - Fork 872
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dd05ff3
commit 1a9a687
Showing
4 changed files
with
141 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
...avaagent-tooling-java9/src/main/java/io/opentelemetry/javaagent/tooling/ModuleOpener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.tooling; | ||
|
||
import static java.util.logging.Level.FINE; | ||
import static java.util.logging.Level.WARNING; | ||
|
||
import java.lang.instrument.Instrumentation; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.logging.Logger; | ||
|
||
public class ModuleOpener { | ||
|
||
private static final Logger logger = Logger.getLogger(ModuleOpener.class.getName()); | ||
|
||
private ModuleOpener() {} | ||
|
||
/** | ||
* Opens JPMS module to a class loader unnamed module | ||
* | ||
* @param classFromTargetModule class from target module | ||
* @param openTo class loader to open module for | ||
* @param packagesToOpen packages to open | ||
*/ | ||
public static void open( | ||
Instrumentation instrumentation, | ||
Class<?> classFromTargetModule, | ||
ClassLoader openTo, | ||
Collection<String> packagesToOpen) { | ||
|
||
Module targetModule = classFromTargetModule.getModule(); | ||
Module openToModule = openTo.getUnnamedModule(); | ||
Set<Module> openToModuleSet = Collections.singleton(openToModule); | ||
Map<String, Set<Module>> missingOpens = new HashMap<>(); | ||
for (String packageName : packagesToOpen) { | ||
if (!targetModule.isOpen(packageName, openToModule)) { | ||
missingOpens.put(packageName, openToModuleSet); | ||
logger.log( | ||
FINE, | ||
() -> | ||
String.format( | ||
"Exposing package '%s' in module '%s' to module '%s'", | ||
packageName, targetModule, openToModule)); | ||
} | ||
} | ||
if (missingOpens.isEmpty()) { | ||
return; | ||
} | ||
|
||
if (!instrumentation.isModifiableModule(targetModule)) { | ||
logger.log(WARNING, "Module '{}' can't be modified", targetModule); | ||
return; | ||
} | ||
|
||
try { | ||
instrumentation.redefineModule( | ||
targetModule, | ||
Collections.<Module>emptySet(), // reads | ||
Collections.<String, Set<Module>>emptyMap(), // exports | ||
missingOpens, // opens | ||
Collections.<Class<?>>emptySet(), // uses | ||
Collections.<Class<?>, List<Class<?>>>emptyMap() // provides | ||
); | ||
} catch (Exception e) { | ||
logger.log(WARNING, "unable to redefine module", e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters