Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obtain Java class loading references hierarchy

While debugging Spring-driven AspectJ LTW (using -verbose:class), I have noticed that one of the classes to be advised is being loaded by the class loader before Spring establishes the connection to the AspectJ weaver.

Considering that Java postpones the loading of a class until it's not possible to delay it more, there must be a reason why that specific class is being loaded so soon.

Is it possible to obtain the "references stack" that provokes the loading of a class to the JVM in a specific moment (so I can try to postpone its usage)? If so, how can I do it?

like image 480
andresp Avatar asked Jul 11 '13 09:07

andresp


People also ask

What is the order of class loading in Java?

Note: The ClassLoader Delegation Hierarchy Model always functions in the order Application ClassLoader->Extension ClassLoader->Bootstrap ClassLoader. The Bootstrap ClassLoader is always given the higher priority, next is Extension ClassLoader and then Application ClassLoader.

Is Java class loaders are hierarchical in nature?

Java Classloaders are hierarchical in nature. Whenever JVM requests to load a class, each classloader first delegates the request to its parent. If the parent fails to find the class to be loaded, then classloader itself tries to load the class using the java.

What is the role of ClassLoader in Java?

Class loaders are responsible for loading Java classes dynamically to the JVM (Java Virtual Machine) during runtime. They're also part of the JRE (Java Runtime Environment). Therefore, the JVM doesn't need to know about the underlying files or file systems in order to run Java programs thanks to class loaders.

What is ClassLoader in Java with example?

Java ClassLoader is used to load the classes at run time. In other words, JVM performs the linking process at runtime. Classes are loaded into the JVM according to need. If a loaded class depends on another class, that class is loaded as well. When we request to load a class, it delegates the class to its parent.


1 Answers

As you already noted, Java (or actually the VM, that runs your code) loads and resolves classes at the time, they are needed. This normally also leads to a knock-on effect for several classes. There is obviously a high chance, that the classes that do not contain the woven code, are loaded prior to the Spring classes.

However, the Java HotSpot VM (the typical VM, when Oracle's Java is installed) can be configured in many ways when being started. One of those options is "-XX:+TraceClassLoading" (note the plus sign; the above mentioned link unfortunately documents a minus sign for this option). There is also another option, that traces the loaded classes in reference order.

With that, you should be able to narrow the problem down. If not, some example code might help. Although I fear, that that would be a bit too large.

like image 156
Seelenvirtuose Avatar answered Sep 27 '22 01:09

Seelenvirtuose