Dynamic generation of Java, dynamic compilation, dynamic loading

Source: Internet
Author: User

I have seen a "rule engine", which is to write Java code directly in the application Web interface, and then save it, the rules will take effect, I have been very strange, how is this implemented? In fact, this is like JSP, the middleware is dynamically compiled into Java files, have been dynamically compiled into class, but also dynamically loaded into the ClassLoader. So, in essence, the pure Java Rule Engine is 100% achievable.

1. Dynamic generation of Java source code. This process is too simple to skip directly. 2, dynamic compilation. I think our own rule engine also has a dynamic compilation, that is, when the BOM model is generated. However, the process execution Javac is called. But that's not a good way to be honest. Because Javac, the command parameters are written in relation to the operating system, that is, Windows and Linux are written in a few different ways. It was later discovered that the JDK provided a dynamically compiled class. Javacompiler Javac;javac = Toolprovider.getsystemjavacompiler (); int compilationresult = Javac.run (Null,null,null, "-G ","-verbose ", javafile), so that it can be compiled dynamically. The first two parameters are input parameters, output parameters, I think there is no use, the third parameter is compiled output information, the default output to System.out.err inside. Starting with the fourth parameter, it is the Javac parameter, which can be separated by an array or directly by a comma. 3, dynamic loading. Dynamic loading is actually called ClassLoader. Of course, the reflection mechanism calls an inner part of the method to make it an externally callable method. File File = new file ("/users/yangming/work/devworkspace/ssac/gx_hx/test/"); URLClassLoader ClassLoader = (urlclassloader) classloader.getsystemclassloader (); Method add = URLClassLoader.class.getDeclaredMethod ("Addurl", New Class[]{url.class}); add.setaccessible (true); Add.invoke (ClassLoader, New Object[]{file.touri (). Tourl ()}); Class C = Classloader.loadclass ("Test"); Object o = C.newinstance (); Method m = C.getdeclaredmethod ("getString"); M.invoke (o, null); This completes the dynamic loading of the class.

Dynamic generation of Java, dynamic compilation, dynamic loading

Related Article

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.