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

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

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

ClassLoader.getResource介绍

[英]Returns the URL of the resource with the specified name. This implementation first tries to use the parent class loader to find the resource; if this fails then #findResource(String) is called to find the requested resource.
[中]返回具有指定名称的资源的URL。此实现首先尝试使用父类装入器查找资源;如果失败,则调用#findResource(String)来查找请求的资源。

代码示例

代码示例来源:origin: spring-projects/spring-framework

@Override
public URL getResource(String name) {
  return this.enclosingClassLoader.getResource(name);
}

代码示例来源:origin: apache/incubator-dubbo

private static boolean hasResource(String path) {
  try {
    return Version.class.getClassLoader().getResource(path) != null;
  } catch (Throwable t) {
    return false;
  }
}

代码示例来源:origin: apache/incubator-dubbo

private static boolean hasResource(String path) {
  try {
    return Version.class.getClassLoader().getResource(path) != null;
  } catch (Throwable t) {
    return false;
  }
}

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

/**
 * Returns the url identifying the resource.
 *
 * <p>See {@link ClassLoader#getResource}
 *
 * @throws NoSuchElementException if the resource cannot be loaded through the class loader,
 *     despite physically existing in the class path.
 */
public final URL url() {
 URL url = loader.getResource(resourceName);
 if (url == null) {
  throw new NoSuchElementException(resourceName);
 }
 return url;
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Resolves a URL for the underlying class path resource.
 * @return the resolved URL, or {@code null} if not resolvable
 */
@Nullable
protected URL resolveURL() {
  if (this.clazz != null) {
    return this.clazz.getResource(this.path);
  }
  else if (this.classLoader != null) {
    return this.classLoader.getResource(this.path);
  }
  else {
    return ClassLoader.getSystemResource(this.path);
  }
}

代码示例来源:origin: jenkinsci/jenkins

private String getViewPage(Class<?> clazz, Collection<String> pageNames, String defaultValue) {
  while(clazz!=Object.class && clazz!=null) {
    for (String pageName : pageNames) {
      String name = clazz.getName().replace('.', '/').replace('$', '/') + "/" + pageName;
      if(clazz.getClassLoader().getResource(name)!=null)
        return '/'+name;
    }
    clazz = clazz.getSuperclass();
  }
  return defaultValue;
}

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

/** Gets URL for base of path containing Finalizer.class. */
URL getBaseUrl() throws IOException {
 // Find URL pointing to Finalizer.class file.
 String finalizerPath = FINALIZER_CLASS_NAME.replace('.', '/') + ".class";
 URL finalizerUrl = getClass().getClassLoader().getResource(finalizerPath);
 if (finalizerUrl == null) {
  throw new FileNotFoundException(finalizerPath);
 }
 // Find URL pointing to base of class path.
 String urlString = finalizerUrl.toString();
 if (!urlString.endsWith(finalizerPath)) {
  throw new IOException("Unsupported path style: " + urlString);
 }
 urlString = urlString.substring(0, urlString.length() - finalizerPath.length());
 return new URL(finalizerUrl, urlString);
}

代码示例来源:origin: spring-projects/spring-framework

protected boolean isAspectJWeavingEnabled(String value, ParserContext parserContext) {
  if ("on".equals(value)) {
    return true;
  }
  else if ("off".equals(value)) {
    return false;
  }
  else {
    // Determine default...
    ClassLoader cl = parserContext.getReaderContext().getBeanClassLoader();
    return (cl != null && cl.getResource(AspectJWeavingEnabler.ASPECTJ_AOP_XML_RESOURCE) != null);
  }
}

代码示例来源:origin: jenkinsci/jenkins

private URL getResource(String fileName) {
  Class<?> c = getClass();
  return c.getClassLoader().getResource(c.getName().replace('.','/').replace('$','/')+ fileName);
}

代码示例来源:origin: jenkinsci/jenkins

@Override
public URL getResource(String name) {
  if (isMasked(name)) return null;
  return super.getResource(name);
}

代码示例来源:origin: eclipse-vertx/vert.x

/**
 * It is possible to determine if a resource from a bundle is a directory based on whether or not the ClassLoader
 * returns null for a path (which does not already contain a trailing '/') *and* that path with an added trailing '/'
 *
 * @param url      the url
 * @return if the bundle resource represented by the bundle URL is a directory
 */
private boolean isBundleUrlDirectory(URL url) {
 return url.toExternalForm().endsWith("/") ||
  getClassLoader().getResource(url.getPath().substring(1) + "/") != null;
}

代码示例来源: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: eclipse-vertx/vert.x

@Override
 public URL getResource(String name) {
  if ("foo".equals(name)) {
   return url;
  }
  return super.getResource(name);
 }
});

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

private String getResourcePath(String resName) {
    URL resource = getClass().getClassLoader().getResource(CERTIFICATE_TEST_DIR + resName);
    if (resource == null) {
      throw new RuntimeException("Resource not found: " + resName);
    }
    return resource.getPath();
  }
}

代码示例来源:origin: square/leakcanary

static File fileFromName(String filename) {
 ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
 URL url = classLoader.getResource(filename);
 return new File(url.getPath());
}

代码示例来源:origin: eclipse-vertx/vert.x

@Override
public void setUp() throws Exception {
 super.setUp();
 // This folder is inside the nested-inf/classes directory, inside nestedroot.jar
 webRoot = "webroot2";
 prevCL = Thread.currentThread().getContextClassLoader();
 URL jarUrl = prevCL.getResource("nestedroot.jar");
 URL rootUrl = new URL("jar:" + jarUrl + "!/nested-inf/classes!/");
 URLClassLoader urlClassLoader = new URLClassLoader(new URL[]{rootUrl}, prevCL);
 Thread.currentThread().setContextClassLoader(urlClassLoader);
}

代码示例来源:origin: eclipse-vertx/vert.x

@Setup
public void setup() {
 ClassLoader classLoader = getClass().getClassLoader();
 small = loadJsonAsBuffer(classLoader.getResource("small.json"));
 large = loadJsonAsBuffer(classLoader.getResource("large.json"));
}

代码示例来源:origin: eclipse-vertx/vert.x

@Setup
public void setup() {
 ClassLoader classLoader = getClass().getClassLoader();
 small = loadJson(classLoader.getResource("small.json"));
 large = loadJson(classLoader.getResource("large.json"));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testFindsExistingResourceWithGetResourceAndNoOverrides() {
  assertNotNull(thisClassLoader.getResource(EXISTING_RESOURCE));
  assertNotNull(overridingLoader.getResource(EXISTING_RESOURCE));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testDoesNotFindExistingResourceWithGetResourceAndNullOverride() {
  assertNotNull(thisClassLoader.getResource(EXISTING_RESOURCE));
  overridingLoader.override(EXISTING_RESOURCE, null);
  assertNull(overridingLoader.getResource(EXISTING_RESOURCE));
}

相关文章