get String Source Java File Manager - Java javax.tools

Java examples for javax.tools:JavaCompiler

Description

get String Source Java File Manager

Demo Code


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.tools.DiagnosticListener;
import javax.tools.FileObject;
import javax.tools.ForwardingJavaFileManager;
import javax.tools.JavaCompiler;
import javax.tools.JavaCompiler.CompilationTask;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileObject;
import javax.tools.JavaFileObject.Kind;
import javax.tools.SimpleJavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;

public class Main{
    private static final Map<String, JavaFileObject> output = new ConcurrentHashMap<String, JavaFileObject>();
    public static JavaFileManager getStringSourceJavaFileManager(
            JavaCompiler compiler,
            DiagnosticListener<? super JavaFileObject> diagnosticListener,
            Locale locale, Charset charset) {

        JavaFileManager fileManager = new ForwardingJavaFileManager<StandardJavaFileManager>(
                compiler.getStandardFileManager(diagnosticListener, locale,
                        charset)) {//from  w  w w . ja  v a2  s . c o m
            @Override
            public JavaFileObject getJavaFileForOutput(Location location,
                    String className, Kind kind, FileObject sibling)
                    throws IOException {
                JavaFileObject jfo = new ByteJavaObject(className, kind);
                output.put(className, jfo);
                return jfo;
            }
        };
        return fileManager;
    }
}

Related Tutorials