Skip to content

Commit

Permalink
[#8890] Temporary: forcefully bind Java9ClassLoader in System.bootLayer
Browse files Browse the repository at this point in the history
  • Loading branch information
youngjin.kim2 committed Jun 2, 2022
1 parent ad1d936 commit 536710d
Showing 1 changed file with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Enumeration;
Expand Down Expand Up @@ -45,6 +47,38 @@ public Java9ClassLoader(String name, URL[] urls, ClassLoader parent, List<String
Objects.requireNonNull(libClass, "libClass");

this.profilerLibClass = new ProfilerLibClass(libClass);

TEMPORARY_bindClassLoaderToBootLayerInOpenJ9(this);
}

/**
* It forcefully executes `System.bootLayer.bindToLoader(classLoader)` in openj9
* @param classLoader classLoader to bind to bootLayer
*/
private void TEMPORARY_bindClassLoaderToBootLayerInOpenJ9(ClassLoader classLoader) {
final String vmName = System.getProperty("java.vm.name");
if (!vmName.toLowerCase().contains("openj9")) {
System.out.printf("java.vm.name: %s, skip binding Java9ClassLoader to bootLayer\n", vmName);
return;
} else {
System.out.printf("java.vm.name: %s, binding Java9ClassLoader to bootLayer\n", vmName);
}

try {
// Pull System.bootLayer
final Field field = System.class.getDeclaredField("bootLayer");
field.setAccessible(true);
final ModuleLayer bootLayer = (ModuleLayer) field.get(null);
field.setAccessible(false);

// Invoke ModuleLayer::bindToLoader
final Method bindToLoader = ModuleLayer.class.getDeclaredMethod("bindToLoader", ClassLoader.class);
bindToLoader.setAccessible(true);
bindToLoader.invoke(bootLayer, classLoader);
bindToLoader.setAccessible(false);
} catch (Exception e) {
e.printStackTrace();
}
}

@Override
Expand Down

0 comments on commit 536710d

Please # to comment.