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