提交 76cea265 编写于 作者: T tianqiao

update version 3.2.1

上级 6ca414af
......@@ -103,5 +103,17 @@ public Object execute(InstructionSet[] instructionSets,IExpressContext<String,Ob
## 18、3.1.7版本[2017-11-17]
(1)bugfix 在自定义操作符的情况下,调用 runner.getOutVarNames Api 可能引发的空指针问题
## 18、3.1.8版本[2018-1-30]
(1)增加扩展功能:ExpressRunner#setIgnoreConstChar(Boolean),设置可以忽略单字符操作,即 'a'自动变成"a"。
\ No newline at end of file
## 19、发布到github,修改pom文件标识
```xml
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>QLExpress</artifactId>
<packaging>jar</packaging>
<version>3.2.0</version>
</dependency>
```
## 3.2.1版本[2018-2-23]
(1)增加扩展功能:ExpressRunner#setIgnoreConstChar(Boolean),设置可以忽略单字符操作,即 'a'自动变成"a"。
(2)增加接口来支持绑定自定义classloader的class的method:ExpressRunner#addFunctionOfClassMethod(String name, Class<?> aClass,...)。
\ No newline at end of file
......@@ -6,7 +6,7 @@
<groupId>com.alibaba</groupId>
<artifactId>QLExpress</artifactId>
<packaging>jar</packaging>
<version>3.2.0</version>
<version>3.2.1</version>
<name>QLExpress</name>
<description>QLExpress is a powerful, lightweight, dynamic language for the Java platform aimed at improving developers’ productivity in different business scenes.</description>
<url>https://github.com/alibaba/QLExpress</url>
......
......@@ -283,6 +283,24 @@ public class ExpressRunner {
aClassName, aFunctionName, aParameterClassTypes,null,null, errorInfo));
}
/**
* 添加一个类的函数定义,例如:Math.abs(double) 映射为表达式中的 "取绝对值(-5.0)"
* @param name 函数名称
* @param aClass 类
* @param aFunctionName 类中的方法名称
* @param aParameterClassTypes 方法的参数类型名称
* @param errorInfo 如果函数执行的结果是false,需要输出的错误信息
* @throws Exception
*/
public void addFunctionOfClassMethod(String name, Class<?> aClass,
String aFunctionName, Class<?>[] aParameterClassTypes,
String errorInfo) throws Exception {
this.addFunction(name, new OperatorSelfDefineClassFunction(name,
aClass, aFunctionName, aParameterClassTypes,null,null, errorInfo));
}
/**
* 添加一个类的函数定义,例如:Math.abs(double) 映射为表达式中的 "取绝对值(-5.0)"
* @param name 函数名称
......@@ -735,4 +753,19 @@ public class ExpressRunner {
public void setShortCircuit(boolean isShortCircuit) {
this.isShortCircuit = isShortCircuit;
}
/**
* 是否忽略charset类型的数据,而识别为string,比如'a' -> "a"
* 默认为不忽略,正常识别为String
*/
public boolean isIgnoreConstChar() {
return this.parse.isIgnoreConstChar();
}
public void setIgnoreConstChar(boolean ignoreConstChar) {
this.parse.setIgnoreConstChar(ignoreConstChar);
}
public void checkySyntax(String text) throws Exception {
Map<String,String> selfDefineClass = new HashMap<String,String> ();
this.parse.parse(this.rootExpressPackage,text, true,selfDefineClass);
}
}
......@@ -20,6 +20,27 @@ public class OperatorSelfDefineClassFunction extends OperatorBase implements Can
Method method;
boolean isReturnVoid;
boolean maybeDynamicParams;
public OperatorSelfDefineClassFunction(String aOperName,Class<?> aOperClass, String aFunctionName,
Class<?>[] aParameterClassTypes,String[] aParameterDesc,String[] aParameterAnnotation,String aErrorInfo) throws Exception {
if (errorInfo != null && errorInfo.trim().length() == 0) {
errorInfo = null;
}
this.name = aOperName;
this.errorInfo = aErrorInfo;
this.functionName = aFunctionName;
this.parameterClasses = aParameterClassTypes;
this.parameterTypes = new String[aParameterClassTypes.length];
this.operDataDesc = aParameterDesc;
this.operDataAnnotation = aParameterAnnotation;
for(int i=0;i<this.parameterClasses.length;i++){
this.parameterTypes[i] = this.parameterClasses[i].getName();
}
operClass = aOperClass;
method = operClass.getMethod(functionName,parameterClasses);
this.isReturnVoid = method.getReturnType().equals(void.class);
this.maybeDynamicParams = DynamicParamsUtil.maybeDynamicParams(parameterClasses);
}
public OperatorSelfDefineClassFunction(String aOperName,String aClassName, String aFunctionName,
Class<?>[] aParameterClassTypes,String[] aParameterDesc,String[] aParameterAnnotation,String aErrorInfo) throws Exception {
......
package com.ql.util.express.match;
public class NodeTypeManagerTestImpl implements INodeTypeManager {
public INodeType findNodeType(String name) {
return new TestNodeTypeImpl(name);
}
}
class TestNodeTypeImpl implements INodeType{
String name;
public TestNodeTypeImpl(String aName){
this.name = aName;
}
public String getName() {
return this.name;
}
public INodeTypeManager getManager() {
throw new RuntimeException("没有实现的方法");
}
@Override
public QLPatternNode getPatternNode() {
throw new RuntimeException("没有实现的方法");
}
}
......@@ -19,11 +19,26 @@ public class ExpressParse {
private static final Log log = LogFactory.getLog(ExpressParse.class);
NodeTypeManager nodeTypeManager;
IExpressResourceLoader expressResourceLoader;
/**
* 是否忽略charset类型的数据,而识别为string,比如'a' -> "a"
* 在计算比如 '1'+'2'=='12'
*/
private boolean ignoreConstChar = false;
/**
* 是否需要高精度计算
*/
private boolean isPrecise = false;
public ExpressParse(NodeTypeManager aNodeTypeManager,IExpressResourceLoader aLoader,boolean aIsPrecise){
public boolean isIgnoreConstChar() {
return ignoreConstChar;
}
public void setIgnoreConstChar(boolean ignoreConstChar) {
this.ignoreConstChar = ignoreConstChar;
}
public ExpressParse(NodeTypeManager aNodeTypeManager, IExpressResourceLoader aLoader, boolean aIsPrecise){
this.nodeTypeManager = aNodeTypeManager;
this.expressResourceLoader = aLoader;
this.isPrecise = aIsPrecise;
......@@ -174,7 +189,7 @@ public class ExpressParse {
tempWord = tempWord.substring(1,tempWord.length() -1);
treeNodeType = nodeTypeManager.findNodeType("CONST");
if(tempWord.length() == 1){ //转换为字符串
if(tempWord.length() == 1 && !ignoreConstChar){ //转换为字符串
tempType =nodeTypeManager.findNodeType("CONST_CHAR");
objectValue = tempWord.charAt(0);
}else{
......
package com.ql.util.express.parse;
public class KeyWordDefine4SQL {
public String[] splitWord={
"+", "-","*", "/",//四则运算:
".",",",":",";","(", ")","[", "]","{","}","?",//分隔符号
"!","<", ">", "<=", ">=", "<>","="
};
public String[] keyWords = new String[] {
"select","from","where","and","or","like","if","then","else","as"
};
public String[] nodeTypeDefines = new String[] {
"in:TYPE=KEYWORD",
"EOF:TYPE=WORDDEF",
"FUNCTION_NAME:TYPE=WORDDEF",
"FUNCTION_DEFINE:TYPE=WORDDEF",
"ID:TYPE=WORDDEF",
"LEFT_BRACKET:TYPE=WORDDEF,DEFINE=(",
"RIGHT_BRACKET:TYPE=WORDDEF,DEFINE=)",
"CONST_BYTE:TYPE=WORDDEF",
"CONST_SHORT:TYPE=WORDDEF",
"CONST_INTEGER:TYPE=WORDDEF",
"CONST_LONG:TYPE=WORDDEF",
"CONST_FLOAT:TYPE=WORDDEF",
"CONST_DOUBLE:TYPE=WORDDEF",
"CONST_NUMBER:TYPE=WORDDEF,DEFINE=CONST_BYTE|CONST_SHORT|CONST_INTEGER|CONST_LONG|CONST_FLOAT|CONST_DOUBLE",
"CONST_CHAR:TYPE=WORDDEF",
"CONST_STRING:TYPE=WORDDEF",
"CONST_BOOLEAN:TYPE=WORDDEF",
"CONST_CLASS:TYPE=WORDDEF",
"CONST:TYPE=GROUP,DEFINE=CONST_NUMBER|CONST_CHAR|CONST_STRING|CONST_BOOLEAN|CONST_CLASS",
// "():TYPE=BLOCK,STARTTAG=(,ENDTAG=)",
// "[]:TYPE=BLOCK,STARTTAG=[,ENDTAG=]",
// "{}:TYPE=BLOCK,STARTTAG={,ENDTAG=}",
// "EXPRESS_CHILD:TYPE=GROUP,DEFINE=()|[]",
"OP_LEVEL1:TYPE=OPERATOR,DEFINE=!",
"OP_LEVEL2:TYPE=OPERATOR,DEFINE=*|/",
"OP_LEVEL3:TYPE=OPERATOR,DEFINE=+|-",
"OP_LEVEL4:TYPE=OPERATOR,DEFINE=in|like",
"OP_LEVEL5:TYPE=OPERATOR,DEFINE=>|>=|<|<=|=|<>",
"OP_LEVEL6:TYPE=OPERATOR,DEFINE=and",
"OP_LEVEL7:TYPE=OPERATOR,DEFINE=or",
"TAB_COL_NAME:TYPE=STATEMENT,DEFINE=ID$(.$ID)*#TAB_COL_NAME",
"CHILD_EXPRESS:TYPE=EXPRESS,DEFINE=LEFT_BRACKET~$EXPRESS$RIGHT_BRACKET~",
"OPDATA:TYPE=EXPRESS,DEFINE=CHILD_EXPRESS|CONST|TAB_COL_NAME",
"TAB_COL_NAME_ALIAS:TYPE=STATEMENT,DEFINE=TAB_COL_NAME$(as^$TAB_COL_NAME){0:1}",
"TAB_COL_LIST:TYPE=STATEMENT,DEFINE=TAB_COL_NAME_ALIAS$(,~$TAB_COL_NAME_ALIAS)*",
"EXPRESS_OP_L1:TYPE=EXPRESS,DEFINE=OP_LEVEL1^*$OPDATA",
"EXPRESS_OP_L2:TYPE=EXPRESS,DEFINE=EXPRESS_OP_L1$(OP_LEVEL2^$EXPRESS_OP_L1)^*",
"EXPRESS_OP_L3:TYPE=EXPRESS,DEFINE=EXPRESS_OP_L2$(OP_LEVEL3^$EXPRESS_OP_L2)^*",
"EXPRESS_OP_L4:TYPE=EXPRESS,DEFINE=EXPRESS_OP_L3$(OP_LEVEL4^$EXPRESS_OP_L3)^*",
"EXPRESS_OP_L5:TYPE=EXPRESS,DEFINE=EXPRESS_OP_L4$(OP_LEVEL5^$EXPRESS_OP_L4)^*",
"EXPRESS_OP_L6:TYPE=EXPRESS,DEFINE=EXPRESS_OP_L5$(OP_LEVEL6^$EXPRESS_OP_L5)^*",
"EXPRESS_OP_L7:TYPE=EXPRESS,DEFINE=EXPRESS_OP_L6$(OP_LEVEL7^$EXPRESS_OP_L6)^*",
"EXPRESS:TYPE=STATEMENT,DEFINE=EXPRESS_OP_L7",
"OP_LIST:TYPE=GROUP,DEFINE=OP_LEVEL1|OP_LEVEL2|OP_LEVEL3|OP_LEVEL4|OP_LEVEL5|OP_LEVEL6|OP_LEVEL7|LEFT_BRACKET|RIGHT_BRACKET|[|]",
"PARAMETER_LIST:TYPE=STATEMENT,DEFINE=EXPRESS$(,~$EXPRESS)*",
"SELECT:TYPE=STATEMENT,DEFINE=(select^$TAB_COL_LIST)$(from^$PARAMETER_LIST)$(where^$EXPRESS){0:1}#SELECT",
"STATEMENT:TYPE=STATEMENT",
"PROGRAM:TYPE=STATEMENT,DEFINE=SELECT",
};
}
......@@ -25,13 +25,6 @@ public class NodeTypeManager implements INodeTypeManager {
public NodeTypeManager() {
this(new KeyWordDefine4Java());
}
public NodeTypeManager(KeyWordDefine4SQL keyWorkdDefine){
this.splitWord = keyWorkdDefine.splitWord;
this.keyWords = keyWorkdDefine.keyWords;
this.nodeTypeDefines = keyWorkdDefine.nodeTypeDefines;
this.initial();
}
public NodeTypeManager(KeyWordDefine4Java keyWorkdDefine){
this.splitWord = keyWorkdDefine.splitWord;
com.ql.util.express.parse.WordSplit.sortSplitWord(this.splitWord);
......
package com.ql.util.express.bugfix;
import com.ql.util.express.DefaultContext;
import com.ql.util.express.ExpressRunner;
import com.ql.util.express.IExpressContext;
import org.junit.Test;
/**
* Created by tianqiao on 18/1/29.
*/
public class IgnoreConstCharTest {
@Test
public void test() throws Exception{
ExpressRunner runner = new ExpressRunner();
runner.setIgnoreConstChar(true);
String exp = "'1'+'2'==\"12\"";
IExpressContext<String, Object> context = new DefaultContext<String, Object>();
Object result = runner.execute(exp,context,null,false,true);
assert ((Boolean) result);
}
}
package com.ql.util.express.bugfix;
import com.ql.util.express.DefaultContext;
import com.ql.util.express.ExpressRunner;
import com.ql.util.express.IExpressContext;
import com.ql.util.express.Operator;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Collection;
/**
* 自定义的LoopAnd,LoopOr,LoopSet function功能
* Created by tianqiao on 18/2/8.
*/
public class LoopFunctionTest {
@Test
public void test() throws Exception{
ExpressRunner runner = new ExpressRunner(false,true);
runner.addFunction("loopAnd", new Operator() {
private ExpressRunner loppRunner = new ExpressRunner();
@Override
public Object executeInner(Object[] list) throws Exception {
if(list[0]==null){
return false;
}
if(!(list[0] instanceof Collection)||((Collection)list[0]).size()==0){
return false;
}
Collection objs = (Collection) list[0];
String exp = (String) list[1];
Integer index = 0;
for(Object obj : objs) {
IExpressContext<String, Object> map = new DefaultContext<String, Object>();
map.put("x",obj);
map.put("index",index++);
try {
Object r = loppRunner.execute(exp, map, null, true, false);
if(r!=null && r instanceof Boolean && (Boolean) r){
continue;
}else{
return false;
}
}catch (Exception e){
return false;
}
}
return true;
}
});
runner.addFunction("loopOr", new Operator() {
private ExpressRunner loppRunner = new ExpressRunner();
@Override
public Object executeInner(Object[] list) throws Exception {
if(list[0]==null){
return false;
}
if(!(list[0] instanceof Collection)||((Collection)list[0]).size()==0){
return false;
}
Collection objs = (Collection) list[0];
String exp = (String) list[1];
Integer index=0;
for(Object obj : objs) {
IExpressContext<String, Object> map = new DefaultContext<String, Object>();
map.put("x",obj);
map.put("index",index++);
try {
Object r = loppRunner.execute(exp, map, null, true, false);
if(r!=null && r instanceof Boolean && (Boolean) r){
return true;
}else{
continue;
}
}catch (Exception e){
return false;
}
}
return false;
}
});
runner.addFunction("loopSet", new Operator() {
private ExpressRunner loppRunner = new ExpressRunner();
@Override
public Object executeInner(Object[] list) throws Exception {
if(list[0]==null){
return false;
}
if(!(list[0] instanceof Collection)||((Collection)list[0]).size()==0){
return false;
}
Collection objs = (Collection) list[0];
String exp = (String) list[1];
Integer index=0;
for(Object obj : objs) {
IExpressContext<String, Object> map = new DefaultContext<String, Object>();
map.put("x",obj);
map.put("index",index++);
try {
loppRunner.execute(exp, map, null, true, false);
}catch (Exception e){
return null;
}
}
return null;
}
});
ArrayList<SkuDO> skuList = createSkuList();
IExpressContext<String, Object> context = new DefaultContext<String, Object>();
String exp = "loopAnd(skuList,'x.price>10')";
context.put("skuList",skuList);
Object result = runner.execute(exp,context,null,false,true);
assert ((Boolean)result);
exp = "loopSet(skuList,'if(index>=2){x.price=9.9}')";
runner.execute(exp,context,null,false,true);
assert (skuList.get(0).getPrice()==10.1);
assert (skuList.get(1).getPrice()==10.1);
assert (skuList.get(2).getPrice()==9.9);
exp = "loopOr(skuList,'x.price<10')";
result = runner.execute(exp,context,null,false,true);
assert ((Boolean)result);
}
private ArrayList<SkuDO> createSkuList() {
ArrayList<SkuDO> skuList = new ArrayList<SkuDO>();
for(int i=0;i<5;i++) {
SkuDO sku = new SkuDO();
sku.setPrice(10.1);
skuList.add(sku);
}
return skuList;
}
public class SkuDO{
private Long id;
private Double price;
private String title;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
}
......@@ -62,7 +62,7 @@ public class WorkflowTest {
expressContext.put("申请人", "小强");
expressContext.put("金额", new Integer(4000));
//执行表达式
// runner.execute(exp, expressContext, null,false, false);
runner.execute(exp, expressContext, null,false, false);
}
/**
......@@ -94,7 +94,7 @@ public class WorkflowTest {
expressContext.put("申请人", "小强");
expressContext.put("金额", new Integer(5000));
// runner.executeByExpressName("example/approve1", expressContext, null, false,false,null);
runner.executeByExpressName("example/approve1", expressContext, null, false,false,null);
}
/**
......@@ -120,7 +120,7 @@ public class WorkflowTest {
expressContext.put("申请人", "小强");
expressContext.put("金额", new Integer(6000));
// runner.executeByExpressName("example/approve", expressContext, null, false,false,null);
runner.executeByExpressName("example/approve", expressContext, null, false,false,null);
}
/**
......@@ -148,7 +148,7 @@ public class WorkflowTest {
expressContext.put("申请人", "小强");
expressContext.put("金额", new Integer(7000));
// runner.executeByExpressName("example/approve1", expressContext, null, false,false,null);
runner.executeByExpressName("example/approve1", expressContext, null, false,false,null);
}
}
package com.ql.util.express.test;
import com.ql.util.express.DefaultContext;
import com.ql.util.express.ExpressRunner;
import com.ql.util.express.IExpressContext;
import com.ql.util.express.InstructionSet;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
public class ErrorListTest {
public boolean isVIP(String nick)
{
return nick.contains("vip_");
}
public Integer getUserLevel(String nick)
{
if(nick.contains("vip_")){
return 3;
}
return 2;
}
@Test
public void testErrorList() throws Exception {
ExpressRunner runner = new ExpressRunner(false, false);
runner.addFunctionOfServiceMethod("isVIP", new ErrorListTest(),
"isVIP", new Class[]{String.class},"$1不是vip");
runner.addFunctionOfServiceMethod("getUserLevel", new ErrorListTest(),
"getUserLevel", new Class[]{String.class},"");
runner.addOperatorWithAlias("大于",">","用户等级不够");
runner.addOperatorWithAlias("是否VIP","isVIP","亲爱的$1,你还不是VIP用户");
testExample(runner,"isVIP('vip_11111')");
testExample(runner,"isVIP('common_11111')");
testExample(runner,"getUserLevel('vip_11111') 大于 2");
testExample(runner,"getUserLevel('common_11111') 大于 2");
}
public void testExample(ExpressRunner runner,String express) throws Exception {
IExpressContext<String, Object> context = new DefaultContext<String, Object>();
List<String> errorList = new ArrayList<String>();
Object r = runner.execute(express,context,errorList,false,false,null);
System.out.println(r);
for(int i=0;i<errorList.size();i++){
System.out.println(errorList.get(i));
}
}
}
package com.ql.util.express.test.newmatch;
import java.util.List;
import org.junit.Test;
import com.ql.util.express.match.QLMatchResult;
import com.ql.util.express.match.QLPattern;
import com.ql.util.express.match.QLPatternNode;
import com.ql.util.express.parse.ExpressNode;
import com.ql.util.express.parse.ExpressParse;
import com.ql.util.express.parse.KeyWordDefine4Java;
import com.ql.util.express.parse.NodeTypeManager;
import com.ql.util.express.parse.Word;
import com.ql.util.express.parse.WordSplit;
public class PatternTest {
@Test
public void testMatch() throws Exception{
String[][] defines = new String[][]{
// {"EXPRESS","(3-2)*(2-1)"},
// {"OPDATA","ABC.B"},
// {"EXPRESS_OP_L1","!ABC"},
// {"EXPRESS","!!3 * !!4 * 5 + 8 + 7 +9 like ABC"},
// {"EXPRESS_OP_L5","7+!!3*4 like 9 +!!2*4"},
// {"SELECT","select TAB.TABLE_NAME + '-ABC',TAB.COL_NAME.B * 100 from A,B"},
{"EXPRESS_OP_L4","-3*-5"},
};
NodeTypeManager manager = new NodeTypeManager(new KeyWordDefine4Java());
if( manager.findNodeType("OP_LIST").isContainerChild(manager.findNodeType("*")) ==false){
throw new Exception("寻找儿子失败");
}
ExpressParse parse = new ExpressParse(manager,null,false);
for(String[] row : defines){
Word[] words = WordSplit.parse(manager.splitWord,row[1]);
// System.out.println("单词分解结果:" + WordSplit.getPrintInfo(words,","));
List<ExpressNode> tempList = parse.transferWord2ExpressNode(null,words,null,true);
System.out.println("单词分析结果:" + ExpressParse.printInfo(tempList,","));
QLPatternNode pattern = manager.findNodeType(row[0]).getPatternNode();
QLMatchResult result =QLPattern.findMatchStatement(manager, pattern, tempList, 0);
if(result == null){
throw new Exception("没有正确的匹配:" + row[0] + ":" + row[1]);
}
System.out.println(result);
}
}
}
package com.ql.util.express.test.newmatch;
import org.junit.Test;
import com.ql.util.express.match.INodeTypeManager;
import com.ql.util.express.match.NodeTypeManagerTestImpl;
import com.ql.util.express.match.QLPattern;
import com.ql.util.express.match.QLPatternNode;
public class QLPatternDefineTest {
INodeTypeManager manager = new NodeTypeManagerTestImpl();
@Test
public void testDefine() throws Exception {
String[] defines = new String[] {
//"ABC",
//"ABC^*",
// "\\(~$ID$\\)~#()",
// "CONST$(+^$(CONST|ID))^{1:222}#SQL",
// "(CONST|ID)$((*|/)^$CONST)^*",
// "(CONST|ID)$(,~$(CONST|ID))*#PARAMETER_LIST",
// "OPDATA$(.->FIELD_CALL^$ID->CONST_STRING)^*",
// "\\(->CHILD_EXPRESS",
"OP_LEVEL1|OP_LEVEL2|OP_LEVEL3|OP_LEVEL4|OP_LEVEL5|OP_LEVEL6|OP_LEVEL7|OP_LEVEL8|OP_LEVEL9|=|LEFT_BRACKET|RIGHT_BRACKET"
};
for (String s : defines) {
QLPatternNode t = QLPattern.createPattern(manager,"ANONY_PATTERN", s);
System.out.println(t);
}
}
}
package com.ql.util.express.test.newmatch;
import org.junit.Test;
import com.ql.util.express.parse.ExpressNode;
import com.ql.util.express.parse.ExpressParse;
import com.ql.util.express.parse.KeyWordDefine4SQL;
import com.ql.util.express.parse.NodeTypeManager;
public class SqlTest {
public String[] testString ={
"select id as id2 from upp_biz_order",
"select id as id2,name as name2 from upp_biz_order where a=1",
"select id as id2,name from upp_biz_order where 1=1",
};
@Test
public void testDefine() throws Exception {
NodeTypeManager manager = new NodeTypeManager(new KeyWordDefine4SQL());
ExpressParse parse = new ExpressParse(manager,null,false);
for(String text : testString){
ExpressNode result = parse.parse(null, text, true, null);
System.out.print(result);
}
}
}
package com.ql.util.express.test.newmatch;
import java.util.ArrayList;
import java.util.List;
import com.ql.util.express.ExpressRunner;
import com.ql.util.express.IExpressContext;
import com.ql.util.express.test.ExpressContextExample;
public class TestMatch {
@org.junit.Test
public void testExpress() throws Exception{
ExpressRunner runner = new ExpressRunner(false,true);
String[][] expressTest = new String[][] {
{"1+2","3"},
{"2-1","1"},
{"2+(-1)","1"},
{"(3+3)*(4+4)/3 + 8 - 2*2","20"},
{"7==8","false"},
{"!((8>=8) && !false) || 9==9","true"},
{"a=(b=9)","9"},
{"int a = 1+3","4"},
{"(int[][] a = new int[2][2]).length","2"},
{" f = (exportDef int b = (int a = 2+3))","5"},
{"name=new String() + 100","100"},
{"name=new String((3+3)*4+\"$\") + 100","24$100"},
{"name=new String(\"xuannan-\") + 100","xuannan-100"},
{"new String[2][10].length","2"},
{"[1,2,3,4,5].length","5"},
{"[1,2,3,4,5][2]","3"},
{"[[1,2],[3,4]].length","2"},
{"1000 + new String(\"abc\").substring(1).substring(1).length() + 100","1101"},
{"new Object().getClass().getAnnotations().length","0"},
{"new Object().getClass().getMethods()[1].getName()","wait"},
// {"Object.class.getName().getClass().getMethods().length","72"},
{"1/2","0"},
{"1/(double)2","0.5"},
{"(double)1/2","0.5"},
{"((Object)\"ABC\").getClass().getName()","java.lang.String"},
{"[[1,2],[3,4]][0][1]","2"},
{"(new String[2])[1]","null"},
{"max(2,100)","100"},
{"max(max(1,2,3),min(2,100))","3"},
{"true?1:2","1"},
{"9==9?100+200:10+20","300"},
{"9==8?100+200:20+20","40"},
{"return a=100+90","190"},
{"alias xuannan qianghui","null"},
{"max 2-1,3,4+2","6"},
{"int a = 2 + 1;b=100;return a + b","103"},
{"{int a = 10;{int a = 20;} return a;}","10"},
{"{int a = 10;{a = 20;} return a;}","20"},
{"if true then return 100 else return 200","100"},
{"if true then {return 100;} else {return 200;}","100"},
{"if (false) then return 100 else return 200","200"},
{"if (1!=1) then {return 100;} else {return 200;}","200"},
{"if true then return 100","100"},
{"if true then {return 100;} ","100"},
{"if (false) then return 100","null"},
{"if (1!=1) then {return 100;}","null"},
{"if (true) return 100; else return 200","100"},
{"if (true) {return 100;} else {return 200;}","100"},
{"if (false) return 100; else return 200","200"},
{"if (1!=1) {return 100;} else {return 200;}","200"},
{"if (false) if(false) return 100;else return 200; else return 300;","300"},
{"int a = 0;{a = a + 100;}{ a= a+ 200;}","300"},
{"int a =0;for(int i=1;i<=10;i++){a = a + i;} return a;","55"},
{"int a =0;for(int i=1;i<10;i++){if(i >5) break; a = a + 100;} return a;","500"},
{"function abc(){return 100;} return abc()","100"},
{"function abc(int a,int b){ return a + b;} return abc(1+100,2*100)","301"},
{"macro abc { return a + 100;} int a = 100; return abc + 100","300"},
{"class Person(String aName,int aYear){" +
"String name = aName;" +
"int year = aYear;" +
"function getName(){return name;}" +
"function getYear(){return year;}" +
"} " +
"Person person =new Person(\"xuannan\",100);" +
"return person.getName() + '-' + person.getYear();"
,"xuannan-100"},
{"map = NewMap('ABC':100,'BCD':200,'DEF':1000 + 1000);return map.get('BCD')","200"},
{"Object[] abc = [];return abc.length","0"},
{"2 in 2","true"},
{"2 in (4,5,6)","false"},
{"(-1)","-1"},
{"/**1,2,3,4**/1+2","3"},
{"1+/**1,2,3,4**/2+3","6"},
{"(new String[3][5])[1].length","5"},
{"class ABC(com.ql.util.express.test.BeanExample bean,String name){"
+"InnerClass a = new InnerClass();"
+ "哈希值:{bean.hashCode();};"
+ "class InnerClass(){" +
"int 计数 =200;" +
"};"
+ "}" +
"return new ABC(new com.ql.util.express.test.BeanExample(),'xuannan').a.计数" ,
"200"
},
{";i=100;;","100"}
};
for (int point = 0; point < expressTest.length; point++) {
String expressStr = expressTest[point][0];
List<String> errorList = new ArrayList<String>();
IExpressContext<String,Object> expressContext = new ExpressContextExample(null);
Object result = runner.execute(expressStr,expressContext, null, false,false);
if (expressTest[point][1].equalsIgnoreCase("null")
&& result != null
|| expressTest[point][1].equalsIgnoreCase(result==null?"null":result.toString()) == false) {
throw new Exception("处理错误,计算结果与预期的不匹配:" + expressStr + " = " + result + "但是期望值是:" + expressTest[point][1]);
}
System.out.println("Example " + point + " : " + expressStr + " = " + result);
System.out.println(expressContext);
if(errorList.size() > 0){
System.out.println("\t\t系统输出的错误提示信息:" + errorList);
}
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册