kangjie пре 5 година
родитељ
комит
2ff2d84274

+ 3 - 3
src/main/java/com/ssssssss/expression/ExpressionEngine.java

@@ -18,12 +18,12 @@ public class ExpressionEngine {
 		ExpressionEngine engine = new ExpressionEngine();
 
 		Map<String, Object> params = new HashMap<>();
-		params.put("abc", "xxx");
+		params.put("abc", "876");
 		ArrayList<Object> list = new ArrayList<>();
 		list.add("987654321");
 		list.add("");
-		params.put("arr", list);
-		Object result = engine.execute("${arr.map(e->abc.hashCode())}", params);
+		params.put("e", list);
+		Object result = engine.execute("${e.map(e->123)}", params);
 		System.out.println(result);
 
 

+ 30 - 3
src/main/java/com/ssssssss/expression/parsing/ArrayLikeLambdaExecutor.java

@@ -1,14 +1,35 @@
 package com.ssssssss.expression.parsing;
 
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.List;
+import java.lang.reflect.Method;
+import java.util.*;
 import java.util.function.Supplier;
 
 public class ArrayLikeLambdaExecutor {
 
+    public static final Set<String> SUPPORT_METHOD = new HashSet<>();
+    public static final Map<String, Method> METHODS;
 
 
+    static {
+        Map<String, Method> temp = new HashMap<>();
+        addSupport(temp, "map");
+        METHODS = Collections.unmodifiableMap(temp);
+    }
+
+    private static void addSupport(Map<String, Method> temp, String name) {
+        SUPPORT_METHOD.add(name);
+        init(temp, name);
+    }
+
+    private static void init(Map<String, Method> initialMap, String name) {
+        try {
+            initialMap.put(name, ArrayLikeLambdaExecutor.class.getMethod(name, Object.class, Object[].class));
+        } catch (NoSuchMethodException e) {
+            e.printStackTrace();
+            throw new RuntimeException(e);
+        }
+    }
+
     @SuppressWarnings("unchecked")
     public static Object map(Object arrayLike, Object... arguments) {
 //        System.err.println("ArrayLikeLambdaExecutor:11 " + arrayLike);
@@ -22,6 +43,12 @@ public class ArrayLikeLambdaExecutor {
         }
         if (arrayLike instanceof Collection) {
             return results;
+        } else if (arrayLike.getClass().isArray()) {
+            return results.toArray();
+        } else if (arrayLike instanceof Iterator) {
+
+        } else if (arrayLike instanceof Enumeration) {
+
         }
         throw new RuntimeException("未实现");
     }

+ 12 - 24
src/main/java/com/ssssssss/expression/parsing/Ast.java

@@ -1015,30 +1015,18 @@ public abstract class Ast {
 					Object arrLikeObj = context.get(parName);
 					if (arrLikeObj instanceof Collection) {
 						Collection<?> coll = (Collection<?>) arrLikeObj;
-
-						if (function instanceof MethodCall) {
-							List<Object> collect = coll.stream().map(o -> {
-								Object result = null;
-								//TODO need multiple params
-								return ((Supplier) () -> {
-									try {
-										context.push();
-										context.setOnCurrentScope(getElement().getSpan().getText(), o);
-										Object res = function.evaluate(template, context);
-										context.pop();
-										return res;
-									} catch (IOException e) {
-										e.printStackTrace();
-										throw new RuntimeException(e);
-									}
-								});
-
-							}).collect(Collectors.toList());
-							return collect;
-						} else {
-							ExpressionError.error("err : function not instanceof MethodCall.", function.getSpan());
-						}
-						return null;
+						return coll.stream().map(o -> ((Supplier) () -> {
+							try {
+								context.push();
+								context.setOnCurrentScope(getElement().getSpan().getText(), o);
+								Object res = function.evaluate(template, context);
+								context.pop();
+								return res;
+							} catch (IOException e) {
+								e.printStackTrace();
+								throw new RuntimeException(e);
+							}
+						})).collect(Collectors.toList());
 					}
 				}
 			} else {

+ 3 - 6
src/main/java/com/ssssssss/expression/parsing/Parser.java

@@ -195,12 +195,9 @@ public class Parser {
 						}
 					}
 					MethodCall methodCall = new MethodCall(new Span(result.getSpan(), closingSpan), (MemberAccess) result, arguments);
-					if ("map".equals(((MemberAccess) result).getName().getText())) {
-						try {
-							methodCall.setCachedMethod(ArrayLikeLambdaExecutor.class.getMethod("map", Object.class, Object[].class));
-						} catch (NoSuchMethodException e) {
-							e.printStackTrace();
-						}
+					String name = ((MemberAccess) result).getName().getText();
+					if (ArrayLikeLambdaExecutor.SUPPORT_METHOD.contains(name)) {
+						methodCall.setCachedMethod(ArrayLikeLambdaExecutor.METHODS.get(name));
 						methodCall.setCachedMethodStatic(true);
 					}
 					result = methodCall;