java.lang.ClassLoader.getSystemClassLoader()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(7.6k)|赞(0)|评价(0)|浏览(239)

本文整理了Java中java.lang.ClassLoader.getSystemClassLoader()方法的一些代码示例,展示了ClassLoader.getSystemClassLoader()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ClassLoader.getSystemClassLoader()方法的具体详情如下:
包路径:java.lang.ClassLoader
类名称:ClassLoader
方法名:getSystemClassLoader

ClassLoader.getSystemClassLoader介绍

[英]Returns the system class loader. This is the parent for new ClassLoader instances and is typically the class loader used to start the application.
[中]返回系统类加载器。这是新类加载器实例的父类,通常是用于启动应用程序的类加载器。

代码示例

代码示例来源:origin: netty/netty

static ClassLoader getSystemClassLoader() {
  if (System.getSecurityManager() == null) {
    return ClassLoader.getSystemClassLoader();
  } else {
    return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
      @Override
      public ClassLoader run() {
        return ClassLoader.getSystemClassLoader();
      }
    });
  }
}

代码示例来源:origin: redisson/redisson

static ClassLoader getSystemClassLoader() {
  if (System.getSecurityManager() == null) {
    return ClassLoader.getSystemClassLoader();
  } else {
    return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
      @Override
      public ClassLoader run() {
        return ClassLoader.getSystemClassLoader();
      }
    });
  }
}

代码示例来源:origin: google/guava

private static ImmutableList<URL> getClassLoaderUrls(ClassLoader classloader) {
 if (classloader instanceof URLClassLoader) {
  return ImmutableList.copyOf(((URLClassLoader) classloader).getURLs());
 }
 if (classloader.equals(ClassLoader.getSystemClassLoader())) {
  return parseJavaClassPath();
 }
 return ImmutableList.of();
}

代码示例来源:origin: skylot/jadx

private static void load(LangLocale locale) {
  ResourceBundle bundle;
  ClassLoader classLoader = ClassLoader.getSystemClassLoader();
  String resName = String.format("i18n/Messages_%s.properties", locale.get());
  URL bundleUrl = classLoader.getResource(resName);
  if (bundleUrl == null) {
    throw new JadxRuntimeException("Locale resource not found: " + resName);
  }
  try (Reader reader = new InputStreamReader(bundleUrl.openStream(), StandardCharsets.UTF_8)) {
    bundle = new PropertyResourceBundle(reader);
  } catch (IOException e) {
    throw new JadxRuntimeException("Failed to load " + resName, e);
  }
  i18nMessagesMap.put(locale, bundle);
}

代码示例来源:origin: alibaba/druid

public static Properties loadFilterConfig() throws IOException {
  Properties filterProperties = new Properties();
  loadFilterConfig(filterProperties, ClassLoader.getSystemClassLoader());
  loadFilterConfig(filterProperties, FilterManager.class.getClassLoader());
  loadFilterConfig(filterProperties, Thread.currentThread().getContextClassLoader());
  loadFilterConfig(filterProperties, FilterManager.class.getClassLoader());
  return filterProperties;
}

代码示例来源:origin: prestodb/presto

private static ImmutableList<URL> getClassLoaderUrls(ClassLoader classloader) {
 if (classloader instanceof URLClassLoader) {
  return ImmutableList.copyOf(((URLClassLoader) classloader).getURLs());
 }
 if (classloader.equals(ClassLoader.getSystemClassLoader())) {
  return parseJavaClassPath();
 }
 return ImmutableList.of();
}

代码示例来源:origin: commons-io/commons-io

@Test public void testResourceToString_ExistingResourceAtSubPackage_WithClassLoader() throws Exception {
  final long fileSize = new File(getClass().getResource("/org/apache/commons/io/FileUtilsTestDataCR.dat").getFile()).length();
  final String content = IOUtils.resourceToString(
      "org/apache/commons/io/FileUtilsTestDataCR.dat",
      StandardCharsets.UTF_8,
      ClassLoader.getSystemClassLoader()
  );
  assertNotNull(content);
  assertEquals(fileSize, content.getBytes().length);
}

代码示例来源:origin: commons-io/commons-io

@Test public void testResourceToString_ExistingResourceAtRootPackage_WithClassLoader() throws Exception {
  final long fileSize = new File(getClass().getResource("/test-file-simple-utf8.bin").getFile()).length();
  final String content = IOUtils.resourceToString(
      "test-file-simple-utf8.bin",
      StandardCharsets.UTF_8,
      ClassLoader.getSystemClassLoader()
  );
  assertNotNull(content);
  assertEquals(fileSize, content.getBytes().length);
}

代码示例来源:origin: google/guava

