package yokohama.unit.translator;

import java.io.File;
import java.net.URI;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import javax.tools.JavaCompiler;
import javax.tools.JavaCompiler.CompilationTask;
import javax.tools.JavaFileObject.Kind;
import javax.tools.SimpleJavaFileObject;
import javax.tools.ToolProvider;
import yokohama.unit.ast_junit.CompilationUnit;

public class JUnitAstCompilerImpl implements JUnitAstCompiler {
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();

    @Override
    public boolean compile(
            Path docyPath,
            CompilationUnit ast,
            String className,
            String packageName,
            List<String> classPath,
            Optional<Path> dest,
            List<String> javacArgs) {
        String javaCode = ast.getText();

        // Compile Java code
        if (compiler == null) {
            System.err.println("Could not get the system Java compiler. Probably either JAVA_HOME variable is not set or it does not point to JDK directory.");
            return false;
        }

        List<String> args = new ArrayList<>();
        if (classPath.size() > 0) {
            args.add("-cp");
            args.add(String.join(File.pathSeparator, classPath));
        }
        if (dest.isPresent()) {
            args.add("-d");
            args.add(dest.get().toString());
        }
        args.addAll(javacArgs);

        CompilationTask task = compiler.getTask(
                null, /* Writer out */
                null, /* JavaFileManager fileManager */
                null, /* DiagnosticListener<? super JavaFileObject> diagnosticListener */
                args,
                null, /* Iterable<String> classes */
                Arrays.asList(new SimpleJavaFileObject(
                        URI.create("string:///"
                                + packageName.replace('.','/') + "/" + className
                                + Kind.SOURCE.extension),
                        Kind.SOURCE) {
                    @Override
                    public CharSequence getCharContent(boolean ignoreEncodingErrors) {
                        return javaCode;
                    }
                }));
        return task.call(); 
    }
}

@@ -102,16 +102,45 @@
 .Path;%0D%0A
+import java.nio.file.Paths;%0D%0A
 import j
@@ -156,24 +156,24 @@
 ArrayList;%0D%0A
-
 import java.
@@ -238,16 +238,88 @@
 ional;%0D%0A
+import javax.tools.Diagnostic;%0D%0Aimport javax.tools.DiagnosticListener;%0D%0A
 import j
@@ -394,16 +394,52 @@
 nTask;%0D%0A
+import javax.tools.JavaFileObject;%0D%0A
 import j
@@ -595,16 +595,139 @@
 onUnit;%0D
+%0Aimport yokohama.unit.position.ErrorMessage;%0D%0Aimport yokohama.unit.position.Position;%0D%0Aimport yokohama.unit.position.Span;%0D
 %0A%0D%0Apubli
@@ -1786,24 +1786,809 @@
 acArgs);%0D%0A%0D%0A
+        List%3CErrorMessage%3E errors = new ArrayList%3C%3E();%0D%0A        DiagnosticListener%3CJavaFileObject%3E diagnosticListener = new DiagnosticListener%3CJavaFileObject%3E() %7B%0D%0A            @Override%0D%0A            public void report(Diagnostic%3C? extends JavaFileObject%3E diagnostic) %7B%0D%0A                if (diagnostic.getKind() != Diagnostic.Kind.ERROR) return;%0D%0A                Path path = Paths.get(diagnostic.getSource().toUri());%0D%0A                int line = (int)diagnostic.getLineNumber();%0D%0A                int column = (int)diagnostic.getColumnNumber();%0D%0A                Span span = Span.of(path, Position.of(line, column));%0D%0A                ErrorMessage errorMessage = new ErrorMessage(diagnostic.getMessage(null), span);%0D%0A                errors.add(errorMessage);%0D%0A            %7D%0D%0A        %7D;%0D%0A%0D%0A
         Comp
@@ -2741,60 +2741,8 @@
     
- null, /* DiagnosticListener%3C? super JavaFileObject%3E
  dia
@@ -2756,19 +2756,17 @@
 Listener
- */
+,
 %0D%0A      

Use DiagnosticListner of Java tools