timowest / scalagen

Java to Scala transformation
Apache License 2.0
216 stars 32 forks source link

Converting Java8 lambdas #77

Open khajavi opened 9 years ago

khajavi commented 9 years ago

Scalagen cannot Convert program like this:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Java8CompareClient {

    public static void main(String[] args) {

        List<String> srcList = Arrays.asList("defg", "defg", "x", "xyz", "zzzzzzzz", "abc", "abcde");

        List<String> srcList1 = new ArrayList(srcList);
        List<String> srcList2 = new ArrayList(srcList);

        Comparator<String> comparator = (x, y) -> x.length() - y.length();

        Collections.sort(srcList1, comparator);

        System.out.println("srcList1: " + srcList1);

        Collections.sort(srcList2, (x, y) -> x.length() - y.length());

        System.out.println("srcList2: " + srcList2);
    }
}
gervaisb commented 8 years ago

:+1: It seems that javaparser library fail on the lambda syntax;

package hello;
import java.util.Optional;

class HelloWorldApp {        
    public static void main(String[] args) {        
        Optional<String> opt = Optional.empty();
        opt.map(s -> s.length());
    }
}

japa.parser.ParseException: Encountered " ">" "> "" at line 10, column 20. Was expecting one of: "boolean" ... "byte" ... "char" ... "double" ... "false" ... "float" ... "int" ... "long" ... "new" ... "null" ... "short" ... "super" ... "this" ... "true" ... "void" ... ... ... ... ... ... ... "(" ... "!" ... "~" ... "++" ... "--" ... "+" ... "-" ...

The latest release support java 8 syntax :

The project now supports parsing Java 1.8 -- https://github.com/javaparser/javaparser#history

metasim commented 8 years ago

+1

nightscape commented 7 years ago

I started a WIP PR for this here: https://github.com/timowest/scalagen/pull/83

nightscape commented 7 years ago

New PR https://github.com/timowest/scalagen/pull/84 supports Java 8 and comments.