Java implementation Custom ClassLoader dynamic decryption class file

Source: Internet
Author: User

Objective

To implement ClassLoader dynamic decryption class file, we must first understand the Java class loading mechanism and understand the parental delegation mechanism. Then customize a classloader that inherits from the ClassLoader.

The article refers to the decryption method (Edcipher.decryptclass (name)) in the previous article, please go to: Java Plus decrypt class file

Code implementation
1 /**2 * Dynamic Load class3  */4  Public classEdclassloaderextendsClassLoader {5 6     PrivateString toolfile = "Com.pub.Secretive";7 8     PrivateEdcipher Edcipher =NewEdcipher ();9 Ten      PublicEdclassloader () { One         Super(Thread.CurrentThread (). Getcontextclassloader ()); A     } -  - @Override the     protectedClass<?> Findclass (String name)throwsClassNotFoundException { -Edcipher Edcipher = This. Edcipher = =NULL?NewEdcipher (): This. Edcipher; -         byte[] data =Edcipher.decryptclass (name); -         intLength = data = =NULL? 0: data.length; +         returnDefineClass (toolfile, data, 0, length); -     } +  A @Override at      PublicClass<?> LoadClass (String s)throwsClassNotFoundException { -         if(!s.contains ("Checker")) { -Class Loadedclass =Findloadedclass (s); -             if(Loadedclass = =NULL) { -Loadedclass =getParent (). LoadClass (s); -                 returnLoadedclass; in}Else { -                 returnLoadedclass; to             } +         } -         returnFindclass (s); the     } *  $     /**Panax Notoginseng * Get classes that need to be decrypted -     */ the      PublicString Gettoolfile () { +         returnToolfile; A     } the  +     //after the class file is loaded dynamically, it needs to be reflected to invoke the method -      Public Static voidMain (string[] args)throwsClassNotFoundException { $Edclassloader Edclassloader =NewEdclassloader (); $Object result =NULL; -         Try { -Class MyClass =Edclassloader.loadclass (Edclassloader.gettoolfile ()); the             //Method1 is the method name . -method = Myclass.getdeclaredmethod ("Method1");WuyiObject obj =myclass.newinstance (); the             //result is the method return value -result =Method.invoke (obj); Wu}Catch(Exception e) { - e.printstacktrace (); About         } $ System.out.println (result); -     } -}

Java implementation Custom ClassLoader dynamic decryption class file

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.