protected Class<?> loadClass(String name, boolean resolve)
throws ClassNotFoundException
{
synchronized (getClassLoadingLock(name)) {
// First, check if the class has already been loaded
Class<?> c = findLoadedClass(name);
if (c == null) {
long t0 = System.nanoTime();
try {
if (parent != null) {
c = parent.loadClass(name, false);
} else {
c = findBootstrapClassOrNull(name);
}
} catch (ClassNotFoundException e) {
// ClassNotFoundException thrown if class not found
// from the non-null parent class loader
}
if (c == null) {
// If still not found, then invoke findClass in order
// to find the class.
long t1 = System.nanoTime();
c = findClass(name);
// this is the defining class loader; record the stats
sun.misc.PerfCounter.getParentDelegationTime().addTime(t1 - t0);
sun.misc.PerfCounter.getFindClassTime().addElapsedTimeFrom(t1);
sun.misc.PerfCounter.getFindClasses().increment();
}
}
if (resolve) {
Class(c);
}
return c;
}
}
package com.mythsman.test;
public class Hello {
public void say() {
System.out.println("This is from Hello v1");
}
}
package com.mythsman.test;
public class Hello {
public void say() {
System.out.println("This is from Hello v2");
}
}
ClassLoader classLoader1=new ClassLoader() {
@Override
public Class<?> loadClass(String s) throws ClassNotFoundException {
try {
if (s.equals("com.mythsman.test.Hello")) {
byte[] classBytes = Files.readAllBytes(Paths.get("/home/myths/Desktop/test/Hello.class.1"));
return defineClass(s, classBytes, 0, classBytes.length);
}else{
return super.loadClass(s);
}
}catch (IOException e) {
throw new ClassNotFoundException(s);
}
}
};
ClassLoader classLoader2=new ClassLoader() {
@Override
public Class<?> loadClass(String s) throws ClassNotFoundException {
try {
if (s.equals("com.mythsman.test.Hello")) {
byte[] classBytes = Files.readAllBytes(Paths.get("/home/myths/Desktop/test/Hello.class.2"));
return defineClass(s, classBytes, 0, classBytes.length);
}else{
return super.loadClass(s);
}
}catch (IOException e) {
throw new ClassNotFoundException(s);
}
}
};
Object helloV1=classLoader1.loadClass("com.mythsman.test.Hello").newInstance();
Object helloV2=classLoader2.loadClass("com.mythsman.test.Hello").newInstance();
helloV1.getClass().getMethod("say").invoke(helloV1);
helloV2.getClass().getMethod("say").invoke(helloV2);
This is from Hello v1 This is from Hello v2
System.out.println("class:"+helloV1.getClass());
System.out.println("class:"+helloV2.getClass());
System.out.println("hashCode:"+helloV1.getClass().hashCode());
System.out.println("hashCode:"+helloV2.getClass().hashCode());
System.out.println("classLoader:"+helloV1.getClass().getClassLoader());
System.out.println("classLoader:"+helloV2.getClass().getClassLoader());
class:class com.mythsman.test.Hello class:class com.mythsman.test.Hello hashCode:1581781576 hashCode:1725154839 classLoader:com.mythsman.test.Main$1@5e2de80c classLoader:com.mythsman.test.Main$2@266474c2
System.out.println(classLoader1.getParent().getParent().getParent()); System.out.println(classLoader1.getParent().getParent()); System.out.println(classLoader1.getParent()); System.out.println(classLoader1 ); System.out.println(ClassLoader.getSystemClassLoader());
null sun.misc.Launcher$ExtClassLoader@60e53b93 sun.misc.Launcher$AppClassLoader@18b4aac2 com.mythsman.test.Main$1@5e2de80c sun.misc.Launcher$AppClassLoader@18b4aac2
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有