Example usage for javax.tools StandardLocation SOURCE_PATH

List of usage examples for javax.tools StandardLocation SOURCE_PATH

Introduction

In this page you can find the example usage for javax.tools StandardLocation SOURCE_PATH.

Prototype

StandardLocation SOURCE_PATH

To view the source code for javax.tools StandardLocation SOURCE_PATH.

Click Source Link

Document

Location to search for existing source files.

Usage

From source file:name.martingeisse.webide.features.java.compiler.memfile.MemoryFileManager.java

@Override
public FileObject getFileForInput(final Location location, final String packageName, final String relativeName)
        throws IOException {
    final String loggingName = "input file; location [" + location + "], package [" + packageName + "], name ["
            + relativeName + "]";
    if (location == StandardLocation.SOURCE_PATH) {
        logger.trace("searching memory files: " + loggingName);
        final String key = getPackageFileName(packageName, relativeName);
        final FileObject file = inputFiles.get(key);
        logger.trace("result for key [" + key + "]: " + file);
        if (file != null) {
            return file;
        }//from w ww. j  a v a  2  s.co m
    } else {
        logger.trace("skipping memory files for " + loggingName);
    }
    return super.getFileForInput(location, packageName, relativeName);
}

From source file:com.joliciel.talismane.utils.compiler.DynamicCompiler.java

public Map<String, Class<?>> compileMany(Map<String, CharSequence> sources, List<String> optionList) {
    List<JavaFileObject> sourceObjects = new ArrayList<JavaFileObject>();

    // prepare the JavaFileObjects to be compiled
    for (String qualifiedName : sources.keySet()) {
        CharSequence sourceCode = sources.get(qualifiedName);

        String className = qualifiedName;
        String packageName = "";
        int lastDot = qualifiedName.lastIndexOf('.');
        if (lastDot >= 0) {
            packageName = qualifiedName.substring(0, lastDot);
            className = qualifiedName.substring(lastDot + 1);
        }// ww w .ja v  a2  s.c  o  m

        InMemoryJavaFileObject sourceObject = new InMemoryJavaFileObject(className, sourceCode);

        this.inMemoryFileManager.prepareJavaFileObject(StandardLocation.SOURCE_PATH, packageName,
                className + ".java", sourceObject);

        sourceObjects.add(sourceObject);
    }

    // Perform the compilation
    CompilationTask task = this.javaCompiler.getTask(null, this.inMemoryFileManager, diagonosticListener,
            optionList, null, sourceObjects);

    Boolean result = task.call();
    if (result == null || !result) {
        LOG.info("Compilation failed.");
        for (JavaFileObject sourceObject : sourceObjects)
            System.out.println(((InMemoryJavaFileObject) sourceObject).getCharContent(true));
        throw new DynamicCompilerException("Compilation failed.");
    }

    // Return the compiled classes
    try {
        Map<String, Class<?>> compiledClasses = new HashMap<String, Class<?>>();
        for (String qualifiedName : sources.keySet()) {
            final Class<?> compiledClass = this.inMemoryClassLoader.loadClass(qualifiedName);
            compiledClasses.put(qualifiedName, compiledClass);
        }
        return compiledClasses;
    } catch (ClassNotFoundException e) {
        throw new DynamicCompilerException(e);
    }
}

From source file:org.cloudfoundry.practical.demo.web.controller.CompilerController.java

private boolean compile(Folders folders, Writer out) throws IOException {
    JavaFileManager standardFileManager = this.compiler.getStandardFileManager(null, null, null);
    ResourceJavaFileManager fileManager = new ResourceJavaFileManager(standardFileManager);
    fileManager.setLocation(StandardLocation.SOURCE_PATH, folders.getSource());
    fileManager.setLocation(StandardLocation.CLASS_OUTPUT, folders.getOutput());
    fileManager.setLocation(StandardLocation.CLASS_PATH, folders.getLibJars());
    Iterable<? extends JavaFileObject> units = fileManager.list(StandardLocation.SOURCE_PATH, "",
            EnumSet.of(Kind.SOURCE), true);
    CompilationTask task = this.compiler.getTask(out, fileManager, null, COMPILER_OPTIONS, null, units);
    return task.call();
}

From source file:com.wavemaker.tools.compiler.ProjectCompiler.java

public String compile(final Project project) {
    try {//from   ww  w . ja v  a2s.c om
        copyRuntimeServiceFiles(project.getWebAppRootFolder(), project.getClassOutputFolder());
        JavaCompiler compiler = new WaveMakerJavaCompiler();
        StandardJavaFileManager standardFileManager = compiler.getStandardFileManager(null, null, null);
        standardFileManager.setLocation(StandardLocation.CLASS_PATH, getStandardClassPath());
        ResourceJavaFileManager projectFileManager = new ResourceJavaFileManager(standardFileManager);
        projectFileManager.setLocation(StandardLocation.SOURCE_PATH, project.getSourceFolders());
        projectFileManager.setLocation(StandardLocation.CLASS_OUTPUT,
                Collections.singleton(project.getClassOutputFolder()));
        projectFileManager.setLocation(StandardLocation.CLASS_PATH, getClasspath(project));
        copyResources(project);
        Iterable<JavaFileObject> compilationUnits = projectFileManager.list(StandardLocation.SOURCE_PATH, "",
                Collections.singleton(Kind.SOURCE), true);
        StringWriter compilerOutput = new StringWriter();
        CompilationTask compilationTask = compiler.getTask(compilerOutput, projectFileManager, null,
                getCompilerOptions(project), null, compilationUnits);
        ServiceDefProcessor serviceDefProcessor = configure(new ServiceDefProcessor(), projectFileManager);
        ServiceConfigurationProcessor serviceConfigurationProcessor = configure(
                new ServiceConfigurationProcessor(), projectFileManager);
        compilationTask.setProcessors(Arrays.asList(serviceConfigurationProcessor, serviceDefProcessor));
        if (!compilationTask.call()) {
            throw new WMRuntimeException("Compile failed with output:\n\n" + compilerOutput.toString());
        }
        return compilerOutput.toString();
    } catch (IOException e) {
        throw new WMRuntimeException("Unable to compile " + project.getProjectName(), e);
    }
}

