Concept, design and implementation of hot replacement in Java classes (2)

Source: Internet
Author: User

    Write custom classloader

    To fully control the class loading process, our custom class loaders need to be inherited directly from the classloader. First, we will introduce some important methods related to hot replacement in the classloader class.

    ◆ Findloadedclass: each class loader maintains its own loaded class namespace, and there cannot be two classes with the same name. All classes loaded by the class loader, whether direct or indirect, are stored in their own namespace, this method is used to check whether the specified class already exists in the namespace. If yes, it returns a reference to the class; otherwise, null is returned. This directly refers to the load that exists in the loading path of the class loader and is loaded by the loader, indirectly refers, the Class Loader delegates the class loading work to other class loaders to complete the actual loading of the class.

    ◆ Getsystemclassloader: The method added in Java2. This method returns the classloader used by the system. You canIn the custom class loader, some work is transferred to the system class loader for processing.

    ◆ Defineclass: This method is a very important method in classloader. It receives class bytecode represented by byte arrays and converts it to a class instance, when converting a class, This method requires that the parent class of the class and the implemented interface class be loaded first.

    ◆ Loadclass: The Entry Method for loading a class. Call this method to perform explicit loading of the class. Through re-implementation of this method, we can fully control and manage the loading process.

    ◆ Resolveclass: link a specified class. This is a necessary method to ensure that the class is available in some cases. For details, see the "execution" section in the Java language specification to describe this method.

    After learning about the above methods, we will implement a custom class loader to complete the loading process: we specify a set of classes that must be directly loaded by the class loaders. When the class loaders load classes, if the class to be loaded belongs to the set that must be loaded by the class loader, it will directly load the class, otherwise, the class loading task is delegated to the class loader of the system.

    Before giving the sample code, there are two points to explain: 1. To realize the coexistence of different versions of the same class, these versions must be loaded by different class loaders, therefore, the loading of these classes cannot be delegated to the system loader, because they only have one copy. 2. To do this, the default Class Loader delegation rule cannot be used. That is to say, the parent loader of the Class Loader we have customized must be set to null. The implementation code of this custom class loader is as follows:

 
 
  1. Class customcl extends classloader {
  2.  
  3. Private string basedir; // The base Directory of the class file to be directly loaded by the Class Loader
  4. Private hashset dynaclazno; // name of the class to be directly loaded by the Class Loader
  5.  
  6. Public customcl (string basedir, string [] clazno ){
  7. Super (null); // specify that the parent loader is null
  8. This. basedir = basedir;
  9. Dynaclazno = new hashset ();
  10. Loadclassbyme (clazno );
  11. }
  12.  
  13. Private void loadclassbyme (string [] clazno ){
  14. For (INT I = 0; I <clazno. length; I ++ ){
  15. Loaddirectly (clazno [I]);
  16. Dynaclazno. Add (clazno [I]);
  17. }
  18. }
  19.  
  20. Private class loaddirectly (string name ){
  21. Class CLS = NULL;
  22. Stringbuffer sb = new stringbuffer (basedir );
  23. String classname = Name. Replace ('.', file. separatorchar) + ". Class ";
  24. SB. append (file. Separator + classname );
  25. File classf = new file (sb. tostring ());
  26. CLS = instantiateclass (name, new fileinputstream (classf ),
  27. Classf. Length ());
  28. Return CLS;
  29. }
  30.  
  31. Private class instantiateclass (string name, inputstream fin, long Len ){
  32. Byte [] Raw = new byte [(INT) Len];
  33. Fin. Read (raw );
  34. Fin. Close ();
  35. Return defineclass (name, raw, 0, raw. Length );
  36. }
  37. Protected class loadclass (string name, Boolean resolve)
  38. Throws classnotfoundexception {
  39. Class CLS = NULL;
  40. CLS = findloadedclass (name );
  41. If (! This. dynaclazno. Contains (name) & CLS = NULL)
  42. CLS = getsystemclassloader (). loadclass (name );
  43. If (CLS = NULL)
  44. Throw new classnotfoundexception (name );
  45. If (RESOLVE)
  46. Resolveclass (CLS );
  47. Return CLS;
  48. }
  49.  
  50. }

In the implementation of the Class Loader, all classes that must be directly loaded by the loader are loaded when the loader is instantiated. When the class is loaded through loadclass, if the class has not been loaded and does not belong to a column that must be loaded by the class loader, It is delegated to the system loader for loading.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.