SoFunction
Updated on 2025-03-08

Methods to run string expressions in Java

In daily development, you will occasionally encounter the situation of running string expressions. Usually, such requirements will further analyze the requirements, then further "specialize", and finally write them directly into hard code. If you do this, it will not be easy to expand. There is also another way to use Java built-in JavaScript engine and other Java to run string expressions, but the built-in engine also has disadvantages. For example, the efficiency of frequently running fragmented strings is very low, and the data interaction with Java is more troublesome, so the idea of ​​writing a "string expression calculation engine"...

The writing process is actually not as troublesome as I imagined. The initial version was written at the end of May this year, but the structure is relatively chaotic. When writing, it is basically written and repaired. Finally, if...else... there are too many such conditions and nestings that I cannot fully understand. Fortunately, the logic is basically perfect, and there is no unexpected situation in the operation (maybe it happened, but I didn't find it), and I used it myself, so I didn't care too much.

Two weeks ago, I took the time to reorganize it, sorted out the structure, expanded some functions, and redefined the "semantic boundaries" of various symbols, so as to ensure that the operators are consistent with the operators themselves, and the logical structure is clearer, so that there will be no unexpected situations.

RunnerUtil has a large reference to JavaScript syntax in terms of syntax, such as using curly braces to represent a key-value pair "object" (which will actually be parsed into HashMap). The key names do not need to be wrapped in single or double quotes. Single and double quotes all represent ordinary strings, and values ​​are taken through dots (.) and square bracket chains, etc. This is also more convenient for students engaged in JavaWeb development. Most of the functions have been implemented now, and the implemented functions have also been tested to ensure that they can operate "in line with expectations". If you have any ideas and suggestions, I hope you will mention them more.

Project address:/xua74453185…

Introduction to basic usage

String expressions can be run directly through a static class called RunnerUtil, which can be run directly to obtain the expression results, or it can be parsed and run when needed. RunnerUtil has the following main methods:

(/* expression */); Run the expression directly and get the result;
("1 + 1"); // 2
(" 'Hello' + ' ' + 'World!' "); // "Hello World!"
(/* expression */, / * data */); Run expressions containing variables,The one behind data is the variable to point to“value”;
(/* expression */); Run directly“Another”expression,And get the results,like:
("Hello {{  'World!'  }}"); // "Hello World!"

It can be seen that #parseRun is an expression that runs a "interpolation syntax", and the wrapped content is run separately as an expression;

A string can contain multiple interpolation syntax expressions, but cannot be nested and crossed, and expressions containing variables can also be run.

Runner runner = (/* expression */);

Parses a string expression to get a "string expression runner" - Runner, and then call its run(/ * data */) method to run and get the result.

Detailed introduction to syntax and operations

As something with certain "language characteristics", it defines some of its own syntax, data types, operation types, etc., but most of them are compatible with Java and JavaScript, and the same symbols have the same or similar language meanings.

Data type:

1. null: This is a keyword, but because it conforms to the definition rules of variables, it should be noted that true and false are also defined as keywords.

2. boolean: true and false

(" null  "); // null
("  true "); // true
("false"); // false
// Excess spaces in expressions are automatically ignored

Numbers: The numbers here uniformly use int and double data in Java. These two types are the only ones that directly participate in the calculation. The difference is whether there are decimal points.

(" 12 "); // 12
(" 12.5 "); // 12.5
// means that the number must be continuous and there must be no spaces in the middle.// Otherwise, an exception will be thrown, such as(" 12. 5"); // Exception(" 1 2 "); // abnormal

The characters representing numbers should be continuous, such as: 25, 36.9, etc.; if it is discontinuous, an exception will be thrown, such as: 2 5, 36.9, etc.;

String: The strings in Java are wrapped in double quotes. Here, the single quotes representing characters will be "requisitioned". The double quotes and single quotes all represent the direct value of ordinary strings. This is also for the convenience of writing (similar to JavaScript), and there will be no char type data...

(" 'abcdef' "); // "abcdef"
(" \"abcdef\" "); // "abcdef"
(" 'abc  def' "); // "abc  def"

List: Actually it is an ArrayList, corresponding to the array in JavaScript. Java arrays also correspond to JavaScript arrays.

(" { } "); 
// Always return an empty ArrayList(" {1,2,,4, } "); 
// Always return an ArrayList containing: 1, 2, null, 4.// You can see that after the last comma, it will be automatically ignored.// If there is no other non-blank symbol between the comma and the comma, one will be inserted. null value

Map: Actually, it is a HashMap, corresponding to an object in JavaScript. Also corresponding to JavaScript objects is ordinary POJO.
Map corresponds to objects in JavaScript, but here the Map keys can be these data types:

null, true / false, number (int / double), string can no longer be other Java objects

(" {:} "); // Always return an empty HashMap,// Pay attention to the similarities and differences between the empty List, both are represented by curly braces.// But there needs to be a colon in the empty map, otherwise it is a List
(" {key: 'value'}");
// Always return a HashMap containing a key-value pair// It can be seen that if the key name of the object is a string, you can be wrapped without quotation marks.// But the value must be wrapped(" {true: 'value'}"); // The key is true/*
  * The true here is not a string, but a boolean.
  * Similarly, null, false, and numbers not wrapped in quotes are data of the corresponding type, not strings
  * Other keys that conform to the naming rules of variables are ordinary strings, and they are also wrapped in single or double quotes.
  */
(" {'true': 'value', 25: false, 'name': \"Zhang San\"}");

Types supported by operations:

Common four mixed operations:+、-、*、/、%、()
(" 1 + 1 "); // 2
(" 1 + (3 * 4)) "); // 13
(" 'Hello ' + \"World!\" "); // "Hello World!"
(" true + false "); // "truefalse"
/*
  * true+false is not allowed in Java
  * But if it is the "+" operation, it is used as ordinary strings;
  * It is equivalent to calling the toString method
  */

Bit operation:&、|、^、<<、>>
(" 1 ^ 1 "); 
(" 1 & 1 "); 
(" 1 | 1 "); 
(" 1 << 1 "); 
(" 1 >> 1 ");

Comparative operations:>、>=、==、<=、<
(" 1 + 1 == 2 "); // true
(" 1 + 1 < 2 "); // false

Logical operations:&&、||、!
("1+1==2 && 5 > 4"); // true

variable:Naming rules and Java variable命名规则相同,at the same time null、true、false 不能作为variable
表达式中包含variable就代表这个表达式在运行得到结果时需要从外部获取数据,If the data cannot be read correctly from the data source,An exception will be thrown when running;

(" 'Hello, ' + name "); // throw an exception
Map data = new HashMap();
("name", "Li Lei!");

(" 'Hello, ' + name ", data); // "Hello, Li Lei!"

Chain value: Chain syntax is very similar to JavaScript

HashMap data = new HashMap(); 

ArrayList list = new ArrayList(); 
(true); 
(false); 
(25); 
('Old Wang next door'); 

HashMap map = new HashMap(); 
("name", "Little Four"); 
("index", 2); 
(true, "true is Boolean type as key"); 

("list", list); 
("map", map); 

("", data); // "Little Four"
("map['name']", data); 
// "Xiao Si" (you can also get the value in this way)
("list[ 2 ]", data);
// 25 (The index value needs to be wrapped in square brackets)
("list[3]", data);
// "Lao Wang next door" (the index value needs to be wrapped in square brackets)
("list[]", data); // 25
// (This is the usage of advanced points, square brackets contain another expression// The return value is an index, and then returns the value pointed to by the index)
("[true]", data); // "true is Boolean type as key"// If you do not include square brackets, true is a direct value, returning true// Then the question is:// What if the incoming data is not a Map or POJO, but a List or an array?(" [1] ", list); // false
// Ah... Tang Zong and Song Zu, a little coquettish!
// This chain syntax is JavaScript Very similar

Running method: Currently, only methods without parameters and one parameter can be run. The variable-length parameter method is incomplete and be cautious.
The data here continues to use the previous data, and the specific data will not be written.

("()", data); // 3
("('name')", data); // "Little Four"("('name').length()", data); // 2
("()", data); // 2
(" [3].length() ", list); // 4
// Tang Zong Song Zu,Looks charming again!

Running static methods: @; Running static methods requires the "@" symbol as a mark. Multi-parameter method calls are not currently supported.
When you open the source code, you will find that this is an entire independent tool library. Many methods are similar to the commons-lang package (I personally think that it is not a duplicate wheel, and there are many different and worse).. Running a static method can also run all the tool methods in this tool library. Running a RunnerUtil has not been stripped out for the time being, and custom static method calls are not supported. However, the functions provided by this tool library are provided by

("@() ");
// 15... (one milliseconds)("@(25) "); // "25"

In summary, it is the string expression operation supported by this tool library. The operations listed above can be nested, connected, but cannot be cross-operated. What we need to do next is to add a multi-parameter method call. I hope it will be helpful to your daily development. I also hope you will give some advice. If a bug occurs, it will definitely be modified as soon as possible. Thank you everyone! !

Summarize

The above is the method of running string expressions in Java introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!