The first phase of the internship was basically completed, and the world of Python and Django was back to Java. When I first came to Baidu, I did not expect that I would still use Java. The new task is also very simple, but I just learned a new Java Script package, which can be used to parse the script syntax.
Javax. Script started with jdk1.6, but now only the Javascript parser implemented by Sun is classified into Jee because it is mainly used to parse JS syntax? However, it is basically enough. The general purpose is to be able to parse general expressions. For example, expressions like x> = 1 (X passed as a parameter) can also use the function Syntax of Js, create a function that can be called at any time in the memory just like a Java function, and convert the objects in JS into Java objects directly.
The Main Types and interfaces in the script package are scriptenginemanager, scriptengine, compiledscript, and bindings.
Scriptenginemanager is a set of factories. You can obtain the factory of a script by name or tag and generate the scriptengine of the script. Currently, only the Javascript factory is available. After getting scriptengine through the factory function, you can use this object to parse the script string and directly call object OBJ = scriptengine. eval (string script). The returned obj is the value of the expression, such as true, false, or Int.
Compiledscript can save the results of parsing a script by scriptengine for multiple calls. After the scriptengine is forcibly converted using the compilable interface, compile (string script) is called to return a compiledscript object. Each time compiledscript is used, compiledscript is called. eval () is applicable to JS functions.
The concept of bindings is a little complicated. I understand that bindings is a container used to store data. It has three levels: global, engine, and local. The first two levels use scriptengine. getbindings () is a unique object, and the local binding is obtained by scriptengine. createbindings () is obtained. It is easy to understand that a new one is generated every time. Global maps to the factory, engine maps to scriptengine, and adds any data or compiled script execution objects to the two, which will exist in each newly generated local binding.
ToCodeIn this example, functionscript can be obtained from stdin or configuration file input to dynamically control the running results of Java code.
Try {scriptengine engine = new scriptenginemanager (). getenginebyname ("JavaScript"); compilable = (compilable) engine; bindings = engine. createbindings (); // bindingstring script = "function add (OP1, OP2) {return OP1 + OP2} Add (A, B)" at the local level )"; // define the function and call compiledscript jsfunction = compilable. compile (SCRIPT); // parse and compile the Script Function bindings. put ("A", 1); bindings. put ("B", 2); // Add the parameter object result = jsfunction through bindings. eval (bindings); system. out. println (result); // call the cached script function object. bindings is passed as the parameter container} catch (scriptexception e ){}
there is also a scriptcontext concept, which is rarely used. It is a tool used to connect scriptengine and bindings. According to JDK, the implementation class of this interface is used to connect the objects in scriptengine and the Host application Program (for example, a range of bindings ).