本文整理了Java中java.lang.Class.getClassLoader()
方法的一些代码示例,展示了Class.getClassLoader()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Class.getClassLoader()
方法的具体详情如下:
包路径:java.lang.Class
类名称:Class
方法名:getClassLoader
[英]Returns the class loader which was used to load the class represented by this Class. Implementations are free to return null for classes that were loaded by the bootstrap class loader. The Android reference implementation, though, always returns a reference to an actual class loader.
[中]返回用于加载此类表示的类的类加载器。对于引导类加载器加载的类,实现可以自由返回null。不过,Android引用实现总是返回对实际类加载器的引用。
代码示例来源:origin: google/guava
private static <T> T newProxy(Class<T> interfaceType, InvocationHandler handler) {
Object object =
Proxy.newProxyInstance(
interfaceType.getClassLoader(), new Class<?>[] {interfaceType}, handler);
return interfaceType.cast(object);
}
代码示例来源:origin: spring-projects/spring-framework
private void initProxyClassLoader() {
if (this.targetFactory instanceof EntityManagerFactoryInfo) {
this.proxyClassLoader = ((EntityManagerFactoryInfo) this.targetFactory).getBeanClassLoader();
}
else {
this.proxyClassLoader = this.targetFactory.getClass().getClassLoader();
}
}
代码示例来源:origin: square/okhttp
@Override public void configureTlsExtensions(
SSLSocket sslSocket, String hostname, List<Protocol> protocols) {
List<String> names = alpnProtocolNames(protocols);
try {
Object alpnProvider = Proxy.newProxyInstance(Platform.class.getClassLoader(),
new Class[] {clientProviderClass, serverProviderClass}, new AlpnProvider(names));
putMethod.invoke(null, sslSocket, alpnProvider);
} catch (InvocationTargetException | IllegalAccessException e) {
throw new AssertionError("failed to set ALPN", e);
}
}
代码示例来源:origin: google/guava
/** Returns the URLs in the class path. */
static URL[] getClassPathUrls() {
return ClassPathUtil.class.getClassLoader() instanceof URLClassLoader
? ((URLClassLoader) ClassPathUtil.class.getClassLoader()).getURLs()
: parseJavaClassPath();
}
}
代码示例来源:origin: google/guava
private static Object newProxyWithEqualsForInterfaces(Class<?>... interfaces) {
return Proxy.newProxyInstance(
AbstractInvocationHandlerTest.class.getClassLoader(),
interfaces,
new DelegatingInvocationHandlerWithEquals("a string"));
}
代码示例来源:origin: google/guava
private static ResourceInfo resourceInfo(Class<?> cls) {
String resource = cls.getName().replace('.', '/') + ".class";
ClassLoader loader = cls.getClassLoader();
return ResourceInfo.of(resource, loader);
}
代码示例来源:origin: google/guava
private URL[] getClassPathUrls() {
ClassLoader classLoader = getClass().getClassLoader();
return classLoader instanceof URLClassLoader
? ((URLClassLoader) classLoader).getURLs()
: parseJavaClassPath().toArray(new URL[0]);
}
代码示例来源:origin: google/guava
static File pickAnyJarFile() throws IOException {
JarFileFinder finder = new JarFileFinder();
try {
finder.scan(JarFileFinder.class.getClassLoader());
throw new IllegalStateException("No jar file found!");
} catch (StopScanningException expected) {
return finder.found;
}
}
代码示例来源:origin: google/guava
@GwtIncompatible // Class.getClassLoader()
private URL[] getClassPathUrls() {
ClassLoader classLoader = getClass().getClassLoader();
return classLoader instanceof URLClassLoader
? ((URLClassLoader) classLoader).getURLs()
: parseJavaClassPath().toArray(new URL[0]);
}
代码示例来源:origin: google/guava
public void testScanFromFile_notJarFile() throws IOException {
ClassLoader classLoader = ClassPathTest.class.getClassLoader();
File notJar = File.createTempFile("not_a_jar", "txt");
ClassPath.DefaultScanner scanner = new ClassPath.DefaultScanner();
try {
scanner.scan(notJar, classLoader);
} finally {
notJar.delete();
}
assertThat(scanner.getResources()).isEmpty();
}
代码示例来源:origin: google/guava
public void testGetPackageName() {
assertEquals("", new ClassInfo("Foo.class", getClass().getClassLoader()).getPackageName());
assertEquals(
"a.b", new ClassInfo("a/b/Foo.class", getClass().getClassLoader()).getPackageName());
}
代码示例来源:origin: google/guava
public void testScanFromFile_fileNotExists() throws IOException {
ClassLoader classLoader = ClassPathTest.class.getClassLoader();
ClassPath.DefaultScanner scanner = new ClassPath.DefaultScanner();
scanner.scan(new File("no/such/file/anywhere"), classLoader);
assertThat(scanner.getResources()).isEmpty();
}
代码示例来源:origin: google/guava
public void testEquals() {
new EqualsTester()
.addEqualityGroup(classInfo(ClassPathTest.class), classInfo(ClassPathTest.class))
.addEqualityGroup(classInfo(Test.class), classInfo(Test.class, getClass().getClassLoader()))
.addEqualityGroup(
new ResourceInfo("a/b/c.txt", getClass().getClassLoader()),
new ResourceInfo("a/b/c.txt", getClass().getClassLoader()))
.addEqualityGroup(new ResourceInfo("x.txt", getClass().getClassLoader()))
.testEquals();
}
代码示例来源:origin: google/guava
public void testScan_classPathCycle() throws IOException {
File jarFile = File.createTempFile("with_circular_class_path", ".jar");
try {
writeSelfReferencingJarFile(jarFile, "test.txt");
ClassPath.DefaultScanner scanner = new ClassPath.DefaultScanner();
scanner.scan(jarFile, ClassPathTest.class.getClassLoader());
assertThat(scanner.getResources()).hasSize(1);
} finally {
jarFile.delete();
}
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void testResolvedMappedHandler() {
DefaultNamespaceHandlerResolver resolver = new DefaultNamespaceHandlerResolver(getClass().getClassLoader());
NamespaceHandler handler = resolver.resolve("http://www.springframework.org/schema/util");
assertNotNull("Handler should not be null.", handler);
assertEquals("Incorrect handler loaded", UtilNamespaceHandler.class, handler.getClass());
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void defaultDomainWithIndex() {
this.manager.setPackagesToScan("org.springframework.orm.jpa.domain");
this.manager.setResourceLoader(new DefaultResourceLoader(
CandidateComponentsTestClassLoader.index(getClass().getClassLoader(),
new ClassPathResource("spring.components", Person.class))));
testDefaultDomain();
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void varargsAgainstProxy_SPR16122() {
SpelExpressionParser parser = new SpelExpressionParser();
Expression expr = parser.parseExpression("process('a', 'b')");
VarargsReceiver receiver = new VarargsReceiver();
VarargsInterface proxy = (VarargsInterface) Proxy.newProxyInstance(
getClass().getClassLoader(), new Class<?>[] {VarargsInterface.class},
(proxy1, method, args) -> method.invoke(receiver, args));
assertEquals("OK", expr.getValue(new StandardEvaluationContext(receiver)));
assertEquals("OK", expr.getValue(new StandardEvaluationContext(proxy)));
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void withLocationAndExplicitNameAndExplicitClassLoader() throws IOException {
PropertySource<?> ps = new ResourcePropertySource("ps1", PROPERTIES_LOCATION, getClass().getClassLoader());
assertEquals("bar", ps.getProperty("foo"));
assertThat(ps.getName(), is("ps1"));
}
代码示例来源:origin: google/guava
public void testNulls() throws IOException {
new NullPointerTester().testAllPublicStaticMethods(ClassPath.class);
new NullPointerTester()
.testAllPublicInstanceMethods(ClassPath.from(getClass().getClassLoader()));
}
代码示例来源:origin: google/guava
public void testGetSimpleName() {
ClassLoader classLoader = getClass().getClassLoader();
assertEquals("Foo", new ClassInfo("Foo.class", classLoader).getSimpleName());
assertEquals("Foo", new ClassInfo("a/b/Foo.class", classLoader).getSimpleName());
assertEquals("Foo", new ClassInfo("a/b/Bar$Foo.class", classLoader).getSimpleName());
assertEquals("", new ClassInfo("a/b/Bar$1.class", classLoader).getSimpleName());
assertEquals("Foo", new ClassInfo("a/b/Bar$Foo.class", classLoader).getSimpleName());
assertEquals("", new ClassInfo("a/b/Bar$1.class", classLoader).getSimpleName());
assertEquals("Local", new ClassInfo("a/b/Bar$1Local.class", classLoader).getSimpleName());
}
内容来源于网络,如有侵权,请联系作者删除!