• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

[class loaders] What is "data that constitutes a definition for the class" ?

 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The definition of a class loader is:

"A class loader is an object that is responsible for loading classes. The class ClassLoader is an abstract class. Given the binary name of a class, a class loader should attempt to locate or generate data that constitutes a definition for the class."


My question is , what exactly is "data that constitutes a definition for the class". Is the binary .class file the data that defines a class , or some other byte[] generated from that .class file (or on whatever else).

Please reply if you know what exactly a class loader does when he actually gets to load a class , on these lines of code:


byte[] classData = getClassImplFromDataBase(className);
Class result = defineClass(classData, 0, classData.length);

In this tutorial that I am reading classData is a byte array , read from a file with .impl extension. Could this mean that the byte data that represents the definition of a class , is not the .class file generated by the compiler at compile time ?

 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's hard to say what exactly is going on since we don't know what "getClassImplFromDataBase" does (is it taken from here?).

A class must be a byte[] by the time ClassLoader gets hold of it, since that is the only way it will accept classes. What happens before that is entirely up to the classloader in use. The most common way would indeed be to read the contents of a file, either a single class in the shape of a .class file, or a collection of classes in a .jar file. The JRE comes with classloader that know how to do this, either from a file system, or via HTTP (for applets). But you can write classloaders that read the data out of a DB -which is apparently what that method does, going by its name-, or the class can be created in memory by libraries such as javassist.

What exactly must be in the byte[] when it gets to ClassLoader is defined in the JVM spec. Its file extension really doesn't matter, as long as the classloader knows how to handle it.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic