0

I am trying to compile a class from a String using Janino. The class contains a lambda expression inside a function but it seems to not recognize the reference operator "->" and "::".

I am getting a CompileException The complete stake trace Exception in thread "main" org.codehaus.commons.compiler.CompileException: Line 1, Column 346: Unexpected token ">" in primary

Below is the code I am using,

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 });
        } 
}
Shafaq
  • 1

1 Answers1

0

Try below code, it will help to resolve your problem.

As you are not collection result me lambda expression, here i had attached \"\")).collect(Collectors.toList())\n" to resolve it.

    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" +
                    "}";
Sagar Gangwal
  • 7,544
  • 3
  • 24
  • 38