Example usage for javax.tools StandardJavaFileManager getJavaFileForInput

List of usage examples for javax.tools StandardJavaFileManager getJavaFileForInput

Introduction

In this page you can find the example usage for javax.tools StandardJavaFileManager getJavaFileForInput.

Prototype

JavaFileObject getJavaFileForInput(Location location, String className, Kind kind) throws IOException;

Source Link

Document

Returns a JavaFileObject file object for input representing the specified class of the specified kind in the given package-oriented location.

Usage

From source file:me.dwtj.java.compiler.utils.CompilationTaskBuilder.java

private CompilationTask buildTask() throws IOException {
    // Configure the compiler itself and its file manager.
    JavaCompiler compiler = getSystemJavaCompiler();
    StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostic, null, null);
    fileManagerConfig.config(fileManager);

    // Use the file manager and the class list to get the compilation units to be compiled.
    for (String c : classes) {
        JavaFileObject srcFile = fileManager.getJavaFileForInput(SOURCE_PATH, c, SOURCE);
        if (srcFile == null) {
            throw new IOException("No such class found on the source path: " + c);
        } else {/*from w  w w. ja  v a  2s . c o m*/
            compilationUnits.add(srcFile);
        }
    }

    // Configure the compilation task itself.
    if (compilationUnits.isEmpty()) {
        String msg = "CompilationTaskBuilder: No compilation units have been added.";
        throw new IllegalStateException(msg);
    }
    CompilationTask task = compiler.getTask(null, // TODO: Support user-defined writer.
            fileManager, diagnostic, options, // TODO: Support more user-defined options.
            null, // TODO: Support user-defined classes.
            compilationUnits);
    task.setProcessors(processors);

    return task;
}