本文整理了Java中java.lang.Class.getResource()
方法的一些代码示例,展示了Class.getResource()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Class.getResource()
方法的具体详情如下:
包路径:java.lang.Class
类名称:Class
方法名:getResource
[英]Returns the URL of the given resource, or null if the resource is not found. The mapping between the resource name and the URL is managed by the class' class loader.
[中]返回给定资源的URL,如果找不到该资源,则返回null。资源名称和URL之间的映射由类的类加载器管理。
代码示例来源:origin: google/guava
/**
* Given a {@code resourceName} that is relative to {@code contextClass}, returns a {@code URL}
* pointing to the named resource.
*
* @throws IllegalArgumentException if the resource is not found
*/
public static URL getResource(Class<?> contextClass, String resourceName) {
URL url = contextClass.getResource(resourceName);
checkArgument(
url != null, "resource %s relative to %s not found.", resourceName, contextClass.getName());
return url;
}
}
代码示例来源:origin: skylot/jadx
public static ImageIcon openIcon(String name) {
String iconPath = "/icons-16/" + name + ".png";
URL resource = Utils.class.getResource(iconPath);
if (resource == null) {
throw new JadxRuntimeException("Icon not found: " + iconPath);
}
return new ImageIcon(resource);
}
代码示例来源:origin: google/guava
private static URL classfile(Class<?> c) {
return c.getResource(c.getSimpleName() + ".class");
}
}
代码示例来源:origin: spring-projects/spring-framework
public JAXBElement<DataHandler> standardClassDataHandler() {
DataSource dataSource = new URLDataSource(getClass().getResource("spring-ws.png"));
DataHandler dataHandler = new DataHandler(dataSource);
return new JAXBElement<>(NAME, DataHandler.class, dataHandler);
}
代码示例来源:origin: google/guava
public void testToString() throws IOException {
URL resource = getClass().getResource("testdata/i18n.txt");
assertEquals(I18N, Resources.toString(resource, Charsets.UTF_8));
assertThat(Resources.toString(resource, Charsets.US_ASCII)).isNotEqualTo(I18N);
}
代码示例来源:origin: google/guava
public void testCopyToOutputStream() throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
URL resource = getClass().getResource("testdata/i18n.txt");
Resources.copy(resource, out);
assertEquals(I18N, out.toString("UTF-8"));
}
代码示例来源:origin: spring-projects/spring-framework
@Test(expected = FileNotFoundException.class)
public void testInputStreamNotFoundOnFileSystemResource() throws IOException {
new FileSystemResource(getClass().getResource("Resource.class").getFile()).createRelative("X").getInputStream();
}
代码示例来源:origin: google/guava
/** Returns the file with the given name under the testdata directory. */
protected final File getTestFile(String name) throws IOException {
File file = new File(getTestDir(), name);
if (!file.exists()) {
URL resourceUrl = IoTestCase.class.getResource("testdata/" + name);
if (resourceUrl == null) {
return null;
}
copy(resourceUrl, file);
}
return file;
}
代码示例来源:origin: google/guava
public void testReadLines() throws IOException {
// TODO(chrisn): Check in a better resource
URL resource = getClass().getResource("testdata/i18n.txt");
assertEquals(ImmutableList.of(I18N), Resources.readLines(resource, Charsets.UTF_8));
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void testFileSystemResourceWithFilePath() throws Exception {
Path filePath = Paths.get(getClass().getResource("Resource.class").toURI());
Resource resource = new FileSystemResource(filePath);
doTestResource(resource);
assertEquals(new FileSystemResource(filePath), resource);
}
代码示例来源:origin: spring-projects/spring-framework
public CallbacksSecurityTests() {
// setup security
if (System.getSecurityManager() == null) {
Policy policy = Policy.getPolicy();
URL policyURL = getClass()
.getResource(
"/org/springframework/beans/factory/support/security/policy.all");
System.setProperty("java.security.policy", policyURL.toString());
System.setProperty("policy.allowSystemProperty", "true");
policy.refresh();
System.setSecurityManager(new SecurityManager());
}
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void testFileSystemResource() throws IOException {
String file = getClass().getResource("Resource.class").getFile();
Resource resource = new FileSystemResource(file);
doTestResource(resource);
assertEquals(new FileSystemResource(file), resource);
}
代码示例来源:origin: google/guava
@AndroidIncompatible // TODO(cpovirk): How significant is this failure?
public void testGetFinalizerUrl() {
assertNotNull(getClass().getResource("internal/Finalizer.class"));
}
代码示例来源:origin: google/guava
private void doExtensiveTest(String resourceName) throws IOException {
Splitter splitter = Splitter.on(CharMatcher.whitespace());
URL url = getClass().getResource(resourceName);
for (String line : Resources.readLines(url, UTF_8)) {
Iterator<String> iterator = splitter.split(line).iterator();
String input = iterator.next();
String expectedOutput = iterator.next();
assertFalse(iterator.hasNext());
assertEquals(expectedOutput, simplifyPath(input));
}
}
}
代码示例来源:origin: spring-projects/spring-framework
@Test(expected = FileNotFoundException.class)
public void testReadableChannelNotFoundOnFileSystemResource() throws IOException {
new FileSystemResource(getClass().getResource("Resource.class").getFile()).createRelative("X").readableChannel();
}
代码示例来源:origin: spring-projects/spring-framework
@Test // SPR-12624
public void checkRelativeLocation() throws Exception {
String locationUrl= new UrlResource(getClass().getResource("./test/")).getURL().toExternalForm();
Resource location = new UrlResource(locationUrl.replace("/springframework","/../org/springframework"));
List<Resource> locations = singletonList(location);
assertNotNull(this.resolver.resolveResource(null, "main.css", locations, null).block(TIMEOUT));
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void checkRelativeLocation() throws Exception {
String locationUrl= new UrlResource(getClass().getResource("./test/")).getURL().toExternalForm();
Resource location = new UrlResource(locationUrl.replace("/springframework","/../org/springframework"));
assertNotNull(this.resolver.resolveResource(null, "main.css", Collections.singletonList(location), null));
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void testFileSystemResourceWithImport() throws URISyntaxException {
String file = getClass().getResource(RESOURCE_CONTEXT.getPath()).toURI().getPath();
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(new FileSystemResource(file));
// comes from "resourceImport.xml"
xbf.getBean("resource1", ResourceTestBean.class);
// comes from "resource.xml"
xbf.getBean("resource2", ResourceTestBean.class);
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void testUrlResourceWithImport() {
URL url = getClass().getResource(RESOURCE_CONTEXT.getPath());
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(new UrlResource(url));
// comes from "resourceImport.xml"
xbf.getBean("resource1", ResourceTestBean.class);
// comes from "resource.xml"
xbf.getBean("resource2", ResourceTestBean.class);
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void testUrlResource() throws IOException {
Resource resource = new UrlResource(getClass().getResource("Resource.class"));
doTestResource(resource);
assertEquals(new UrlResource(getClass().getResource("Resource.class")), resource);
Resource resource2 = new UrlResource("file:core/io/Resource.class");
assertEquals(resource2, new UrlResource("file:core/../core/io/./Resource.class"));
assertEquals("test.txt", new UrlResource("file:/dir/test.txt?argh").getFilename());
assertEquals("test.txt", new UrlResource("file:\\dir\\test.txt?argh").getFilename());
assertEquals("test.txt", new UrlResource("file:\\dir/test.txt?argh").getFilename());
}
内容来源于网络,如有侵权,请联系作者删除!