Fix masked class loading for JDK 9
Summary:
In JDK 9 platform classes maybe in the platform class loader returned by
ClassLoader.getPlatformClassLoader. See:
Test Plan: tested
Reviewers: jrtom
Subscribers: #core_team
@@ -848,7 +848,20 @@ public class JavaCompilationUnitExtractor {
private static class MaskedClassLoader extends ClassLoader {
private MaskedClassLoader() {
// delegate only to the bootclasspath
- super(null);
+ super(getPlatformClassLoader());
+ }
+
+ // TODO(T281): remove reflection and call ClassLoader.getPlatformClassLoader() directly
+ // once JDK 8 compatibility is no longer required.
+ private static ClassLoader getPlatformClassLoader() {
+ try {
+ // In JDK 9, all platform classes are visible to the platform class loader:
+ // https://docs.oracle.com/javase/9/docs/api/java/lang/ClassLoader.html#getPlatformClassLoader--
+ return (ClassLoader) ClassLoader.class.getMethod("getPlatformClassLoader").invoke(null);
+ } catch (ReflectiveOperationException e) {
+ // In earlier releases, set 'null' as the parent to delegate to the boot class loader.
+ return null;
+ }
}
@Override
