17

In the documentation I found:

Class objects are constructed automatically by the Java Virtual Machine as classes are loaded and by calls to the defineClass method in the class loader.

I checked the source code, but didn't find the place defineClass to be called e.g. from loadClass method. Could you show me, please, who and when call defineClass method according to this scheme:

scheme

Picture source

Stefan Ferstl
  • 5,135
  • 3
  • 33
  • 41
Rudziankoŭ
  • 10,681
  • 20
  • 92
  • 192
  • 1
    When you're dealing with JVM internals (magic!), a good suspicion is that things are happening in native code (C/C++/asm). – Nayuki Dec 03 '15 at 16:17
  • Sure it could be, but the question is: when does it(Class object appears) happen? The implementation language doesn't matter – Rudziankoŭ Dec 03 '15 at 16:29

3 Answers3

11

The defineClass() method is called during the invocation of ClassLoader#loadClass(). However, this is not directly done within the java.lang.ClassLoader class but in one of its subclasses, e.g. in URLClassLoader#findClass().

The call to ClassLoader#defineClass() ends up in a call to one of the native methods defineClass1() or defineClass2(). The C implementations of these methods can be found in OpenJDK in src/share/native/java/lang/ClassLoader.c.

Stefan Ferstl
  • 5,135
  • 3
  • 33
  • 41
5

java.lang.ClassLoader is such a big class. Using your GrepCode link (which is for java 6-b14 version) you can find at line 267 the public loadClassmethod.

This method, calls a protected loadClassmethod at line 308 and this method try to load a previosly loaded class using:

  • findLoadedClass which in the ends calls Native methods,
  • Calling parent.loadClass,
  • findBootstrapClass0 (a native method also) if there is no parent,
  • And finally findClass if no class is found.

This is important to say, because ClassLoader tries to reuse already loaded clases, keep in mind.

But, where is defineClass invoked? No place from this abstract class, but if you use reference tool from GrepCode and search where it is used defineClass (see here results) you will find a lot of concrete classes which in the end calls definClass.

It is not straightforward, some of these classes, override defineClass while others calls its own loadClass which then calls ... and so on, but finally it calls defineClass.

Don't forget that defineClass of ClassLoader ends in one of three native methods that are responsible of JVM magic: defineClass0, defineClass1 and/or defineClass2

Edit

Native function defineClass0 calls Java_java_lang_ClassLoader_defineClass0 from ClassLoader.cand the same for 1 and 2 functions.

This functions creates the required class using JVM_DefineClassWithSource defined in jvm.h and implemented in openjdk\hotspot\src\share\vm\prims\jvm.cpp.

This last file defines jvm_define_class_common function which in the end is the function that creates the required class. Finally, this function calls JNIHandles::make_local to allocate the class. You can see the code of this last function in openjdk\hotspot\src\share\vm\runtime\jniHandles.cpp

Hope it answer your question.

malaguna
  • 4,183
  • 1
  • 17
  • 33
1
class NetworkClassLoader extends ClassLoader {
         String host;
         int port;

         public Class findClass(String name) {
             byte[] b = loadClassData(name);
             return defineClass(name, b, 0, b.length);
         }

         private byte[] loadClassData(String name) {
             // load the class data from the connection
              . . .
         }
     }
Hiren
  • 51
  • 5
  • This is the extract fomr Oracle dosumentation.defineClass is a method of ClassLoader which converts an array of bytes into an instance of class Class. I hope it helps – Hiren Dec 12 '15 at 05:04