Permalink
Browse files

Fixed getVariableNames could not process map,list and array

  • Loading branch information...
1 parent 6e21abe commit d781a4c34c5d6d047ab947708445ac190ec25c26 @killme2008 committed Dec 26, 2011
@@ -93,12 +93,12 @@
private static AviatorClassLoader aviatorClassLoader;
/**
- * Optimized for compile speed,this is the default option
+ * Optimized for compile speed
*/
public static final int COMPILE = 0;
/**
- * Optimized for execute speed
+ * Optimized for execute speed,this is the default option
*/
public static final int EVAL = 1;
@@ -234,9 +234,10 @@ public AviatorClassLoader run() {
/**
- * set optimize level,default AviatorEvaluator.COMPILE
+ * set optimize level,default AviatorEvaluator.EVAL
*
- * @see COMPILE,EVAL
+ * @see #COMPILE
+ * @see #EVAL
*
* @param value
*/
@@ -1,5 +1,7 @@
package com.googlecode.aviator;
+import java.util.ArrayList;
+import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
@@ -17,7 +19,15 @@
public BaseExpression(List<String> varNames) {
super();
- this.varNames = varNames;
+ LinkedHashSet<String> tmp = new LinkedHashSet<String>(varNames.size());
+ // process nested names
+ for (String name : varNames) {
+ if (name.contains(".")) {
+ name = name.substring(0, name.indexOf("."));
+ }
+ tmp.add(name);
+ }
+ this.varNames = new ArrayList<String>(tmp);
}
@@ -331,6 +331,18 @@ public Expression getResult() {
}
}
}
+ else if (delegateToken.getDelegateTokenType() == DelegateTokenType.Array) {
+ Token<?> realToken = delegateToken.getToken();
+ if (realToken.getType() == TokenType.Variable) {
+ varName = token.getLexeme();
+ if (!variables.containsKey(varName)) {
+ variables.put(varName, 1);
+ }
+ else {
+ variables.put(varName, variables.get(varName) + 1);
+ }
+ }
+ }
break;
}
}
@@ -47,20 +47,34 @@ public void testExec() {
String exp1 = "b-c+a";
String exp2 = "2*3.14*(R-r)+b/c";
String exp3 = "f>d?k:a";
+ String exp4 = "map.a > map.b ? list[0][1]: y";
assertEquals(8, AviatorEvaluator.exec(exp1, 6, 2, 4));
assertEquals(104, AviatorEvaluator.exec(exp1, 99, 3, 8));
assertEquals(1.14d, AviatorEvaluator.exec(exp1, 3.14, 3, 1));
assertEquals(6.28, AviatorEvaluator.exec(exp2, 4, 3, 0, 100));
assertEquals(13.304d, AviatorEvaluator.exec(exp2, 5, 3.2, 4, 2.0));
assertEquals(3.14d, AviatorEvaluator.exec(exp3, 2, 1, 3.14, 9));
- assertEquals(9, AviatorEvaluator.exec(exp3,1,2,3.14,9));
+ assertEquals(9, AviatorEvaluator.exec(exp3, 1, 2, 3.14, 9));
+
+ Map<String, Integer> map = new HashMap<String, Integer>();
+ map.put("a", 100);
+ map.put("b", 99);
+ int[][] list = new int[1][2];
+ list[0]=new int[2];
+ list[0][1] = 2000;
+ int y = 999;
+ assertEquals(2000L, AviatorEvaluator.exec(exp4, map, list, y));
+ map.put("a", 99);
+ assertEquals(999L, AviatorEvaluator.exec(exp4, map, list, y));
}
- @Test(expected=IllegalArgumentException.class)
+
+ @Test(expected = IllegalArgumentException.class)
public void testExecIllegalArguments() {
- AviatorEvaluator.exec("a-b+c",1,2);
+ AviatorEvaluator.exec("a-b+c", 1, 2);
}
+
@Test
public void testCompileCache() {
Expression exp1 = AviatorEvaluator.compile("1+3", true);
@@ -447,6 +447,20 @@ public void testGetVariableNames() {
assertEquals("e", vars.get(3));
assertEquals("c", vars.get(4));
+ // Test map or list as variable
+ expression = AviatorEvaluator.compile("map.a>10 && list[0][1]<3 && bean.c == bean.x || bean.d == y", true);
+ assertNotNull(expression);
+ vars = expression.getVariableNames();
+ assertEquals(4, vars.size());
+ assertTrue(vars.contains("map"));
+ assertTrue(vars.contains("list"));
+ assertTrue(vars.contains("bean"));
+ assertTrue(vars.contains("y"));
+ assertEquals("map", vars.get(0));
+ assertEquals("list", vars.get(1));
+ assertEquals("bean", vars.get(2));
+ assertEquals("y", vars.get(3));
+
}

0 comments on commit d781a4c

Please sign in to comment.