Duplicate :
|
|
Duplicate :
|
|
Relates :
|
|
Relates :
|
|
Relates :
|
|
Relates :
|
VirtualMachine.allClasses() does not return a loaded class when the class is not initialized. This prevents reloading of the uninitialized class in our application. Consider the test case: public class Main { public static void foo(Foo f) { } public static void main(String[] args) throws ClassNotFoundException { //load class, but don't initialize it Class.forName("Foo", false, Main.class.getClassLoader()); //alternative: call Main.class.getMethods(); new Foo().bar(); // breakpoint here } } class Foo { static { System.out.println("Foo initialized"); } void bar() { System.out.println("yy"); } } When debugger stops at breakpoint, where Foo is loaded but not initialized, VirtualMachine.allClasses() does not contain a class Foo as it should, according to https://docs.oracle.com/javase/8/docs/jdk/api/jpda/jdi/com/sun/jdi/VirtualMachine.html#allClasses() jdk1.8.0_131\bin\jdb" Main Initializing jdb ... > stop at Main:9 Deferring breakpoint Main:9. It will be set after the class is loaded. > run run Main Set uncaught java.lang.Throwable Set deferred uncaught java.lang.Throwable > VM Started: Set deferred breakpoint Main:9 Breakpoint hit: "thread=main", Main.main(), line=9 bci=12 9 new Foo().bar(); // breakpoint here main[1] class Foo "Foo" is not a valid id or class name. main[1] It's not until we initialize Foo that it is then returned by VirtualMachine.allClasses(): main[1] step > Step completed: "thread=main", Foo.<clinit>(), line=16 bci=0 16 System.out.println("Foo initialized"); main[1] class Foo Class: Foo extends: java.lang.Object
|