下面列出了java.net.URLClassLoader#getResource ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Returns true if any of the given resources could be found on the given class loader
*
* @param project project object
* @param paths array of strings as path
* @return boolean value indicating whether that project has resource or not
*/
public static boolean hasResource(MavenProject project, String... paths) {
URLClassLoader compileClassLoader = getCompileClassLoader(project);
for (String path : paths) {
try {
if (compileClassLoader.getResource(path) != null) {
return true;
}
} catch (Exception e) {
// ignore
}
}
return false;
}
private URL getSwaggerFileUrl(File jarFile, JarEntry entry) {
try {
URLClassLoader urlClassLoader = new URLClassLoader(new URL[] {jarFile.toURI().toURL()});
return urlClassLoader.getResource(entry.getName());
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
}
/** Maybe connect, if not keep track of the problem.
*/
private synchronized void tryToConnect() {
if (connected || exception != null) {
return;
}
try {
URLClassLoader l;
String cnb = url.getHost();
if (cnb.isEmpty()) {
l = globalClassLoader.get();
} else {
l = classLoaderMap.get().get(cnb);
if (l == null) {
throw new IOException("no loader for " + cnb);
}
}
String path = url.getPath().substring(1);
URL u = l.getResource(path);
if (u == null) {
throw new FileNotFoundException(path + " in " + Arrays.toString(l.getURLs()));
}
real = u.openConnection();
real.connect();
connected = true;
} catch (IOException ioe) {
exception = ioe;
}
}
private static URI getUri(final URLClassLoader loader, final String name) {
final URL url = loader.getResource(name);
if (url == null) {
throw new CorruptXmlFileException(name);
}
return URI.create(url.toString().replace(" ", "%20"));
}
@Test(dataProvider = "resourcedata")
public void testResources(String style, URL url) throws Throwable {
//System.out.println(" testing " + style + " url: " + url);
URL[] urls = {url};
URLClassLoader cldr = new URLClassLoader(urls);
Class<?> vcls = cldr.loadClass("version.Version");
// verify we are loading a runtime versioned class
MethodType mt = MethodType.methodType(int.class);
MethodHandle mh = MethodHandles.lookup().findVirtual(vcls, "getVersion", mt);
Assert.assertEquals((int)mh.invoke(vcls.newInstance()),
style.equals("unversioned") ? 8 : Runtime.version().major());
// now get a resource and verify that we don't have a fragment attached
Enumeration<URL> vclsUrlEnum = cldr.getResources("version/Version.class");
Assert.assertTrue(vclsUrlEnum.hasMoreElements());
URL vclsUrls[] = new URL[] {
vcls.getResource("/version/Version.class"),
vcls.getResource("Version.class"),
cldr.getResource("version/Version.class"),
vclsUrlEnum.nextElement()
};
Assert.assertFalse(vclsUrlEnum.hasMoreElements());
for (URL vclsUrl : vclsUrls) {
String fragment = vclsUrl.getRef();
Assert.assertNull(fragment);
// and verify that the the url is a reified pointer to the runtime entry
String rep = vclsUrl.toString();
//System.out.println(" getResource(\"/version/Version.class\") returned: " + rep);
if (style.equals("http")) {
Assert.assertTrue(rep.startsWith("jar:http:"));
} else {
Assert.assertTrue(rep.startsWith("jar:file:"));
}
String suffix;
if (style.equals("unversioned")) {
suffix = ".jar!/version/Version.class";
} else {
suffix = ".jar!/META-INF/versions/" + Runtime.version().major()
+ "/version/Version.class";
}
Assert.assertTrue(rep.endsWith(suffix));
}
cldr.close();
}