mardi 24 août 2021

Is reflection is for classloader?

I know that reflection is a technique for calling methods or manipulating bytecode without knowing the concrete type.

Recently, while studying classloaders, I came across an article that reflection is for classloaders to dynamically load classes and do type checking. Is this true?

The article

The original text is not in English, so the translation may be a bit strange.

Java dynamically loads classes. That is, all code is linked to the JVM at runtime. Every class is dynamically linked to the JVM and loaded into memory at the moment that class is referenced. Java's runtime library ([JDK installation directory]/jre/lib/rt.jar) is no exception. This dynamic class loading is done through the class loader system of Java, and the class loader provided by Java is expressed through java.lang.ClassLoader. When the JVM starts, it creates a bootstrap class loader and then reads the first class, Object, into the system.

Loading a class dynamically at runtime means that the JVM has no information about the class. In other words, the JVM does not know information about methods, fields, and inheritance relationships of classes. Therefore, the classloader should be able to obtain the necessary information when loading a class and check whether the class is correct. If you can't do this, the JVM may have mismatched versions of the .class files, and it will be impossible to type-check. The JVM has the ability to analyze classes internally, and from JDK 1.1, developers can analyze these classes through reflection.





Aucun commentaire:

Enregistrer un commentaire