From source file:name.martingeisse.webide.features.java.compiler.memfile.MemoryFileManager.java

@Override
public JavaFileObject getJavaFileForInput(final Location location, final String className, final Kind kind)
        throws IOException {
    final String loggingName = "java input file; location [" + location + "], class [" + className + "], kind ["
            + kind + "]";
    if (location == StandardLocation.SOURCE_PATH) {
        logger.trace("searching memory files: " + loggingName);
        final String key = getJavaFileName(className, kind);
        final FileObject file = inputFiles.get(key);
        logger.trace("result for key [" + key + "]: " + file);
        if (file != null) {
            if (file instanceof JavaFileObject) {
                return (JavaFileObject) file;
            } else {
                logger.trace("file is not a JavaFileObject, returning 'not found'");
                return null;
            }//from www .j  a  v  a 2  s . c  o  m
        }
    } else {
        logger.trace("skipping memory files for " + loggingName);
    }
    return super.getJavaFileForInput(location, className, kind);
}

From source file:com.wavemaker.tools.compiler.ProjectCompiler.java

public String compile(Folder webAppRootFolder, Iterable<Folder> sources, Folder destination,
        Iterable<Resource> classpath) {
    try {/*from  w ww  .j  a va2 s. c o m*/
        if (webAppRootFolder != null) {
            copyRuntimeServiceFiles(webAppRootFolder, destination);
        }
        JavaCompiler compiler = new WaveMakerJavaCompiler();
        StandardJavaFileManager standardFileManager = compiler.getStandardFileManager(null, null, null);
        standardFileManager.setLocation(StandardLocation.CLASS_PATH, getStandardClassPath());
        ResourceJavaFileManager projectFileManager = new ResourceJavaFileManager(standardFileManager);
        projectFileManager.setLocation(StandardLocation.SOURCE_PATH, sources);
        ArrayList<Folder> destinations = new ArrayList<Folder>();
        destinations.add(destination);
        projectFileManager.setLocation(StandardLocation.CLASS_OUTPUT, destinations);
        projectFileManager.setLocation(StandardLocation.CLASS_PATH, classpath);
        Iterable<JavaFileObject> compilationUnits = projectFileManager.list(StandardLocation.SOURCE_PATH, "",
                Collections.singleton(Kind.SOURCE), true);
        StringWriter compilerOutput = new StringWriter();
        CompilationTask compilationTask = compiler.getTask(compilerOutput, projectFileManager, null,
                getCompilerOptions(), null, compilationUnits);
        if (!compilationTask.call()) {
            throw new WMRuntimeException("Compile failed with output:\n\n" + compilerOutput.toString());
        }
        return compilerOutput.toString();
    } catch (IOException e) {
        throw new WMRuntimeException("Compile error: " + e);
    }
}

From source file:name.martingeisse.webide.features.java.compiler.memfile.MemoryFileManager.java

@Override
public boolean hasLocation(final Location location) {
    if ((location == StandardLocation.SOURCE_PATH || location == StandardLocation.CLASS_OUTPUT)) {
        logger.trace("memory file manager has location [" + location + "]");
        return true;
    } else {// w w w  .  j  a  v a  2  s. com
        logger.trace(
                "memory file manager doesn't have location [" + location + "], passing to next file manager");
        return super.hasLocation(location);
    }
}

From source file:name.martingeisse.webide.features.java.compiler.memfile.MemoryFileManager.java

/**
 * /*  w  w w . j av  a2  s  .c  o  m*/
 */
private Iterable<JavaFileObject> listThis(final Location location, final String packageName,
        final Set<Kind> kinds, final boolean recurse) throws IOException {

    // determine the file map to use
    final Map<String, IMemoryFileObject> fileMap;
    if (location == StandardLocation.SOURCE_PATH && kinds.contains(Kind.SOURCE)) {
        fileMap = inputFiles;
    } else if (location == StandardLocation.CLASS_OUTPUT && kinds.contains(Kind.CLASS)) {
        fileMap = outputFiles;
    } else {
        return new ArrayList<JavaFileObject>();
    }

    // create a filtered iterator
    final Pattern namePattern = Pattern.compile("\\/" + (packageName.replace(".", "\\/")) + "\\/[^\\/]+");
    return new AbstractIterableWrapper<IMemoryFileObject, JavaFileObject>(fileMap.values()) {
        @Override
        protected JavaFileObject handleElement(final IMemoryFileObject element) {
            if (element instanceof JavaFileObject) {
                if (namePattern.matcher(element.getName()).matches()) {
                    return (JavaFileObject) element;
                }
            }
            return null;
        }
    };

}