从janino中的字符串编译lambda表达式

ndh0cuux  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(649)

我试图用janino从字符串编译一个类。该类在函数中包含lambda表达式,但似乎无法识别引用运算符“->”和“:”。
我在线程“main”org.codehaus.commons.compiler.compileeException中得到一个compileeException完整的桩跟踪异常:第1行,第346列:主线程中的意外标记“>”
下面是我使用的代码,

public class LambdaFromJanino{

      public static void main(String[] args) throws Exception
        {
            String CLASS_NAME = "Foo";
            String codeStr = "import java.util.Arrays;" + 
                        "import java.util.List;"+
                        "import java.util.stream.Collectors;"+
                        "public class " + CLASS_NAME + " {" +
                        "public static void main(String[] args) {" +
                        "System.out.println(\"Hello \" + args[0]);" +
                        "List<String> result = lambdaOut(args);"+
                        //"result.forEach(System.out::println);"+
                        "System.out.println(\"this is result \"+result.get(0));"+
                        "}" +
                        "static List lambdaOut(String[] arr)  {  " +
                        "return  Arrays.stream(arr).map( x -> x.replaceAll(\"[a-zA-Z]\", \"\"))" +
                        ".collect(Collectors.toList()); }; " +
                        "}";
            SimpleCompiler compiler = new SimpleCompiler();
            compiler.cook( codeStr ); // compile the string
            // get the loaded class
            Class<?> cl = compiler.getClassLoader().loadClass(CLASS_NAME);
            // Invoke the "public static main(String[])" method
            Method mainMeth = cl.getMethod("main", new Class[] { String[].class });
            String[] methArgs = new String[] { args[0], args[1], args[2] }; // one input
            mainMeth.invoke(null, new Object[] { methArgs });
        } 
}
kyxcudwk

kyxcudwk1#

尝试下面的代码,它将有助于解决您的问题。
由于你不是收集结果我lambda表达式,这里我已经附上 \"\")).collect(Collectors.toList())\n" 去解决它。

public static void main(String[] args) throws Exception
                {
                    String CLASS_NAME = "Foo";
                    String codeStr = "import java.util.Arrays;" + 
                                "import java.util.List;"+
                                "import java.util.stream.Collectors;"+
                                "public class " + CLASS_NAME + " {" +
                                "public static void main(String[] args) {" +
                                "System.out.println(\"Hello \" + args[0]);" +
                                "List<String> result = lambdaOut(args);"+
                                //"result.forEach(System.out::println);"+
                                "System.out.println(\"this is result \"+result.get(0));"+
                                "}" +
                                "static List lambdaOut(String[] arr)  {  " +
"return  Arrays.stream(arr).map( x -> x.replaceAll(\"[a-zA-Z]\", \"\")).collect(Collectors.toList())\n" +
                    ".collect(Collectors.toList()); }; \n" +
                    "}";

相关问题