In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you how Java uses JEXL to achieve dynamic expression compilation, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Background
When working on a project, there is a sudden need for:
The system needs to obtain data from multiple data sources, process them, and finally output multiple fields. The calculation rule of a field is generally a simple value plus a little conditional judgment at most.
And need to change dynamically! For example, the value of a field a, if a > 10 output 10 dint a = 10, then output a, you need to change the code, test and release it before you can use it in the production environment.
One or two such fields is fine, and if the fields on which the whole system depends have such properties, then we need to find a way to implement dynamic loading logic.
The following JEXL can solve this problem.
Introduction to JEXL (Java Expression Language)
JEXL-Apache Commons JEXL Overview
Here are some examples to introduce the use of JEXL
Example
Maven dependencies:
Org.apache.commons commons-jexl 2.0
Regular expression matching
First, write a public method:
Public class Util {public static boolean regMatch (String regEx, String str) {Pattern pattern = Pattern.compile (regEx); return pattern.matcher (str). Matches ();}
Here are the methods called using JEXL
Public void RL () {JexlContext jc = new MapContext (); String str = "1234 567890"; jc.set ("Util", new Util ()); jc.set ("str", str); jc.set ("ans", ""); String expression = "ans = Util.regMatch (" [u4e00-u9fa5] {10,} ", str)"; Expression e = new JexlEngine (). Create_Expression (expression) E.evaluate (jc); System.out.println (jc.get ("ans"));}
The expression variable in the code is an expression that can be compiled dynamically. Note here that all variables that appear in the expression need to be set into JexlContext beforehand, otherwise an error will be reported. There are many forms of errors:
If ① does not have set "Util", an exception will be thrown while the program is running.
Org.apache.commons.jexl2.JexlException: TmpTest.RL@40! [13jue 40]: 'ans = QeUtil.regMatch (' [one-month] {10,}', str); 'attempting to call method on null
If ② does not have set "str", the program will not throw an exception and output null. If you have null processing in your regMatch method, the null result will be output. If there is no null processing, the output on the console is as follows:
Warning: TmpTest.RL@39! [36 undefined variable str 39]: 'ans = QeUtil.regMatch (' [one-horse] {10,}', str); 'undefined variable str
February 21, 2017 4:00:41 afternoon org.apache.commons.jexl2.JexlEngine invocationFailed
Warning: TmpTest.RL@39! [13jue 40]: 'ans = QeUtil.regMatch (' [one-month] {10,}', str); 'method invocation error
Java.lang.NullPointerException
If ③ does not have set "ans", the program will run normally and output the correct value
To be on the safe side, it is recommended that all variables that appear in the expression need to be set into the JexlContext in advance
Cycle
JEXL supports two kinds of loops:
For (item: list) {x = x + item;}
And
While (x lt 10) {x = x + 2;}
Here is an example of using while:
Public void loop () {JexlContext jc = new MapContext (); jc.set ("a", 1); jc.set ("b", "0"); jc.set ("ans", new StringBuffer ()); Expression e = new JexlEngine (). Create_Expression ("while (a < 10) {a = a + 1bomans.append (b);}"); e.evaluate (jc); System.out.println (jc.get ("ans"));}
Getset method call
JEXL supports incoming objects and calls the object's methods
The following example of a simple getset method:
Public void getSet () {TmpTest tmpTest = new TmpTest (); tmpTest.setA (1); JexlContext jc = new MapContext (); jc.set ("tmpTest", tmpTest); jc.set ("ans", ""); Expression e = new JexlEngine (). Create_Expression ("ans = tmpTest.getA ()"); e.evaluate (jc); System.out.println (jc.get ("ans")) E = new JexlEngine () .create_Expression ("ans = tmpTest.setA (2)"); e.evaluate (jc); TmpTest tmpTest1 = (TmpTest) jc.get ("tmpTest"); System.out.println (tmpTest1.getA ());}
The above use case will output 1 and then 2 on the console.
These are all the contents of the article "how Java uses JEXL to compile dynamic expressions". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.