bjmashibing / jvm Goto Github PK
View Code? Open in Web Editor NEWJVM Demo Code
JVM Demo Code
BUG说明:
1.这里读取class文件时,判断条件应该是while ((b = fis.read()) != -1),而不是!=0
2.这里没报错是因为根本没用到自定义的findClass,项目中已经存在的类直接由Launcher$AppClassLoader加载出来了
顺便提两个问题:
1.因为不在项目空间的class才会用到自定义加载器,那么没有类的格式,怎么使用它啊?
2.为啥自定义的类加载器,走自己的findClass或者由AppClassLoader加载时,不会执行被加载类的静态代码块中的打印命令呢?
打破双亲委派机制的方法 1, 重写loadClass()有的结论有问题,当然也跟自定义加载器有关,跟楼上的问题的bug2类似:
T011_ClassReloading1 -> 在只重写findClass方法的基础上,多次调用msbClassLoader.loadClass("com.mashibing.jvm.Hello")方法,因为Hello这个类是被APPClassLoader加载过的,所以不管调用多少次msbClassLoader.loadClass方法,返回的Class都是同一个,而如果调用的是一个全新的、从未被任何加载器加载过的class,结论就不一样了,调用两次msbClassLoader.loadClass方法,如果两次中间不把msbClassLoader重新new,则loadClass返回的class是同一个,如果重新new了,那么两次的load结果返回的Class是不同的;
T012_ClassReloading2 -> 在重写loadClass的基础上,第一次调用m.loadClass("com.mashibing.jvm.Hello")会重新覆盖掉APPClassLoader加载的Hello.class,此时的Hello的加载器会变为m,而m 重新new之后在loadClass,将会再次覆盖,这种情况下,无论被加载的类是否被任何的加载器加载过,只要是新new 的m,调用m.loadClass,都会重新加载一次,重新加载的和之前的class不一样。
总结,在只重写findClass基础上,如果加载的从未加载过的class,同一个加载器加载多少次,返回值都是一个,new的加载器加载的是不同的;在重写loadClass的基础上,同一个加载器加载多少次都是新的,新new的加载器,每次new,每次load都会产生不同的class
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.