public void testResourceScanner() throws IOException {
 ResourceScanner scanner = new ResourceScanner();
 scanner.scan(ClassLoader.getSystemClassLoader());
 assertThat(scanner.resources).contains("com/google/common/reflect/ClassPathTest.class");
}

代码示例来源:origin: commons-io/commons-io

@Test public void testResourceToByteArray_ExistingResourceAtRootPackage_WithClassLoader() throws Exception {
  final long fileSize = new File(getClass().getResource("/test-file-utf8.bin").getFile()).length();
  final byte[] bytes = IOUtils.resourceToByteArray("test-file-utf8.bin", ClassLoader.getSystemClassLoader());
  assertNotNull(bytes);
  assertEquals(fileSize, bytes.length);
}

代码示例来源:origin: commons-io/commons-io

@Test public void testResourceToByteArray_ExistingResourceAtSubPackage_WithClassLoader() throws Exception {
  final long fileSize = new File(getClass().getResource("/org/apache/commons/io/FileUtilsTestDataCR.dat").getFile()).length();
  final byte[] bytes = IOUtils.resourceToByteArray("org/apache/commons/io/FileUtilsTestDataCR.dat", ClassLoader.getSystemClassLoader());
  assertNotNull(bytes);
  assertEquals(fileSize, bytes.length);
}

代码示例来源:origin: commons-io/commons-io

@Test public void testResourceToString_NullCharset_WithClassLoader() throws Exception {
  IOUtils.resourceToString("test-file-utf8.bin", null, ClassLoader.getSystemClassLoader());
}

代码示例来源:origin: commons-io/commons-io

@Test(expected = IOException.class) public void testResourceToString_NonExistingResource_WithClassLoader() throws Exception {
  IOUtils.resourceToString("non-existing-file.bin", StandardCharsets.UTF_8, ClassLoader.getSystemClassLoader());
}

代码示例来源:origin: commons-io/commons-io

@Test(expected = IOException.class) public void testResourceToByteArray_NonExistingResource_WithClassLoader() throws Exception {
  IOUtils.resourceToByteArray("non-existing-file.bin", ClassLoader.getSystemClassLoader());
}

代码示例来源:origin: commons-io/commons-io

@Test(expected = IOException.class) public void testResourceToURL_NonExistingResource_WithClassLoader() throws Exception {
  IOUtils.resourceToURL("non-existing-file.bin", ClassLoader.getSystemClassLoader());
}

代码示例来源:origin: commons-io/commons-io

@Test public void testResourceToURL_ExistingResourceAtRootPackage_WithClassLoader() throws Exception {
  final URL url = IOUtils.resourceToURL("test-file-utf8.bin", ClassLoader.getSystemClassLoader());
  assertNotNull(url);
  assertTrue(url.getFile().endsWith("/test-file-utf8.bin"));
}

代码示例来源:origin: commons-io/commons-io

@Test public void testResourceToURL_ExistingResourceAtSubPackage_WithClassLoader() throws Exception {
  final URL url = IOUtils.resourceToURL(
      "org/apache/commons/io/FileUtilsTestDataCR.dat",
      ClassLoader.getSystemClassLoader()
  );
  assertNotNull(url);
  assertTrue(url.getFile().endsWith("/org/apache/commons/io/FileUtilsTestDataCR.dat"));
}

代码示例来源:origin: commons-io/commons-io

@Test public void testResourceToURL_Null_WithClassLoader() throws Exception {
  boolean exceptionOccurred = false;
  try {
    IOUtils.resourceToURL(null, ClassLoader.getSystemClassLoader());
    fail();
  } catch (final NullPointerException npe) {
    exceptionOccurred = true;
    assertNotNull(npe);
  }
  assertTrue(exceptionOccurred);
}

代码示例来源:origin: commons-io/commons-io

@Test public void testResourceToByteArray_Null_WithClassLoader() throws Exception {
  boolean exceptionOccurred = false;
  try {
    IOUtils.resourceToByteArray(null, ClassLoader.getSystemClassLoader());
    fail();
  } catch (final NullPointerException npe) {
    exceptionOccurred = true;
    assertNotNull(npe);
  }
  assertTrue(exceptionOccurred);
}

代码示例来源:origin: commons-io/commons-io

@Test public void testResourceToString_NullResource_WithClassLoader() throws Exception {
  boolean exceptionOccurred = false;
  try {
    IOUtils.resourceToString(null, StandardCharsets.UTF_8, ClassLoader.getSystemClassLoader());
    fail();
  } catch (final NullPointerException npe) {
    exceptionOccurred = true;
    assertNotNull(npe);
  }
  assertTrue(exceptionOccurred);
}

相关文章